I did not realize this had to be three submissions. My first progress was the add lives to the asteroid game.

For this feature, I added a health system so the player has three
lives instead of losing instantly. In nav_game.py, I added "health": 3
to the game state, and in spaceship.py I created a take_damage method
that subtracts one life when the ship is hit. If the player's health
reaches zero, the game ends. This change is important because it
makes the game less punishing, gives the player more chances to
recover from mistakes, and makes gameplay feel more balanced and fun.
This commit is contained in:
angelotr
2025-12-08 23:39:17 -05:00
parent 3816c0bac0
commit 47a9e1abe3
9 changed files with 179 additions and 0 deletions

59
spaceship.py Normal file
View File

@@ -0,0 +1,59 @@
# spaceship.py
# ------------
# By MWC Contributors
# This module defines a spaceship agent class.
from bullet import Bullet
class Spaceship:
name = "ship"
character = '^'
def __init__(self, board_size):
board_width, board_height = board_size
self.position = (board_width // 2, board_height - 1)
def handle_keystroke(self, keystroke, game):
x, y = self.position
new_position = None
# Move LEFT
if keystroke.name in ("KEY_LEFT", "a", "A"):
new_position = (x - 1, y)
# Move RIGHT
elif keystroke.name in ("KEY_RIGHT", "d", "D"):
new_position = (x + 1, y)
# Move UP
elif keystroke.name in ("KEY_UP", "w", "W"):
new_position = (x, y - 1)
# Move DOWN
elif keystroke.name in ("KEY_DOWN", "s", "S"):
new_position = (x, y + 1)
# SHOOT (m or M)
elif keystroke.name in ("m", "M"):
bx, by = x, y - 1
if game.on_board((bx, by)):
bullet = Bullet((bx, by))
game.add_agent(bullet)
return # dont move on shoot
else:
return # ignore other keys
# If we are trying to move:
if new_position is not None and game.on_board(new_position):
if game.is_empty(new_position):
self.position = new_position
else:
# We bumped into something (probably an asteroid)
self.take_damage(game)
def take_damage(self, game):
"""Reduce health; end game if health reaches 0."""
game.state["health"] -= 1
if game.state["health"] <= 0:
game.end()