-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
192 lines (174 loc) · 5.17 KB
/
code.py
File metadata and controls
192 lines (174 loc) · 5.17 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
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
# Add your Python code here. E.g.
from microbit import *
shake0 = Image("00000:"
"78900:"
"78900:"
"78900:"
"00000")
shake1 = Image("00000:"
"07890:"
"07890:"
"07890:"
"00000")
shake2 = Image("00000:"
"00789:"
"00789:"
"00789:"
"00000")
shake3 = Image("00000:"
"00888:"
"00888:"
"00888:"
"00000")
shake4 = Image("00000:"
"00987:"
"00987:"
"00987:"
"00000")
shake5 = Image("00000:"
"09870:"
"09870:"
"09870:"
"00000")
shake6 = Image("00000:"
"98700:"
"98700:"
"98700:"
"00000")
shake7 = Image("00000:"
"88800:"
"88800:"
"88800:"
"00000")
SHAKE_IMAGES = [shake0, shake1, shake2, shake3, shake4, shake5, shake6, shake7]
happyUp = Image("00000:"
"09090:"
"00000:"
"90009:"
"09990")
happyRight = Image("09000:"
"90090:"
"90000:"
"90090:"
"09000")
happyDown = Image("09990:"
"90009:"
"00000:"
"09090:"
"00000")
happyLeft = Image("00090:"
"09009:"
"00009:"
"09009:"
"00090")
HAPPY_IMAGES = [happyUp, happyRight, happyDown, happyLeft]
ARROW_WE_IMAGE = Image("09090:"
"66066:"
"99999:"
"66066:"
"09090")
def game_shake():
imageIndex = 0
imageNumber = len(SHAKE_IMAGES)
while True:
currentImage = SHAKE_IMAGES[imageIndex]
display.show(currentImage)
sleep(50)
imageIndex = (imageIndex + 1) % imageNumber
if accelerometer.was_gesture("shake"):
return True
def game_arrows():
steps = ['l', 'r', 'l', 'l', 'b', 'r', 'b']
stepIndex = 0
stepNumber = len(steps)
while stepIndex < stepNumber:
currentStep = steps[stepIndex]
currentImage = None
if currentStep == 'l':
currentImage = Image.ARROW_W
elif currentStep == 'r':
currentImage = Image.ARROW_E
else:
currentImage = ARROW_WE_IMAGE
display.show(currentImage)
while True:
leftPressed = button_a.is_pressed()
rightPressed = button_b.is_pressed()
leftPressedOnly = leftPressed and (not rightPressed)
rightPressedOnly = (not leftPressed) and rightPressed
bothPressed = leftPressed and rightPressed
correctPressed = (currentStep == 'l' and leftPressedOnly) or (currentStep == 'r' and rightPressedOnly) or (currentStep == 'b' and bothPressed)
if correctPressed:
stepIndex = stepIndex + 1
break
def game_facedown():
happyImages = [happyUp, happyDown]
imageIndex = 0
imageNumber = len(happyImages)
time = 0
isDownDone = False
while True:
time = (time + 1) % 2000
if time > 1000:
imageIndex = 1
else:
imageIndex = 0
currentImage = happyImages[imageIndex]
display.show(currentImage)
gesture = accelerometer.current_gesture()
if gesture == "down":
isDownDone = True
if isDownDone and gesture == "up":
return True
def game_collect():
level = [[0, 0, 0, 5, 0],
[0, 5, 0, 0, 0],
[0, 0, 0, 0, 5],
[0, 5, 0, 5, 0],
[0, 0, 5, 5, 0]]
currentPosition = [0, 0] # row, column
sleepIndex = 0
sleepMax = 5
while True:
# Left-right
leftIncrease = button_a.get_presses()
rightIncrease = button_b.get_presses()
currentPosition[1] = (currentPosition[1] - leftIncrease + rightIncrease) % 5
# Down
sleepIndex = sleepIndex + 1
if sleepIndex == sleepMax:
currentPosition[0] = (currentPosition[0] + 1) % 5
sleepIndex = 0
# Collect points
level[currentPosition[0]][currentPosition[1]] = 0
# Render image
currentImageArray = [sublist[:] for sublist in level]
currentImageArray[currentPosition[0]][currentPosition[1]] = 9
currentImageArrayWithStringEntries = map(lambda sublist: map(str, sublist), currentImageArray)
currentImageArrayWithStringRows = map(lambda sublist: ''.join(sublist), currentImageArrayWithStringEntries)
currentImageArrayWithStringMatrix = ':'.join(currentImageArrayWithStringRows)
currentImage = Image(currentImageArrayWithStringMatrix)
display.show(currentImage)
# Check if done
flattenedList = [item for sublist in level for item in sublist]
zeroCount = flattenedList.count(0)
if zeroCount == 25:
return True
# Wait
sleep(1)
while True:
display.scroll('1')
game_arrows()
display.scroll('2')
game_shake()
display.scroll('3')
game_collect()
display.scroll('4')
game_facedown()
happyAnimation = []
happyAnimation.extend(HAPPY_IMAGES)
happyAnimation.extend(HAPPY_IMAGES)
happyAnimation.extend(HAPPY_IMAGES)
while True:
display.scroll('NYERTEL!!!')
display.show(happyAnimation, 200)