-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_oop.py
More file actions
353 lines (297 loc) · 12.4 KB
/
game_oop.py
File metadata and controls
353 lines (297 loc) · 12.4 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
import random
from math import sqrt
import pyglet
from pyglet import shapes
from pyglet.window import FPSDisplay, key
class Sounds:
def __init__(self):
import sys
if sys.platform == "linux":
pyglet.options['audio'] = ('openal', 'pulse', 'silent')
else:
pyglet.options['audio'] = ('openal', 'directsound', 'silent')
pyglet.resource.path = ['res/sounds']
pyglet.resource.reindex()
self.beep = pyglet.resource.media('ping_pong_8bit_beeep.ogg', streaming=False)
self.peep = pyglet.resource.media('ping_pong_8bit_peeeeeep.ogg', streaming=False)
self.plop = pyglet.resource.media('ping_pong_8bit_plop.ogg', streaming=False)
class Drawing:
def __init__(self, width, height, keys=None, ai=False):
self.window_width = width
self.window_height = height
self.keys = keys
self.scale_factor = (self.window_width + self.window_height) // 2
self.computer_player = ai
@staticmethod
def draw_rect(x, y, width, height, color=(255, 255, 255)):
rectangle = shapes.Rectangle(x, y, width, height, color=color)
rectangle.draw()
@staticmethod
def draw_line(x1, y1, x2, y2):
line = shapes.Line(x1, y1, x2, y2)
line.draw()
class Paddle(Drawing):
def __init__(self, side, sounds, *args):
super().__init__(*args)
self.side = side
self.width = self.window_width // 57
self.height = self.window_height // 8
self.speed = self.scale_factor // 80
self.sound = sounds
if self.side == 'left':
self.x = self.window_width // 10
self.y = self.window_height // 2 - self.height // 2
elif self.side == 'right':
self.x = self.window_width - self.width - self.window_width // 10
self.y = self.window_height // 2 - self.height // 2
def draw(self):
self.draw_rect(self.x, self.y, self.width, self.height)
def check_collisions(self, ball):
# check for collisions of ball with paddle
if (self.x < ball.x < self.x + self.width and
self.y < ball.y < self.y + self.height):
# set fly direction depending on where it hit the paddle
# (t is 0.5 if hit at top, 0 at center, -0.5 at bottom)
t = ((ball.y - self.y) / self.height) - 0.5
if self.side == 'left':
ball.ball_dir_x = abs(ball.ball_dir_x) # force it to be positive
elif self.side == 'right':
ball.ball_dir_x = -abs(ball.ball_dir_x) # force it to be negative
ball.ball_dir_y = t
ball.ball_speed += 0.5
if self.sound: self.sound.plop.play()
def ai_player(self, ball, game_difficulty):
if game_difficulty == 'normal' and random.random() > 0.5:
pass
else:
if ball.ball_dir_x == 1:
if (self.y + self.height / 2) < self.window_height // 2 - self.height / 2:
self.y += self.speed
elif (self.y + self.height / 2) > self.window_height // 2 + self.height / 2:
self.y -= self.speed
elif ball.ball_dir_x == -1:
if (self.y + self.height / 2) < ball.y - self.height / 2:
self.y += self.speed
elif (self.y + self.height / 2) > ball.y + self.height / 2:
self.y -= self.speed
def update(self, ball, game_difficulty=None):
if self.side == 'left':
if not self.computer_player:
if self.keys[key.W]:
self.y += self.speed
if self.keys[key.S]:
self.y -= self.speed
else:
self.ai_player(ball, game_difficulty)
if self.side == 'right':
if self.keys[key.UP]:
self.y += self.speed
if self.keys[key.DOWN]:
self.y -= self.speed
self.check_collisions(ball)
class Ball(Drawing):
def __init__(self, sounds=None, *args):
super().__init__(*args)
self.reset()
self.sound = sounds
def reset(self):
self.x = self.window_width // 2
self.y = self.window_height // 2
self.ball_dir_x = random.choice([-1, 1])
self.ball_dir_y = 0.0
self.ball_size = self.scale_factor // 57
self.ball_speed = self.scale_factor // 80
def draw(self):
self.draw_rect(self.x - self.ball_size / 2,
self.y - self.ball_size / 2,
self.ball_size,
self.ball_size)
def check_collisions(self, scores):
# check for left wall collision
if self.x < 0:
self.x = self.window_width / 2
self.y = self.window_height / 2
self.ball_dir_x = abs(self.ball_dir_x) # force it to be positive
self.ball_dir_y = 0
scores[0]['right'] += 1
scores[2].label.text = str(scores[0]['right'])
if self.sound: self.sound.peep.play()
self.reset()
# check for right wall collision
if self.x > self.window_width:
self.x = self.window_width / 2
self.y = self.window_height / 2
self.ball_dir_x = -abs(self.ball_dir_x) # force it to be negative
self.ball_dir_y = 0
scores[0]['left'] += 1
scores[1].label.text = str(scores[0]['left'])
if self.sound: self.sound.peep.play()
self.reset()
# check for top wall collision
if self.y > self.window_height:
self.ball_dir_y = -abs(self.ball_dir_y) # force it to be negative
if self.sound: self.sound.beep.play()
# check for bottom wall collision
if self.y < 0:
self.ball_dir_y = abs(self.ball_dir_y) # force it to be positive
if self.sound: self.sound.beep.play()
def vec2_norm(self, x: int, y: float):
global ball_dir_x, ball_dir_y
# sets a vectors length to 1 (which means that x + y == 1)
length = sqrt((x * x) + (y * y))
if length != 1.0:
length = 1.0 / length
x *= length
y *= length
ball_dir_y = x
ball_dir_y = y
def update(self, scores):
self.x += self.ball_dir_x * self.ball_speed
self.y += self.ball_dir_y * self.ball_speed
self.check_collisions(scores)
self.vec2_norm(self.x, self.y)
class Line(Drawing):
def __init__(self, *args):
super().__init__(*args)
self.x1 = self.window_width // 2
self.y1 = 0.0
self.x2 = self.window_width // 2
self.y2 = self.window_height
self.line_width = 2
def draw(self):
self.draw_line(self.x1, self.y1,
self.x2, self.y2)
class Score():
def __init__(self, side, window_width, window_height, text, batch, *args):
super().__init__(*args)
self.label = pyglet.text.Label()
self.label.font_name = 'Arial'
self.label.anchor_x = 'center'
self.label.anchor_y = 'center'
self.label.batch = batch
self.label.text = text
self.label.font_size = window_width//11
if side == 'left':
self.label.x = window_width // 2 - window_width // 10
self.label.y = window_height - window_height // 10
elif side == 'right':
self.label.x = window_width // 2 + window_width // 10
self.label.y = window_height - window_height // 10
def reset(self):
self.label.text = '0'
class MenuItem():
def __init__(self, window_width, window_height, text, modifier, *args):
super().__init__(*args)
self.label = pyglet.text.Label()
self.label.font_name = 'Arial'
self.label.anchor_x = 'center'
self.label.anchor_y = 'center'
self.label.text = text
self.label.font_size = (window_width + window_height) // 26
self.label.x = window_width // 2
self.label.y = window_height - modifier * window_height // 10
def reset(self):
self.label.text = '0'
def draw(self):
self.label.draw()
class Game(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_location(100, 100)
self.set_caption('Pong')
self.set_mouse_visible(False)
self.frame_rate = 1/60.
self.fps_display = FPSDisplay(self)
self.keys = key.KeyStateHandler()
self.main_batch = pyglet.graphics.Batch()
self.ai = True
self.play_sounds = True
if self.play_sounds:
self.sounds = Sounds()
self.paddle_left = Paddle('left', self.sounds, self.width, self.height, self.keys, self.ai)
self.paddle_right = Paddle('right', self.sounds, self.width, self.height, self.keys)
self.ball = Ball(self.sounds, self.width, self.height, self.keys)
self.line = Line(self.width, self.height, self.keys)
self.score_left = Score('left', self.width, self.height, '0', batch=self.main_batch)
self.score_right = Score('right', self.width, self.height, '0', batch=self.main_batch)
score = {'left': 0, 'right': 0}
self.scores = [score, self.score_left, self.score_right]
self.game_menu = True
self.start_game = MenuItem(self.width, self.height, "Start Game", modifier=2)
self.start_game.bold = True
self.difficulty = MenuItem(self.width, self.height, "Difficulty: ", modifier=4)
self.game_difficulty = 'normal'
self.difficulty.label.text += self.game_difficulty
self.exit_game = MenuItem(self.width, self.height, "Exit Game", modifier=6)
self.menu_items = [self.start_game, self.difficulty, self.exit_game]
self.current_index = 0
self.current_selection = self.menu_items[0].label
def draw_menu(self):
self.start_game.draw()
self.difficulty.draw()
self.exit_game.draw()
def draw_game_objects(self):
self.fps_display.draw()
self.paddle_left.draw()
self.paddle_right.draw()
self.ball.draw()
self.line.draw()
# self.score_left.draw()
# self.score_right.draw()
def on_draw(self):
self.clear()
if self.game_menu:
self.draw_menu()
else:
self.draw_game_objects()
self.main_batch.draw()
def choose_menu_item(self, symbol, modifier):
if symbol == key.DOWN:
self.current_selection.bold = False
self.current_selection.font_size -= 10
self.current_index += 1
if self.current_index > 2:
self.current_index = 0
self.current_selection = self.menu_items[self.current_index].label
self.current_selection.bold = True
self.current_selection.font_size += 10
elif symbol == key.UP:
self.current_selection.bold = False
self.current_selection.font_size -= 10
self.current_index -= 1
if self.current_index < 0:
self.current_index = 2
self.current_selection = self.menu_items[self.current_index].label
self.current_selection.bold = True
self.current_selection.font_size += 10
elif self.current_index == 1 and symbol == key.RIGHT:
self.game_difficulty = 'harder'
self.current_selection.text = 'Difficulty: ' + self.game_difficulty
elif self.current_index == 1 and symbol == key.LEFT:
self.game_difficulty = 'normal'
self.current_selection.text = 'Difficulty: ' + self.game_difficulty
elif symbol == key.ENTER:
if self.current_index == 0:
self.game_menu = False
elif self.current_index == 1:
pass
elif self.current_index == 2:
pyglet.app.exit()
def on_key_press(self, symbol, modifier):
if self.game_menu:
self.choose_menu_item(symbol, modifier)
else:
if self.keys[key.ESCAPE]:
pyglet.app.exit()
self.push_handlers(self.keys)
def update(self, dt):
if self.game_menu:
pass
else:
self.ball.update(self.scores)
self.paddle_left.update(self.ball, self.game_difficulty)
self.paddle_right.update(self.ball)
if __name__ == '__main__':
game = Game(width=800, height=600, resizable=True)
pyglet.clock.schedule_interval(game.update, game.frame_rate)
pyglet.app.run()