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
85
86
87
88
89
90
91
92
93
94
95
96
|
import blockext
from blockext import *
@predicate("not %b")
def not_(value):
return not value
@command("say %s for %n secs")
def say_for_secs(text="Hello", duration=5):
import time
print(text)
time.sleep(duration)
@command("play note %n", blocking=True)
def play_note(note):
print("DING {note}".format(note=note))
time.sleep(2)
menu("pizza", ["tomato", "cheese", "hawaii"])
@reporter("colour of %m.pizza flavour pizza")
def pizza_colour(pizza="tomato"):
return {
"tomato": "red",
"cheese": "yellow",
"hawaii": "orange",
}[pizza]
@reporter("id %s")
def id(text):
return text
"""
@predicate("true or false")
def tf():
import random
return random.choice((True, False))
@reporter("numify %b")
def numify(value):
return int(not value)
@command("set light to %b")
def set_light(boolean_value):
print("light is {}".format("on" if boolean_value else "off"))
menu("city", ["Boston", "Bournemouth", "Barcelona", "Belgium"])
@reporter("lookup weather in %m.city", blocking=True)
def weather(city="Boston"):
import time
return "{weather} in {city}".format(city=city,
weather=["sunny", "cloudy", "rainy", "snowy"][int(time.time() % 4)])
menu("motor", [1, 2, 3, "A", "B", "C"])
@command("move motor %d.motor by %n degrees", blocking=True)
def move_motor(motor="A", angle=4):
print "move {motor} by {angle} degrees!".format(motor=motor, angle=angle)
time.sleep(1)
print "..."
@reporter("light sensor value")
def light():
import random
return random.randint(0, 100)
@command("drive %n steps")
def move(steps):
print("." * steps)
[" ", "beep", "playBeep"],
[" ", "set beep volume to %n", "setVolume", 5],
["r", "beep volume", "volume"],
[" ", "order %m.pizzaFlavour pizza for %m.deliverOrCollect",
"orderPizza"],
["r", "colour of %m.pizzaFlavour pizza", "pizzaColour"],
["r", "random upto %n", "randomThingy"]
],
"menus": {
"pizzaFlavour": ["tomato", "cheese", "mozarella", "hawaiian"],
"deliverOrCollect": ["delivery", "collection"]
}
}
"""
if __name__ == "__main__":
blockext.run("Fancy Spaceship", 1234)
|