Skip to content

Commit 2c6f50e

Browse files
committed
New features and related optimizations to make it fit
- display score on dotstar wing - score increment every 25 steps rather than 10 - 10% speed increase every 100 steps - add some comments
1 parent 2bba5da commit 2c6f50e

File tree

1 file changed

+63
-45
lines changed

1 file changed

+63
-45
lines changed

examples/gauntlet_game/main.py

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
A gaunlet running game using the dotstar wing and the joy wing.
2525
"""
2626

27-
import time
28-
import random
27+
from time import sleep
28+
from random import randint
2929

3030
import board
3131
import busio
@@ -34,70 +34,88 @@
3434

3535
i2c = busio.I2C(board.SCL, board.SDA)
3636
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,
4660
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)
4963

5064

5165
def run():
5266
"""Play the game."""
5367

54-
player_position_col = 6
68+
player_x = 6
5569
score = 0
5670
steps = 0
71+
step_delay = 0.15
72+
offset = 4
5773

5874
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()
6177

62-
offset = 4
6378

6479
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)
6785
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)
7186

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
7292
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:
85101
score += 1
102+
elif r:
103+
return steps, score
104+
105+
# Show player sprite
106+
wing.set_color(3, player_x, player)
86107

87-
wing.set_color(3, player_position_col, player)
108+
# Update some things and sleep a bit
88109
wing.show()
89110
steps += 1
90-
if steps % 10 == 0:
111+
if steps % 25 == 0:
91112
score += 1
92-
time.sleep(0.1)
113+
if steps % 100 == 0:
114+
step_delay *= 0.9
115+
sleep(step_delay)
93116

94117
while True:
95118
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

Comments
 (0)