-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.py
202 lines (133 loc) · 5.56 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
import pygame
import random
def move_ball():
global b, ball_xspeed, ball_yspeed, player_score,opponent_score , score_timer
b.x += ball_xspeed
b.y += ball_yspeed
if b.top <= 0 or b.bottom >= SCREEN_HEIGHT-10:
ball_yspeed *= - 1
if b.left <= 0:
opponent_score +=1
pygame.mixer.Sound.play(sound)
score_timer = pygame.time.get_ticks()
ball_restart()
if b.right >= SCREEN_WIDTH:
player_score += 1
pygame.mixer.Sound.play(sound)
score_timer = pygame.time.get_ticks()
ball_restart()
if b.colliderect(player) or b.colliderect(opponent):
ball_xspeed *= -1
pygame.mixer.Sound.play(score_sound)
def player_move():
if player.top <= 16:
player.top = 16
if player.bottom >= SCREEN_HEIGHT-20:
player.bottom = SCREEN_HEIGHT-20
def opponent_move():
if opponent.top < b.y:
opponent.top += opponenet_speed
if opponent.bottom > b.y:
opponent.bottom -= opponenet_speed
if opponent.top <= 0:
opponent.top = 0
if opponent.bottom >= SCREEN_HEIGHT:
opponent.bottom -= opponenet_speed
def ball_restart():
global ball_xspeed, ball_yspeed, score_timer
current_time = pygame.time.get_ticks()
b.center = (SCREEN_WIDTH//2, SCREEN_HEIGHT//2)
if (current_time - score_timer) < 2100:
ball_xspeed , ball_yspeed = 0 , 0
else:
ball_yspeed = 7 * random.choice((1,-1))
ball_xspeed = 7 * random.choice((1,-1))
score_timer = 0
pygame.init() # INITIALIZE THE MODULES OF PYGAME
pygame.mixer.pre_init(44100,-16,2,512)
# game color
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
BLUE = ( 0, 0, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
# screen height and width
SCREEN_WIDTH = 700
SCREEN_HEIGHT = 600
FPS = 30 # frame rate per second
# game player and ball variable
ball_xspeed = 7
ball_yspeed = 7
player_speed = 0
opponenet_speed = 6
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT)) # SET SCREEN WIDTH AND HEIGHT
pygame.display.set_caption("Pong Game") # Set the screen caption of our game video
clock = pygame.time.Clock()
b = pygame.draw.rect(screen, (211,211,211),[SCREEN_WIDTH//2,SCREEN_HEIGHT//2,20,20] )
player = pygame.draw.rect(screen, BLUE,[0,SCREEN_HEIGHT//2,20,100] )
opponent = pygame.draw.rect(screen, BLUE,[SCREEN_WIDTH-20,(SCREEN_HEIGHT)//2, 20,100] )
net = pygame.draw.line(screen, WHITE, [SCREEN_WIDTH//2,5], [SCREEN_WIDTH//2, SCREEN_HEIGHT-10], 2)
# game text
player_score = 0
opponent_score = 0
font = pygame.font.Font(r"SourceSansPro-Regular.ttf",30)
game_end_font = pygame.font.Font(r"SourceSansPro-Regular.ttf",20)
# score timer
score_timer = None
# game sound effect
sound = pygame.mixer.Sound("pong.ogg")
score_sound = pygame.mixer.Sound("score.ogg")
game_end = False
while not game_end:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_end = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_w:
player_speed += 7
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
player_speed -= 7
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP or event.key == pygame.K_w:
player_speed -= 7
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
player_speed += 7
if event.key == pygame.K_RETURN:
ball_yspeed = 7 * random.choice((1,-1))
ball_xspeed = 7 * random.choice((1,-1))
player_score = 0
opponent_score = 0
if score_timer:
ball_restart()
player.top += player_speed
screen.fill(BLACK)
pygame.draw.rect(screen, BLUE, player)
pygame.draw.rect(screen, BLUE, opponent)
pygame.draw.rect(screen, WHITE, net )
pygame.draw.ellipse(screen, WHITE, b)
move_ball()
player_move()
opponent_move()
if player_score >= 1 and opponent_score < 11:
player_score_text = font.render(f"Player Won",False,BLUE)
screen.blit(player_score_text, ((SCREEN_WIDTH//3)//2,100))
ball_xspeed , ball_yspeed = 0, 0
player = pygame.draw.rect(screen, BLUE,[0,SCREEN_HEIGHT//2,20,100] )
opponent = pygame.draw.rect(screen, BLUE,[SCREEN_WIDTH-20,(SCREEN_HEIGHT)//2, 20,100] )
# check if user want play again
game_end_message = game_end_font.render(f"Press Enter to play again",False,WHITE)
screen.blit(game_end_message, ((SCREEN_WIDTH//3)//2-30,200))
if opponent_score >= 11 and player_score < 11:
opponent_score_text = font.render(f"Opponent Won",False,RED)
screen.blit(opponent_score_text, ((SCREEN_WIDTH-(SCREEN_WIDTH//2))+((SCREEN_WIDTH)//2)//2-90,100))
ball_xspeed , ball_yspeed = 0, 0
game_end_message = game_end_font.render(f"Press Enter to play again",False,WHITE)
screen.blit(game_end_message, ((SCREEN_WIDTH-(SCREEN_WIDTH//2))+((SCREEN_WIDTH)//2)//2-90,200))
player = pygame.draw.rect(screen, BLUE,[0,SCREEN_HEIGHT//2,20,100] )
opponent = pygame.draw.rect(screen, BLUE,[SCREEN_WIDTH-20,(SCREEN_HEIGHT)//2, 20,100] )
player_score_text = font.render(f"{player_score}",False,BLUE)
screen.blit(player_score_text, ((SCREEN_WIDTH//2)//2,30))
opponent_text = font.render(f"{opponent_score}",False,RED)
screen.blit(opponent_text, ((SCREEN_WIDTH-(SCREEN_WIDTH//2))+((SCREEN_WIDTH)//2)//2,30))
pygame.display.update()
clock.tick(60)