-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace_rocket.py
More file actions
85 lines (56 loc) · 1.78 KB
/
Copy pathspace_rocket.py
File metadata and controls
85 lines (56 loc) · 1.78 KB
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
75
76
77
78
79
80
81
82
83
84
"""Space Rocket demo
Shows keyboard input, async update, path tracing and object detection.
<h3>Use arrow keys to move</h3>
"""
from turtleps import *
from pyscript import document
screen = Screen()
ispazio = "img/bg-space-1.gif"
irazzo = "img/vh-rocket-1ut.gif"
istella = "img/ob-energy.gif"
screen.register_shape(ispazio)
screen.register_shape(irazzo)
screen.register_shape(istella)
await ge_init()
screen.background.shape(ispazio)
rocket = Sprite()
rocket.shape(irazzo) # rocket guarda in su
rocket.pensize(5)
rocket.pendown()
rocket.tilt(-90) # aggiustiamo l'orientamento finchè l'*immagine* guardi a destra
stella = Sprite()
stella.shape(istella)
stella.goto(-130,130)
tasti = set()
def tasto_down(evento):
tasti.add(evento.key)
def tasto_up(evento):
if evento.key in tasti:
tasti.remove(evento.key)
document.onkeydown = tasto_down
document.onkeyup = tasto_up
attesa = 0.02
async def muovi_stella():
while True:
await stella.slide(-130, 110, 1)
await stella.slide(-130, 90, 1)
async def muovi_razzo():
while True:
# workaround per il lag: https://github.com/CoderDojoTrento/turtle-pyscript/issues/18
rocket.color('yellow')
if "ArrowUp" in tasti:
rocket.forward(4)
if "ArrowLeft" in tasti:
rocket.left(5)
if "ArrowRight" in tasti:
rocket.right(5)
await asyncio.sleep(attesa) # ATTENZIONE all'indentazione!
async def scopri():
while True:
if rocket.x < -100 and rocket.y > 100:
await rocket.say("Hai trovato una nuova stella!", 2)
await asyncio.sleep(attesa) # ATTENZIONE all'indentazione!
await rocket.say("Let's go!", 2)
asyncio.gather(muovi_stella(),
muovi_razzo(),
scopri())