diff --git a/__pycache__/Player.cpython-313.pyc b/__pycache__/Player.cpython-313.pyc index 8327235..5ebc007 100644 Binary files a/__pycache__/Player.cpython-313.pyc and b/__pycache__/Player.cpython-313.pyc differ diff --git a/__pycache__/food.cpython-313.pyc b/__pycache__/food.cpython-313.pyc new file mode 100644 index 0000000..c77e4c2 Binary files /dev/null and b/__pycache__/food.cpython-313.pyc differ diff --git a/food.py b/food.py new file mode 100644 index 0000000..3409732 --- /dev/null +++ b/food.py @@ -0,0 +1,18 @@ + +import random + +class Food: + name = "food" + character = "*" + + def __init__(self, board_size): + self.board_width, self.board_height = board_size + self.position = self._random_position() + + def _random_position(self): + x = random.randint(0, self.board_width - 1) + y = random.randint(0, self.board_height - 1) + return (x, y) + + def respawn(self): + self.position = self._random_position() diff --git a/nav_game.py b/nav_game.py index 30c7f2a..81d1afb 100644 --- a/nav_game.py +++ b/nav_game.py @@ -1,9 +1,10 @@ from retro.game import Game from player import Player - +from food import Food board_size = (100, 25) -player = Player(board_size) -game = Game([player],{"score": 0}, board_size=board_size, color="pink", debug=True,) +food = Food(board_size) -game.play() +player = Player(board_size) +game = Game([player, food],{"score": 0}, board_size=board_size, color="pink", debug=True,) +game.play() \ No newline at end of file diff --git a/player.py b/player.py index 3030511..321b263 100644 --- a/player.py +++ b/player.py @@ -10,33 +10,47 @@ class Player: self.position = (start_x, start_y) self.body = [self.position] self.direction = (1, 0) + + def handle_keystroke(self, keystroke, game): + x, y = self.body[0] - def handle_keystroke(self, keystroke, game): - x, y = self.body[0] + if keystroke.name in ("KEY_LEFT", "KEY_RIGHT", "KEY_UP", "KEY_DOWN"): + if keystroke.name == "KEY_LEFT": + self.direction = (-1, 0) + elif keystroke.name == "KEY_RIGHT": + self.direction = (1, 0) + elif keystroke.name == "KEY_UP": + self.direction = (0, -1) + elif keystroke.name == "KEY_DOWN": + self.direction = (0, 1) - if keystroke.name in ("KEY_LEFT", "KEY_RIGHT", "KEY_UP", "KEY_DOWN"): + dx, dy = self.direction + new_head = (x + dx, y + dy) - if keystroke.name == "KEY_LEFT": - self.direction = (-1, 0) - elif keystroke.name == "KEY_RIGHT": - self.direction = (1, 0) - elif keystroke.name == "KEY_UP": - self.direction = (0, -1) - elif keystroke.name == "KEY_DOWN": - self.direction = (0, 1) + if not game.on_board(new_head): + game.end() + return - dx, dy = self.direction - new_head = (x + dx, y + dy) + if new_head in self.body: + game.end() + return - if not game.on_board(new_head): - game.end() - return + food = None + for actor in game.actors: + if getattr(actor, "name", None) == "food": + food = actor + break - if new_head in self.body: - game.end() - return + if food is not None and new_head == food.position: + + self.body.insert(0, new_head) + + if hasattr(food, "respawn"): + food.respawn() + else: + + self.body.insert(0, new_head) + self.body.pop() - self.body.insert(0, new_head) - self.body.pop() - - self.position = self.body[0] + + self.position = self.body[0]