From 84791c488c8ae5bca0f18dc07ca398088d062d91 Mon Sep 17 00:00:00 2001 From: ltuffery <123221865+ltuffery@users.noreply.github.com> Date: Sun, 4 May 2025 22:50:33 +0000 Subject: [PATCH] chores: entity location --- ai/train.py | 2 +- ai/utils.py | 2 +- engine/entity/apple.py | 4 ++-- engine/entity/entity.py | 6 +++--- engine/entity/snake.py | 9 +++++---- engine/game.py | 22 +++++++++++++--------- main.py | 9 ++++----- 7 files changed, 29 insertions(+), 25 deletions(-) diff --git a/ai/train.py b/ai/train.py index 4792ea2..fa75fee 100644 --- a/ai/train.py +++ b/ai/train.py @@ -4,7 +4,7 @@ import engine.settings as settings from engine.exception.gameover import GameOver import ai.replay as replay -from ai.utils import get_Q, action +from ai.utils import get_Q, action # Exploration rate EPSILON = settings.EPSILON diff --git a/ai/utils.py b/ai/utils.py index 8c56ba3..adb9b11 100644 --- a/ai/utils.py +++ b/ai/utils.py @@ -40,4 +40,4 @@ def action(Q: dict, state: list[bool], explo: float) -> int: if np.random.uniform() < explo: return np.random.randint(0, len(Direction)) # Exploration else: - return max(range(4), key=lambda a: get_Q(Q, state, a)) # Exploitation \ No newline at end of file + return max(range(4), key=lambda a: get_Q(Q, state, a)) # Exploitation diff --git a/engine/entity/apple.py b/engine/entity/apple.py index 7a602ee..267363b 100644 --- a/engine/entity/apple.py +++ b/engine/entity/apple.py @@ -26,7 +26,7 @@ class Apple(Entity): __apple_type (AppleType): The type of the apple (RED or GREEN). """ - def __init__(self, world: World, x: int, y: int, apple_type: AppleType): + def __init__(self, world: World, apple_type: AppleType): """ Initializes the apple with a given type and position. @@ -36,7 +36,7 @@ def __init__(self, world: World, x: int, y: int, apple_type: AppleType): y (int): The Y-coordinate of the apple's initial position. apple_type (AppleType): The type of apple (red or green). """ - super().__init__(x, y) + super().__init__() self.__world = world self.__apple_type = apple_type diff --git a/engine/entity/entity.py b/engine/entity/entity.py index 10f77f4..b23e4d1 100644 --- a/engine/entity/entity.py +++ b/engine/entity/entity.py @@ -10,7 +10,7 @@ class Entity(ABC): __y (int): The Y-coordinate of the entity. """ - def __init__(self, x: int, y: int): + def __init__(self): """ Initializes an entity with specified coordinates. @@ -18,8 +18,8 @@ def __init__(self, x: int, y: int): x (int): The X-coordinate of the entity. y (int): The Y-coordinate of the entity. """ - self.__x = x - self.__y = y + self.__x = 0 + self.__y = 0 def get_x(self) -> int: """ diff --git a/engine/entity/snake.py b/engine/entity/snake.py index 5a8c9e0..2c97e9b 100644 --- a/engine/entity/snake.py +++ b/engine/entity/snake.py @@ -22,7 +22,7 @@ class Snake(Entity): The last movement direction of the snake. """ - def __init__(self, world: World, x: int, y: int, direction: Direction): + def __init__(self, world: World): """ Initializes a snake in the given world at a specific position and direction. @@ -33,13 +33,14 @@ def __init__(self, world: World, x: int, y: int, direction: Direction): y (int): The initial Y position of the snake's head. direction (Direction): The initial movement direction of the snake. """ - super().__init__(x, y) + super().__init__() self.__body: deque[tuple[int, int]] = deque() self.__world: World = world - self.__last_direction: Direction = direction + self.__last_direction: Direction = random.choice(list(Direction)) - dir_x, dir_y = direction.value + dir_x, dir_y = self.__last_direction.value + x, y = self.get_position() # Create the initial body of the snake (3 segments) for _ in range(3): diff --git a/engine/game.py b/engine/game.py index fce9e33..c0e82b7 100644 --- a/engine/game.py +++ b/engine/game.py @@ -1,7 +1,6 @@ from engine.world import World from engine.entity.snake import Snake from engine.entity.apple import Apple, AppleType -from engine.direction import Direction class Game: @@ -33,12 +32,12 @@ def start(self) -> None: (one green and one red) at the starting position (0, 0). """ self.__world = World() - self.__snake = Snake(self.__world, 0, 0, Direction.EAST) + self.__snake = Snake(self.__world) self.__world.spawn_entity(self.__snake) - self.__world.spawn_entity(Apple(self.__world, 0, 0, AppleType.GREEN)) - self.__world.spawn_entity(Apple(self.__world, 0, 0, AppleType.GREEN)) - self.__world.spawn_entity(Apple(self.__world, 0, 0, AppleType.RED)) + self.__world.spawn_entity(Apple(self.__world, AppleType.GREEN)) + self.__world.spawn_entity(Apple(self.__world, AppleType.GREEN)) + self.__world.spawn_entity(Apple(self.__world, AppleType.RED)) def get_snake(self) -> Snake: """ @@ -69,8 +68,11 @@ def set_snake(self, head: tuple[int, int], body: list[tuple]) -> None: body (list[tuple[int, int]]): A list of (x, y) tuples representing the snake's body segments. """ - self.__snake = Snake(self.__world, head[0], head[1], Direction.EAST) + self.__snake = Snake(self.__world) + self.__snake.set_x(head[0]) + self.__snake.set_y(head[1]) self.__snake.set_body(body) + self.__world.add_entity(self.__snake) def set_apples(self, apples: list[tuple[int, int, bool]]) -> None: @@ -86,7 +88,9 @@ def set_apples(self, apples: list[tuple[int, int, bool]]) -> None: """ for apple in apples: apple_type = AppleType.GREEN if apple[2] else AppleType.RED + apple_obj = Apple(self.__world, apple_type) + + apple_obj.set_x(apple[0]) + apple_obj.set_y(apple[1]) - self.__world.add_entity( - Apple(self.__world, apple[0], apple[1], apple_type) - ) + self.__world.add_entity(apple_obj) diff --git a/main.py b/main.py index fb15f65..c24ff76 100644 --- a/main.py +++ b/main.py @@ -9,13 +9,12 @@ sys.path.append(os.path.abspath(os.path.dirname(__file__))) world = World() -snake = Snake(world, 5, 5, Direction.SOUTH) -green_apple = Apple(world, 2, 2, AppleType.GREEN) -red_apple = Apple(world, 2, 2, AppleType.RED) +snake = Snake(world) world.spawn_entity(snake) -world.spawn_entity(green_apple) -world.spawn_entity(red_apple) +world.spawn_entity(Apple(world, AppleType.GREEN)) +world.spawn_entity(Apple(world, AppleType.GREEN)) +world.spawn_entity(Apple(world, AppleType.RED)) while True: world.render()