phc: + hero move accel

This commit is contained in:
Dmitry Belyaev 2024-04-01 00:40:23 +03:00
parent 98a2e240e3
commit 49f15f9a21

View File

@ -27,6 +27,7 @@ import tempfile
import urllib.request import urllib.request
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from contextlib import contextmanager from contextlib import contextmanager
from enum import Enum
from random import choice, randrange, sample from random import choice, randrange, sample
from typing import NamedTuple, Optional from typing import NamedTuple, Optional
@ -211,6 +212,13 @@ def get_maze_sz(maze: list[list[int]]) -> Coords:
return Coords(len(maze[0]), len(maze)) return Coords(len(maze[0]), len(maze))
class Direction(Enum):
LEFT = 1
RIGHT = 2
UP = 3
DOWN = 4
class GameObject(ABC): class GameObject(ABC):
"""обобщение игрового элемента""" """обобщение игрового элемента"""
@ -274,6 +282,8 @@ class Hero(GameObject):
self.rect.topleft = coords self.rect.topleft = coords
self.active = True self.active = True
self.looking_right = False self.looking_right = False
self._speed = 1
self.direction = Direction.RIGHT
# картинка изначально влево, а надо бы начинать со взгляда вправо # картинка изначально влево, а надо бы начинать со взгляда вправо
self.flip() self.flip()
@ -284,8 +294,17 @@ class Hero(GameObject):
"""Проверка пересечения со стенами""" """Проверка пересечения со стенами"""
new_rect = self.rect.copy() new_rect = self.rect.copy()
new_rect.topleft = coords new_rect.topleft = coords
new_rect.scale_by_ip(0.99, 0.99)
return self.scene.walls.check_collision(new_rect) return self.scene.walls.check_collision(new_rect)
@property
def speed(self):
return self._speed
@speed.setter
def speed(self, value):
self._speed = value % 14 + 1
def _reduce_step(self, coords): def _reduce_step(self, coords):
"""Уменьшение шага движения, с целью подойти вплотную к стене""" """Уменьшение шага движения, с целью подойти вплотную к стене"""
delta = coords - self.coords delta = coords - self.coords
@ -315,12 +334,30 @@ class Hero(GameObject):
self.looking_right = not self.looking_right self.looking_right = not self.looking_right
self._surface = pygame.transform.flip(self.surface, True, False) self._surface = pygame.transform.flip(self.surface, True, False)
def move(self, direction: Coords, step: int = 1): def update_direction(self, direction: Coords):
if direction.x != 0: if direction.x != 0:
going_right = direction.x > 0 going_right = direction.x > 0
if self.looking_right != going_right: if self.looking_right != going_right:
self.flip() self.flip()
self.coords += direction * step
if direction.x < 0:
new_direction = Direction.LEFT
elif direction.x > 0:
new_direction = Direction.RIGHT
elif direction.y < 0:
new_direction = Direction.UP
elif direction.y > 0:
new_direction = Direction.DOWN
if new_direction != self.direction:
self.speed = 0
self.direction = new_direction
else:
self.speed += 1
def move(self, direction: Coords, step: int = 1):
self.update_direction(direction)
self.coords += direction * step * self._speed // 3
self.scene.coins.collect(self.rect) self.scene.coins.collect(self.rect)
def move_left(self, step: int = 1): def move_left(self, step: int = 1):
@ -339,7 +376,7 @@ class Hero(GameObject):
if not self.active: if not self.active:
return return
wide, short = 10, 2 wide, short = 5, 1
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
match event.key: match event.key:
case pygame.K_UP: case pygame.K_UP: