resize bricks

This commit is contained in:
Dmitry Belyaev 2024-03-28 00:39:34 +03:00
parent 4ef2aea6ee
commit cc5afff252

View File

@ -23,7 +23,6 @@ import os
from typing import NamedTuple from typing import NamedTuple
from contextlib import contextmanager from contextlib import contextmanager
import types
import tempfile import tempfile
import base64 import base64
import pickle import pickle
@ -81,6 +80,13 @@ class Point(NamedTuple):
return self.__class__(self.x - other, self.y - other) return self.__class__(self.x - other, self.y - other)
return NotImplemented return NotImplemented
def __floordiv__(self, other):
if isinstance(other, self.__class__):
return self.__class__(self.x // other.x, self.y // other.y)
if isinstance(other, (int, float)):
return self.__class__(self.x // other, self.y // other)
return NotImplemented
def swap(self): def swap(self):
return self.__class__(self.y, self.x) return self.__class__(self.y, self.x)
@ -103,9 +109,11 @@ data = {
def make_level(scene_sz, hard): def make_level(scene_sz, hard):
data["maze"] = maze_gen() data["maze"] = maze_gen(6, 6)
# размер ячейки # размер ячейки
box_sz = Point(scene_sz.x // len(data["maze"][0]), scene_sz.y // len(data["maze"])) maze_sz = Point(len(data["maze"][0]), len(data["maze"]))
box_sz = scene_sz // maze_sz
data["hero_pos"] = find_start(data) data["hero_pos"] = find_start(data)
data["hero_img_right"] = PIL.ImageTk.PhotoImage( data["hero_img_right"] = PIL.ImageTk.PhotoImage(
@ -116,12 +124,16 @@ def make_level(scene_sz, hard):
data["hero_moved"] = True data["hero_moved"] = True
# отрисовка стен # отрисовка стен
brick_img = PIL.Image.open(hard["assets/brick.png"])
if brick_img.size != box_sz:
brick_img = brick_img.resize(box_sz)
brick_img.save(hard["assets/brick.png"])
for i, row in enumerate(data["maze"]): for i, row in enumerate(data["maze"]):
for j, item in enumerate(row): for j, item in enumerate(row):
if item < 1: if item < 1:
continue continue
p = Point(j, i).transform(box_sz) p = Point(j, i).transform(box_sz)
obj = image(*p, hard["assets/brick.png"]) image(*p, hard["assets/brick.png"])
def update(): def update():
# отрабатываем изменение направления # отрабатываем изменение направления
@ -170,23 +182,22 @@ def move_hero(data, delta):
def keyPressed(event): def keyPressed(event):
delta = None dx = dy = 0
match event.keysym: match event.keysym:
case "Left" | "a": case "Left" | "a":
delta = Point(-1, 0) dx = -1
data["right"] = False data["right"] = False
case "Right" | "d": case "Right" | "d":
delta = Point(1, 0) dx = 1
data["right"] = True
case "Up" | "w": case "Up" | "w":
delta = Point(0, -1) dy = -1
case "Down" | "s": case "Down" | "s":
delta = Point(0, 1) dy = 1
case "Escape" | "q": case "Escape" | "q":
killTimer(data["update_timer"]) killTimer(data["update_timer"])
close() close()
if delta is not None: if dx or dy:
move_hero(data, delta) move_hero(data, Point(dx, dy))
def update(): def update():