2026-04-16 16:48:29 +02:00

27 lines
1.0 KiB
Python

import pygame
from game_object import GameObject
from snake_segment import SnakeSegment
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)]
def draw(self, surface: pygame.Surface) -> None:
for segment in self.__segments:
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()