Refactor docs
This commit is contained in:
parent
b6069a2db7
commit
70d9b856f8
1 changed files with 17 additions and 36 deletions
53
the_snake.py
53
the_snake.py
|
|
@ -44,13 +44,10 @@ clock = pygame.time.Clock()
|
||||||
|
|
||||||
# Тут опишите все классы игры.
|
# Тут опишите все классы игры.
|
||||||
class GameObject:
|
class GameObject:
|
||||||
"""
|
"""Базовый класс для объекта игры."""
|
||||||
Базовый класс для объекта игры.
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""
|
"""Инициализация объекта."""
|
||||||
Инициализация объекта.
|
|
||||||
"""
|
|
||||||
self.position_ = []
|
self.position_ = []
|
||||||
|
|
||||||
def generate_random_position(self) -> list[int]:
|
def generate_random_position(self) -> list[int]:
|
||||||
|
|
@ -66,16 +63,13 @@ class GameObject:
|
||||||
]
|
]
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
"""
|
"""Рендер объекта при помощи pygame."""
|
||||||
Рендер объекта при помощи pygame.
|
|
||||||
"""
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Food(GameObject):
|
class Food(GameObject):
|
||||||
"""
|
"""Базовые класс для еды."""
|
||||||
Базовые класс для еды.
|
|
||||||
"""
|
|
||||||
# Съедобность еды,
|
# Съедобность еды,
|
||||||
# Влияет на прибавление
|
# Влияет на прибавление
|
||||||
# или отнимание длины змейки.
|
# или отнимание длины змейки.
|
||||||
|
|
@ -89,22 +83,16 @@ class Food(GameObject):
|
||||||
CHANGE_CELLS: int
|
CHANGE_CELLS: int
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""
|
"""Инициализация еды."""
|
||||||
Инициализация еды.
|
|
||||||
"""
|
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.update_position()
|
self.update_position()
|
||||||
|
|
||||||
def update_position(self):
|
def update_position(self):
|
||||||
"""
|
"""Смена позиции еды."""
|
||||||
Смена позиции еды.
|
|
||||||
"""
|
|
||||||
self.position_ = self.generate_random_position()
|
self.position_ = self.generate_random_position()
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
"""
|
"""Рендер еды при помощи pygame."""
|
||||||
Рендер еды при помощи pygame.
|
|
||||||
"""
|
|
||||||
rect = pygame.Rect(self.position_, (GRID_SIZE, GRID_SIZE))
|
rect = pygame.Rect(self.position_, (GRID_SIZE, GRID_SIZE))
|
||||||
pygame.draw.rect(screen, self.COLOR, rect)
|
pygame.draw.rect(screen, self.COLOR, rect)
|
||||||
pygame.draw.rect(screen, BORDER_COLOR, rect, 1)
|
pygame.draw.rect(screen, BORDER_COLOR, rect, 1)
|
||||||
|
|
@ -115,6 +103,7 @@ class Apple(Food):
|
||||||
Класс для яблок.
|
Класс для яблок.
|
||||||
Наследован от Food.
|
Наследован от Food.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
IS_EATABLE = True
|
IS_EATABLE = True
|
||||||
COLOR = APPLE_COLOR
|
COLOR = APPLE_COLOR
|
||||||
CHANGE_CELLS = 2
|
CHANGE_CELLS = 2
|
||||||
|
|
@ -125,19 +114,17 @@ class Onion(Food):
|
||||||
Класс для чесноков.
|
Класс для чесноков.
|
||||||
Наследован от Food.
|
Наследован от Food.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
IS_EATABLE = False
|
IS_EATABLE = False
|
||||||
COLOR = ONION_COLOR
|
COLOR = ONION_COLOR
|
||||||
CHANGE_CELLS = 1
|
CHANGE_CELLS = 1
|
||||||
|
|
||||||
|
|
||||||
class Snake(GameObject):
|
class Snake(GameObject):
|
||||||
"""
|
"""Класс для змеек."""
|
||||||
Класс для змеек.
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""
|
"""Инициализация змейки."""
|
||||||
Инициализация змейки.
|
|
||||||
"""
|
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
self.position_.append(self.generate_random_position())
|
self.position_.append(self.generate_random_position())
|
||||||
|
|
@ -146,9 +133,7 @@ class Snake(GameObject):
|
||||||
self.change_tail = 0
|
self.change_tail = 0
|
||||||
|
|
||||||
def move(self):
|
def move(self):
|
||||||
"""
|
"""Обработка движения змейки."""
|
||||||
Обработка движения змейки.
|
|
||||||
"""
|
|
||||||
handle_keys(self)
|
handle_keys(self)
|
||||||
|
|
||||||
if self.next_direction is None:
|
if self.next_direction is None:
|
||||||
|
|
@ -210,9 +195,7 @@ class Snake(GameObject):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
"""
|
"""Рендер змейки при помощи pygame."""
|
||||||
Рендер змейки при помощи pygame.
|
|
||||||
"""
|
|
||||||
for position in self.position_:
|
for position in self.position_:
|
||||||
rect = pygame.Rect(position, (GRID_SIZE, GRID_SIZE))
|
rect = pygame.Rect(position, (GRID_SIZE, GRID_SIZE))
|
||||||
pygame.draw.rect(screen, SNAKE_COLOR, rect)
|
pygame.draw.rect(screen, SNAKE_COLOR, rect)
|
||||||
|
|
@ -280,9 +263,7 @@ def handle_keys(game_object: GameObject):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""
|
"""Точка входа для программы."""
|
||||||
Точка входа для программы.
|
|
||||||
"""
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
snake = Snake()
|
snake = Snake()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue