-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpicopong.py
More file actions
395 lines (352 loc) · 11.5 KB
/
Copy pathpicopong.py
File metadata and controls
395 lines (352 loc) · 11.5 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#Import libraries
#import picounicorn
import picographics
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2, PEN_RGB332
from pimoroni import Button
import jpegdec
import sys
import utime
#picounicorn.init()
display = picographics.PicoGraphics(display=picographics.DISPLAY_PICO_DISPLAY_2, pen_type=PEN_RGB332)
display.clear()
display.update()
# From CPython Lib/colorsys.py
def hsv_to_rgb(h, s, v):
if s == 0.0:
return v, v, v
i = int(h * 6.0)
f = (h * 6.0) - i
p = v * (1.0 - s)
q = v * (1.0 - s * f)
t = v * (1.0 - s * (1.0 - f))
i = i % 6
if i == 0:
return v, t, p
if i == 1:
return q, v, p
if i == 2:
return p, v, t
if i == 3:
return p, q, v
if i == 4:
return t, p, v
if i == 5:
return v, p, q
#Define setup variables
#w = picounicorn.get_width()#16
#h = picounicorn.get_height()#7
w, h = display.get_bounds()
startballx=7
startbally=3
startdirH=1
startdirV=0
startscoreXY=0
startscoreAB=0
onlistAB=[2,3,4]
onlistXY=[2,3,4]
#Create lists of pixels for the height/width of the whole display/ball area
listW=[]#makes a list 0-15
for i in range(w):
listW.append(i)
listH=[]#makes a list 0-6
for i in range(h):
listH.append(i)
listWball=[]#makes a list 1-14
for i in range(1,15):
listWball.append(i)
listHball=[]#makes a list 0-6
for i in range(0,7):
listHball.append(i)
# Define colours
playerXYcolours=(0,114,178)
playerABcolours=(255,70,160)
ballcolours=playerABcolours
#Set constant variables for pong title, scoring and win message
pongtitle=[[" "],
["RRR RRR RRR RRRX"],
["R R R R R R R RX"],
["RRR R R R R RRRX"],
["R R R R R R "],
["R RRR R R RRRX"],
[" "]]
BLANKSECTION= [" " for i in range(h)]
NUMZERO=[" ",
"XXX",
"X X",
"X X",
"X X",
"XXX",
" "]
NUMONE=[" ",
" X ",
"XX ",
" X ",
" X ",
"XXX",
" "]
NUMTWO=[" ",
"XXX",
" X",
"XXX",
"X ",
"XXX",
" "]
NUMTHREE=[" ",
"XXX",
" X",
"XXX",
" X",
"XXX",
" "]
NUMFOUR=[" ",
"X X",
"X X",
"XXX",
" X",
" X",
" "]
NUMFIVE=[" ",
"XXX",
"X ",
"XXX",
" X",
"XXX",
" "]
DASH=[" ",
" ",
" ",
" XX ",
" ",
" ",
" "]
LETTERW=[" ",
"X X",
"X X",
"X X",
"X X X",
"XXXXX",
" "]
LETTERI=[" ",
"XXX",
" X ",
" X ",
" X ",
"XXX",
" "]
LETTERN=[" ",
"X X",
"XX X",
"X XX",
"X X",
"X X",
" "]
PUNCEXCALM=[" ",
"X ",
"X ",
"X ",
" ",
"X ",
" "]
#Create dictionaries to store scoring numbers/text
scoredict={0:NUMZERO,1:NUMONE,2:NUMTWO,3:NUMTHREE,4:NUMFOUR,5:NUMFIVE,"dash":DASH}
textdict={"W":LETTERW,"I":LETTERI,"N":LETTERN,"!":PUNCEXCALM}
#Function to clear display by setting pixels to black
def cleardisplay():
for x in range(w):
for y in range(h):
picounicorn.set_pixel(x, y, 0, 0, 0)
#Function to update display with letters/numbers
def updatedisplay(displaymap):
x=0
y=0
for row in displaymap:
for line in row:
for char in line:
if x < w and y <h:
if char == "X":
r, g, b = 255,255,255
elif char == "R":
r, g, b = [int(c * 255) for c in hsv_to_rgb(x / w, y / h, 1.0)]
elif char == "D":
r, g, b = 200,200,40
elif char == "A":
r, g, b = playerABcolours
elif char == "Y":
r, g, b = playerXYcolours
else:
r,g,b=0,0,0
picounicorn.set_pixel(x, y, r, g, b)
x+=1
x=0
y+=1
return displaymap
#Function to generate an 2D array to represent the current score in the format: PlayerABscore - PlayerXYscore
def generatescore(scoreAB,scoreXY):
scoreABpix = [item.replace("X","A") for item in scoredict[scoreAB]]
scoreXYpix = [item.replace("X","Y") for item in scoredict[scoreXY]]
dashpix = [item.replace("X","D") for item in scoredict["dash"]]
fulldisplay = [["{}{}{}{}{}".format(BLANKSECTION[i],scoreABpix[i],dashpix[i],scoreXYpix[i],BLANKSECTION[i])] for i in range(h)]
return fulldisplay
#Function to generate a 2D array of specified text e.g. "WIN!"
def generatemessage(winningcolour):
text1=[item.replace("X",winningcolour) for item in textdict["W"]]
text2=[item.replace("X",winningcolour) for item in textdict["I"]]
text3=[item.replace("X",winningcolour) for item in textdict["N"]]
text4=[item.replace("X",winningcolour) for item in textdict["!"]]
fulldisplay = [["{} {} {} {} ".format(text1[i],text2[i],text3[i],text4[i])] for i in range(h)]
return fulldisplay
#Function to make the "display map" seem as though it is "scrolling" (loop)
def scrolldisplay(displaymap):
scrollmap=[]
rowcount=0
for row in displaymap:
scrollmap.append([])
for line in row:
scrollmap[rowcount]= [line[1:]+line[0]]
rowcount+=1
return scrollmap
#Display the title screen for 2 seconds
updatedisplay(pongtitle)
utime.sleep(2)
#Function to create the ball and ball trail(using previous ball co-ords)
def ball(currentballx,currentbally,prevballx,prevbally,prevball2x,prevball2y,ballcolours):
for x in listWball:
for y in listHball:
if x == currentballx:
if y == currentbally:
r,g,b=ballcolours
picounicorn.set_pixel(x, y, r, g, b)
elif x == prevballx:
if y == prevbally:
r,g,b=[round(element * 0.3) for element in ballcolours]
picounicorn.set_pixel(x, y, r, g, b)
elif x == prevball2x:
if y == prevball2y:
r,g,b=[round(element * 0.2) for element in ballcolours]
picounicorn.set_pixel(x, y, r, g, b)
else:
r,g,b=0,0,0
picounicorn.set_pixel(x, y, r, g, b)
return currentballx,currentbally,prevballx,prevbally,prevball2x,prevball2y
#Initial invocation of "ball" function
ball(startballx,startbally,startballx,startbally,startballx,startbally,ballcolours)
ballx=startballx
bally=startbally
dirH=startdirH
dirV=startdirV
scoreAB=startscoreAB
scoreXY=startscoreXY
#Function to create playerAB paddle (in the first column)
def lightcontrolAB(onlistAB):
for x in range(1):
for y in listH:
if y in onlistAB:
r,g,b=playerABcolours
else:
r,g,b=0,0,0
picounicorn.set_pixel(x, y, r, g, b)
#Function to create playerXY paddle (in the last column)
def lightcontrolXY(onlistXY):
for x in range(15,16):
for y in listH:
if y in onlistXY:
r,g,b=playerXYcolours
else:
r,g,b=0,0,0
picounicorn.set_pixel(x, y, r, g, b)
#Function to determine the ball colour & direction (based on it's position)and update scoring
def ballposition(currentballx,currentbally,currentdirH,currentdirV,scoreAB,scoreXY,ballcolours,startdirH):
if currentballx == listWball[0]:#if ball is in the furthest left column
ballcolours=playerABcolours
if currentbally in onlistAB:
currentdirH=1
if currentbally == onlistAB[0]:
currentdirV=-1
if currentbally == onlistAB[2]:
currentdirV=1
else:
scoreXY+=1
startdirH=1
currentballx=startballx
currentbally=startbally
currentdirH=startdirH
currentdirV=startdirV
cleardisplay()
updatedisplay(generatescore(scoreAB,scoreXY))
utime.sleep(1)
elif currentballx == listWball[-1]:#if ball is in the furthest right column
ballcolours=playerXYcolours
if currentbally in onlistXY:
currentdirH= -1
if currentbally == onlistXY[0]:
currentdirV=-1
if currentbally == onlistXY[2]:
currentdirV=1
else:
scoreAB+=1
startdirH=- 1
currentballx=startballx
currentbally=startbally
currentdirH=startdirH
currentdirV=startdirV
cleardisplay()
updatedisplay(generatescore(scoreAB,scoreXY))
utime.sleep(1)
elif currentballx in listWball:
if currentbally == listHball[0]:
currentdirV=1
elif currentbally == listHball[-1]:
currentdirV=-1
prevball2x=currentballx-currentdirH
prevball2y=currentbally-currentdirV
prevballx=currentballx
prevbally=currentbally
currentballx=currentballx+currentdirH
currentbally=currentbally+currentdirV
ball(currentballx,currentbally,prevballx,prevbally,prevball2x,prevball2y,ballcolours)
return currentballx,currentbally,currentdirH,currentdirV,scoreAB,scoreXY,ballcolours,startdirH
#Function which runs the game
while True:
ballx,bally,dirH,dirV,scoreAB,scoreXY,ballcolours,startdirH = ballposition(ballx,bally,dirH,dirV,scoreAB,scoreXY,ballcolours,startdirH)
if scoreAB >=5 or scoreXY >=5:
if scoreAB>=5:
winningcolour="A"
else:
winningcolour="Y"
cleardisplay()
currentdisplaymap=updatedisplay(generatemessage(winningcolour))
# for i in range(w*3):
while True:
currentdisplaymap=updatedisplay(scrolldisplay(currentdisplaymap))
utime.sleep(0.1)
if picounicorn.is_pressed(picounicorn.BUTTON_A) or picounicorn.is_pressed(picounicorn.BUTTON_B) or picounicorn.is_pressed(picounicorn.BUTTON_X) or picounicorn.is_pressed(picounicorn.BUTTON_Y):
ballx=startballx
bally=startbally
dirH=startdirH
dirV=startdirV
scoreAB=startscoreAB
scoreXY=startscoreXY
updatedisplay(pongtitle)
utime.sleep(2)
ball(startballx,startbally,startballx,startbally,startballx,startbally,ballcolours)
break
else:
if picounicorn.is_pressed(picounicorn.BUTTON_A):
if onlistAB[0]!=0:
onlistAB.insert(0,onlistAB[0]-1)
onlistAB=onlistAB[:-1]
elif picounicorn.is_pressed(picounicorn.BUTTON_B):
if onlistAB[-1]!=6:
onlistAB.insert(len(onlistAB),onlistAB[-1]+1)
onlistAB=onlistAB[1:]
if picounicorn.is_pressed(picounicorn.BUTTON_X):
if onlistXY[0]!=0:
onlistXY.insert(0,onlistXY[0]-1)
onlistXY=onlistXY[:-1]
elif picounicorn.is_pressed(picounicorn.BUTTON_Y):
if onlistXY[-1]!=6:
onlistXY.insert(len(onlistXY),onlistXY[-1]+1)
onlistXY=onlistXY[1:]
lightcontrolAB(onlistAB)
lightcontrolXY(onlistXY)
utime.sleep(0.1)