Files
project_game/bullet.py
angelotr a91f781f76 For my final entry, I tried to incorporate the ability to shoot bullet from the spaceshit to destory the asteroids.
I attempted to add a shooting feature where the player presses the
M key to fire bullets upward. Originally, I tried making the space bar
the key to shoot but this has shown to be difficult as it did not
shoot bullets using M or the space bar. I created a new Bullet class
and added code to spaceship.py that should create a bullet agent above
the ship.However, the shooting did not work because Retro was not
detecting the M key the way the code expected. Even though the feature
is not working yet, it is an important mechanic because it would allow
the player to clear asteroids and add a new layer of interaction and
strategy to the game. I was annoyed with the idea that I was not able
to get shooting to work but ultimately I decided to keep it within my
code as a way to show that my previous did work and eventhough
the shooting mechanic did not it is important for me to look back at it
one day. Even if it doesn't work, it was an attempt at an idea.
2025-12-09 00:42:44 -05:00

32 lines
991 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