From f12b5e084bd12dea8c7a57dde4b8209caf6ea8aa Mon Sep 17 00:00:00 2001 From: Firemark Date: Mon, 9 Dec 2019 22:13:41 +0100 Subject: [PATCH] support game over --- src/game/game.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/game/game.py b/src/game/game.py index 783e98e..c837116 100755 --- a/src/game/game.py +++ b/src/game/game.py @@ -144,6 +144,7 @@ def setup(self): self.setup_world() def setup_world(self): + self.game_over = False self.player = Player([128, 128]) self.bullets = [] self.enemies = [] @@ -174,6 +175,15 @@ def update_collision(self): self.player.score += 100 pyxel.play(0, 1) + if self.game_over: + return + + for enemy in self.enemies: + if enemy.has_collision(self.player) or self.player.has_collision(enemy): + enemy.is_destroyed = True + self.game_over = True + pyxel.play(1, 2) + def spawn_enemies(self): if pyxel.frame_count % 50 == 20: @@ -188,6 +198,11 @@ def keyboard(self): pyxel.quit() return + if self.game_over: + if pyxel.btnp(pyxel.KEY_R): + self.setup_world() + return + if pyxel.btn(pyxel.KEY_UP): self.player.move_up() if pyxel.btn(pyxel.KEY_DOWN): @@ -205,8 +220,15 @@ def keyboard(self): def draw(self): pyxel.cls(0) - pyxel.text(0, 0, "Score: %d" % self.player.score, 7) - self.player.draw() + if self.game_over: + pyxel.text(80, 40, "GAME OVER", pyxel.frame_count % 16) + pyxel.text(80, 50, "Total score: %d" % self.player.score, 8) + pyxel.text(80, 60, "Press Q to quit", 8) + pyxel.text(80, 70, "Press R to restart", 8) + else: + pyxel.text(0, 0, "Score: %d" % self.player.score, 7) + self.player.draw() + self.draw_objs(self.bullets) self.draw_objs(self.enemies)