-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLearning-Guide-13
More file actions
374 lines (311 loc) · 13.8 KB
/
Learning-Guide-13
File metadata and controls
374 lines (311 loc) · 13.8 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env python3
# Created by Sean McLeod
# Created on January 2021
# This is the "Space Aliens" program on the PyBadge
import ugame
import stage
import time
import random
import board
import neopixel
import supervisor
import constants
def splash_scene():
# this function is the splash scene game loop
# get sound ready
coin_sound = open("coin.wav", 'rb')
sound = ugame.audio
sound.stop()
sound.mute(False)
sound.play(coin_sound)
# an image bank for CircuitPython
image_bank_mt_background = stage.Bank.from_bmp16("mt_game_studio.bmp")
# sets the background to image 0 in the image bank
background = stage.Grid(image_bank_mt_background, constants.SCREEN_X,
constants.SCREEN_Y)
# used this program to split the image into tile:
# https://ezgif.com/sprite-cutter/ezgif-5-818cdbcc3f66.png
background.tile(2, 2, 0) # blank white
background.tile(3, 2, 1)
background.tile(4, 2, 2)
background.tile(5, 2, 3)
background.tile(6, 2, 4)
background.tile(7, 2, 0) # blank white
background.tile(2, 3, 0) # blank white
background.tile(3, 3, 5)
background.tile(4, 3, 6)
background.tile(5, 3, 7)
background.tile(6, 3, 8)
background.tile(7, 3, 0) # blank white
background.tile(2, 4, 0) # blank white
background.tile(3, 4, 9)
background.tile(4, 4, 10)
background.tile(5, 4, 11)
background.tile(6, 4, 12)
background.tile(7, 4, 0) # blank white
background.tile(2, 5, 0) # blank white
background.tile(3, 5, 0)
background.tile(4, 5, 13)
background.tile(5, 5, 14)
background.tile(6, 5, 0)
background.tile(7, 5, 0) # blank white
# create a stage for the background to show up on
# and set the frame rate to 60fps
game = stage.Stage(ugame.display, constants.FPS)
# set the layers, items show up in order
game.layers = [background]
# render the background and inital location of sprite list
# most likely you will only render background once per scene
game.render_block()
# repeat forever, game loop
while True:
# Wait for 1 seconds
time.sleep(1.0)
menu_scene()
def menu_scene():
# this function is the menu scene
# image banks for CircuitPython
image_bank_mt_background = stage.Bank.from_bmp16("mt_game_studio.bmp")
# add text objects
text = []
text1 = stage.Text(width=29, height=12, font=None, palette=constants.RED_PALETTE, buffer=None)
text1.move(20, 10)
text1.text("MT Game Studios")
text.append(text1)
text2 = stage.Text(width=29, height=12, font=None, palette=constants.RED_PALETTE, buffer=None)
text2.move(40, 110)
text2.text("PRESS START")
text.append(text2)
# sets the background to image 0 in the image bank
background = stage.Grid(image_bank_mt_background, constants.SCREEN_X,
constants.SCREEN_Y)
# create a stage for the background to show up on
# and set the frame rate to 60fps
game = stage.Stage(ugame.display, constants.FPS)
# set the layers, items show up in order
game.layers = text + [background]
# render the background and initial location of sprite list
# most likely you will only render background once per scene
game.render_block()
# repeat forever, game loop
while True:
# get user input
keys = ugame.buttons.get_pressed()
# Start button selected
if keys & ugame.K_START != 0:
game_scene()
# update game logic
game.tick() # wait until refresh rate finishes
def game_scene():
# this function is the main game scene
# for score
score = 0
score_text = stage.Text(width=29, height=14)
score_text.clear()
score_text.cursor(0,0)
score_text.move(1,1)
score_text.text("Score: {0}".format(score))
def show_alien():
# this function takes an alien from off screen and moves it on screen
for alien_number in range(len(aliens)):
if aliens[alien_number].x < 0:
aliens[alien_number].move(random.randint(0 + constants.SPRITE_SIZE,
constants.SCREEN_X - constants.SPRITE_SIZE),
constants.OFF_TOP_SCREEN)
break
# image banks for CircuitPython
image_bank_background = stage.Bank.from_bmp16("space_aliens_background.bmp")
image_bank_sprites = stage.Bank.from_bmp16("space_aliens.bmp")
# buttons that you want to keep state information on
a_button = constants.button_state["button_up"]
b_button = constants.button_state["button_up"]
start_button = constants.button_state["button_up"]
select_button = constants.button_state["button_up"]
# get sound ready
pew_sound = open("pew.wav", 'rb')
boom_sound = open("boom.wav", 'rb')
crash_sound = open("crash.wav", 'rb')
sound = ugame.audio
sound.stop()
sound.mute(False)
# sets the background to image 0 in the image bank
background = stage.Grid(image_bank_background, constants.SCREEN_X,
constants.SCREEN_Y)
for x_location in range(constants.SCREEN_GRID_X):
for y_location in range(constants.SCREEN_GRID_Y):
tile_picked = random.randint(1, 3)
background.tile(x_location, y_location, tile_picked)
ship = stage.Sprite(image_bank_sprites, 5, 75, constants.SCREEN_Y - (2 * constants.SPRITE_SIZE))
# create list of lasers for when we shoot
aliens = []
for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS):
a_single_alien = stage.Sprite(image_bank_sprites, 9,
constants.OFF_SCREEN_X,
constants.OFF_SCREEN_Y)
aliens.append(a_single_alien)
# place 1 alien on the screen
show_alien()
# create list of lasers for when we shoot
lasers = []
for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS):
a_single_laser = stage.Sprite(image_bank_sprites, 10,
constants.OFF_SCREEN_X,
constants.OFF_SCREEN_Y)
lasers.append(a_single_laser)
# create a stage for the background to show up on
# and set the frame rate to 60fps
game = stage.Stage(ugame.display, constants.FPS)
# set the layers, items show up in order
game.layers = [score_text] + lasers + [ship] + aliens + [background]
# render the background and initial location of sprite list
# most likely you will only render background once per scene
game.render_block()
# repeat forever, game loop
while True:
# get user input
keys = ugame.buttons.get_pressed()
# A button to fire
if keys & ugame.K_O != 0:
if a_button == constants.button_state["button_up"]:
a_button = constants.button_state["button_just_pressed"]
elif a_button == constants.button_state["button_just_pressed"]:
a_button = constants.button_state["button_still_pressed"]
else:
if a_button == constants.button_state["button_still_pressed"]:
a_button = constants.button_state["button_released"]
else:
a_button = constants.button_state["button_up"]
if keys & ugame.K_X != 0:
pass
if keys & ugame.K_START != 0:
pass
if keys & ugame.K_SELECT != 0:
pass
if keys & ugame.K_RIGHT != 0:
if ship.x < (constants.SCREEN_X - constants.SPRITE_SIZE):
ship.move((ship.x + constants.SPRITE_MOVEMENT_SPEED), ship.y)
else:
ship.move((constants.SCREEN_X - constants.SPRITE_SIZE), ship.y)
if keys & ugame.K_LEFT != 0:
if ship.x > 0:
ship.move((ship.x - constants.SPRITE_MOVEMENT_SPEED), ship.y)
else:
ship.move(0, ship.y)
if keys & ugame.K_UP != 0:
pass
if keys & ugame.K_DOWN != 0:
pass
# update game logic
# play sound if A was just button_just_pressed
if a_button == constants.button_state["button_just_pressed"]:
# fire a laser, if we have enough power (have not used up all the lasers)
for laser_number in range(len(lasers)):
if lasers[laser_number].x < 0:
lasers[laser_number].move(ship.x, ship.y)
sound.play(pew_sound)
break
# each frame move the lasers, that have been fired up
for laser_number in range(len(lasers)):
if lasers[laser_number].x > 0:
lasers[laser_number].move(lasers[laser_number].x,
lasers[laser_number].y -
constants.LASER_SPEED)
if lasers[laser_number].y < constants.OFF_TOP_SCREEN:
lasers[laser_number].move(constants.OFF_SCREEN_X,
constants.OFF_SCREEN_Y)
# each frame move the aliens down, that are on the screen
for alien_number in range(len(aliens)):
if aliens[alien_number].x > 0:
aliens[alien_number].move(aliens[alien_number].x,
aliens[alien_number].y +
constants.ALIEN_SPEED)
if aliens[alien_number].y > constants.SCREEN_Y:
aliens[alien_number].move(constants.OFF_SCREEN_X,
constants.OFF_SCREEN_Y)
show_alien()
score -= 1
if score < 0:
score = 0
score_text.clear()
score_text.cursor(0,0)
score_text.move(1,1)
score_text.text("Score: {0}".format(score))
# each frame check if any of the lasers are touching any of the aliens
for laser_number in range(len(lasers)):
if lasers[laser_number].x > 0:
for alien_number in range(len(aliens)):
if aliens[alien_number].x > 0:
if stage.collide(lasers[laser_number].x + 6, lasers[laser_number].y + 2,
lasers[laser_number].x + 11, lasers[laser_number].y + 12,
aliens[alien_number].x + 1, aliens[alien_number].y,
aliens[alien_number].x + 15, aliens[alien_number].y + 15):
# you hit an alien
aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y)
lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y)
sound.stop()
sound.play(boom_sound)
show_alien()
show_alien()
score = score + 1
score_text.clear()
score_text.cursor(0,0)
score_text.move(1,1)
score_text.text("Score: {0}".format(score))
# each frame check if any aliens are touching the space ship
for alien_number in range(len(aliens)):
if aliens[alien_number].x > 0:
if stage.collide(aliens[alien_number].x + 1, aliens[alien_number].y,
aliens[alien_number].x + 15, aliens[alien_number].y + 15,
ship.x, ship.y,
ship.x + 15, ship.y + 15):
# alien hit the ship
sound.stop()
sound.play(crash_sound)
time.sleep(3.0)
game_over_scene(score)
# redraw sprite list
game.render_sprites(lasers + [ship] + aliens)
game.tick() # wait until refresh rate finishes
def game_over_scene(final_score):
# this function is the game over scene
# turn off sound from last scene
sound = ugame.audio
sound.stop()
# image banks for CircuitPython
image_bank_2 = stage.Bank.from_bmp16("mt_game_studio.bmp")
# sets the background to image 0 in the image bank
background = stage.Grid(image_bank_2, constants.SCREEN_GRID_X,
constants.SCREEN_GRID_Y)
# add text objects
text = []
text1 = stage.Text(width=29, height=14)
text1.move(22, 20)
text1.text("Final Score: {:0>2d}".format(final_score))
text.append(text1)
text2 = stage.Text(width=29, height=14)
text2.move(43, 60)
text2.text("GAME OVER")
text.append(text2)
text3 = stage.Text(width=29, height=14)
text3.move(32,110)
text3.text("PRESS SELECT")
text.append(text3)
# create a stage for the background to show up on
# and set the frame rate to 60fps
game = stage.Stage(ugame.display, constants.FPS)
# set the layers, items show up in order
game.layers = text + [background]
# render the background and initial location of sprite list
# most likely you will only render background once per scene
game.render_block()
# repeat forever, game loop
while True:
# get user input
keys = ugame.buttons.get_pressed()
# Start button selected
if keys & ugame.K_SELECT != 0:
supervisor.reload()
# update game logic
game.tick() # wait until refresh rate finishes
if __name__ == "__main__":
splash_scene()