Move snake.
This commit is contained in:
parent
7833713d31
commit
b354a7dd90
@ -1,3 +1,6 @@
|
|||||||
class GameObject:
|
class GameObject:
|
||||||
def draw(self, surface):
|
def draw(self, surface):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
pass
|
||||||
@ -4,13 +4,18 @@ from input_manager import InputManager
|
|||||||
from snake import Snake
|
from snake import Snake
|
||||||
from game_object import GameObject
|
from game_object import GameObject
|
||||||
|
|
||||||
def draw(window: Window, game_objs: list[GameObject]) -> None:
|
def draw_all(window: Window, game_objs: list[GameObject]) -> None:
|
||||||
window.reset()
|
window.reset()
|
||||||
|
|
||||||
for obj in game_objs:
|
for obj in game_objs:
|
||||||
window.draw_object(obj)
|
window.draw_object(obj)
|
||||||
|
|
||||||
|
|
||||||
|
def update_all(game_objs: list[GameObject]) -> None:
|
||||||
|
for obj in game_objs:
|
||||||
|
obj.update()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
window = Window(title='Snake', size=(800, 600), background_color=(0, 128, 0))
|
window = Window(title='Snake', size=(800, 600), background_color=(0, 128, 0))
|
||||||
input_manager = InputManager()
|
input_manager = InputManager()
|
||||||
@ -27,7 +32,8 @@ if __name__ == '__main__':
|
|||||||
while last_input != InputManager.QUIT:
|
while last_input != InputManager.QUIT:
|
||||||
last_input = input_manager.process_input()
|
last_input = input_manager.process_input()
|
||||||
|
|
||||||
draw(window, game_objs)
|
draw_all(window, game_objs)
|
||||||
|
update_all(game_objs)
|
||||||
|
|
||||||
clock.tick(framerate)
|
clock.tick(framerate)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|||||||
@ -13,3 +13,15 @@ class Snake(GameObject):
|
|||||||
def draw(self, surface: pygame.Surface) -> None:
|
def draw(self, surface: pygame.Surface) -> None:
|
||||||
for segment in self.__segments:
|
for segment in self.__segments:
|
||||||
segment.draw(surface)
|
segment.draw(surface)
|
||||||
|
|
||||||
|
def update(self) -> None:
|
||||||
|
head_pos = self.__segments[0].get_position()
|
||||||
|
head_radius = self.__segments[0].get_radius()
|
||||||
|
head_color = self.__segments[0].get_color()
|
||||||
|
|
||||||
|
new_head = SnakeSegment(center=(head_pos[0]-2*head_radius, head_pos[1]),
|
||||||
|
radius=head_radius,
|
||||||
|
color=head_color)
|
||||||
|
|
||||||
|
self.__segments.insert(0, new_head)
|
||||||
|
self.__segments.pop()
|
||||||
@ -13,3 +13,12 @@ class SnakeSegment(GameObject):
|
|||||||
color=self.__color,
|
color=self.__color,
|
||||||
center=self.__center,
|
center=self.__center,
|
||||||
radius=self.__radius)
|
radius=self.__radius)
|
||||||
|
|
||||||
|
def get_position(self) -> tuple:
|
||||||
|
return self.__center
|
||||||
|
|
||||||
|
def get_radius(self) -> int|float:
|
||||||
|
return self.__radius
|
||||||
|
|
||||||
|
def get_color(self) -> tuple:
|
||||||
|
return self.__color
|
||||||
Loading…
x
Reference in New Issue
Block a user