generated from mwc/project_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.
31 lines
986 B
Python
31 lines
986 B
Python
from asteroid import Asteroid
|
|
|
|
class Bullet:
|
|
"""A bullet fired by the spaceship that moves upward and destroys asteroids."""
|
|
character = '|'
|
|
|
|
def __init__(self, position):
|
|
self.position = position
|
|
|
|
def play_turn(self, game):
|
|
# Move bullet up one row each turn
|
|
x, y = self.position
|
|
y -= 1
|
|
|
|
# If the bullet goes off the top, remove it
|
|
if y < 0:
|
|
game.remove_agent(self)
|
|
return
|
|
|
|
self.position = (x, y)
|
|
|
|
# Check if we hit an asteroid
|
|
# We loop over a copy of the agents list because we may remove agents
|
|
for agent in list(game.agents):
|
|
if isinstance(agent, Asteroid) and agent.position == self.position:
|
|
# Destroy the asteroid and the bullet
|
|
game.remove_agent(agent)
|
|
game.remove_agent(self)
|
|
# Reward points for hitting an asteroid
|
|
game.state["score"] += 10
|
|
return |