Add snake collision with wall.
This commit is contained in:
parent
a7735e2084
commit
0e72331f45
@ -5,5 +5,9 @@ class GameObject:
|
||||
def draw(self, surface: pygame.Surface) -> None:
|
||||
pass
|
||||
|
||||
def update(self, user_input: int) -> None:
|
||||
pass
|
||||
def update(self, user_input: int, game_objs: list['GameObject']) -> None:
|
||||
pass
|
||||
|
||||
def get_bounding_boxes(self) -> list[pygame.rect.Rect]:
|
||||
return []
|
||||
|
||||
@ -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):
|
||||
|
||||
@ -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()
|
||||
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]
|
||||
@ -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
|
||||
return self.__color
|
||||
|
||||
def get_bounding_boxes(self) -> list[pygame.rect.Rect]:
|
||||
return [self.__bounding_box]
|
||||
@ -18,4 +18,7 @@ class Wall(GameObject):
|
||||
def draw(self, surface):
|
||||
for wall in self.__walls:
|
||||
pygame.draw.rect(surface, self.__color, wall)
|
||||
|
||||
|
||||
|
||||
def get_bounding_boxes(self):
|
||||
return self.__walls
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user