py_stepik/mod_graph/anim4_img.py

142 lines
78 KiB
Python
Raw Permalink Normal View History

2024-03-24 13:29:37 +00:00
"""
https://stepik.org/lesson/468999/step/4?unit=459820
Рисунок управляется стрелками
Для выхода нужно нажать Q или Esc
Картинки захардкожены в прямо в скрипт
Зависимости: graph pillow
Библиотека graph: https://kpolyakov.spb.ru/download/pygraph.zip
"""
import graph
from graph import *
from typing import NamedTuple
from contextlib import contextmanager
import types
import tempfile
import base64
consts = types.SimpleNamespace()
consts.VK_Q = 81
consts.VK_W = 87
consts.VK_A = 65
consts.VK_S = 83
consts.VK_D = 68
# объявление переменные для хранения картинки
hardcoded_image: bytes
hardcoded_bg: bytes
class Point(NamedTuple):
"""
Вспомогательный класс для упрощения работы с координатами (сложение)
"""
x: int | float
y: int | float
def __add__(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 __sub__(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
data = {"delta": Point(0, 0), "callbacks": []}
@contextmanager
def hardcoded_as_tmp(b64data: bytes, ext: str = ".png"):
with tempfile.NamedTemporaryFile(
mode="wb", suffix=ext, delete_on_close=False
) as fp:
data = base64.b64decode(b64data)
fp.write(data)
fp.close()
yield fp.name
def make_ghost_on_scene(start_pos=None):
start_pos = start_pos or Point(0, 0)
scene_sz = Point(1000, 1000)
box_sz = Point(200, 150)
box_offset = Point(400, 425)
# рисуем фон
with hardcoded_as_tmp(hardcoded_bg) as bg_filename:
image(0, 0, bg_filename)
# рисуем картинку
box_pos = start_pos + box_offset
with hardcoded_as_tmp(hardcoded_image) as img_filename:
obj = image(*box_pos, img_filename)
def update():
delta = data["delta"]
coord = Point(xCoord(obj), yCoord(obj)) - start_pos
if (
coord.x + delta.x < 0
or coord.x + box_sz.x + delta.x > scene_sz.x
or coord.y + delta.y < 0
or coord.y + box_sz.y + delta.y > scene_sz.y
):
return
moveObjectBy(obj, delta.x, delta.y)
data.setdefault("callbacks", []).append(update)
def keyPressed(event):
match event.keycode:
case graph.VK_LEFT:
data["delta"] = Point(-15, 0)
case graph.VK_RIGHT:
data["delta"] = Point(15, 0)
case graph.VK_UP:
data["delta"] = Point(0, -15)
case graph.VK_DOWN:
data["delta"] = Point(0, 15)
case consts.VK_Q | graph.VK_ESCAPE:
killTimer(data["update_timer"])
close()
def update():
for callback in data["callbacks"]:
callback()
data["delta"] = Point(0, 0)
def main():
canvasSize(1000, 1000)
make_ghost_on_scene()
data["update_timer"] = onTimer(update, 40)
onKey(keyPressed)
run()
2024-03-24 14:48:38 +00:00
2024-03-24 13:29:37 +00:00
# image = ghost.png ----
hardcoded_image = b"iVBORw0KGgoAAAANSUhEUgAAAMgAAACWCAYAAACb3McZAAAAxHpUWHRSYXcgcHJvZmlsZSB0eXBlIGV4aWYAAHjabVDbDcMwCPxnio7AKw4ex2lSqRt0/GKDqyTqST4Dh84YOD7vFzw6mBR0Wa3UUtChVSs3DwwDbTChDh54To2udaglBfaS+C2RWtZp1ulnEFfzaDkZ2TOF7SpUTX+7GeVD0idiD/Y5URoJh0Bp0OJbWKqt5y9sB15hcaCTVdzSLLRbrqtvb1/8HWE+hASdRUr0SD8K0jwQZ0+8kaSMmKKek/hC/u1pAr579VnF7llVHgAAAYNpQ0NQSUNDIHByb2ZpbGUAAHicfZE9SMNAHMVfU6VSKg7tUMQhQ3Wyi4o4lioWwUJpK7TqYHLpFzRpSFJcHAXXgoMfi1UHF2ddHVwFQfADxNnBSdFFSvxfUmgR48FxP97de9y9A4R2nanmQAJQNcvIppJiobgqBl7hRxQhhBGUmKmnc4t5eI6ve/j4ehfnWd7n/hzDSslkgE8kTjDdsIg3iGc3LZ3zPnGEVSWF+Jx40qALEj9yXXb5jXPFYYFnRox8dp44QixW+ljuY1Y1VOIZ4piiapQvFFxWOG9xVutN1r0nf2GopK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ICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICA
# background=bg1k.png ----
hardcoded_bg = b"iVBORw0KGgoAAAANSUhEUgAAA+gAAAPoCAYAAABNo9T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MDAiCiAgIHhtcDpNb2RpZnlEYXRlPSIyMDI0OjAzOjI0VDE1OjQ3OjA5KzAzOjAwIj4KICAgPHhtcE1NOkhpc3Rvcnk+CiAgICA8cmRmOlNlcT4KICAgICA8cmRmOmxpCiAgICAgIHN0RXZ0OmFjdGlvbj0ic2F2ZWQiCiAgICAgIHN0RXZ0OmNoYW5nZWQ9Ii8iCiAgICAgIHN0RXZ0Omluc3RhbmNlSUQ9InhtcC5paWQ6MzRlYmZhMGUtNWYwZC00N2ZkLTk5NjYtODVjZjU0MTM4YzI0IgogICAgICBzdEV2dDpzb2Z0d2FyZUFnZW50PSJHaW1wIDIuMTAgKFdpbmRvd3MpIgogICAgICBzdEV2dDp3aGVuPSIyMDI0LTAzLTI0VDE1OjQ3OjE2Ii8+CiAgICA8L3JkZjpTZXE+CiAgIDwveG1wTU06SGlzdG9yeT4KICA8L3JkZjpEZXNjcmlwdGlvbj4KIDwvcmRmOlJERj4KPC94OnhtcG1ldGE+CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgIC
main()