-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.py
executable file
·199 lines (163 loc) · 6.34 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
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
import pyxel
import random
screen_x_dim: int = 128
screen_y_dim: int = 80
paddle_height: int = 10
paddle_width: int = 2
paddle_speed: int = 3
paddle_padding_from_screen = 5
puck_speed: int = 2
puck_size: int = 2
class Puck:
def __init__(self):
self.side_length = puck_size
self.x = screen_x_dim // 2
self.y = screen_y_dim // 2
self.color = 7
self.move_down = bool(random.getrandbits(1))
self.move_right = bool(random.getrandbits(1))
def move(self):
if self.y <= 0:
self.move_down = True
if self.y + self.side_length >= screen_y_dim:
self.move_down = False
if self.move_down:
self.y += puck_speed
if not self.move_down:
self.y -= puck_speed
if self.move_right:
self.x += puck_speed
if not self.move_right:
self.x -= puck_speed
class Paddle:
def __init__(self, x_position, color):
self.color = color
self.height = paddle_height
self.y = (screen_y_dim // 2) - (self.height // 2)
self.x = x_position
self.width = paddle_width
self.speed = paddle_speed
def move_up(self):
if self.y > 0:
self.y = self.y - paddle_speed
def move_down(self):
if (self.y + self.height) < screen_y_dim:
self.y = self.y + paddle_speed
def paddle_and_puck_collide(paddle: Paddle, puck: Puck) -> bool:
"""
Given a paddle and a puck, returns whether the two collide.
"""
if paddle.x < screen_x_dim // 2:
# We are dealing with the left paddle
if puck.x <= paddle.x + paddle.width:
if puck.y < paddle.y + paddle.height // 2:
if puck.y + puck.side_length >= paddle.y:
pyxel.play(0, 0)
return True
else:
if puck.y <= paddle.y + paddle.height:
pyxel.play(0, 0)
return True
else:
if puck.x + puck.side_length >= paddle.x:
if puck.y < paddle.y + paddle.height // 2:
if puck.y + puck.side_length >= paddle.y:
pyxel.play(0, 0)
return True
else:
if puck.y <= paddle.y + paddle.height:
pyxel.play(0, 0)
return True
return False
class App:
def __init__(self, x_dim, y_dim):
self.screen_x_dim = x_dim
self.screen_y_dim = y_dim
self.score1 = 0
self.score2 = 0
self.game_paused: bool = True
self.best_of_msg: str = "BEST OF 9"
self.p1won: bool = False
self.p2won: bool = False
self.bg_music_playing: bool = False
pyxel.init(self.screen_x_dim, self.screen_y_dim, caption="PONG", fullscreen=True)
pyxel.load("assets.pyxres")
pyxel.run(self.update, self.draw)
def start_bg_music(self):
if not self.bg_music_playing:
pyxel.play(1, 3, loop=True)
self.bg_music_playing = True
def stop_bg_music(self):
if self.bg_music_playing:
pyxel.stop(1)
self.bg_music_playing = False
def update(self):
if pyxel.btnp(pyxel.KEY_SPACE):
if self.p1won or self.p2won:
pyxel.quit()
else:
self.game_paused = not self.game_paused
if not self.game_paused:
self.start_bg_music()
# Listening to input
if pyxel.btn(pyxel.KEY_Q):
paddle1.move_up()
if pyxel.btn(pyxel.KEY_A):
paddle1.move_down()
if pyxel.btn(pyxel.KEY_O):
paddle2.move_up()
if pyxel.btn(pyxel.KEY_L):
paddle2.move_down()
# move puck
the_puck.move()
# check collision for both paddles
if the_puck.x <= paddle_width + paddle_padding_from_screen:
if paddle_and_puck_collide(paddle1, the_puck):
the_puck.move_right = True
elif the_puck.x + the_puck.side_length >= screen_x_dim - paddle_padding_from_screen - \
paddle_width:
if paddle_and_puck_collide(paddle2, the_puck):
the_puck.move_right = False
# update points and set ball
if the_puck.x <= 0:
self.score2 += 1
self.stop_bg_music()
pyxel.play(0, 1)
the_puck.x = paddle2.x - paddle2.width
the_puck.y = paddle2.y + paddle2.height // 2
if self.score2 >= 5:
self.best_of_msg = "BRAVO P2!"
self.p2won = True
self.stop_bg_music()
pyxel.play(0, 2)
self.game_paused = True
if the_puck.x + the_puck.side_length >= screen_x_dim:
self.score1 += 1
self.stop_bg_music()
pyxel.play(0, 1)
the_puck.x = paddle1.x + paddle1.width
the_puck.y = paddle1.y + paddle1.height // 2
if self.score1 >= 5:
self.best_of_msg = "BRAVO P1!"
self.p1won = True
self.stop_bg_music()
pyxel.play(0, 2)
self.game_paused = True
def draw(self):
if not self.game_paused:
pyxel.cls(0)
pyxel.rect(the_puck.x, the_puck.y, the_puck.side_length, the_puck.side_length,
the_puck.color)
pyxel.rect(paddle1.x, paddle1.y, paddle1.width, paddle1.height, paddle1.color)
pyxel.rect(paddle2.x, paddle2.y, paddle2.width, paddle2.height, paddle2.color)
pyxel.text(screen_x_dim // 3, paddle_padding_from_screen, str(self.score1), 7)
pyxel.text(screen_x_dim - screen_x_dim // 3, paddle_padding_from_screen, str(self.score2), 7)
else:
best_of_width = len(self.best_of_msg) * 3
pyxel.text(screen_x_dim // 2 - best_of_width // 2 - 6, screen_y_dim - screen_y_dim // 3,
self.best_of_msg, 7)
pyxel.text(screen_x_dim // 2 - 17, screen_y_dim - screen_y_dim // 3 + 6, "SPACEBAR", 7)
paddle1 = Paddle(paddle_padding_from_screen, 5)
paddle2 = Paddle(screen_x_dim - paddle_width - paddle_padding_from_screen, 8)
the_puck = Puck()
App(screen_x_dim, screen_y_dim)