Files
project_game/asteroid.py
angelotr 47a9e1abe3 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.
2025-12-08 23:39:17 -05:00

36 lines
1.0 KiB
Python

# asteroid.py
# ------------
# By MWC Contributors
# This module defines an asteroid agent class.
class Asteroid:
character = 'O'
def __init__(self, position, speed=2):
# speed = how often it moves (1 = every turn, 2 = every 2 turns, etc.)
self.position = position
self.speed = speed
def play_turn(self, game):
width, height = game.board_size
# Only move on some turns (for variable speed)
if game.turn_number % self.speed != 0:
return
x, y = self.position
# If at bottom, remove asteroid
if y == height - 1:
game.remove_agent(self)
else:
ship = game.get_agent_by_name('ship')
new_position = (x, y + 1)
# If we hit the ship, damage it and remove this asteroid
if new_position == ship.position:
if hasattr(ship, "take_damage"):
ship.take_damage(game)
game.remove_agent(self)
else:
self.position = new_position