Fixed typo
[one-dimensional-fireworks.git] / fireworks4.py
1 from microbit import *
2 import neopixel
3
4 NP_COUNT = 60
5 BURST_SIZE = 10
6
7 np = neopixel.NeoPixel(pin0, NP_COUNT)
8
9 BLUE = (0, 0, 64)
10 RED = (64, 0, 0)
11 OFF = (0, 0, 0)
12
13 #########################################
14 # Add these functions
15 def fade_all(pixels, fade_by=0.9):
16 for i in range(NP_COUNT):
17 fade(pixels, i, fade_by=fade_by)
18 pixels.show()
19
20 def fade(pixels, n, fade_by=0.9):
21 r, g, b = pixels[n]
22 pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by))
23 # End of functions to add
24 #########################################
25
26 def explode(pixels):
27 initial_colour = (255, 128, 128)
28
29 for i in range(BURST_SIZE):
30 pixels[NP_COUNT - BURST_SIZE + i] = initial_colour
31 pixels[NP_COUNT - BURST_SIZE - i] = initial_colour
32 fade_all(pixels, fade_by=0.95) # Add this line
33 # pixels.show() # Remove this line
34 # time.sleep(0.5) # Remove this line
35
36 for _ in range(30): # Add this line
37 fade_all(pixels) # Add this line
38
39 def reset(pixels):
40 for n in range(NP_COUNT):
41 pixels[n] = OFF
42 pixels[0] = BLUE
43 np.show()
44
45 def shoot_firework(pixels):
46 for pixel in range(NP_COUNT - BURST_SIZE):
47 pixels[pixel] = RED
48 pixels.show()
49 sleep(20)
50 pixels[pixel] = OFF
51 pixels.show()
52
53
54 reset(np)
55 last_gesture = accelerometer.current_gesture()
56
57 while True:
58 gesture = accelerometer.current_gesture()
59 if gesture != last_gesture or button_a.is_pressed():
60 # set off a firework
61 shoot_firework(np)
62 explode(np)
63 reset(np)
64 last_gesture = gesture