Compare commits
4 Commits
5d33259119
...
d5637d989e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d5637d989e | ||
|
|
a34198e290 | ||
|
|
d820318814 | ||
|
|
31e85ee33b |
@ -38,7 +38,7 @@ if __name__ == '__main__':
|
||||
game_objs = create_game_objects(window)
|
||||
|
||||
clock = pygame.time.Clock()
|
||||
framerate = 25
|
||||
framerate = 15
|
||||
|
||||
last_input = None
|
||||
|
||||
|
||||
@ -8,14 +8,34 @@ class Prey(GameObject):
|
||||
self.__bounding_box = pygame.rect.Rect(0, 0, size, size)
|
||||
self.__color = color
|
||||
self.__window_size = window_size
|
||||
self.__next_pos = None
|
||||
self.__place_randomly()
|
||||
|
||||
def __place_randomly(self) -> None:
|
||||
self.__bounding_box.center = [random.randint(0, max_val)
|
||||
for max_val in self.__window_size]
|
||||
self.__next_pos = [random.randint(0, max_val)
|
||||
for max_val in self.__window_size]
|
||||
|
||||
|
||||
def __collides_with_other(self, pos: tuple, game_objs: list[GameObject]) -> bool:
|
||||
box = pygame.rect.Rect(self.__bounding_box)
|
||||
box.center = pos
|
||||
|
||||
for obj in game_objs:
|
||||
if not isinstance(obj, Prey) and box.collidelist(obj.get_bounding_boxes()) > -1:
|
||||
return True
|
||||
|
||||
|
||||
def draw(self, surface: pygame.surface.Surface) -> None:
|
||||
pygame.draw.rect(surface, color=self.__color, rect=self.__bounding_box)
|
||||
|
||||
|
||||
def update(self, user_input: int, game_objs: list[GameObject]):
|
||||
self.__bounding_box.center = self.__next_pos
|
||||
|
||||
while self.__collides_with_other(self.__next_pos, game_objs):
|
||||
self.__place_randomly()
|
||||
|
||||
|
||||
def get_bounding_boxes(self) -> list[pygame.rect.Rect]:
|
||||
return [self.__bounding_box]
|
||||
|
||||
|
||||
@ -3,6 +3,7 @@ from game_object import GameObject
|
||||
from snake_segment import SnakeSegment
|
||||
from snake_state_machine import SnakeStateMachine
|
||||
from wall import Wall
|
||||
from prey import Prey
|
||||
|
||||
|
||||
class Snake(GameObject):
|
||||
@ -14,6 +15,7 @@ class Snake(GameObject):
|
||||
|
||||
self.__state_machine = SnakeStateMachine()
|
||||
self.__is_dead = False
|
||||
self.__shall_grow = False
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
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:
|
||||
for obj in game_objs:
|
||||
if self.__collides_with_wall(obj) or self.__collides_with_self(obj):
|
||||
self.__is_dead = True
|
||||
elif self.__collides_with_prey(obj):
|
||||
self.__shall_grow = True
|
||||
|
||||
|
||||
def draw(self, surface: pygame.Surface) -> None:
|
||||
@ -35,6 +43,8 @@ class Snake(GameObject):
|
||||
segment.draw(surface)
|
||||
|
||||
def update(self, user_input: int, game_objs: list[GameObject]) -> None:
|
||||
self.__shall_grow = False
|
||||
|
||||
if not self.__is_dead:
|
||||
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])
|
||||
|
||||
self.__segments.insert(0, new_head)
|
||||
self.__segments.pop()
|
||||
|
||||
if not self.__shall_grow:
|
||||
self.__segments.pop()
|
||||
|
||||
|
||||
def get_bounding_boxes(self) -> list[pygame.rect.Rect]:
|
||||
return [seg.get_bounding_boxes()[0] for seg in self.__segments]
|
||||
Loading…
x
Reference in New Issue
Block a user