-
Notifications
You must be signed in to change notification settings - Fork 1
/
microbit.py
258 lines (212 loc) · 6.39 KB
/
microbit.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#
# Author - Phil Hall, November 2022
# License - MIT
#
from microbit import *
import music
import radio
import machine
snooze = 50
max_players = 20
JOY_TOLERANCE = 300
SCREEN = 5
# Message IDs
# 2 - message from host to client
# 1 - message from client to host
# 0 - global message to all clients
# Messages Sent
# 1, -1 (enroll me), machine_id, screen size
# 1, 1 (button presses), player, buttons pressed
# Messages Received
# 2, player, 0, machine_id
# 2, -1, machine_id, too many players
# 2, -2, machine_id, game already started
# 2, player, 1, winner
# 2, player, 2, screen, player x, player y, compass
#
# 0, Ready
# 0, Game Over
# pallet dictionary
# (other) players, grass colour one , grass colour two
# enemy, wall, the winning spot
map_colors = {'p' : 3, ',' : 1, '.' : 0, 'w' : 9, 'X' : 5}
class CrashError(Exception):
pass
def plot(xxx, yyy, ilume):
display.set_pixel(xxx, yyy, ilume)
def draw_screen(screen):
global map_colors
save_x = None
save_y = None
for xxx in range(SCREEN):
for yyy in range(SCREEN):
if screen[yyy][xxx] != 'X':
plot(xxx, yyy, map_colors[screen[yyy][xxx]])
else:
save_x = xxx
save_y = yyy
return (save_x, save_y)
radio.config(channel = 14, queue = int(max_players * 1.5), length = 96)
radio.on()
speaker.on()
display.scroll("Two Weeks")
mach_id = machine.unique_id()
radio.send("1,-1," + str(mach_id) + "," + str(SCREEN))
player = None
die = False
# Enroll a new client
while True:
message = radio.receive()
if message is None:
sleep(snooze)
else:
message = eval("(" + message + ")")
# global message
if message[0] == 0:
display.scroll(message[1], wait = False)
# message to a client is it me?
elif message[0] == 2:
if str(mach_id) == str(message[2]):
player = message[1]
if player == -1 or player == -2:
music.play(["c2:2"], pin0, wait = False)
display.scroll(message[3])
die = True
break
break
if not die:
display.scroll(" Player " + str(player + 1), wait = False, loop = True)
# monitor the radio for the go message
while not die:
message = radio.receive()
if message is None:
sleep(snooze)
else:
message = eval("(" + message + ")")
#global message
if message[0] == 0:
if message[1] == "Ready":
for i in range(5):
music.play(["c4:2"], pin0, False)
display.show(str(5 - i))
sleep(1000)
break
else:
display.scroll(message[1], wait = False)
# message to this client not now!!!
elif message[0] == 2 and message[1] == player:
display.scroll("Game server, 'Ready' skipped")
raise CrashError("CrashError - packet out of sequence.")
# play the game - main loop
xxx = 0
yyy = 0
player_x = 0
player_y = 0
loops = 0
winner = 0
ilume = 7
delta = -1
flash = True
buttons = ""
save_scr = None
screen_num = None
compass = None
exit_x = None
exit_y = None
while not die:
if winner == 1:
winner = 2
sleep(snooze)
screen = None
# get all messages until my screen appears
while True:
message = radio.receive()
if message is None:
break
else:
message = eval("(" + message + ")")
print(player, str(message))
# global message, this must be game over
if message[0] == 0:
radio.off()
display.scroll(message[1])
display.show(Image.SAD_FACE)
while True:
sleep(1000)
# winner
elif message[0] == 2 and message[1] == player:
if message[2] == 1:
music.play(music.POWER_UP, pin0, False)
display.scroll(message[3], wait = False)
winner = 1
else:
player_x = message[4]
player_y = message[5]
compass = message[6]
screen = []
for i in range(SCREEN):
screen.append(message[3][i * SCREEN: (i + 1) * SCREEN])
break
if not screen is None:
save_scr = screen
# draw screen, compass and player
if winner == 0 and not screen is None and not button_b.is_pressed():
screen_num = 0
exit_x, exit_y = draw_screen(screen)
if winner == 1:
sleep(5000)
if not compass is None and (button_b.is_pressed() or winner == 1):
if winner != 1:
screen_num = 1
if compass == -2:
display.show(Image.HAPPY)
elif compass == -1:
display.show(Image.SQUARE)
else:
display.show(Image.ALL_CLOCKS[compass])
if screen_num == 0 and winner == 0:
if not exit_x is None and not exit_y is None:
if flash:
plot(exit_x, exit_y, map_colors['X'])
else:
plot(exit_x, exit_y, 0)
plot(player_x, player_y, ilume)
if screen_num == 1 and not button_b.is_pressed() and winner != 1:
if not save_scr is None:
draw_screen(save_scr)
screen_num = 0
if loops % 2 == 0:
ilume += delta
if ilume < 3:
delta = 1
if ilume > 6:
delta = -1
if loops % 5 == 0:
flash = not flash
# game over
if winner == 2:
break
# detect buttons, up, down, left, right
joy_x = accelerometer.get_x()
joy_y = accelerometer.get_y()
if winner == 0:
if joy_y < -JOY_TOLERANCE:
if not 'u' in buttons:
buttons += 'u'
if joy_y > JOY_TOLERANCE:
if not 'd' in buttons:
buttons += 'd'
if joy_x < -JOY_TOLERANCE:
if not 'l' in buttons:
buttons += 'l'
if joy_x > JOY_TOLERANCE:
if not 'r' in buttons:
buttons += 'r'
# send buttons to server
if loops % 15 == 0 and buttons != "":
radio.send("1,1," + str(player) + ",'" + buttons + "'")
buttons = ""
if loops == 30000:
loops = 0
loops += 1
radio.off()