Add length for snake.

This commit is contained in:
paulusja 2026-04-16 16:30:56 +02:00
parent 30c4773f13
commit 7833713d31
3 changed files with 24 additions and 8 deletions

View File

@ -15,6 +15,7 @@ if __name__ == '__main__':
window = Window(title='Snake', size=(800, 600), background_color=(0, 128, 0))
input_manager = InputManager()
game_objs = [Snake(start_position=(400, 300),
length=5,
radius=10,
color=(255, 0, 255))]

View File

@ -1,15 +1,15 @@
import pygame
from game_object import GameObject
from snake_segment import SnakeSegment
class Snake(GameObject):
def __init__(self, start_position: tuple|list, radius: float|int, color: tuple) -> None:
self.__pos = start_position
self.__radius = radius
self.__color = color
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:
pygame.draw.circle(surface=surface,
color=self.__color,
center=self.__pos,
radius=self.__radius)
for segment in self.__segments:
segment.draw(surface)

15
snake/snake_segment.py Normal file
View File

@ -0,0 +1,15 @@
import pygame
from game_object import GameObject
class SnakeSegment(GameObject):
def __init__(self, center: tuple|list, radius: float|int, color: tuple) -> None:
self.__center = center
self.__radius = radius
self.__color = color
def draw(self, surface: pygame.Surface) -> None:
pygame.draw.circle(surface=surface,
color=self.__color,
center=self.__center,
radius=self.__radius)