Compare commits
No commits in common. "d5637d989eb2882d07f2ddc108b312226eb7cdd8" and "5d33259119cb8261384a6e40edcb043993a70089" have entirely different histories.
d5637d989e
...
5d33259119
@ -38,7 +38,7 @@ if __name__ == '__main__':
|
|||||||
game_objs = create_game_objects(window)
|
game_objs = create_game_objects(window)
|
||||||
|
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
framerate = 15
|
framerate = 25
|
||||||
|
|
||||||
last_input = None
|
last_input = None
|
||||||
|
|
||||||
|
|||||||
@ -8,34 +8,14 @@ class Prey(GameObject):
|
|||||||
self.__bounding_box = pygame.rect.Rect(0, 0, size, size)
|
self.__bounding_box = pygame.rect.Rect(0, 0, size, size)
|
||||||
self.__color = color
|
self.__color = color
|
||||||
self.__window_size = window_size
|
self.__window_size = window_size
|
||||||
self.__next_pos = None
|
|
||||||
self.__place_randomly()
|
self.__place_randomly()
|
||||||
|
|
||||||
def __place_randomly(self) -> None:
|
def __place_randomly(self) -> None:
|
||||||
self.__next_pos = [random.randint(0, max_val)
|
self.__bounding_box.center = [random.randint(0, max_val)
|
||||||
for max_val in self.__window_size]
|
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:
|
def draw(self, surface: pygame.surface.Surface) -> None:
|
||||||
pygame.draw.rect(surface, color=self.__color, rect=self.__bounding_box)
|
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,7 +3,6 @@ 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):
|
||||||
@ -15,7 +14,6 @@ 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):
|
||||||
@ -26,16 +24,10 @@ 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:
|
||||||
@ -43,8 +35,6 @@ 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)
|
||||||
|
|
||||||
@ -53,10 +43,7 @@ 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