diff --git a/__pycache__/catcher.cpython-311.pyc b/__pycache__/catcher.cpython-311.pyc new file mode 100644 index 0000000..ea740a9 Binary files /dev/null and b/__pycache__/catcher.cpython-311.pyc differ diff --git a/__pycache__/fruit.cpython-311.pyc b/__pycache__/fruit.cpython-311.pyc new file mode 100644 index 0000000..502d57e Binary files /dev/null and b/__pycache__/fruit.cpython-311.pyc differ diff --git a/catcher.py b/catcher.py new file mode 100644 index 0000000..b6b0a80 --- /dev/null +++ b/catcher.py @@ -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) diff --git a/fruit.py b/fruit.py new file mode 100644 index 0000000..0b7ced6 --- /dev/null +++ b/fruit.py @@ -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 \ No newline at end of file diff --git a/game.py b/game.py index 3e25746..7b47540 100644 --- a/game.py +++ b/game.py @@ -1,32 +1,20 @@ from random import randint from retro.game import Game +from retro.graph import Graph +from catcher import Catcher +from fruit import Fruit -class Agent: - RIGHT = (1, 0) - UP = (0, -1) - LEFT = (-1, 0) - DOWN = (0, 1) - character = ">" - direction = RIGHT - position = (15, 0) - name = "agent" - color = "white_on_black" - display = True +g = Graph() +g.get_or_create_edge(26, 10, 26, 22) +agents = g.get_agents() +for agent in agents: + agent.color = "white_on_indigo" - def handle_keystroke(self, keystroke, game): - x, y = self.position - if keystroke.name == "M": - self.direction = self.RIGHT - self.character = '>' - elif keystroke.name == "I": - self.direction = self.LEFT - self.character = '<' +agents = [ + Catcher((14, 24)), + Fruit((14, 2)), +] - def can_move(self, position, game): - on_board = game.on_board(position) - empty = game.is_empty(position) - -if __name__ == '__main__': - player = Agent() - game = Game([player], {'score': 0}, board_size:=(24, 20), framerate:=12) - game.play() +state = {'Score': 0} +game = Game(agents, state, board_size=(35, 25), framerate=24, color="white_on_indigo") +game.play() \ No newline at end of file diff --git a/nav.py b/nav.py index 9ccab28..518baf9 100644 --- a/nav.py +++ b/nav.py @@ -2,7 +2,7 @@ from random import randint from retro.game import Game HEIGHT = 25 -WIDTH = 25 +WIDTH = 2 class Spaceship: """A player-controlled agent which moves left and right, dodging asteroids.