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