Initial commit
[one-dimensional-fireworks.git] / fireworks5.py
1 from microbit import *
2 import neopixel
3 import random # Add this line
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 # Add this block
15 FIREWORK_COLOURS = [ (255, 128, 128), (128, 255, 128), (128, 128, 255)
16 , (255, 255, 128), (255, 128, 255), (128, 255, 255)
17 ]
18
19 def fade_all(pixels, fade_by=0.9):
20 for i in range(NP_COUNT):
21 fade(pixels, i, fade_by=fade_by)
22 pixels.show()
23
24 def fade(pixels, n, fade_by=0.9):
25 r, g, b = pixels[n]
26 pixels[n] = (int(r * fade_by), int(g * fade_by), int(b * fade_by))
27
28 def explode(pixels):
29 initial_colour = random.choice(FIREWORK_COLOURS) # Change this line
30
31 for i in range(BURST_SIZE):
32 pixels[NP_COUNT - BURST_SIZE + i] = initial_colour
33 pixels[NP_COUNT - BURST_SIZE - i] = initial_colour
34 fade_all(pixels, fade_by=0.95) # Add this line
35
36 for _ in range(30):
37 fade_all(pixels)
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