forked from murilopolese/pixelkit-micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.py
More file actions
105 lines (103 loc) · 2.76 KB
/
pong.py
File metadata and controls
105 lines (103 loc) · 2.76 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
import PixelKit as kit
from random import randint as random
t = 0
ballPosition = [7, 4]
ballDirection = [1, 1]
padPosition = 2
padSize = 2
gameOver = False
level = 12
score = 0
def reset(button=False):
global t
global ballPosition
global ballDirection
global padPosition
global gameOver
global level
global score
t = 0
ballPosition = [7, 4]
ballDirection = [1, 1]
padPosition = 2
gameOver = False
level = 12
score = 0
def renderBall():
kit.set_pixel(ballPosition[0], ballPosition[1], [20, 20, 20])
def renderLeftPad():
for i in range(0, padSize):
kit.set_pixel(0, int(padPosition + i), [0, 0, 10])
def renderRightPad():
for i in range(0, padSize):
kit.set_pixel(15, int(padPosition + i), [0, 10, 0])
def renderFrame():
renderBall()
renderLeftPad()
renderRightPad()
def hitPad():
return ballPosition[1] >= int(padPosition) and ballPosition[1] <= int(padPosition)+(padSize-1)
def updateBall():
global gameOver
global level
global score
newPosition = [
ballPosition[0] + ballDirection[0],
ballPosition[1] + ballDirection[1]
];
if newPosition[1] > 7 or newPosition[1] < 0:
ballDirection[1] = ballDirection[1] * -1
if newPosition[0] == 15 or newPosition[0] == 0:
if hitPad():
ballDirection[0] = ballDirection[0] * -1
level = level - 0.5
if level < 3:
level = 3
score = score + 1
else:
gameOver = True
ballPosition[0] = ballPosition[0] + ballDirection[0]
ballPosition[1] = ballPosition[1] + ballDirection[1]
def onJoystickUp():
global padPosition
if padPosition > 0:
padPosition = padPosition - 0.2
def onJoystickDown():
global padPosition
if padPosition < 5:
padPosition = padPosition + 0.2
def onDial(value):
global padPosition
dialPosition = int((value/4095)*7)
if dialPosition >= 0 and dialPosition <= 7-(padSize-1):
padPosition = dialPosition
def nothing():
return None
kit.on_joystick_up = nothing
kit.on_joystick_down = nothing
kit.on_joystick_left = nothing
kit.on_joystick_right = nothing
kit.on_button = reset
kit.on_button_a = reset
kit.on_button_b = reset
kit.on_dial = onDial
def loop():
global t
t = t + 1
kit.check_controls()
if not gameOver:
if t % int(level) == 0:
updateBall()
kit.clear()
renderFrame()
kit.render()
else:
kit.set_background([10, 10, 0])
for i in range(0, score):
kit.set_pixel(i%16, int(i/16), [10, 0, 0])
kit.render();
def start():
reset()
while True:
loop()
start()