Add length for snake.
This commit is contained in:
parent
30c4773f13
commit
7833713d31
@ -15,6 +15,7 @@ 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()
|
||||||
game_objs = [Snake(start_position=(400, 300),
|
game_objs = [Snake(start_position=(400, 300),
|
||||||
|
length=5,
|
||||||
radius=10,
|
radius=10,
|
||||||
color=(255, 0, 255))]
|
color=(255, 0, 255))]
|
||||||
|
|
||||||
|
|||||||
@ -1,15 +1,15 @@
|
|||||||
import pygame
|
import pygame
|
||||||
from game_object import GameObject
|
from game_object import GameObject
|
||||||
|
from snake_segment import SnakeSegment
|
||||||
|
|
||||||
|
|
||||||
class Snake(GameObject):
|
class Snake(GameObject):
|
||||||
def __init__(self, start_position: tuple|list, radius: float|int, color: tuple) -> None:
|
def __init__(self, start_position: tuple|list, length: int, radius: float|int, color: tuple) -> None:
|
||||||
self.__pos = start_position
|
self.__segments = [SnakeSegment(center=(start_position[0]+2*radius*idx, start_position[1]),
|
||||||
self.__radius = radius
|
radius=radius,
|
||||||
self.__color = color
|
color=color)
|
||||||
|
for idx in range(length)]
|
||||||
|
|
||||||
def draw(self, surface: pygame.Surface) -> None:
|
def draw(self, surface: pygame.Surface) -> None:
|
||||||
pygame.draw.circle(surface=surface,
|
for segment in self.__segments:
|
||||||
color=self.__color,
|
segment.draw(surface)
|
||||||
center=self.__pos,
|
|
||||||
radius=self.__radius)
|
|
||||||
15
snake/snake_segment.py
Normal file
15
snake/snake_segment.py
Normal 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)
|
||||||
Loading…
x
Reference in New Issue
Block a user