-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpingpong2.py
More file actions
150 lines (125 loc) · 5.13 KB
/
Copy pathpingpong2.py
File metadata and controls
150 lines (125 loc) · 5.13 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
import pygame
pygame.init()
WIDTH, HEIGHT = 700,500 #set width and height for window dynamically
WIN = pygame.display.set_mode((WIDTH,HEIGHT)) #set up window
pygame.display.set_caption("Ping Pong") #Diplays title of the window
FPS = 60 #Defining Frame rate
WHITE = (255,255,255)
BLACK = (0,0,0)
PADDLE_HEIGHT,PADDLE_WIDTH = 100,20
BALL_RADIUS = 7
SCORE_FONT = pygame.font.SysFont("comicsans", 50)
class Paddle: #defining the paddle
COLOR = WHITE
VEL = 4
def __init__(self,x,y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
def draw(self, win):
pygame.draw.rect(win, self.COLOR, (self.x, self.y, self.width, self.height))
def move(self, up = True):
if up:
self.y -= self.VEL
else:
self.y += self.VEL
class Ball:
MAX_VEL = 5
COLOR = WHITE
def __init__(self, x,y, radius):
self.x = self.original_x = x
self.y = self.original_y = y
self.radius = radius
self.x_vel = self.MAX_VEL
self.y_vel = 0
# this is how we draw the ball
def draw(self, win):
pygame.draw.circle(win, self.COLOR, (self.x,self.y),self.radius)
#this is how we move the ball
def move(self):
self.x += self.x_vel
self.y += self.y_vel
def reset(self):
self.x = self.original_x
self.y = self.original_y
self.y_vel = 0
self.x_vel *= -1
def draw(win, paddles, ball, left_score, right_score):
win.fill(BLACK)
left_score_text = SCORE_FONT.render(f"{left_score}", 1, WHITE)
right_score_text = SCORE_FONT.render(f"{right_score}", 1, WHITE)
win.blit(left_score_text, (WIDTH//4 - left_score_text.get_width()//2,20)) #score to be drown in the first quater
win.blit(right_score_text, (WIDTH*(3/4) - right_score_text.get_width() // 2, 20)) #score to be drown in the third quater
for paddle in paddles:
paddle.draw(win)
#Drawing a dotted line at the centre of the screen
for i in range(10, HEIGHT, HEIGHT//20):
if i%2 == 1:
continue
pygame.draw.rect(win, WHITE, (WIDTH//2 - 5, i, 10, HEIGHT//20))
ball.draw(win)
pygame.display.update()
def handle_collision(ball, left_paddle,right_paddle):
#Handles collision with the ceiling
if ball.y + ball.radius >= HEIGHT:
ball.y_vel *= -1 #reverses the direction of the ball
elif ball.y - ball.radius <=0:
ball.y_vel *= -1
#check which paddle is being hit
if ball.x_vel <0:
if ball.y >= left_paddle.y and ball.y <= left_paddle.y + left_paddle.height:
if ball.x - ball.radius <= left_paddle.x + left_paddle.width:
ball.x_vel *= -1
middle_y = left_paddle.y + left_paddle.height / 2
difference_in_y = middle_y - ball.y
reduction_factor = (left_paddle.height/2) / ball.MAX_VEL
y_vel = difference_in_y /reduction_factor
ball.y_vel = -1* y_vel
else:
if ball.y >= right_paddle.y and ball.y <= right_paddle.y + right_paddle.height:
if ball.x + ball.radius >= right_paddle.x:
ball.x_vel *= -1
middle_y = right_paddle.y + right_paddle.height / 2
difference_in_y = middle_y - ball.y
reduction_factor = (right_paddle.height/2) / ball.MAX_VEL
y_vel = difference_in_y /reduction_factor
ball.y_vel = -1* y_vel
def handle_paddle_movement(keys, left_paddle, right_paddle):
if keys[pygame.K_w] and left_paddle.y - left_paddle.VEL >=0:
left_paddle.move(up=True)
if keys[pygame.K_s] and left_paddle.y + left_paddle.VEL + left_paddle.height <= HEIGHT:
left_paddle.move(up=False)
if keys[pygame.K_UP] and right_paddle.y - right_paddle.VEL >=0:
right_paddle.move(up=True)
if keys[pygame.K_DOWN] and right_paddle.y + right_paddle.VEL + right_paddle.height <= HEIGHT:
right_paddle.move(up=False)
def main(): #main loop of the program
run = True
clock = pygame.time.Clock()
left_paddle = Paddle(10, HEIGHT//2 - PADDLE_HEIGHT//2, PADDLE_WIDTH, PADDLE_HEIGHT)
right_paddle = Paddle(WIDTH - 10 - PADDLE_WIDTH, HEIGHT//2 - PADDLE_HEIGHT//2, PADDLE_WIDTH, PADDLE_HEIGHT)
ball = Ball(WIDTH//2, HEIGHT//2, BALL_RADIUS)
left_score = 0
right_score = 0
while run:
clock.tick(FPS) #makes sure the game can't run faster than 60FPS
draw(WIN, [left_paddle, right_paddle], ball, left_score,right_score)
for event in pygame.event.get():
if event.type == pygame.QUIT: #event to quit the window
run = False
break
keys = pygame.key.get_pressed()
handle_paddle_movement(keys, left_paddle, right_paddle)
ball.move()#moves the ball
handle_collision(ball, left_paddle, right_paddle)
#logic to calculate the score
if ball.x<0:
right_score += 1
ball.reset()
elif ball.x>WIDTH:
left_score += 1
ball.reset()
pygame.quit()
if __name__ == '__main__': #this ensures that the main module is being run for the main function to run
main()