|
| 1 | +import time |
| 2 | +from random import randint |
| 3 | +from micropython import const |
| 4 | +import board |
| 5 | +import terminalio |
| 6 | +import displayio |
| 7 | +import adafruit_imageload |
| 8 | +import digitalio |
| 9 | +import simpleio |
| 10 | +from gamepadshift import GamePadShift |
| 11 | +from adafruit_display_text import label |
| 12 | + |
| 13 | +# setup for PyBadge buttons |
| 14 | +BUTTON_LEFT = const(128) |
| 15 | +BUTTON_UP = const(64) |
| 16 | +BUTTON_DOWN = const(32) |
| 17 | +BUTTON_RIGHT = const(16) |
| 18 | +BUTTON_SEL = const(8) |
| 19 | +BUTTON_START = const(4) |
| 20 | +BUTTON_A = const(2) |
| 21 | +BUTTON_B = const(1) |
| 22 | + |
| 23 | +pad = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK), |
| 24 | + digitalio.DigitalInOut(board.BUTTON_OUT), |
| 25 | + digitalio.DigitalInOut(board.BUTTON_LATCH)) |
| 26 | + |
| 27 | +current_buttons = pad.get_pressed() |
| 28 | +last_read = 0 |
| 29 | + |
| 30 | +# enables speaker |
| 31 | +speakerEnable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) |
| 32 | +speakerEnable.switch_to_output(value=True) |
| 33 | + |
| 34 | +# Sprite cell values |
| 35 | +EMPTY = 0 |
| 36 | +BLINKA_1 = 1 |
| 37 | +BLINKA_2 = 2 |
| 38 | +SPARKY = 3 |
| 39 | +HEART = 4 |
| 40 | +JUMP_1 = 5 |
| 41 | +JUMP_2 = 6 |
| 42 | + |
| 43 | +# creates display |
| 44 | +display = board.DISPLAY |
| 45 | +# scale=2 allows the sprites to be bigger |
| 46 | +group = displayio.Group(max_size=30, scale=2) |
| 47 | + |
| 48 | +# Blinka sprite setup |
| 49 | +blinka, blinka_pal = adafruit_imageload.load("/spritesNew.bmp", |
| 50 | + bitmap=displayio.Bitmap, |
| 51 | + palette=displayio.Palette) |
| 52 | + |
| 53 | +# creates a transparent background for Blinka |
| 54 | +blinka_pal.make_transparent(7) |
| 55 | +blinka_grid = displayio.TileGrid(blinka, pixel_shader=blinka_pal, |
| 56 | + width=2, height=1, |
| 57 | + tile_height=16, tile_width=16, |
| 58 | + default_tile=EMPTY) |
| 59 | +blinka_grid.x = 0 |
| 60 | +blinka_grid.y = 32 |
| 61 | + |
| 62 | +blinka_group = displayio.Group() |
| 63 | +blinka_group.append(blinka_grid) |
| 64 | + |
| 65 | +# first Sparky sprite |
| 66 | +sparky0, sparky0_pal = adafruit_imageload.load("/spritesNew.bmp", |
| 67 | + bitmap=displayio.Bitmap, |
| 68 | + palette=displayio.Palette) |
| 69 | +sparky0_pal.make_transparent(7) |
| 70 | +sparky0_grid = displayio.TileGrid(sparky0, pixel_shader=sparky0_pal, |
| 71 | + width=1, height=1, |
| 72 | + tile_height=16, tile_width=16, |
| 73 | + default_tile=SPARKY) |
| 74 | +# all Sparky sprites begin off screen |
| 75 | +sparky0_grid.x = 100 |
| 76 | +sparky0_grid.y = 32 |
| 77 | + |
| 78 | +sparky0_group = displayio.Group() |
| 79 | +sparky0_group.append(sparky0_grid) |
| 80 | + |
| 81 | +# 2nd Sparky sprite |
| 82 | +sparky1, sparky1_pal = adafruit_imageload.load("/spritesNew.bmp", |
| 83 | + bitmap=displayio.Bitmap, |
| 84 | + palette=displayio.Palette) |
| 85 | +sparky1_pal.make_transparent(7) |
| 86 | +sparky1_grid = displayio.TileGrid(sparky1, pixel_shader=sparky1_pal, |
| 87 | + width=1, height=1, |
| 88 | + tile_height=16, tile_width=16, |
| 89 | + default_tile=SPARKY) |
| 90 | +sparky1_grid.x = 100 |
| 91 | +sparky1_grid.y = 32 |
| 92 | + |
| 93 | +sparky1_group = displayio.Group() |
| 94 | +sparky1_group.append(sparky1_grid) |
| 95 | + |
| 96 | +# 3rd Sparky sprite |
| 97 | +sparky2, sparky2_pal = adafruit_imageload.load("/spritesNew.bmp", |
| 98 | + bitmap=displayio.Bitmap, |
| 99 | + palette=displayio.Palette) |
| 100 | +sparky2_pal.make_transparent(7) |
| 101 | +sparky2_grid = displayio.TileGrid(sparky2, pixel_shader=sparky2_pal, |
| 102 | + width=1, height=1, |
| 103 | + tile_height=16, tile_width=16, |
| 104 | + default_tile=SPARKY) |
| 105 | +sparky2_grid.x = 100 |
| 106 | +sparky2_grid.y = 32 |
| 107 | + |
| 108 | +sparky2_group = displayio.Group() |
| 109 | +sparky2_group.append(sparky2_grid) |
| 110 | + |
| 111 | +# heart sprite group |
| 112 | +life_bit, life_pal = adafruit_imageload.load("/spritesNew.bmp", |
| 113 | + bitmap=displayio.Bitmap, |
| 114 | + palette=displayio.Palette) |
| 115 | +life_grid = displayio.TileGrid(life_bit, pixel_shader=life_pal, |
| 116 | + width=3, height=1, |
| 117 | + tile_height=16, tile_width=16, |
| 118 | + default_tile=HEART) |
| 119 | + |
| 120 | +life_group = displayio.Group() |
| 121 | +life_group.append(life_grid) |
| 122 | + |
| 123 | +# adding all graphics groups to the main display group |
| 124 | +group.append(blinka_group) |
| 125 | +group.append(sparky0_group) |
| 126 | +group.append(sparky1_group) |
| 127 | +group.append(sparky2_group) |
| 128 | +group.append(life_group) |
| 129 | + |
| 130 | +# text area for the running score |
| 131 | +score_text = " " |
| 132 | +font = terminalio.FONT |
| 133 | +score_color = 0x0000FF |
| 134 | + |
| 135 | +# text for "game over" graphic |
| 136 | +game_over_text = label.Label(font, text = " ", color = 0xFF00FF) |
| 137 | +# score text |
| 138 | +score_area = label.Label(font, text=score_text, color=score_color) |
| 139 | +# text for "new game" graphic |
| 140 | +new_game_text = label.Label(font, text = " ", color = 0xFF00FF) |
| 141 | + |
| 142 | +# coordinants for text areas |
| 143 | +score_area.x = 57 |
| 144 | +score_area.y = 6 |
| 145 | +game_over_text.x = 13 |
| 146 | +game_over_text.y = 30 |
| 147 | +new_game_text.x = 8 |
| 148 | +new_game_text.y = 30 |
| 149 | +# creating a text display group |
| 150 | +text_group = displayio.Group() |
| 151 | +text_group.append(score_area) |
| 152 | +text_group.append(game_over_text) |
| 153 | +text_group.append(new_game_text) |
| 154 | +# adding text group to main display group |
| 155 | +group.append(text_group) |
| 156 | + |
| 157 | +# displaying main display group |
| 158 | +display.show(group) |
| 159 | + |
| 160 | +# state for hit detection |
| 161 | +crash = False |
| 162 | +# states to see if a Sparky is on screen |
| 163 | +sparky0 = False |
| 164 | +sparky1 = False |
| 165 | +sparky2 = False |
| 166 | + |
| 167 | +# array of Sparky states |
| 168 | +sparky_states = [sparky0, sparky1, sparky2] |
| 169 | +# array of x location for Sparky's |
| 170 | +sparky_x = [sparky0_grid.x, sparky1_grid.x, sparky2_grid.x] |
| 171 | + |
| 172 | +# function to display the heart sprites for lives |
| 173 | +def life(): |
| 174 | + for _ in range(0, 3): |
| 175 | + life_grid[_, 0] = EMPTY |
| 176 | + for hearts in range(life_count): |
| 177 | + life_grid[hearts, 0] = HEART |
| 178 | + |
| 179 | +# lives at beginning of the game |
| 180 | +life_count = 3 |
| 181 | + |
| 182 | +# variables for scoring |
| 183 | +jump_score = 0 |
| 184 | +total_score = 0 |
| 185 | +bonus = 0 |
| 186 | +# state for Blinka being in default 'slither' mode |
| 187 | +snake = True |
| 188 | +# state to check if Blinka has jumped over Sparky |
| 189 | +cleared = False |
| 190 | +# state for the end of a game |
| 191 | +end = False |
| 192 | +# state for a new game beginning |
| 193 | +new_game = True |
| 194 | +# state for detecting game over |
| 195 | +game_over = False |
| 196 | +# variable to change between Blinka's two slither sprites |
| 197 | +b = 1 |
| 198 | +# variable to hold time.monotonic() count for Blinka slither animation |
| 199 | +slither = 0 |
| 200 | +# variables to hold time.monotonic() count to delay Sparky spawning |
| 201 | +blue = 0 |
| 202 | +smoke = 0 |
| 203 | +monster = 0 |
| 204 | + |
| 205 | +while True: |
| 206 | + |
| 207 | + # checks if button has been pressed |
| 208 | + if (last_read + 0.01) < time.monotonic(): |
| 209 | + buttons = pad.get_pressed() |
| 210 | + last_read = time.monotonic() |
| 211 | + # new game |
| 212 | + if new_game and not game_over: |
| 213 | + # graphics for new game splash screen |
| 214 | + blinka_grid.y = 16 |
| 215 | + blinka_grid[0] = JUMP_1 |
| 216 | + blinka_grid[1] = JUMP_2 |
| 217 | + sparky0_grid.x = 5 |
| 218 | + sparky1_grid.x = 40 |
| 219 | + sparky2_grid.x = 65 |
| 220 | + score_area.text = 300 |
| 221 | + new_game_text.text = "BLINKA JUMP" |
| 222 | + life() |
| 223 | + # if start is pressed... |
| 224 | + if current_buttons != buttons: |
| 225 | + if buttons & BUTTON_START: |
| 226 | + # prepares display for gameplay |
| 227 | + print("start game") |
| 228 | + new_game_text.text = " " |
| 229 | + life_count = 3 |
| 230 | + start = time.monotonic() |
| 231 | + new_game = False |
| 232 | + end = False |
| 233 | + sparky0_grid.x = 100 |
| 234 | + sparky1_grid.x = 100 |
| 235 | + sparky2_grid.x = 100 |
| 236 | + # if game has started... |
| 237 | + if not game_over and not new_game: |
| 238 | + # gets time.monotonic() to have a running score |
| 239 | + mono = time.monotonic() |
| 240 | + score = mono - start |
| 241 | + # adds 10 points every time a Sparky is cleared |
| 242 | + total_score = score + jump_score |
| 243 | + # displays score as text |
| 244 | + score_area.text = int(total_score) |
| 245 | + |
| 246 | + # puts Sparky states and x location into callable arrays |
| 247 | + for s in range(3): |
| 248 | + sparky_state = sparky_states[s] |
| 249 | + sparky_location = sparky_x[s] |
| 250 | + |
| 251 | + # Sparkys are generated using a staggered delay |
| 252 | + # and matching an int to a random int |
| 253 | + # 1st Sparky |
| 254 | + if (blue + 0.03) < time.monotonic(): |
| 255 | + if randint(1, 15) == 3: |
| 256 | + sparky_states[0] = True |
| 257 | + blue = time.monotonic() |
| 258 | + # 2nd Sparky |
| 259 | + if (smoke + 0.07) < time.monotonic(): |
| 260 | + if randint(1, 15) == 7: |
| 261 | + sparky_states[1] = True |
| 262 | + smoke = time.monotonic() |
| 263 | + # 3rd Sparky |
| 264 | + if (monster + 0.12) < time.monotonic(): |
| 265 | + if randint(1, 15) == 12: |
| 266 | + sparky_states[2] = True |
| 267 | + monster = time.monotonic() |
| 268 | + # if a Sparky is generated, it scrolls across the screen 1 pixel at a time |
| 269 | + # 1st Sparky |
| 270 | + if sparky_states[0] is True: |
| 271 | + sparky0_grid.x -= 1 |
| 272 | + sparky_x[0] = sparky0_grid.x |
| 273 | + display.refresh(target_frames_per_second=120) |
| 274 | + # when a Sparky is 16 pixels off the display, |
| 275 | + # it goes back to its starting position |
| 276 | + if sparky0_grid.x is -16: |
| 277 | + sparky_states[0] = False |
| 278 | + sparky0_grid.x = 100 |
| 279 | + sparky_x[0] = sparky0_grid.x |
| 280 | + # 2nd Sparky |
| 281 | + if sparky_states[1] is True: |
| 282 | + sparky1_grid.x -= 1 |
| 283 | + sparky_x[1] = sparky1_grid.x |
| 284 | + display.refresh(target_frames_per_second=120) |
| 285 | + if sparky1_grid.x is -16: |
| 286 | + sparky_states[1] = False |
| 287 | + sparky1_grid.x = 100 |
| 288 | + sparky_x[1] = sparky1_grid.x |
| 289 | + # 3rd Sparky |
| 290 | + if sparky_states[2] is True: |
| 291 | + sparky2_grid.x -= 1 |
| 292 | + sparky_x[2] = sparky2_grid.x |
| 293 | + display.refresh(target_frames_per_second=120) |
| 294 | + if sparky2_grid.x is -16: |
| 295 | + sparky_states[2] = False |
| 296 | + sparky2_grid.x = 100 |
| 297 | + sparky_x[2] = sparky2_grid.x |
| 298 | + |
| 299 | + # if no lives are left then the game ends |
| 300 | + if life_count is 0: |
| 301 | + game_over = True |
| 302 | + |
| 303 | + # if the A button is pressed then Blinka is no longer in the default |
| 304 | + # slither animation aka she jumps |
| 305 | + if current_buttons != buttons: |
| 306 | + if buttons & BUTTON_A: |
| 307 | + snake = False |
| 308 | + |
| 309 | + # heart sprites are displayed to show life count |
| 310 | + life() |
| 311 | + |
| 312 | + # if Blinka is slithering... |
| 313 | + if snake: |
| 314 | + # Blinka default location |
| 315 | + blinka_grid.y = 32 |
| 316 | + # empty 2nd tile so that the jump sprite can be shown using |
| 317 | + # the same tilegrid |
| 318 | + blinka_grid[1] = EMPTY |
| 319 | + # every .15 seconds Blinka's slither sprite changes |
| 320 | + # so that her slithering is animated |
| 321 | + # b holds tilegrid position to display correct sprite |
| 322 | + if (slither + 0.15) < time.monotonic(): |
| 323 | + blinka_grid[0] = b |
| 324 | + b += 1 |
| 325 | + slither = time.monotonic() |
| 326 | + if b > 2: |
| 327 | + b = 1 |
| 328 | + # if a Sparky collides with Blinka while she is slithering... |
| 329 | + for s in range(3): |
| 330 | + if sparky_x[s] == 8 and blinka_grid.y == 32: |
| 331 | + # tone is played |
| 332 | + simpleio.tone(board.SPEAKER, 493.88, 0.05) |
| 333 | + simpleio.tone(board.SPEAKER, 349.23, 0.05) |
| 334 | + # lose a life |
| 335 | + life_count = life_count - 1 |
| 336 | + # if the A button is pressed then... |
| 337 | + else: |
| 338 | + # Blinka JUMPS |
| 339 | + # y location changes one row up and both jump sprites are shown |
| 340 | + blinka_grid.y = 16 |
| 341 | + blinka_grid[0] = JUMP_1 |
| 342 | + blinka_grid[1] = JUMP_2 |
| 343 | + # if Blinka jumps over a Sparky... |
| 344 | + for j in range(3): |
| 345 | + if sparky_x[j] == 8 and not cleared: |
| 346 | + # 10 points to the player |
| 347 | + bonus += 1 |
| 348 | + jump_score = bonus * 10 |
| 349 | + cleared = True |
| 350 | + # special victory tone is played |
| 351 | + simpleio.tone(board.SPEAKER, 523.25, 0.005) |
| 352 | + simpleio.tone(board.SPEAKER, 783.99, 0.005) |
| 353 | + # resets back to Blinka animation |
| 354 | + snake = True |
| 355 | + # resets that Blinka has not jumped over a Sparky |
| 356 | + cleared = False |
| 357 | + |
| 358 | + # if there are no more lives, the game is over |
| 359 | + if game_over and not new_game: |
| 360 | + # game over text is displayed |
| 361 | + game_over_text.text = "GAME OVER" |
| 362 | + score_area.text = " " |
| 363 | + # end game tone is played |
| 364 | + # and then the screen holds with the last |
| 365 | + # sprites on screen and game over text |
| 366 | + if not end: |
| 367 | + simpleio.tone(board.SPEAKER, 220, 0.05) |
| 368 | + simpleio.tone(board.SPEAKER, 207.65, 0.05) |
| 369 | + simpleio.tone(board.SPEAKER, 196, 0.5) |
| 370 | + end = True |
| 371 | + |
| 372 | + # if the start button is pressed... |
| 373 | + if (current_buttons != buttons) and game_over: |
| 374 | + if buttons & BUTTON_START: |
| 375 | + # display, states and score are reset for gameplay |
| 376 | + game_over_text.text = " " |
| 377 | + life_count = 3 |
| 378 | + start = time.monotonic() |
| 379 | + game_over = False |
| 380 | + end = False |
| 381 | + total_score = 0 |
| 382 | + jump_score = 0 |
| 383 | + bonus = 0 |
| 384 | + score = 0 |
| 385 | + blue = 0 |
| 386 | + smoke = 0 |
| 387 | + monster = 0 |
| 388 | + sparky0_grid.x = 100 |
| 389 | + sparky1_grid.x = 100 |
| 390 | + sparky2_grid.x = 100 |
| 391 | + # game begins again with all Sparky's off screen |
0 commit comments