diff --git a/__pycache__/apple.cpython-313.pyc b/__pycache__/apple.cpython-313.pyc new file mode 100644 index 0000000..1e22292 Binary files /dev/null and b/__pycache__/apple.cpython-313.pyc differ diff --git a/__pycache__/apple_sprawner.cpython-313.pyc b/__pycache__/apple_sprawner.cpython-313.pyc new file mode 100644 index 0000000..a67bf10 Binary files /dev/null and b/__pycache__/apple_sprawner.cpython-313.pyc differ diff --git a/__pycache__/player.cpython-313.pyc b/__pycache__/player.cpython-313.pyc index 9ae46c9..aa53fb4 100644 Binary files a/__pycache__/player.cpython-313.pyc and b/__pycache__/player.cpython-313.pyc differ diff --git a/apple.py b/apple.py new file mode 100644 index 0000000..11cc55b --- /dev/null +++ b/apple.py @@ -0,0 +1,46 @@ +class Apple: + character = 'A' + + def __init__(self, position): + self.position = position + self.attached = False + self.offset = (0,0) + + def play_turn(self, game): + width, height = game.board_size + player = game.get_agent_by_name('player') + if self.attached: + px, py = player.position + ox, oy = self. offset + self.position = (px + ox, py + oy) + return + x, y = self.position + if game.turn_number % 2 == 0: + new_position= (x +1, y) + + if new_position == player.position: + self.attach_to_player(player) + elif game.on_board(new_position): + self.position = new_position + else: + game.remove_agent(self) + + def attach_to_player(self, player): + self.attached = True + index = len(player.attached_apples) + self.offset = (-1 - index, 0) + player.attached_apples.append(self) + + + + + + # if y == height - 1: + # game.remove_agent(self) + # else: + # player = game.get_agent_by_name('player') + # new_position = (x+1, y) + # if new_position == player.position: + # game.end() + # else: + # self.position = new_position diff --git a/apple_sprawner.py b/apple_sprawner.py new file mode 100644 index 0000000..27998dc --- /dev/null +++ b/apple_sprawner.py @@ -0,0 +1,19 @@ +from random import randint +from apple import Apple + +class AppleSpawner: + display = False + + def play_turn(self, game): + width, height = game.board_size + game.state['score'] += 1 + if self.should_spawn_apple(game.turn_number): + x = randint(0,width - 1) + y = randint(0, height - 1) + + if game.is_empty((x,y)): + apple = Apple((x,y)) + game.add_agent(apple) + + def should_spawn_apple(self, turn_number): + return randint(0, 1000) < turn_number diff --git a/game.py b/game.py index 0b9469b..108c135 100644 --- a/game.py +++ b/game.py @@ -1,7 +1,11 @@ from retro.game import Game from player import Player +from apple import Apple +from apple_sprawner import AppleSpawner board_size = (100, 25) player = Player(board_size) -game = Game([player], {"score": 0}, board_size=board_size, color = "white") +apple = Apple((board_size[0] // 2, 0)) +spawner = AppleSpawner() +game = Game([player,spawner], {"score": 0}, board_size=board_size, color = "white") game.play() \ No newline at end of file diff --git a/player.py b/player.py index 0cd08e5..3799df5 100644 --- a/player.py +++ b/player.py @@ -5,6 +5,7 @@ class Player: def __init__(self, board_size): board_width, board_height = board_size self.position = (board_width // 2, board_height - 1) + self.attached_apples = [] def handle_keystroke(self, keystroke, game): x, y = self.position @@ -22,7 +23,5 @@ class Player: new_position = (x, y + 1) if game.on_board(new_position): - if game.is_empty(new_position): - self.position = new_position - else: - game.end() \ No newline at end of file + self.position = new_position + #self.check_self_collision(game) \ No newline at end of file