forked from bhaveshlohana/HacktoberFest2020-Contributions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pong.py
239 lines (192 loc) · 7.79 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
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
import pygame
import sys
import random
from math import *
pygame.init()
width = 600
height = 400
display = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pong")
clock = pygame.time.Clock()
background = (27, 38, 49)
white = (236, 240, 241)
top = white
bottom = white
left = white
right = white
margin = 4
scoreLeft = 0
scoreRight = 0
maxScore = 20
font = pygame.font.SysFont("Small Fonts", 30)
largeFont = pygame.font.SysFont("Small Fonts", 60)
# Draw the Boundary of Board
def boundary():
global top, bottom, left, right
pygame.draw.rect(display, left, (0, 0, margin, height))
pygame.draw.rect(display, top, (0, 0, width, margin))
pygame.draw.rect(display, right, (width - margin, 0, margin, height))
pygame.draw.rect(display, bottom, (0, height - margin, width, margin))
# Paddle Class
class Paddle:
def __init__(self, position):
self.w = 10
self.h = self.w * 8
self.paddleSpeed = 6
if position == -1:
self.x = 1.5 * margin
else:
self.x = width - 1.5 * margin - self.w
self.y = height / 2 - self.h / 2
# Show the Paddle
def show(self):
pygame.draw.rect(display, white, (self.x, self.y, self.w, self.h))
# Move the Paddle
def move(self, ydir):
self.y += self.paddleSpeed * ydir
if self.y < 0:
self.y -= self.paddleSpeed * ydir
elif self.y + self.h > height:
self.y -= self.paddleSpeed * ydir
leftPaddle = Paddle(-1)
rightPaddle = Paddle(1)
# Ball Class
class Ball:
def __init__(self, color):
self.r = 20
self.x = width / 2 - self.r / 2
self.y = height / 2 - self.r / 2
self.color = color
self.angle = random.randint(-75, 75)
if random.randint(0, 1):
self.angle += 180
self.speed = 8
# Show the Ball
def show(self):
pygame.draw.ellipse(display, self.color, (self.x, self.y, self.r, self.r))
# Move the Ball
def move(self):
global scoreLeft, scoreRight
self.x += self.speed * cos(radians(self.angle))
self.y += self.speed * sin(radians(self.angle))
if self.x + self.r > width - margin:
scoreLeft += 1
self.angle = 180 - self.angle
if self.x < margin:
scoreRight += 1
self.angle = 180 - self.angle
if self.y < margin:
self.angle = - self.angle
if self.y + self.r >= height - margin:
self.angle = - self.angle
# Check and Reflect the Ball when it hits the paddle
def checkForPaddle(self):
if self.x < width / 2:
if leftPaddle.x < self.x < leftPaddle.x + leftPaddle.w:
if leftPaddle.y < self.y < leftPaddle.y + 10 or leftPaddle.y < self.y + self.r < leftPaddle.y + 10:
self.angle = -45
if leftPaddle.y + 10 < self.y < leftPaddle.y + 20 or leftPaddle.y + 10 < self.y + self.r < leftPaddle.y + 20:
self.angle = -30
if leftPaddle.y + 20 < self.y < leftPaddle.y + 30 or leftPaddle.y + 20 < self.y + self.r < leftPaddle.y + 30:
self.angle = -15
if leftPaddle.y + 30 < self.y < leftPaddle.y + 40 or leftPaddle.y + 30 < self.y + self.r < leftPaddle.y + 40:
self.angle = -10
if leftPaddle.y + 40 < self.y < leftPaddle.y + 50 or leftPaddle.y + 40 < self.y + self.r < leftPaddle.y + 50:
self.angle = 10
if leftPaddle.y + 50 < self.y < leftPaddle.y + 60 or leftPaddle.y + 50 < self.y + self.r < leftPaddle.y + 60:
self.angle = 15
if leftPaddle.y + 60 < self.y < leftPaddle.y + 70 or leftPaddle.y + 60 < self.y + self.r < leftPaddle.y + 70:
self.angle = 30
if leftPaddle.y + 70 < self.y < leftPaddle.y + 80 or leftPaddle.y + 70 < self.y + self.r < leftPaddle.y + 80:
self.angle = 45
else:
if rightPaddle.x + rightPaddle.w > self.x + self.r > rightPaddle.x:
if rightPaddle.y < self.y < leftPaddle.y + 10 or leftPaddle.y < self.y + self.r < leftPaddle.y + 10:
self.angle = -135
if rightPaddle.y + 10 < self.y < rightPaddle.y + 20 or rightPaddle.y + 10 < self.y + self.r < rightPaddle.y + 20:
self.angle = -150
if rightPaddle.y + 20 < self.y < rightPaddle.y + 30 or rightPaddle.y + 20 < self.y + self.r < rightPaddle.y + 30:
self.angle = -165
if rightPaddle.y + 30 < self.y < rightPaddle.y + 40 or rightPaddle.y + 30 < self.y + self.r < rightPaddle.y + 40:
self.angle = 170
if rightPaddle.y + 40 < self.y < rightPaddle.y + 50 or rightPaddle.y + 40 < self.y + self.r < rightPaddle.y + 50:
self.angle = 190
if rightPaddle.y + 50 < self.y < rightPaddle.y + 60 or rightPaddle.y + 50 < self.y + self.r < rightPaddle.y + 60:
self.angle = 165
if rightPaddle.y + 60 < self.y < rightPaddle.y + 70 or rightPaddle.y + 60 < self.y + self.r < rightPaddle.y + 70:
self.angle = 150
if rightPaddle.y + 70 < self.y < rightPaddle.y + 80 or rightPaddle.y + 70 < self.y + self.r < rightPaddle.y + 80:
self.angle = 135
# Show the Score
def showScore():
leftScoreText = font.render("Player 1 : " + str(scoreLeft), True, white)
rightScoreText = font.render("Player 2 : " + str(scoreRight), True, white)
display.blit(leftScoreText, (3 * margin, 3 * margin))
display.blit(rightScoreText, (width / 2 + 3 * margin, 3 * margin))
# Game Over
def gameOver():
if scoreLeft == maxScore or scoreRight == maxScore:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
close()
if event.key == pygame.K_r:
reset()
if scoreLeft == maxScore:
playerWins = largeFont.render("Left Player Wins!", True, red)
elif scoreRight == maxScore:
playerWins = largeFont.render("Right Player Wins!", True, blue)
display.blit(playerWins, (width / 2 - 100, height / 2))
pygame.display.update()
def reset():
global scoreLeft, scoreRight
scoreLeft = 0
scoreRight = 0
board()
def close():
pygame.quit()
sys.exit()
def board():
loop = True
leftChange = 0
rightChange = 0
ball = Ball(white)
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
close()
if event.key == pygame.K_SPACE or event.key == pygame.K_p:
Pause()
if event.key == pygame.K_r:
reset()
if event.key == pygame.K_w:
leftChange = -1
if event.key == pygame.K_s:
leftChange = 1
if event.key == pygame.K_UP:
rightChange = -1
if event.key == pygame.K_DOWN:
rightChange = 1
if event.type == pygame.KEYUP:
leftChange = 0
rightChange = 0
leftPaddle.move(leftChange)
rightPaddle.move(rightChange)
ball.move()
ball.checkForPaddle()
display.fill(background)
showScore()
ball.show()
leftPaddle.show()
rightPaddle.show()
boundary()
gameOver()
pygame.display.update()
clock.tick(60)
board()