Skip to content

Commit e03761b

Browse files
committed
Smash snake, fix matrix.
1 parent 2599b41 commit e03761b

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

game.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class Direction:
3333

3434

3535
class DrawableObject:
36-
EMPTY = 1
37-
APPLE = 2
38-
SNAKE = 3
36+
EMPTY = ""
37+
APPLE = "@"
38+
SNAKE = "#"
3939

4040

4141
class Snake:
@@ -103,6 +103,11 @@ def move(self, direction):
103103
def eat(self):
104104
self.is_eating = True
105105

106+
def is_smashed(self):
107+
for i in range(1, len(self.skeleton)):
108+
if self.head.current_position == self.skeleton[i].current_position:
109+
return True
110+
return False
106111

107112
class Apple:
108113
def __init__(self, height: int, width: int):
@@ -123,15 +128,15 @@ class BaseDisplay:
123128
def __init__(self, width: int, height: int):
124129
self.width = width
125130
self.height = height
126-
self.matrix = [[False for j in range(width)] for i in range(height)]
131+
self.matrix = [[DrawableObject.EMPTY for j in range(width)] for i in range(height)]
127132

128133
def set_position(self, position: Position, obj):
129134
self.matrix[position.y][position.x] = obj
130135

131136
def clear(self):
132137
for i in range(self.height):
133138
for j in range(self.width):
134-
self.matrix[i][j] = False
139+
self.matrix[i][j] = DrawableObject.EMPTY
135140

136141
def draw(self):
137142
raise NotImplementedError()
@@ -142,12 +147,10 @@ def draw(self):
142147
print("")
143148
for i in range(self.height):
144149
for j in range(self.width):
145-
if self.matrix[i][j] == DrawableObject.SNAKE:
146-
print("#", end="")
147-
elif self.matrix[i][j] == DrawableObject.APPLE:
148-
print("@", end="")
149-
else:
150+
if self.matrix[i][j] == DrawableObject.EMPTY:
150151
print(".", end="")
152+
else:
153+
print(self.matrix[i][j], end="")
151154
print("")
152155
print("")
153156

@@ -183,10 +186,12 @@ def step(self):
183186
self.display.clear()
184187
direction = self.controller.get_direction()
185188
self.snake.move(direction)
189+
if self.snake.is_smashed():
190+
raise Exception()
191+
for vertebra in self.snake.skeleton:
192+
self.display.set_position(vertebra.current_position, DrawableObject.SNAKE)
186193
if self.snake.head.current_position == self.apple.position:
187194
self.snake.eat()
188195
self.apple.respawn(self.display.matrix)
189-
for vertebra in self.snake.skeleton:
190-
self.display.set_position(vertebra.current_position, DrawableObject.SNAKE)
191196
self.display.set_position(self.apple.position, DrawableObject.APPLE)
192197
self.display.draw()

main.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
from game import Game
44

55

6-
WIDHT = 10
7-
HEIGHT = 10
8-
g = Game(WIDHT, HEIGHT)
6+
def play():
7+
WIDHT = 3
8+
HEIGHT = WIDHT
9+
g = Game(WIDHT, HEIGHT)
10+
while True:
11+
try:
12+
g.step()
13+
except Exception:
14+
print("Oops...")
15+
g = Game(WIDHT, HEIGHT)
16+
# utime.sleep(1)
917

1018

11-
while True:
12-
g.step()
13-
# utime.sleep(1)
19+
play()

tests.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,23 @@ def test_snake_move():
4646

4747

4848
def test_apple_respawn():
49-
size = 10
50-
matrix = [[False for j in range(size)] for i in range(size)]
49+
size = 3
50+
matrix = [
51+
["#", "", ""],
52+
["#", "#", ""],
53+
["#", "", "#"],
54+
]
5155
apple = Apple(size, size)
5256
assert apple.position.x == 0
5357
assert apple.position.y == 0
54-
assert apple.width == 10
55-
assert apple.height == 10
56-
for i in range(10):
58+
for i in range(100):
59+
apple.respawn(matrix)
5760
x = apple.position.x
5861
y = apple.position.y
59-
apple.respawn(matrix)
60-
assert 0 <= apple.position.x < size
61-
assert 0 <= apple.position.y < size
62+
assert 0 <= x < size
63+
assert 0 <= y < size
64+
assert x != y
65+
assert x != 0
6266

6367

6468
def test_randchoice():

0 commit comments

Comments
 (0)