Let snake grow when it hits the prey.
This commit is contained in:
parent
31e85ee33b
commit
d820318814
@ -3,6 +3,7 @@ from game_object import GameObject
|
|||||||
from snake_segment import SnakeSegment
|
from snake_segment import SnakeSegment
|
||||||
from snake_state_machine import SnakeStateMachine
|
from snake_state_machine import SnakeStateMachine
|
||||||
from wall import Wall
|
from wall import Wall
|
||||||
|
from prey import Prey
|
||||||
|
|
||||||
|
|
||||||
class Snake(GameObject):
|
class Snake(GameObject):
|
||||||
@ -14,6 +15,7 @@ class Snake(GameObject):
|
|||||||
|
|
||||||
self.__state_machine = SnakeStateMachine()
|
self.__state_machine = SnakeStateMachine()
|
||||||
self.__is_dead = False
|
self.__is_dead = False
|
||||||
|
self.__shall_grow = False
|
||||||
|
|
||||||
|
|
||||||
def __collides_with_wall(self, obj: GameObject):
|
def __collides_with_wall(self, obj: GameObject):
|
||||||
@ -24,10 +26,16 @@ class Snake(GameObject):
|
|||||||
return isinstance(obj, Snake) and self.get_bounding_boxes()[0].collidelist(obj.get_bounding_boxes()[1:]) > -1
|
return isinstance(obj, Snake) and self.get_bounding_boxes()[0].collidelist(obj.get_bounding_boxes()[1:]) > -1
|
||||||
|
|
||||||
|
|
||||||
|
def __collides_with_prey(self, obj: GameObject):
|
||||||
|
return isinstance(obj, Prey) and self.get_bounding_boxes()[0].colliderect(obj.get_bounding_boxes()[0])
|
||||||
|
|
||||||
|
|
||||||
def __update_on_collision(self, game_objs: list[GameObject]) -> None:
|
def __update_on_collision(self, game_objs: list[GameObject]) -> None:
|
||||||
for obj in game_objs:
|
for obj in game_objs:
|
||||||
if self.__collides_with_wall(obj) or self.__collides_with_self(obj):
|
if self.__collides_with_wall(obj) or self.__collides_with_self(obj):
|
||||||
self.__is_dead = True
|
self.__is_dead = True
|
||||||
|
elif self.__collides_with_prey(obj):
|
||||||
|
self.__shall_grow = True
|
||||||
|
|
||||||
|
|
||||||
def draw(self, surface: pygame.Surface) -> None:
|
def draw(self, surface: pygame.Surface) -> None:
|
||||||
@ -35,6 +43,8 @@ class Snake(GameObject):
|
|||||||
segment.draw(surface)
|
segment.draw(surface)
|
||||||
|
|
||||||
def update(self, user_input: int, game_objs: list[GameObject]) -> None:
|
def update(self, user_input: int, game_objs: list[GameObject]) -> None:
|
||||||
|
self.__shall_grow = False
|
||||||
|
|
||||||
if not self.__is_dead:
|
if not self.__is_dead:
|
||||||
self.__update_on_collision(game_objs)
|
self.__update_on_collision(game_objs)
|
||||||
|
|
||||||
@ -43,7 +53,10 @@ class Snake(GameObject):
|
|||||||
new_head = self.__state_machine.get_state().get_next_head(self.__segments[0])
|
new_head = self.__state_machine.get_state().get_next_head(self.__segments[0])
|
||||||
|
|
||||||
self.__segments.insert(0, new_head)
|
self.__segments.insert(0, new_head)
|
||||||
|
|
||||||
|
if not self.__shall_grow:
|
||||||
self.__segments.pop()
|
self.__segments.pop()
|
||||||
|
|
||||||
|
|
||||||
def get_bounding_boxes(self) -> list[pygame.rect.Rect]:
|
def get_bounding_boxes(self) -> list[pygame.rect.Rect]:
|
||||||
return [seg.get_bounding_boxes()[0] for seg in self.__segments]
|
return [seg.get_bounding_boxes()[0] for seg in self.__segments]
|
||||||
Loading…
x
Reference in New Issue
Block a user