-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpong.py
146 lines (119 loc) · 4.34 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
import pygame
import random
import math
pygame.init()
#setting clock and speed
clock = pygame.time.Clock()
speed = 30
#setting the dimension of game window
display_width = 500
display_height = 300
x = 100
y = 100
radius = 10
dx=3
dy=3
paddle_x = 10
paddle_y = 10
paddle_width = 3
paddle_height = 40
play_score = 0
def randomize_start():
global x,y,dy
x=random.randint(int(display_width/4),display_width-20)
y=random.randint(10,display_height-10)
if random.randint(0,2) % 2 == 0:
dy *= -1
#checking condition for game over
def game_over():
endgame = True
display.fill((0,0,0))
font_title = pygame.font.Font(None,36)
font_instructions = pygame.font.Font(None , 24)
global play_score
end_score = "Your final score :" + str(play_score)
scorebox = font_instructions.render(end_score, True, (255,255,255))
score_rect = scorebox.get_rect(center = (int(display_width/2),int(paddle_height/2.1)))
display.blit(scorebox,score_rect)
announcement = font_title.render("Game Over", True , (255,255,255))
announcement_rect = announcement.get_rect(center = (int(display_width/2),int(display_height/2)))
display.blit(announcement,announcement_rect)
qinstruction = font_instructions.render("Press q to quit", True , (255,255,255))
qinstruction_rect = qinstruction.get_rect(center = (int(display_width/2),int(display_height/1.5)))
display.blit(qinstruction,qinstruction_rect)
rinstruction = font_instructions.render("Press r to play again" , True , (255,255,255))
rinstruction_rect = rinstruction.get_rect(center = (int(display_width/2),int(display_height/1.3)))
display.blit(rinstruction,rinstruction_rect)
pygame.display.flip()
while(endgame):
for event in pygame.event.get():
if event.type == pygame.quit:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
exit()
if event.key == pygame.K_r:
endgame = False
def hit_back():
if x + radius > display_width:
return True
return False
def hit_sides():
if y - radius < 0:
return True
if y + radius > display_height:
return True
return False
def hit_paddle():
global play_score
if x - radius <= paddle_x + paddle_width and y > paddle_y and y < paddle_y + paddle_height:
play_score += 100
return True
return False
display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Let's Pong")
welcome_screen = pygame.font.Font(None,30)
welcome = welcome_screen.render("Let's play pong",True , (255,255,255))
welcome_rect = welcome.get_rect(center = (int(display_width/2),int(display_height/2)))
display.blit(welcome,welcome_rect)
startmsg = welcome_screen.render("Hit 'y' to start or autostart in 10 seconds", True ,(255,255,255))
startmsg_rect = startmsg.get_rect(center = (int(display_width/2),int(display_height/4)))
display.blit(startmsg,startmsg_rect)
pygame.display.flip()
pygame.time.set_timer(pygame.USEREVENT,10000) #sets timer
timer_active = True
while(timer_active):
for event in pygame.event.get():
if event.type == pygame.USEREVENT:
timer_active = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y:
timer_active = False
randomize_start()
while True:
clock.tick(speed)
pressed_key = pygame.key.get_pressed()
if pressed_key[pygame.K_DOWN] or pressed_key[pygame.K_s]:
if paddle_y + paddle_height + 10 <= display_height:
paddle_y += 10
if pressed_key[pygame.K_UP] or pressed_key[pygame.K_w]:
if paddle_y - 10 >= 0:
paddle_y -= 10
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.QUIT()
display.fill((0,0,0))
x+=dx
y+=dy
pygame.draw.rect(display , (255,255,255) , (paddle_x,paddle_y,paddle_width,paddle_height)) #draws rectangle
pygame.draw.circle(display, (255,255,255), (x,y) , radius) #draws circle
# print (x)
if x < radius: #checking whether x is less than radius
game_over() #calling game over
play_score = 0
randomize_start() #calling randomize start
if hit_paddle() or hit_back():
dx *= -1
if hit_sides():
dy *= -1
pygame.display.update()