Compare commits

..

No commits in common. "512bd42572eafe36c764863e57cc49c585fdb5d2" and "6efab241a5e865016b1227b84670b45ed9326504" have entirely different histories.

View File

@ -10,7 +10,9 @@
Картинки берутся из папки assets, если их нет то автоматически скачиваются из репозитория.
Если скачать не получилось то будут нарисованы заглушки.
Зависимости: pygame
Зависимости: pygame pillow
TODO: отбработка выхода из лабиринта
"""
import os
@ -19,10 +21,12 @@ import tempfile
import urllib.request
from abc import ABC, abstractmethod
from contextlib import contextmanager
from random import choice, randrange, sample
from random import choice, randrange
from typing import NamedTuple, Optional
import PIL
import pygame
from PIL import ImageDraw
def download_asset(asset, path):
@ -43,20 +47,13 @@ def download_asset(asset, path):
def make_stub_image(path, name):
"""Создание пустой картинки, на случай если скачать не получилось"""
img = pygame.surface.Surface((200, 200), flags=pygame.SRCALPHA)
img.fill((255, 255, 255, 0))
pygame.draw.line(img, "#ff000065", (5, 5), (195, 195), 2)
pygame.draw.line(img, "#ff000065", (195, 5), (5, 195), 2)
rect = pygame.Rect(5, 5, 190, 190)
pygame.draw.rect(img, "black", rect, 3)
font = pygame.font.SysFont("Arial", 44)
text1 = font.render(name, True, "blue")
text1_rect = text1.get_rect()
text1_rect.center = img.get_rect().center
img.blit(text1, text1_rect)
pygame.image.save(img, path)
img = PIL.Image.new("RGBA", (200, 200))
draw = ImageDraw.Draw(img)
draw.rectangle([(50, 50), (150, 150)], outline="black", width=2)
draw.line((50, 50, 150, 150), fill="red", width=2)
draw.line((50, 150, 150, 50), fill="red", width=2)
draw.text((50, 170), name, fill="blue")
img.save(path)
@contextmanager
@ -134,6 +131,16 @@ class Coords(NamedTuple):
return cls(0, 0)
def resize_img(assets: dict, name: str, sz: Coords):
"""
Изменение размера картинки и сохранение в файл
"""
img = PIL.Image.open(assets[name])
if img.size != sz:
img = img.resize(sz)
img.save(assets[name])
def choose_plural(amount, declensions):
"""Возвращает количество объектов в виде строки
например 5 копеек, 1 копейка и т.д.
@ -276,13 +283,8 @@ class Hero(GameObject):
super().__init__(coords, parent, assets)
self._surface = pygame.image.load(self.assets["ghost.png"])
self.rect = self.surface.get_rect()
sf = Coords(0.8, 0.8)
self._surface, self.rect = self.scene.scale_box(self.surface, self.rect, sf)
self.rect.topleft = coords
self.active = True
self.looking_right = False
# картинка изначально влево, а надо бы начинать со взгляда вправо
self.flip()
def draw(self):
self.parent.surface.blit(self.surface, self.rect)
@ -318,57 +320,36 @@ class Hero(GameObject):
has_collision = self._check_collision(coords)
super().set_coords(coords)
def flip(self):
self.looking_right = not self.looking_right
self._surface = pygame.transform.flip(self.surface, True, False)
def move(self, direction: Coords, step: int = 1):
if direction.x != 0:
going_right = direction.x > 0
if self.looking_right != going_right:
self.flip()
self.coords += direction * step
self.scene.coins.collect(self.rect)
def move_left(self, step: int = 1):
self.move(Coords(-1, 0), step)
def move_right(self, step: int = 1):
self.move(Coords(1, 0), step)
def move_up(self, step: int = 1):
self.move(Coords(0, -1), step)
def move_down(self, step: int = 1):
self.move(Coords(0, 1), step)
def handle_event(self, event: pygame.event.Event):
if not self.active:
return
wide, short = 30, 5
up, down = Coords(0, -1), Coords(0, 1)
left, right = Coords(-1, 0), Coords(1, 0)
da, db = 30, 5
if event.type == pygame.KEYDOWN:
match event.key:
case pygame.K_UP:
self.move_up(wide)
self.coords += up * da
case pygame.K_DOWN:
self.move_down(wide)
self.coords += down * da
case pygame.K_LEFT:
self.move_left(wide)
self.coords += left * da
case pygame.K_RIGHT:
self.move_right(wide)
self.coords += right * da
case pygame.K_w:
self.move_up(short)
self.coords += up * db
case pygame.K_s:
self.move_down(short)
self.coords += down * db
case pygame.K_a:
self.move_left(short)
self.coords += left * db
case pygame.K_d:
self.move_right(short)
self.coords += right * db
class WallBlock(GameObject):
"""объект элемента стены"""
"""объект елемента стены"""
def __init__(
self,
@ -380,9 +361,6 @@ class WallBlock(GameObject):
self._surface = pygame.image.load(self.assets["brick.png"])
self.rect = self.surface.get_rect()
self.rect.topleft = coords
# уменьшаем размер монетки
sf = Coords(1, 1)
self._surface, self.rect = self.scene.scale_box(self.surface, self.rect, sf)
def draw(self):
self.parent.surface.blit(self.surface, self.rect)
@ -418,103 +396,6 @@ class Walls(GameObject):
return False
class Coin(GameObject):
"""объект монетки"""
def __init__(
self,
coords: Coords,
parent: GameObject,
assets: dict | None = None,
):
super().__init__(coords, parent, assets)
self._surface = pygame.image.load(self.assets["coin.png"])
self.rect = self.surface.get_rect()
self.rect.topleft = coords
# уменьшаем размер монетки
sf = Coords(0.7, 0.7)
self._surface, self.rect = self.scene.scale_box(self.surface, self.rect, sf)
def draw(self):
self.parent.surface.blit(self.surface, self.rect)
class Coins(GameObject):
"""объект коллекции монеток"""
def __init__(
self,
parent: GameObject,
maze: list[list[int]],
box_sz: Coords,
count: int,
assets: dict | None = None,
):
super().__init__(Coords.zero(), parent, assets)
self.box_sz = box_sz
self._capacity = count
free_points = []
excluded = Coords(0, 1), get_maze_sz(maze) - Coords(1, 2)
for i, row in enumerate(maze):
for j, item in enumerate(row):
p = Coords(j, i)
if item < 1 and p not in excluded:
free_points.append(p)
continue
coin_points = sample(free_points, min(count, len(free_points)))
self.coins = [
Coin(point.transform(box_sz), self, self.assets) for point in coin_points
]
self.collected_coins = []
# Надпись, если все монетки собраны
font = pygame.font.SysFont("Arial", 30)
text = "Все монетки собраны!"
self.done_txt = font.render(text, 1, "#050366e3")
self.done_txt_rect = self.done_txt.get_rect()
self.done_txt_rect.topleft = Coords(10, 10)
@property
def capacity(self):
return self._capacity
@property
def coins_left(self):
return len(self.coins)
@property
def coins_collected(self):
return self.capacity - self.coins_left
@property
def all_collected(self):
return self.coins_left == 0
def draw(self):
for coin in self.collected_coins:
coin.draw()
for coin in self.coins:
coin.draw()
if self.all_collected:
self.parent.surface.blit(self.done_txt, self.done_txt_rect)
def add_to_collected(self, coin: Coin):
last_pos = Coords(10, 10)
if self.collected_coins:
last_pos = Coords(*self.collected_coins[-1].rect.topright)
last_pos -= Coords(coin.rect.width // 2, 0)
coin.coords = last_pos
self.collected_coins.append(coin)
def collect(self, rect: pygame.Rect):
mined = [*filter(lambda coin: coin.rect.colliderect(rect), self.coins)]
for coin in mined:
self.coins.remove(coin)
self.add_to_collected(coin)
class EndLevel(GameObject):
def __init__(self, scene: GameObject):
super().__init__(Coords.zero(), scene, scene.assets)
@ -536,19 +417,10 @@ class EndLevel(GameObject):
self.hint_rect.center = self.parent.rect.center
self.hint_rect = self.hint_rect.move(Coords(0, 300))
# Надпись для хорошего финала
text = "Все монетки собраны!"
self.goodtxt = font_hint.render(text, 1, "#96081ba4")
self.goodtxt_rect = self.goodtxt.get_rect()
self.goodtxt_rect.center = self.parent.rect.center
self.goodtxt_rect = self.goodtxt_rect.move(Coords(0, -100))
def draw(self):
if not self.active:
return
if self.scene.coins.all_collected:
self.parent.surface.blit(self.image, self.rect)
self.parent.surface.blit(self.goodtxt, self.goodtxt_rect)
self.parent.surface.blit(self.surface, self.rect)
self.parent.surface.blit(self.hint, self.hint_rect)
@ -567,36 +439,34 @@ class Scene(GameObject):
# кнопки для выхода из игры
exit_keys = (pygame.K_ESCAPE, pygame.K_q)
def __init__(
self, assets: dict, screen_sz: Coords, maze_sz: Coords, coins_count: int
):
def __init__(self, assets: dict, screen_sz: Coords, maze_sz: Coords):
super().__init__(Coords.zero(), None, assets)
self.maze = maze_gen(*maze_sz)
maze_sz = get_maze_sz(self.maze)
box_sz = screen_sz // get_maze_sz(self.maze)
self.box_sz = box_sz
self._surface = pygame.display.set_mode(screen_sz)
self.surface.fill("white")
self.rect = self._surface.get_rect()
self.background = pygame.image.load(self.assets["bg1k.png"])
self.background = pygame.transform.scale(self.background, self.rect.size)
resize_img(self.assets, "brick.png", box_sz)
hero_sz = Coords(*map(int, box_sz * 0.8))
resize_img(self.assets, "ghost.png", hero_sz)
hero_y_offset = (box_sz.y - hero_sz.y) // 2 + box_sz.y
self._surface = pygame.display.set_mode(screen_sz)
self.rect = self._surface.get_rect()
self.hero = Hero(Coords(0, hero_y_offset), self)
resize_img(assets, "bg1k.png", screen_sz)
self.background = pygame.image.load(self.assets["bg1k.png"])
self.done = False
self.level_completed = False
self.maze = maze_gen(6, 6)
self.walls = Walls(self, self.maze, box_sz)
self.coins = Coins(self, self.maze, box_sz, coins_count)
self.end = EndLevel(self)
self.end.active = False
self.want_new_level = False
self.exit_rect = self.get_exit_rect()
# #для тестирования экрана конца уровня
# для тестирования экрана конца уровня
# self.hero.coords = Coords(*self.exit_rect.topleft) + Coords(
# -self.box_sz.x // 2, 5
# )
@ -621,22 +491,13 @@ class Scene(GameObject):
def draw(self):
if self.done:
return
self.surface.fill("white")
self.surface.blit(self.background, self.coords)
if self.level_completed:
self.end.draw()
else:
# pygame.draw.rect(self._surface, pygame.Color("#42c53d25"), self.get_exit_rect())
self.hero.draw()
self.walls.draw()
self.coins.draw()
def scale_box(
self, surface: pygame.Surface, rect: pygame.Rect, scale_factor: Coords
):
rect.size = self.box_sz
rect.scale_by_ip(*scale_factor)
surface = pygame.transform.scale(surface, rect.size)
return surface, rect
def handle_event(self, event: pygame.event.Event):
if self.done:
@ -661,14 +522,14 @@ class Scene(GameObject):
def game(assets):
pygame.init()
screen_sz = Coords(1000, 1000)
maze_sz = Coords(6, 6)
coins_count = 10
pygame.display.set_caption("Призрачный лабиринт: сокровища небесного замка")
pygame.display.set_caption("Движение рисунка на Pygame")
want_new_level = True
while want_new_level:
scene = Scene(assets, screen_sz, maze_sz, coins_count)
scene = Scene(assets, screen_sz, maze_sz)
scene.event_loop()
want_new_level = scene.want_new_level
@ -676,13 +537,11 @@ def game(assets):
def main():
pygame.init()
assets = [
"bg1k.png",
"ghost.png",
"brick.png",
"win.png",
"coin.png",
]
with get_assets(assets) as assets:
game(assets)