-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpong.py
More file actions
276 lines (246 loc) · 7.96 KB
/
pong.py
File metadata and controls
276 lines (246 loc) · 7.96 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import gc
import random
import time
import micropython
import qrcode
from machine import ADC, Pin
from micropython import const
from picographics import DISPLAY_TUFTY_2040, PicoGraphics
from pimoroni import Button
display: PicoGraphics = PicoGraphics(display=DISPLAY_TUFTY_2040)
WIDTH: int
HEIGHT: int
WIDTH, HEIGHT = display.get_bounds()
button_a: Button = Button(7, invert=False)
button_b: Button = Button(8, invert=False)
button_c: Button = Button(9, invert=False)
button_up: Button = Button(22, invert=False)
button_down: Button = Button(6, invert=False)
# Set this to whatever text you want the QR code to be.
qrtext: str = "https://github.com/DNSGeek/Random-Stuff/blob/master/pong.py"
# Set your names here. It will be autoscaled and centered
firstname: str = "FIRSTNAME"
lastname: str = "LASTNAME"
namesize: int = 5
pheight: int = const(50)
class Player:
def __init__(self, xp: int) -> None:
self.xpos: int = xp
self.ypos: int = 130
self.score: int = 0
self.ybottom: int = self.ypos + pheight
def reset(self) -> None:
self.ypos = 130
self.score = 0
self.ybottom = self.ypos + pheight
@micropython.viper
def collision(self, bally: int) -> bool:
ballybot: int = int(bally) + 20
ypos: int = int(self.ypos)
ybottom: int = int(self.ybottom)
if ypos <= int(bally) <= ybottom:
return True
if ypos <= ballybot <= ybottom:
return True
return False
@micropython.native
def move(self, lr: bool, bally: int) -> None:
if (self.xpos == 0 and lr) or (self.xpos == 300 and not lr):
ballybot: int = bally + 20
silly: bool = random.getrandbits(1) == 0
if self.ybottom < bally:
if silly:
if self.ypos > 92:
self.ypos -= 1
self.ybottom = self.ypos + pheight
else:
if self.ypos < 190:
self.ypos += 1
self.ybottom = self.ypos + pheight
if self.ypos > ballybot:
if silly:
if self.ypos < 190:
self.ypos += 1
self.ybottom = self.ypos + pheight
else:
if self.ypos > 92:
self.ypos -= 1
self.ybottom = self.ypos + pheight
def getBacklightLevel(light: ADC) -> float:
# Keep the display dim to save battery
reading: float = float(light.read_u16())
# Values seem to be between 0.0 - 25000.0
bl: float = (reading / 25000.0) + 0.1
# Lower than 0.4 seems to turn off the backlight
bl = max(bl, 0.4)
bl = min(bl, 1.0)
return bl
def clearScreen() -> None:
display.set_pen(0)
display.clear()
def computeNameSize() -> None:
global namesize
maxwidth: int = const(240)
namesize = 5
fnamewidth: int = display.measure_text(firstname, namesize)
lnamewidth: int = display.measure_text(lastname, namesize)
namewidth: int = max(fnamewidth, lnamewidth)
while namewidth > maxwidth:
if namesize == 1:
return
namesize -= 1
fnamewidth = display.measure_text(firstname, namesize)
lnamewidth = display.measure_text(lastname, namesize)
namewidth = max(fnamewidth, lnamewidth)
def displayScore(p1s: int, p2s: int) -> None:
fnamewidth: int = display.measure_text(firstname, namesize)
lnamewidth: int = display.measure_text(lastname, namesize)
fnoffset: int = ((240 - fnamewidth) // 2) + 40
lnoffset: int = ((240 - lnamewidth) // 2) + 40
display.set_pen(255)
display.text(f"{p1s}", 0, 0, scale=4)
display.set_pen(28)
display.text(firstname, fnoffset, 0, scale=namesize)
display.text(lastname, lnoffset, 40, scale=namesize)
display.set_pen(255)
display.text(f"{p2s}", 280, 0, scale=4)
display.line(0, 90, 320, 90)
@micropython.native
def displayPlayers(p1h: int, p2h: int) -> None:
display.set_pen(224)
display.rectangle(0, p1h, 20, 50)
display.rectangle(300, p2h, 20, 50)
@micropython.native
def moveBall(
bx: int, by: int, lr: bool, ud: bool, p1s: Player, p2s: Player
) -> tuple[int, int, bool, bool]:
if lr is False:
# We're moving right
if bx > 280:
bx = 160
by = 120
p1s.score += 1
lr = random.getrandbits(1) == 0
ud = random.getrandbits(1) == 0
else:
bx += 1
else:
if bx < 20:
bx = 160
by = 120
p2s.score += 1
lr = random.getrandbits(1) == 0
ud = random.getrandbits(1) == 0
else:
bx -= 1
if ud is False:
if by > 220:
ud = True
by -= 1
else:
by += 1
else:
if by < 92:
ud = False
by += 1
else:
by -= 1
return bx, by, lr, ud
@micropython.native
def displayBall(bx: int, by: int, color: int) -> None:
display.set_pen(color)
display.rectangle(bx, by, 20, 20)
@micropython.native
def detectCollision(bx: int, by: int, p1c: Player, p2c: Player, lr: bool) -> bool:
if bx == 20:
if p1c.collision(by):
lr = not lr
if bx == 280:
if p2c.collision(by):
lr = not lr
return lr
def showQRCode(lx: ADC) -> None:
clearScreen()
code = qrcode.QRCode()
code.set_text(qrtext)
xs: int
ys: int
xs, ys = code.get_size()
x_size: int = WIDTH // xs
y_size: int = HEIGHT // ys
pixel_size: int = min(x_size, y_size)
offset_x: int = (WIDTH // 2) - ((xs * pixel_size) // 2)
offset_y: int = (HEIGHT // 2) - ((ys * pixel_size) // 2)
display.set_pen(255)
display.rectangle(0, 0, WIDTH, HEIGHT)
for qx in range(xs - 1):
for qy in range(ys - 1):
borw: bool = code.get_module(qx, qy)
xp: int = qx * pixel_size
yp: int = qy * pixel_size
display.set_pen(255 if borw else 0)
display.rectangle(xp + offset_x, yp + offset_y, pixel_size, pixel_size)
display.set_backlight(1.0)
display.update()
time.sleep(10)
display.set_backlight(getBacklightLevel(lx))
# Do the basic initialization
gc.collect()
gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
random.seed()
computeNameSize()
display.set_font("bitmap8")
lux_pwr: Pin = Pin(27, Pin.OUT)
lux_pwr.value(1)
del lux_pwr # memory is tight, remove anything not needed
lux: ADC = ADC(26)
display.set_backlight(getBacklightLevel(lux))
p1: Player = Player(0)
p2: Player = Player(300)
lorr: bool = random.getrandbits(1) == 0
uord: bool = random.getrandbits(1) == 0
x: int = 160
y: int = 100
# [red, orange, yellow, green, blue, indigo, violet]
c: tuple[int, ...] = (224, 236, 252, 28, 3, 102, 66)
index: int = 0
count: int = 0
high: int = const(15)
while True:
clearScreen()
displayScore(p1.score, p2.score)
p1.move(lorr, y)
p2.move(lorr, y)
x, y, lorr, uord = moveBall(x, y, lorr, uord, p1, p2)
displayPlayers(p1.ypos, p2.ypos)
lorr = detectCollision(x, y, p1, p2, lorr)
displayBall(x, y, c[index])
count = (count + 1) % 10
if not count:
index = (index + 1) % 7
display.set_backlight(getBacklightLevel(lux))
gc.collect()
gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
if p1.score == high or p2.score == high:
clearScreen()
display.set_pen(255)
winner: str = "Player 1" if p1.score == high else "Player 2"
display.text(f"{winner} wins!", 5, 100, scale=5)
display.update()
time.sleep(3)
p1.reset()
p2.reset()
x = 160
y = 100
gc.collect()
gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
display.update()
if (
button_a.is_pressed
or button_b.is_pressed
or button_c.is_pressed
or button_up.is_pressed
or button_down.is_pressed
):
showQRCode(lux)
time.sleep_ms(16) # ~60fps cap; reduces CPU burn and saves battery