-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.py
106 lines (89 loc) · 2.29 KB
/
pong.py
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
#Very simple Pong for RaspberyPi and SenseHat
#(c) Kuba Siatkowski
import time
from sense_hat import SenseHat
sense = SenseHat()
sense.set_rotation(180)
sense.clear()
#ball
x, y = 4, 7
#pad
pl, pr = 3 , 4
#ball move
xi, yi = -1, -1
#initial speed
speed = 0.5
#speed up factor
speedinc = 0.05
#initial points
points = 0
#grow pad every x points
growth = 5
while True:
sense.clear()
#move and draw the ball
if x == 7:
xi = -1
if x == 0:
xi = 1
x = x + xi
y = y + yi
sense.set_pixel(x, y, ((x * 16), (255 - x * 16), (255 - x * y)))
#move the pad
for event in sense.stick.get_events():
if event.action == 'pressed' and event.direction == 'right':
if pl > 0:
pl -= 1
pr -= 1
if event.action == 'pressed' and event.direction == 'left':
if pr < 7:
pr += 1
pl += 1
#distraction when middle button is pressed
if event.action == 'pressed' and event.direction == 'middle':
sense.clear(48,48,48)
#draw the pad
for i in range (0,8):
if i < pl:
sense.set_pixel (i,0,(0,0,0))
elif i > pr:
sense.set_pixel (i,0,(0,0,0))
else:
sense.set_pixel (i,0,(255,255,255))
time.sleep(speed)
if y == 7:
yi = -1
if y == 1:
#if ball hit the pad
if pl -1 <= x <= pr + 1:
yi = 1
points = points + 1
speed = speed * 0.8
#increase speed
if speed < 0:
speed = 0.025
#make the pad wider
if points % growth == 0:
if pl >= 1:
pl -= 1
else:
pr += 1
#reset the game if ball missed the pad
else:
sense.clear(255,0,0)
speed = 0.5
points = 0
pl, pr = 3,4
3,4
time.sleep(1)
x = 4
y = 7
#reset pad size if it is too wide
if pr > 8:
sense.clear(0,255,0)
time.sleep(0.5)
pl, pr = 3,4
speed = speed * 20
#debug messages
msg = "x = {0}, y = {1}, pr = {2}, pl = {3}, speed = {4}, points = {5}".format(x, y ,pr, pl, speed, points)
print (msg)