|
24 | 24 | A gaunlet running game using the dotstar wing and the joy wing.
|
25 | 25 | """
|
26 | 26 |
|
27 |
| -import time |
28 |
| -import random |
| 27 | +from time import sleep |
| 28 | +from random import randint |
29 | 29 |
|
30 | 30 | import board
|
31 | 31 | import busio
|
|
34 | 34 |
|
35 | 35 | i2c = busio.I2C(board.SCL, board.SDA)
|
36 | 36 | ss = Adafruit_seesaw.Seesaw(i2c)
|
37 |
| -wing = dotstar_featherwing.DotstarFeatherwing(board.D13, board.D11) |
38 |
| - |
39 |
| -black = (0, 0, 0) |
40 |
| -background = (32, 8, 0) |
41 |
| -blue = (0, 0, 64) |
42 |
| -player = (0, 255, 0) |
43 |
| - |
44 |
| -row = (background, background, background, background, |
45 |
| - background, background, background, background, |
| 37 | +wing = dotstar_featherwing.DotstarFeatherwing(board.D13, board.D11, 0.1) |
| 38 | + |
| 39 | +black = 0x000000 |
| 40 | +wall = 0x200800 # must not have any blue, must have red |
| 41 | +pellet = 0x000040 # must have blue |
| 42 | +player = 0x00FF00 |
| 43 | + |
| 44 | +numbers = { |
| 45 | + ' ': [0, 0, 0], |
| 46 | + '0': [30, 33, 30], |
| 47 | + '1': [34, 63, 32], |
| 48 | + '2': [50, 41, 38], |
| 49 | + '3': [33, 37, 26], |
| 50 | + '4': [ 7, 4, 63], |
| 51 | + '5': [23, 37, 25], |
| 52 | + '6': [30, 41, 25], |
| 53 | + '7': [49, 9, 7], |
| 54 | + '8': [26, 37, 26], |
| 55 | + '9': [38, 41, 30], |
| 56 | +} |
| 57 | + |
| 58 | +row = (wall, wall, wall, wall, |
| 59 | + wall, wall, wall, wall, |
46 | 60 | black, black, black, black, black,
|
47 |
| - background, background, background, background, |
48 |
| - background, background, background, background) |
| 61 | + wall, wall, wall, wall, |
| 62 | + wall, wall, wall, wall) |
49 | 63 |
|
50 | 64 |
|
51 | 65 | def run():
|
52 | 66 | """Play the game."""
|
53 | 67 |
|
54 |
| - player_position_col = 6 |
| 68 | + player_x = 6 |
55 | 69 | score = 0
|
56 | 70 | steps = 0
|
| 71 | + step_delay = 0.15 |
| 72 | + offset = 4 |
57 | 73 |
|
58 | 74 | for _ in range(wing.rows):
|
59 |
| - wing.shift_into_top(row, 4) |
60 |
| - wing.show() |
| 75 | + wing.shift_into_top(row, offset) |
| 76 | + wing.show() |
61 | 77 |
|
62 |
| - offset = 4 |
63 | 78 |
|
64 | 79 | while True:
|
65 |
| - wing.set_color(3, player_position_col, black) |
66 |
| - offset = min(max(0, offset + random.randint(-1, 1)), 9) |
| 80 | + # remove player sprite |
| 81 | + wing.set_color(3, player_x, black) |
| 82 | + |
| 83 | + # shift/advance the track |
| 84 | + offset = min(max(0, offset + randint(-1, 1)), 9) |
67 | 85 | wing.shift_into_top(row, offset)
|
68 |
| - if random.randint(1, 20) == 1: |
69 |
| - pos = random.randint(8, 12) - offset |
70 |
| - wing.set_color(0, pos, blue) |
71 | 86 |
|
| 87 | + # Maybe add a pellet |
| 88 | + if randint(1, 20) == 1: |
| 89 | + wing.set_color(0, randint(8, 12) - offset, pellet) |
| 90 | + |
| 91 | + # Adjust player position |
72 | 92 | joy_x = ss.analog_read(3)
|
73 |
| - if joy_x < 256 and player_position_col > 0: |
74 |
| - player_delta = -1 |
75 |
| - elif joy_x > 768 and player_position_col < 11: |
76 |
| - player_delta = 1 |
77 |
| - else: |
78 |
| - player_delta = 0 |
79 |
| - player_position_col += player_delta |
80 |
| - |
81 |
| - under_player = wing.get_color(3, player_position_col) |
82 |
| - if under_player == background: |
83 |
| - return steps, score |
84 |
| - elif under_player == blue: |
| 93 | + if joy_x < 256 and player_x > 0: |
| 94 | + player_x -= 1 |
| 95 | + elif joy_x > 768 and player_x < 11: |
| 96 | + player_x += 1 |
| 97 | + |
| 98 | + # Check for collisions |
| 99 | + r, _, b = wing.get_color(3, player_x) |
| 100 | + if b: |
85 | 101 | score += 1
|
| 102 | + elif r: |
| 103 | + return steps, score |
| 104 | + |
| 105 | + # Show player sprite |
| 106 | + wing.set_color(3, player_x, player) |
86 | 107 |
|
87 |
| - wing.set_color(3, player_position_col, player) |
| 108 | + # Update some things and sleep a bit |
88 | 109 | wing.show()
|
89 | 110 | steps += 1
|
90 |
| - if steps % 10 == 0: |
| 111 | + if steps % 25 == 0: |
91 | 112 | score += 1
|
92 |
| - time.sleep(0.1) |
| 113 | + if steps % 100 == 0: |
| 114 | + step_delay *= 0.9 |
| 115 | + sleep(step_delay) |
93 | 116 |
|
94 | 117 | while True:
|
95 | 118 | result = run()
|
96 |
| - # got here because of a crash, so report and restart |
97 |
| - print 'Score: {} Steps: {}'.format(result[1], result[0]) |
98 |
| - wing.clear() |
99 |
| - wing.show() |
100 |
| - wing.fill((255, 0, 0)) |
101 |
| - wing.show() |
102 |
| - wing.clear() |
103 |
| - wing.show() |
| 119 | + # got here because of a crash, so report score and restart |
| 120 | + wing.shift_in_string(numbers, '{:03d}'.format(result[1]), 0x101010) |
| 121 | + sleep(5) |
0 commit comments