-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong_game.py
188 lines (117 loc) · 3.38 KB
/
pong_game.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
#!/usr/bin/env python
import pygame
import math
from random import randint
import time
pygame.init()
pygame.font.init()
WIN_SIZE = (1600, 900)
win = pygame.display.set_mode(WIN_SIZE)
pygame.display.flip()
tk = pygame.time.Clock()
PLAYER_RECT_1 = pygame.Rect(30, 350, 30, 150)
PLAYER_RECT_2 = pygame.Rect(1540, 350, 30, 150)
BALL_RECT = pygame.Rect(1600/2, 900/2, 30, 30)
winning_side = 0
speed = -10
player_color = (255, 0, 0)
ball_y_offset = 0
ball_y_offset_mulitplier = 0.1
yOffset = 3
ball_speed = 15
def p1():
global PLAYER_RECT_1
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
PLAYER_RECT_1.y += speed
if keys[pygame.K_s]:
PLAYER_RECT_1.y -= speed
if PLAYER_RECT_1.y > 750:
PLAYER_RECT_1.y = 750
elif PLAYER_RECT_1.y < 0:
PLAYER_RECT_1.y = 0
pygame.draw.rect(win, player_color, PLAYER_RECT_1)
def p2():
global PLAYER_RECT_2
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
PLAYER_RECT_2.y += speed
if keys[pygame.K_DOWN]:
PLAYER_RECT_2.y -= speed
if PLAYER_RECT_2.y > 750:
PLAYER_RECT_2.y = 750
elif PLAYER_RECT_2.y < 0:
PLAYER_RECT_2.y = 0
pygame.draw.rect(win, player_color, PLAYER_RECT_2)
direction = randint(1, 3)
if direction == 2:
ball_direction = -1
else:
ball_direction = 1
def ball():
global ball_pos
global ball_direction
global ball_y_offset
global BALL_RECT
global PLAYER_RECT_1
global PLAYER_RECT_2
global winning_side
global is_playing
if BALL_RECT.colliderect(PLAYER_RECT_1):
ball_direction = 1
ball_y_offset = ((PLAYER_RECT_1.y + PLAYER_RECT_1.h/2) - BALL_RECT.y) * ball_y_offset_mulitplier
elif BALL_RECT.colliderect(PLAYER_RECT_2):
ball_direction = -1
ball_y_offset = ((PLAYER_RECT_2.y + PLAYER_RECT_2.h/2) - BALL_RECT.y) * ball_y_offset_mulitplier
if BALL_RECT.y < 0:
ball_y_offset *= -1
elif BALL_RECT.y > 900 - BALL_RECT.h:
ball_y_offset *= -1
BALL_RECT.x += ball_direction * ball_speed
BALL_RECT.y += ball_y_offset
if BALL_RECT.x < 0:
winning_side = 1
is_playing = False
elif BALL_RECT.x > 1680:
winning_side = 2
is_playing = False
pygame.draw.rect(win, (255, 255, 255), BALL_RECT)
is_running = True
is_playing = True
win.fill((0, 0, 255))
p1()
p2()
ball()
pygame.display.update()
time.sleep(3)
while is_running:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
is_running = False
win.fill((0, 0, 255))
if is_playing:
p1()
p2()
ball()
else:
win.fill((255, 255, 0))
myfont = pygame.font.SysFont('FiraMono Bold', 100)
if winning_side == 1:
textsurface = myfont.render('RIGHT SIDE WON!', False, (0, 0, 0))
elif winning_side == 2:
textsurface = myfont.render('LEFT SIDE WON!', False, (0, 0, 0))
win.blit(textsurface,(1680/2,900/2))
pygame.display.update()
BALL_RECT.x = 1600/2
BALL_RECT.y = 900/2
PLAYER_RECT_1.x = 30
PLAYER_RECT_1.y = 350
PLAYER_RECT_2.x = 1540
PLAYER_RECT_2.y = 350
ball_y_offset = 0
time.sleep(2)
winning_side = 0
is_playing = True
pygame.display.update()
tk.tick(60)