-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathmain.py
74 lines (60 loc) · 1.95 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from pathlib import Path
from typing import NamedTuple
from idom import component, html, run, use_state
from idom.widgets import image
HERE = Path(__file__)
CHARACTER_IMAGE = (HERE.parent / "static" / "bunny.png").read_bytes()
class Position(NamedTuple):
x: int
y: int
angle: int
def rotate(degrees):
return lambda old_position: Position(
old_position.x,
old_position.y,
old_position.angle + degrees,
)
def translate(x=0, y=0):
return lambda old_position: Position(
old_position.x + x,
old_position.y + y,
old_position.angle,
)
@component
def Scene():
position, set_position = use_state(Position(100, 100, 0))
return html.div(
{"style": {"width": "225px"}},
html.div(
{
"style": {
"width": "200px",
"height": "200px",
"background_color": "slategray",
}
},
image(
"png",
CHARACTER_IMAGE,
{
"style": {
"position": "relative",
"left": f"{position.x}px",
"top": f"{position.y}.px",
"transform": f"rotate({position.angle}deg) scale(2, 2)",
}
},
),
),
html.button(
{"on_click": lambda e: set_position(translate(x=-10))}, "Move Left"
),
html.button(
{"on_click": lambda e: set_position(translate(x=10))}, "Move Right"
),
html.button({"on_click": lambda e: set_position(translate(y=-10))}, "Move Up"),
html.button({"on_click": lambda e: set_position(translate(y=10))}, "Move Down"),
html.button({"on_click": lambda e: set_position(rotate(-30))}, "Rotate Left"),
html.button({"on_click": lambda e: set_position(rotate(30))}, "Rotate Right"),
)
run(Scene)