Skip to content

Commit

Permalink
support game over
Browse files Browse the repository at this point in the history
  • Loading branch information
firemark committed May 3, 2023
1 parent 3922ace commit f12b5e0
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/game/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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)

Expand Down

0 comments on commit f12b5e0

Please sign in to comment.