Initial commit
[one-dimensional-fireworks.git] / fireworks7.py
1 from microbit import *
2 import neopixel
3 import random
4
5 NP_COUNT = 60
6 BURST_SIZE = 10
7 DISPLAY_ANIMATION_SPEED = 500 # Add this line
8
9 np = neopixel.NeoPixel(pin0, NP_COUNT)
10
11 BLUE = (0, 0, 64)
12 RED = (64, 0, 0)
13 OFF = (0, 0, 0)
14
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, first=0, last=NP_COUNT, fade_by=0.9):
20 for i in range(first, last):
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)
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
35 , first=(NP_COUNT - BURST_SIZE - i)
36 , last= (NP_COUNT - BURST_SIZE + 1)
37 )
38
39 for _ in range(30):
40 fade_all(pixels, first=(NP_COUNT - 2 * BURST_SIZE))
41
42 def reset(pixels):
43 for n in range(NP_COUNT):
44 pixels[n] = OFF
45 pixels[0] = BLUE
46 np.show()
47 display.show(Image.DIAMOND) # Add this line
48
49
50 def shoot_firework(pixels):
51 display.show(Image.ARROW_S) # Add this line
52 for pixel in range(NP_COUNT - BURST_SIZE):
53 pixels[pixel] = RED
54 pixels.show()
55 sleep(20)
56 pixels[pixel] = OFF
57 pixels.show()
58
59
60 reset(np)
61 display_timer = 0 # Add this line
62 last_gesture = accelerometer.current_gesture()
63
64 while True:
65 gesture = accelerometer.current_gesture()
66 if gesture != last_gesture or button_a.is_pressed():
67 # set off a firework
68 shoot_firework(np)
69 explode(np)
70 reset(np)
71 last_gesture = gesture
72
73 #########################################
74 # Add these lines
75 else:
76 # animate the waiting
77 display_timer = (display_timer + 1) % DISPLAY_ANIMATION_SPEED
78 if display_timer > (DISPLAY_ANIMATION_SPEED / 2):
79 display.show(Image.DIAMOND_SMALL)
80 else:
81 display.show(Image.DIAMOND)
82 # End of lines to add
83 #########################################