se_uebungen/snake/snake.py
2026-04-23 14:39:41 +02:00

25 lines
990 B
Python

import pygame
from game_object import GameObject
from snake_segment import SnakeSegment
from snake_state_machine import SnakeStateMachine
class Snake(GameObject):
def __init__(self, start_position: tuple|list, length: int, radius: float|int, color: tuple) -> None:
self.__segments = [SnakeSegment(center=(start_position[0]+2*radius*idx, start_position[1]),
radius=radius,
color=color)
for idx in range(length)]
self.__state_machine = SnakeStateMachine()
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()