Compare commits
No commits in common. "49f15f9a21ae4dfd34fef7ddc1007e7ab015c6cb" and "81bebce3e930e2e33359e51bcba2916bf37ed7ae" have entirely different histories.
49f15f9a21
...
81bebce3e9
@ -27,7 +27,6 @@ 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
|
||||||
|
|
||||||
@ -212,13 +211,6 @@ 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):
|
||||||
"""обобщение игрового элемента"""
|
"""обобщение игрового элемента"""
|
||||||
|
|
||||||
@ -282,8 +274,6 @@ 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()
|
||||||
|
|
||||||
@ -294,17 +284,8 @@ 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
|
||||||
@ -334,30 +315,12 @@ 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 update_direction(self, direction: Coords):
|
def move(self, direction: Coords, step: int = 1):
|
||||||
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):
|
||||||
@ -376,7 +339,7 @@ class Hero(GameObject):
|
|||||||
if not self.active:
|
if not self.active:
|
||||||
return
|
return
|
||||||
|
|
||||||
wide, short = 5, 1
|
wide, short = 30, 5
|
||||||
if event.type == pygame.KEYDOWN:
|
if event.type == pygame.KEYDOWN:
|
||||||
match event.key:
|
match event.key:
|
||||||
case pygame.K_UP:
|
case pygame.K_UP:
|
||||||
@ -683,14 +646,11 @@ class Scene(GameObject):
|
|||||||
self.end.handle_event(event)
|
self.end.handle_event(event)
|
||||||
|
|
||||||
def event_loop(self):
|
def event_loop(self):
|
||||||
clock = pygame.time.Clock()
|
|
||||||
pygame.key.set_repeat(50, 30)
|
|
||||||
while not self.done:
|
while not self.done:
|
||||||
for event in pygame.event.get():
|
event = pygame.event.wait()
|
||||||
self.handle_event(event)
|
self.handle_event(event)
|
||||||
self.draw()
|
self.draw()
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
clock.tick(30)
|
|
||||||
|
|
||||||
|
|
||||||
def game(assets):
|
def game(assets):
|
||||||
|
Loading…
Reference in New Issue
Block a user