-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchristmas2022.py
More file actions
160 lines (129 loc) · 3.7 KB
/
Copy pathchristmas2022.py
File metadata and controls
160 lines (129 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import time
import random
from random import seed
from random import randint
import picographics
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_RGB332
from pimoroni import Button
import jpegdec
import sys
display = picographics.PicoGraphics(display=picographics.DISPLAY_PICO_DISPLAY, pen_type=PEN_RGB332)
display.clear()
display.update()
image_num = 1
sw, sh = display.get_bounds()
button_a = Button(12)
button_b = Button(13)
button_x = Button(14)
button_y = Button(15)
# Create a new JPEG decoder for our PicoGraphics
j = jpegdec.JPEG(display)
# Classes
class Ball:
def __init__(self, x, y, r, dx, dy, pen):
self.x = x
self.y = y
self.r = r
self.dx = dx
self.dy = dy
self.pen = pen
# Functions
def show_image(idImage):
# Open the JPEG file
filename = "image_" + str(idImage) + ".jpeg"
image = j.open_file(filename)
# Decode the JPEG
j.decode(0, 0, jpegdec.JPEG_SCALE_FULL)
jw = j.get_width()
jh = j.get_height()
pw = int((sw - jw)/2)
ph = int((sh - jh)/2)
display.clear()
j.decode(pw, ph, jpegdec.JPEG_SCALE_FULL)
# Display the result
display.update()
def show_balls():
display.set_pen(BG)
for ball in balls:
ball.x += ball.dx
ball.y += ball.dy
xmax = sw - ball.r
xmin = ball.r
ymax = sh - ball.r
ymin = ball.r
if ball.x < xmin or ball.x > xmax:
ball.dx *= -1
if ball.y < ymin or ball.y > ymax:
ball.dy *= -1
display.set_pen(ball.pen)
display.circle(int(ball.x), int(ball.y), int(ball.r))
display.update()
def show_random_balls():
display.set_pen(BG)
for ball in balls:
ball.x = randint(0, sw)
ball.y = randint(0, sh)
display.set_pen(ball.pen)
display.circle(int(ball.x), int(ball.y), int(ball.r))
display.update()
# initialise shapes
seed(999)
balls = []
for i in range(0, 5):
r = random.randint(0, 10) + 3
balls.append(
Ball(
random.randint(r, r + (sw - 2 * r)),
random.randint(r, r + (sh - 2 * r)),
5,
(14 - 4) / 2,
(14 - 4) / 2,
display.create_pen(random.randint(200, 255), random.randint(200, 255), random.randint(200, 255)),
)
)
BG = display.create_pen(40, 40, 40)
show_image(image_num)
ballcount = 0
while True:
ballcount += 1
if button_a.read():
print("A pressed")
image_num += 1
if image_num > 9:
image_num = 9
show_image(image_num)
elif button_b.read():
print("B pressed")
image_num -= 1
if image_num < 1:
image_num = 1
show_image(image_num)
elif button_y.read():
print("Y pressed - exiting")
display.clear()
display.update()
sys.exit("B pressed - exiting")
elif button_x.read():
print("X pressed")
while True:
image_num = randint(1, 9)
show_image(image_num)
for _ in range(0, 20):
show_random_balls()
time.sleep(0.1)
if button_y.read():
print("Y pressed")
image_num = 1
show_image(image_num)
break
if button_b.read():
print("B pressed - exiting")
display.clear()
display.update()
sys.exit("B pressed - exiting")
show_random_balls()
if ballcount == 50:
image_num = randint(1, 9)
show_image(image_num)
ballcount = 0;
time.sleep(0.1)