-
Notifications
You must be signed in to change notification settings - Fork 1
/
pong.py
159 lines (125 loc) · 4.06 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
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
from micropython import const
from machine import Pin, ADC
from lib.neopixelmatrix import NeoPixelMatrix
from urandom import randint, randrange
from utime import sleep_ms
GPIO_ADC_PIN = const(32)
GPIO_MATRIX_PIN = const(23)
MATRIX_ROWS = const(10)
MATRIX_COLS = const(16)
class Paddle:
def __init__(self, neopixel, color: tuple):
"""
paddle constructor
:param neopixel: NeoPixel object
:param color: RGB color tuple
"""
self.nps = neopixel
self.color = tuple(color)
self.height = 3
self.pos_x = 0
self.pos_y = None
def draw_paddle(self) -> None:
"""
draw paddle on NeoPixel matrix
:return: None
"""
self.nps.set_coordinate(self.pos_x, self.pos_y, self.color)
self.nps.vline(self.pos_x, self.pos_y - 1, 3, self.color)
class Ball:
def __init__(self, neopixel, color: tuple):
"""
ball constructor
:param neopixel: NeoPixel object
:param color: RGB color tuple
"""
self.nps = neopixel
self.color = tuple(color)
self.pos_x = randint(3, MATRIX_COLS - 2)
self.pos_y = randint(3, MATRIX_ROWS - 2)
self.direction_x = [-1, 1][randrange(0, 2)]
self.direction_y = [-1, 1][randrange(0, 2)]
def draw_ball(self) -> None:
"""
draw ball on NeoPixel matrix
:return: None
"""
self.nps.set_coordinate(self.pos_x, self.pos_y, self.color)
class Score:
def __init__(self, neopixel):
"""
score constructor
:param neopixel: RGB color tuple
"""
self.nps = neopixel
self.score = 0
def draw_score(self):
"""
draw score on NeoPixel matrix
:return: None
"""
if self.score == 1:
self.nps.set_coordinate(MATRIX_COLS - 1, 0, (20, 0, 0))
if self.score == 2:
self.nps.set_coordinate(MATRIX_COLS - 1, 0, (20, 0, 0))
self.nps.set_coordinate(MATRIX_COLS - 2, 0, (20, 0, 0))
if self.score == 3:
self.nps.set_coordinate(MATRIX_COLS - 1, 0, (20, 0, 0))
self.nps.set_coordinate(MATRIX_COLS - 2, 0, (20, 0, 0))
self.nps.set_coordinate(MATRIX_COLS - 3, 0, (20, 0, 0))
if __name__ == '__main__':
adc = ADC(Pin(GPIO_ADC_PIN, Pin.IN))
adc.atten(ADC.ATTN_11DB)
nps = NeoPixelMatrix(pin=GPIO_MATRIX_PIN, rows=MATRIX_ROWS, cols=MATRIX_COLS)
paddle = Paddle(nps, color=(0, 10, 0))
ball = Ball(nps, color=(0, 0, 20))
score = Score(nps)
nps.clear_matrix()
nps.set_digit(3, 7, 2, (0, 20, 0))
nps.write_matrix()
sleep_ms(500)
nps.clear_matrix()
nps.set_digit(2, 7, 2, (0, 20, 0))
nps.write_matrix()
sleep_ms(500)
nps.clear_matrix()
nps.set_digit(1, 7, 2, (0, 20, 0))
nps.write_matrix()
sleep_ms(500)
while True:
adc_val = adc.read_u16()
paddle.pos_y = int(adc_val * (MATRIX_ROWS / 65535))
ball.pos_x += ball.direction_x
ball.pos_y += ball.direction_y
if paddle.pos_y >= 8:
paddle.pos_y = 8
if paddle.pos_y <= 1:
paddle.pos_y = 1
if ball.pos_x == 0:
if ball.pos_x or ball.pos_y in {(paddle.pos_y - 1), paddle.pos_y, (paddle.pos_y + 1)}:
ball.direction_x = 1
if ball.pos_x == -1:
score.score += 1
ball.pos_x = randint(3, MATRIX_COLS - 2)
ball.pos_y = randint(3, MATRIX_ROWS - 2)
if score.score > 3:
break
if ball.pos_x == (MATRIX_COLS - 1):
ball.direction_x = -1
if ball.pos_y < 1:
ball.direction_y = 1
if ball.pos_y > (MATRIX_ROWS - 2):
ball.direction_y = -1
nps.clear_matrix()
score.draw_score()
paddle.draw_paddle()
ball.draw_ball()
nps.write_matrix()
sleep_ms(150)
for i in range(0, 2):
nps.fill_matrix((20, 0, 0))
nps.write_matrix()
sleep_ms(500)
nps.fill_matrix((0, 0, 0))
nps.write_matrix()
sleep_ms(500)