generated from StRobertCHSCS/python-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaster.py
456 lines (293 loc) · 11.6 KB
/
Master.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
'''
-------------------------------------------------------------------------------
Name: Retro_Ping_Pong.py
Purpose: To recreate Retro Ping Pong
Author: Tang. E, Chou. I
Created: date in 26/01/2020
------------------------------------------------------------------------------
'''
import arcade
import random
###
SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 720
SCREEN_TITLE = "Retro Ping Pong"
#Score for Player 1
Count_1 = 0
#Score for Player 2
Count_2 = 0
#Allows player 1 to move paddle up
up_pressed = False
#Allows player 1 to move paddle down
down_pressed = False
#Allows player 2 to move paddle up
up_2pressed = False
#Allows player 2 to move paddle down
down_2pressed = False
#When the ball is drawn
new_ball = False
#How fast the ball moves
MOVEMENT_SPEED = 10
#Determines normal pong or action pong
game_mode = 0
class Ball:
"""Movement of the Ball"""
def __init__(self):
self.x = 0
self.y = 0
self.change_x = 0
self.change_y = 0
self.size = 0
self.color = None
class Rectangle1:
"""Movement of the Player 1 Paddle"""
def __init__(self):
self.x = 0
self.y = 0
self.change_x = 0
self.change_y = 0
self.size = 0
self.color = None
class Rectangle2:
"""Movement of the Player 2 Paddle"""
def __init__(self):
self.x = 0
self.y = 0
self.change_x = 0
self.change_y = 0
self.size = 0
self.color = None
class Wall:
def __init__(self):
self.x = 0
self.y = 0
self.size = 0
self.color = None
def make_rectangle1():
"""Creates the Player 1 Paddle"""
rectangle1 = Rectangle1()
rectangle1.size = 14
rectangle1.x = 10
rectangle1.y = 350
rectangle1.change_y = 0
rectangle1.color = arcade.color.WHITE
return rectangle1
def make_rectangle2():
"""Creates the Player 2 Paddle"""
rectangle2 = Rectangle2()
rectangle2.size = 14
rectangle2.x = 1270
rectangle2.y = 350
rectangle2.change_y = 0
rectangle2.color = arcade.color.WHITE
return rectangle2
def make_wall():
"""Creates the Player 2 Paddle"""
wall = Wall()
wall.size = 20
wall.x = 640
wall.y = 360
wall.color = arcade.color.WHITE
return wall
def make_ball():
"""Places the ball in the center and shoots it in a random direction"""
global MOVEMENT_SPEED
global Count_1
global Count_2
global new_ball, game_mode
ball = Ball()
# Size of the ball
ball.size = 14
# Starting position of the ball.
# Take into account the ball size so we don't spawn on the edge.
ball.x = 640
ball.y = 360
# Speed and direction of rectangle
if game_mode == 1:
if Count_1 >= Count_2:
ball.change_x = (MOVEMENT_SPEED*100+1) / 100
ball.change_y = (MOVEMENT_SPEED*100+1) / 100
elif Count_2 > Count_1:
ball.change_x = (-MOVEMENT_SPEED*100+1) / 100
ball.change_y = (-MOVEMENT_SPEED*100+1) / 100
if game_mode == 2:
ball.change_x = (MOVEMENT_SPEED*100+1) / 100
ball.change_y = (MOVEMENT_SPEED*100+1) / 100
# Color
ball.color = arcade.color.WHITE
return ball
class MyGame(arcade.Window):
""" Main application class. """
def Title_Screen():
global game_mode
title = arcade.load_texture("Images/Title Screen.png")
if game_mode == 0:
arcade.draw_texture_rectangle(title.width, title.height, title.width,title.height, title, 0)
def __init__(self):
"""Creates the Background"""
global new_ball
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
self.ball_list = []
ball = make_ball()
self.rectangle1_list = []
rectangle1 = make_rectangle1()
self.rectangle1_list.append(rectangle1)
self.rectangle2_list = []
rectangle2 = make_rectangle2()
self.rectangle2_list.append(rectangle2)
self.wall_list = []
wall = make_wall()
self.wall_list.append(wall)
def on_draw(self):
"""
Render the screen.
"""
global Count_1, Count_2
global game_mode, new_ball
# This command has to happen before we start drawing
arcade.start_render()
title = arcade.load_texture("Images/Title Screen.png")
if game_mode == 0:
arcade.draw_texture_rectangle(title.width//2, title.height//2, title.width,title.height, title, 0)
if game_mode == 1 or game_mode == 2:
texture = arcade.load_texture("Images/background.png")
arcade.draw_texture_rectangle(texture.width//2, texture.height//2, texture.width,texture.height, texture, 0)
for ball in self.ball_list:
arcade.draw_circle_filled(ball.x, ball.y, ball.size, ball.color)
ball = make_ball()
for rectangle1 in self.rectangle1_list:
arcade.draw_rectangle_filled(rectangle1.x, rectangle1.y, rectangle1.size, 150, rectangle1.color)
rectangle1 = make_rectangle1()
for rectangle2 in self.rectangle2_list:
arcade.draw_rectangle_filled(rectangle2.x, rectangle2.y, rectangle2.size, 150, rectangle2.color)
rectangle2 = make_rectangle2()
if game_mode ==2:
for wall in self.wall_list:
arcade.draw_rectangle_filled(640, 360, 20, 200, arcade.color.WHITE)
if game_mode == 3:
end1_screen = arcade.load_texture("Images/Player_1_Victory.png")
arcade.draw_texture_rectangle(end1_screen.width//2, end1_screen.height//2, end1_screen.width,end1_screen.height, end1_screen, 0)
if game_mode == 4:
end2_screen = arcade.load_texture("Images/Player_2_Victory.png")
arcade.draw_texture_rectangle(end2_screen.width//2, end2_screen.height//2, end2_screen.width,end2_screen.height, end2_screen, 0)
if game_mode == 1 or game_mode == 2:
#def make_score_board():
Player_1 = "{}" .format(Count_1)
Player_2 = "{}" .format(Count_2)
arcade.draw_text(Player_1, 590, 660, arcade.color.WHITE, 30)
arcade.draw_text(Player_2, 690, 660, arcade.color.WHITE, 30)
if Count_1 == 7:
game_mode = 3
if Count_2 ==7:
game_mode = 4
def on_mouse_press(self, x, y, button, modifiers):
"""
Called whenever the mouse button is clicked.
"""
if len(self.ball_list) == 0:
ball = make_ball()
self.ball_list.append(ball)
def on_update(self, delta_time):
""" Movement and game logic """
global Count_1, Count_2, MOVEMENT_SPEED, on_draw,up_2pressed, up_pressed, down_pressed, down_2pressed, game_mode, arcade_mode
if game_mode == 1 or game_mode == 2 :
for ball in self.ball_list:
ball.x += ball.change_x
ball.y += ball.change_y
if ball.y < ball.size:
ball.change_y *= -1
if ball.y > SCREEN_HEIGHT - ball.size:
ball.change_y *= -1
if ball.x >= 1300 and Count_1 <= 6:
self.ball_list.pop(0)
ball.x = 640
ball.y = 360
Count_1 += 1
if ball.x <= -20 and Count_2 <= 6:
self.ball_list.pop(0)
ball.x = 640
ball.y = 360
Count_2 += 1
for rectangle2 in self.rectangle2_list:
if ball.x > rectangle2.x-10 and ball.y < rectangle2.y + 77 and ball.y > rectangle2.y - 77:
ball.change_x *= -1
ball.change_y *= -1
ball.change_y -= 1
if ball.x > rectangle2.x-5 and ball.y < rectangle2.y + 80 and ball.y > rectangle2.y - 80:
ball.change_x *= 1
ball.change_y *= 1
for rectangle1 in self.rectangle1_list:
if ball.x < rectangle1.x+10 and ball.y < rectangle1.y + 77 and ball.y > rectangle1.y - 77:
ball.change_x *= -1
ball.change_y *= -1
ball.change_y += 1
if ball.x > rectangle1.x+5 and ball.y < rectangle1.y + 80 and ball.y > rectangle1.y - 80:
ball.change_x *= 1
ball.change_y *= 1
if game_mode == 2:
for ball in self.ball_list:
for wall in self.wall_list:
if ball.x < wall.x+10 and ball.x > wall.x - 10 and ball.y < wall.y + 77 and ball.y > wall.y - 77:
ball.change_x *= -1
ball.change_y *= -1
ball.change_y += 1
if ball.x >= 1300 and Count_1 <= 6:
self.ball_list.pop(0)
ball.x = 640
ball.y = 360
Count_1 += 1
if ball.x <= -20 and Count_2 <= 6:
self.ball_list.pop(0)
ball.x = 640
ball.y = 360
Count_2 += 1
for rectangle2 in self.rectangle2_list:
if up_pressed == True:
rectangle2.y += 10
elif down_pressed == True:
rectangle2.y -= 10
for rectangle1 in self.rectangle1_list:
if down_2pressed == True:
rectangle1.y -= 10
elif up_2pressed == True:
rectangle1.y += 10
def on_key_press(self, key, modifiers):
"""Called whenever a key is pressed"""
global up_pressed, down_pressed, up_2pressed, down_2pressed, game_mode, new_ball
if key == arcade.key.UP:
up_pressed = True
elif key == arcade.key.DOWN:
down_pressed = True
elif key == arcade.key.W:
up_2pressed = True
elif key == arcade.key.S:
down_2pressed = True
elif key == arcade.key.N:
new_ball = True
if game_mode == 0:
if key == arcade.key.G:
game_mode = 1
elif key == arcade.key.H:
game_mode = 2
def on_key_release(self, key, modifiers):
"""Called when the user releases a key"""
global up_pressed, down_pressed, up_2pressed, down_2pressed, new_ball
if key == arcade.key.UP:
up_pressed = False
elif key == arcade.key.DOWN:
down_pressed = False
elif key == arcade.key.W:
up_2pressed = False
elif key == arcade.key.S:
down_2pressed = False
elif key == arcade.key.N:
new_ball = False
if key == arcade.key.G:
game_mode = 1
elif key == arcade.key.H:
game_mode = 2
def main():
MyGame()
arcade.run()
if __name__ == "__main__":
main()