Add game objects
This commit is contained in:
parent
9d4e40985a
commit
25b286e9d4
1 changed files with 75 additions and 8 deletions
79
the_snake.py
79
the_snake.py
|
|
@ -1,3 +1,4 @@
|
|||
from abc import ABC, abstractclassmethod
|
||||
from random import choice, randint
|
||||
|
||||
import pygame
|
||||
|
|
@ -33,30 +34,96 @@ SPEED = 20
|
|||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32)
|
||||
|
||||
# Заголовок окна игрового поля:
|
||||
pygame.display.set_caption('Змейка')
|
||||
pygame.display.set_caption("Змейка")
|
||||
|
||||
# Настройка времени:
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
|
||||
# Тут опишите все классы игры.
|
||||
...
|
||||
class GameObject(ABC):
|
||||
GRID_HEIGHT: int = GRID_HEIGHT
|
||||
GRID_WIDTH: int = GRID_WIDTH
|
||||
GRID_SIZE: int = GRID_SIZE
|
||||
COLOR: tuple
|
||||
BORDER_COLOR: tuple
|
||||
|
||||
def __init__(
|
||||
self, color: tuple, border_color: tuple, position: list = None
|
||||
):
|
||||
self.COLOR = color
|
||||
self.BORDER_COLOR = border_color
|
||||
self.position_ = position or [self.generate_random_position()]
|
||||
|
||||
def generate_random_position(self) -> list[int]:
|
||||
return [
|
||||
randint(0, self.GRID_WIDTH - 1),
|
||||
randint(0, self.GRID_HEIGHT - 1),
|
||||
]
|
||||
|
||||
@abstractclassmethod
|
||||
def draw(cls):
|
||||
pass
|
||||
|
||||
|
||||
class Snake(GameObject):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def move(self):
|
||||
pass
|
||||
|
||||
def draw(self):
|
||||
pass
|
||||
|
||||
def update_keys(self):
|
||||
pass
|
||||
|
||||
|
||||
class Apple(GameObject):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def update_position(self):
|
||||
self.position_ = self.generate_random_position()
|
||||
|
||||
@property
|
||||
def position(self):
|
||||
return self.position_[0]
|
||||
|
||||
@position.setter
|
||||
def position(self, value: int):
|
||||
self.position_[0] = value
|
||||
|
||||
def draw(self):
|
||||
rect = pygame.Rect(
|
||||
self.position, (self.GRID_SIZE, self.GRID_SIZE)
|
||||
)
|
||||
pygame.draw.rect(screen, self.COLOR, rect)
|
||||
pygame.draw.rect(screen, self.BORDER_COLOR, rect, 1)
|
||||
|
||||
|
||||
def main():
|
||||
# Инициализация PyGame:
|
||||
pygame.init()
|
||||
# Тут нужно создать экземпляры классов.
|
||||
...
|
||||
snake = Snake(color=SNAKE_COLOR, border_color=BORDER_COLOR)
|
||||
apple = Apple(color=APPLE_COLOR, border_color=BORDER_COLOR)
|
||||
|
||||
# while True:
|
||||
# clock.tick(SPEED)
|
||||
while True:
|
||||
snake.update_keys()
|
||||
snake.move()
|
||||
|
||||
snake.draw()
|
||||
apple.draw()
|
||||
|
||||
clock.tick(SPEED)
|
||||
|
||||
# Тут опишите основную логику игры.
|
||||
# ...
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue