This commit is contained in:
kdang
2025-12-16 09:23:39 -05:00
parent 93becc1a2b
commit 51765051b1
6 changed files with 134 additions and 28 deletions

Binary file not shown.

Binary file not shown.

48
catcher.py Normal file
View File

@@ -0,0 +1,48 @@
class CatcherPiece:
character = "-"
color = "white_on_indigo"
def __init__(self, position):
self.position = position
class Catcher:
width = 7
display = False
pieces = []
name = "Player"
color = "white_on_indigo"
def __init__(self, position):
self.position = position
def play_turn(self, game):
if not self.pieces:
self.create_pieces(game)
def handle_keystroke(self, keystroke, game):
x, y = self.position
width, height = game.board_size
if keystroke.name == "KEY_LEFT":
if 0 < x:
self.position = (x-1, y)
self.update_piece_positions()
if keystroke.name == "KEY_RIGHT":
if x + self.width < width:
self.position = (x+1, y)
self.update_piece_positions()
def create_pieces(self, game):
x, y = self.position
self.pieces = []
for i in range(self.width):
piece = CatcherPiece((x + i, y))
self.pieces.append(piece)
game.add_agent(piece)
def update_piece_positions(self):
x, y = self.position
for i, piece in enumerate(self.pieces):
piece.position = (x + i, y)
def can_move(self, position, game):
on_board = game.on_board(position)
empty = game.is_empty(position)

70
fruit.py Normal file
View File

@@ -0,0 +1,70 @@
class FruitPiece:
character = "@"
color = "green_on_indigo"
def __init__(self, position):
self.position = position
class Fruit:
width = 2
height = 2
display = False
pieces = []
character = "@"
color = "green_on_indigo"
def __init__(self, position):
self.position = position
def play_turn(self, game):
if not self.pieces:
self.create_pieces(game)
if game.turn_number % 2 == 0:
x, y = self.position
if y == 24:
game.remove_agent(self)
else:
catcher = game.get_agent_by_name('catcher')
new_position = (x, y + 1)
if new_position == catcher.position:
catcher.explode()
game.end()
else:
self.position = new_position
def create_pieces(self, game):
x, y = self.position
self.pieces = []
for i in range(self.width):
piece = FruitPiece((x + i, y))
self.pieces.append(piece)
game.add_agent(piece)
for i in range(self.height):
piece = FruitPiece((x, y + i))
self.pieces.append(piece)
game.add_agent(piece)
def update_piece_positions(self):
if game.turn_number % 2 == 0:
self.set_color()
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:
ship.explode()
game.end()
else:
self.position = new_position
class FruitSpawner:
display = False
def play_turn(self, game):
if self.should_spawn_fruit(game.turn_number):
asteroid = Fruit((randint(0, WIDTH - 1), 0))
game.add_agent(fruit)
def should_spawn_fruit(self, turn_number):
return randint(0, 1000) < turn_number

42
game.py
View File

@@ -1,32 +1,20 @@
from random import randint from random import randint
from retro.game import Game from retro.game import Game
from retro.graph import Graph
from catcher import Catcher
from fruit import Fruit
class Agent: g = Graph()
RIGHT = (1, 0) g.get_or_create_edge(26, 10, 26, 22)
UP = (0, -1) agents = g.get_agents()
LEFT = (-1, 0) for agent in agents:
DOWN = (0, 1) agent.color = "white_on_indigo"
character = ">"
direction = RIGHT
position = (15, 0)
name = "agent"
color = "white_on_black"
display = True
def handle_keystroke(self, keystroke, game): agents = [
x, y = self.position Catcher((14, 24)),
if keystroke.name == "M": Fruit((14, 2)),
self.direction = self.RIGHT ]
self.character = '>'
elif keystroke.name == "I":
self.direction = self.LEFT
self.character = '<'
def can_move(self, position, game): state = {'Score': 0}
on_board = game.on_board(position) game = Game(agents, state, board_size=(35, 25), framerate=24, color="white_on_indigo")
empty = game.is_empty(position) game.play()
if __name__ == '__main__':
player = Agent()
game = Game([player], {'score': 0}, board_size:=(24, 20), framerate:=12)
game.play()

2
nav.py
View File

@@ -2,7 +2,7 @@ from random import randint
from retro.game import Game from retro.game import Game
HEIGHT = 25 HEIGHT = 25
WIDTH = 25 WIDTH = 2
class Spaceship: class Spaceship:
"""A player-controlled agent which moves left and right, dodging asteroids. """A player-controlled agent which moves left and right, dodging asteroids.