-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
197 lines (161 loc) · 6.03 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from utils import randchoice
class Position:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
def __str__(self):
return "x: {}, y: {}".format(self.x, self.y)
def copy(self):
return Position(self.x, self.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
class Vertebra:
def __init__(self, current_position: Position, next_position: Position):
self.current_position = current_position
self.next_position = next_position
def __str__(self):
return "Current: {}, Next: {}".format(self.current_position, self.next_position)
class Direction:
UP = -1
DOWN = 1
LEFT = -2
RIGHT = 2
class DrawableObject:
EMPTY = ""
APPLE = "@"
SNAKE = "#"
class Snake:
def __init__(self, width, height, initial_position: Position, direction: int):
self.width = width
self.height = height
self.direction = direction
head = Vertebra(initial_position, None)
self.skeleton = [head]
self.is_eating = False
@property
def head(self):
return self.skeleton[0]
def _calc_next_head_direction(self, direction):
if direction is None:
# No move to the same direction as previously
direction = self.direction
if direction == -self.direction:
# Ban 180 degree rotation
direction = self.direction
return direction
def _calc_next_head_position(self, direction: int) -> Position:
next_position = self.head.current_position.copy()
if direction == Direction.UP:
next_position.y -= 1
elif direction == Direction.DOWN:
next_position.y += 1
elif direction == Direction.LEFT:
next_position.x -= 1
elif direction == Direction.RIGHT:
next_position.x += 1
# Slam into wall
if next_position.y >= self.height:
next_position.y = 0
elif next_position.y < 0:
next_position.y = self.height - 1
if next_position.x >= self.width:
next_position.x = 0
elif next_position.x < 0:
next_position.x = self.width - 1
return next_position
def move(self, direction):
direction = self._calc_next_head_direction(direction)
next_position = self._calc_next_head_position(direction)
if self.is_eating:
self.is_eating = False
# Apple has been eaten, create new head
self.head.next_position = next_position.copy()
vertebra = Vertebra(next_position.copy(), None)
self.skeleton.insert(0, vertebra)
else:
self.head.current_position = next_position
for i in range(1, len(self.skeleton)):
self.skeleton[i].current_position = self.skeleton[i].next_position
self.skeleton[i].next_position = self.skeleton[i - 1].current_position
if direction is not None:
self.direction = direction
def eat(self):
self.is_eating = True
def is_smashed(self):
for i in range(1, len(self.skeleton)):
if self.head.current_position == self.skeleton[i].current_position:
return True
return False
class Apple:
def __init__(self, height: int, width: int):
self.position = Position(0, 0)
self.width = width
self.height = height
def respawn(self, matrix: list):
non_full_row_indexes = [i for i in range(self.height) if not all(matrix[i])]
y = randchoice(non_full_row_indexes)
non_full_col_indexes = [j for j in range(self.width) if not matrix[y][j]]
x = randchoice(non_full_col_indexes)
self.position.x = x
self.position.y = y
class BaseDisplay:
def __init__(self, width: int, height: int):
self.width = width
self.height = height
self.matrix = [[DrawableObject.EMPTY for j in range(width)] for i in range(height)]
def set_position(self, position: Position, obj):
self.matrix[position.y][position.x] = obj
def clear(self):
for i in range(self.height):
for j in range(self.width):
self.matrix[i][j] = DrawableObject.EMPTY
def draw(self):
raise NotImplementedError()
class ConsoleDisplay(BaseDisplay):
def draw(self):
print("")
for i in range(self.height):
for j in range(self.width):
if self.matrix[i][j] == DrawableObject.EMPTY:
print(".", end="")
else:
print(self.matrix[i][j], end="")
print("")
print("")
class Controller:
def get_direction(self) -> int:
print("Enter key: ")
chars = input()
if not chars:
return None
key = chars[-1]
if key == "w":
return Direction.UP
if key == "s":
return Direction.DOWN
if key == "a":
return Direction.LEFT
if key == "d":
return Direction.RIGHT
return None
class Game:
def __init__(self, width: int, height: int):
initial_position = Position(int(width / 2), int(height / 2))
self.snake = Snake(width, height, initial_position, Direction.RIGHT)
self.apple = Apple(width, height)
self.display = ConsoleDisplay(width, height)
self.apple.respawn(self.display.matrix)
self.controller = Controller()
def step(self):
self.display.clear()
direction = self.controller.get_direction()
self.snake.move(direction)
if self.snake.is_smashed():
raise Exception()
for vertebra in self.snake.skeleton:
self.display.set_position(vertebra.current_position, DrawableObject.SNAKE)
if self.snake.head.current_position == self.apple.position:
self.snake.eat()
self.apple.respawn(self.display.matrix)
self.display.set_position(self.apple.position, DrawableObject.APPLE)
self.display.draw()