This code is very different from Unit 1. Unit 1 was very top to bottom. This is more object driven. Instead of one loop or code there are agents that act to put everything together as a focus of what agent should be doing for the game to become a game such as the spaceship, asteroid, asteroid spawner are all used to generate the game. This code seems so real and more day to day life in my opinion.

This commit is contained in:
erbrown
2025-12-14 20:45:39 -05:00
parent 2f90ac3797
commit b8c69441d5
7 changed files with 65 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -2,3 +2,23 @@
# ------------ # ------------
# By MWC Contributors # By MWC Contributors
# This module defines an asteroid agent class. # This module defines an asteroid agent class.
class Asteroid:
character = 'O'
def __init__(self, position):
self.position = position
def play_turn(self, game):
width, height = game.board_size
if game.turn_number % 1 == 0:
x, y = self.position
if y == height - 1:
game.remove_agent(self)
else:
ship = game.get_agent_by_name('ship')
new_position = (x, y + 1)
if new_position == ship.position:
game.end()
else:
self.position = new_position

View File

@@ -2,3 +2,18 @@
# ------------------- # -------------------
# By MWC Contributors # By MWC Contributors
# This module defines an AsteroidSpawner agent class. # This module defines an AsteroidSpawner agent class.
from random import randint
from asteroid import Asteroid
class AsteroidSpawner:
display = False
def play_turn(self, game):
width, height = game.board_size
game.state['score'] += 1
if self.should_spawn_asteroid(game.turn_number):
asteroid = Asteroid((randint(0, width - 1), 0))
game.add_agent(asteroid)
def should_spawn_asteroid(self, turn_number):
return randint(0, 1000) < turn_number

View File

@@ -2,3 +2,12 @@
# ------------ # ------------
# By MWC Contributors # By MWC Contributors
# This class implements a simple game where a spaceship avoids asteroids. # This class implements a simple game where a spaceship avoids asteroids.
from retro.game import Game
from spaceship import Spaceship
from asteroid_spawner import AsteroidSpawner
board_size = (25, 25)
ship = Spaceship(board_size)
spawner = AsteroidSpawner()
game = Game([ship, spawner], {"score": 0}, board_size=board_size)
game.play()

View File

@@ -2,3 +2,24 @@
# ------------ # ------------
# By MWC Contributors # By MWC Contributors
# This module defines a spaceship agent class. # This module defines a spaceship agent class.
class Spaceship:
name = "ship"
character = '^'
color = "turquoise2"
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
if keystroke.name in ("KEY_LEFT", "KEY_RIGHT"):
if keystroke.name == "KEY_LEFT":
new_position = (x - 1, y)
else:
new_position = (x + 1, y)
if game.on_board(new_position):
if game.is_empty(new_position):
self.position = new_position
else:
game.end()