diff --git a/snake/game_object.py b/snake/game_object.py index 9346d83..7cd0821 100644 --- a/snake/game_object.py +++ b/snake/game_object.py @@ -5,5 +5,9 @@ class GameObject: def draw(self, surface: pygame.Surface) -> None: pass - def update(self, user_input: int) -> None: - pass \ No newline at end of file + def update(self, user_input: int, game_objs: list['GameObject']) -> None: + pass + + def get_bounding_boxes(self) -> list[pygame.rect.Rect]: + return [] + \ No newline at end of file diff --git a/snake/main.py b/snake/main.py index daa392e..c85addd 100644 --- a/snake/main.py +++ b/snake/main.py @@ -15,7 +15,7 @@ def draw_all(window: Window, game_objs: list[GameObject]) -> None: def update_all(game_objs: list[GameObject], user_input: int) -> None: for obj in game_objs: - obj.update(user_input) + obj.update(user_input, game_objs) def create_game_objects(window): diff --git a/snake/snake.py b/snake/snake.py index 6658b02..f7abdba 100644 --- a/snake/snake.py +++ b/snake/snake.py @@ -2,6 +2,7 @@ import pygame from game_object import GameObject from snake_segment import SnakeSegment from snake_state_machine import SnakeStateMachine +from wall import Wall class Snake(GameObject): @@ -12,14 +13,32 @@ class Snake(GameObject): for idx in range(length)] self.__state_machine = SnakeStateMachine() + self.__is_dead = False + + + def __collides_with_wall(self, obj: GameObject): + return isinstance(obj, Wall) and self.get_bounding_boxes()[0].collidelist(obj.get_bounding_boxes()) > -1 + + + def __update_on_collision(self, game_objs: list[GameObject]) -> None: + for obj in game_objs: + if self.__collides_with_wall(obj): + self.__is_dead = True + def draw(self, surface: pygame.Surface) -> None: for segment in self.__segments: segment.draw(surface) - def update(self, user_input: int=None) -> None: - self.__state_machine.update(user_input) - new_head = self.__state_machine.get_state().get_next_head(self.__segments[0]) - - self.__segments.insert(0, new_head) - self.__segments.pop() \ No newline at end of file + def update(self, user_input: int, game_objs: list[GameObject]) -> None: + if not self.__is_dead: + self.__update_on_collision(game_objs) + + self.__state_machine.update(user_input) + new_head = self.__state_machine.get_state().get_next_head(self.__segments[0]) + + self.__segments.insert(0, new_head) + self.__segments.pop() + + def get_bounding_boxes(self) -> list[pygame.rect.Rect]: + return [seg.get_bounding_boxes()[0] for seg in self.__segments] \ No newline at end of file diff --git a/snake/snake_segment.py b/snake/snake_segment.py index 07a860c..88621f7 100644 --- a/snake/snake_segment.py +++ b/snake/snake_segment.py @@ -1,24 +1,28 @@ import pygame from game_object import GameObject + class SnakeSegment(GameObject): def __init__(self, center: tuple|list, radius: float|int, color: tuple) -> None: - self.__center = center - self.__radius = radius + self.__bounding_box = pygame.rect.Rect(0, 0, radius*2, radius*2) + self.__bounding_box.center = center self.__color = color def draw(self, surface: pygame.Surface) -> None: pygame.draw.circle(surface=surface, color=self.__color, - center=self.__center, - radius=self.__radius) + center=self.__bounding_box.center, + radius=self.__bounding_box.width*0.5) def get_position(self) -> tuple: - return self.__center + return self.__bounding_box.center def get_radius(self) -> int|float: - return self.__radius + return self.__bounding_box.width*0.5 def get_color(self) -> tuple: - return self.__color \ No newline at end of file + return self.__color + + def get_bounding_boxes(self) -> list[pygame.rect.Rect]: + return [self.__bounding_box] \ No newline at end of file diff --git a/snake/wall.py b/snake/wall.py index 9424e76..b3ebaa1 100644 --- a/snake/wall.py +++ b/snake/wall.py @@ -18,4 +18,7 @@ class Wall(GameObject): def draw(self, surface): for wall in self.__walls: pygame.draw.rect(surface, self.__color, wall) - \ No newline at end of file + + + def get_bounding_boxes(self): + return self.__walls