Skip to content

Commit e7e3c71

Browse files
committed
Add well-defined defeat conditions. Closes #23.
1 parent 069cea3 commit e7e3c71

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

components/core.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -464,16 +464,11 @@ class GameModel(Publisher):
464464

465465
def __init__(self):
466466
super(GameModel, self).__init__()
467+
self.endgame = False
467468

468469
def render(self, **kwargs):
469470
"""
470471
Helps GameScreens represent and render relevant parts of this model.
471472
Return an object which your GameScreen knows how to render.
472473
"""
473474
pass
474-
475-
def is_endgame(self):
476-
"""
477-
Determines whether the game is in an end state.
478-
"""
479-
return False

demo/snake/game.py

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
class SnakeScreen(GameScreen):
1111

12+
GAME_OVER_FONT_SIZE = 28
13+
GAME_OVER_FONT = pygame.font.Font("fonts/forcedsquare/forcedsquare.ttf", GAME_OVER_FONT_SIZE)
14+
1215
def __init__(self, config, grid_size):
1316
super(SnakeScreen, self).__init__(config, SnakeGameModel(grid_size[0], grid_size[1]))
1417
screen_size = config.get_config_val("window_size")
@@ -30,6 +33,12 @@ def draw_screen(self, window):
3033
fp_rect = QuadraticGrid.make_rect(self.game_model.food_point, self.block_width, self.block_height)
3134
pygame.draw.rect(window, Colors.NIGHT_GRAY, fp_rect, 0)
3235

36+
if self.game_model.is_endgame():
37+
# Render a "Game Over" sign
38+
game_over_render = SnakeScreen.GAME_OVER_FONT.render("GAME OVER", True, Colors.HUMAN_RED)
39+
window.blit(game_over_render, (50, 50))
40+
41+
3342
def draw_unchanging(self, window):
3443
super(SnakeScreen, self).draw_unchanging(window)
3544
original_dims = self.config.get_config_val("window_size")
@@ -71,6 +80,11 @@ def event_handler(event):
7180
self.game_screen.model.move_snake(inverse)
7281
self.debug_queue.log("WALL COLLISION", logging.CRITICAL)
7382
self.debug_queue.log("snake head now at %s" % str(self.game_screen.model.snake.head))
83+
self.game_screen.model.endgame = True
84+
elif self.game_screen.model.snake.head in self.game_screen.model.snake.enumerate_snake_squares(False):
85+
inverse = QuadraticGrid.Movements.INVERSE_DIRECTION[movement]
86+
self.game_screen.model.move_snake(inverse)
87+
self.game_screen.model.endgame = True
7488
else:
7589
self.game_screen.model.last_move_reversible = False
7690
except VectorDirectionException:

demo/snake/model.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ def __init__(self):
2424
"""
2525
self.joints = []
2626

27-
def enumerate_snake_squares(self):
27+
def enumerate_snake_squares(self, include_head=True):
2828
"""
2929
Returns a set of tuples indicating the squares the snake is occupying.
3030
This does not take into account the grid in which the snake is moving.
3131
"""
3232
snake_squares = set()
3333
c_origin = self.head
34-
snake_squares.add(self.head)
34+
if include_head:
35+
snake_squares.add(self.head)
3536

3637
for c_end in self.joints:
3738
c_direction = QuadraticGrid.Movements.compute_direction(c_origin, c_end)
@@ -106,6 +107,9 @@ def __generate_food_point(self):
106107
random.randint(0, self.width - 1))
107108

108109
def move_snake(self, direction, reversible=False):
110+
if self.endgame:
111+
return
112+
109113
movector = direction
110114
if movector is None:
111115
raise ValueError("Invalid direction input %s." % direction)
@@ -117,7 +121,7 @@ def move_snake(self, direction, reversible=False):
117121
try:
118122
inverse_direction = QuadraticGrid.Movements.INVERSE_DIRECTION[current_direction]
119123
except KeyError:
120-
self.endgame = True
124+
pass
121125

122126
if movector == inverse_direction and not self.last_move_reversible:
123127
raise VectorDirectionException("Impossible to reverse last movement.")

fonts/forcedsquare/forcedsquare.ttf

49.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)