-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
224 lines (201 loc) · 7.82 KB
/
main.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
from typing import Any
import pygame
import sys
import random
pygame.init()
pygame.display.set_caption('Pong')
display_w, display_h = 800, 400
display = pygame.display.set_mode((display_w, display_h))
clock = pygame.time.Clock()
#Sounds
pygame.mixer.init()
bounce_sound = pygame.mixer.Sound('sounds/bounce-8bit.wav')
bounce_sound.set_volume(0.1)
win_sound = pygame.mixer.Sound('sounds/win.ogg')
win_sound.set_volume(0.1)
class Player(pygame.sprite.Sprite):
def __init__(self, is_player) -> None:
super().__init__()
self.y = 10
self.score = 0
if is_player:
self.x = 5
self.paddle = pygame.Rect(self.x, self.y, 10, 70)
self.player = True
else:
self.x = display_w - 15
self.paddle = pygame.Rect(self.x, self.y, 10, 70)
self.player = False
def draw_paddle(self):
pygame.draw.rect(display, 'WHITE', self.paddle)
def player_input(self):
keys = pygame.key.get_pressed()
if self.player:
if keys[pygame.K_w]:
self.paddle.y -= 10
if keys[pygame.K_s]:
self.paddle.y += 10
else:
if keys[pygame.K_UP]:
self.paddle.y -= 10
if keys[pygame.K_DOWN]:
self.paddle.y += 10
def follow_ball(self):
delay_timer = pygame.time.get_ticks()
ball_y = ball.y
if delay_timer > 1000:
if ball_y <= self.paddle.y:
self.paddle.y -= 5
else:
self.paddle.y += 5
def check_collision(self):
if self.paddle.top <= 10:
self.paddle.top = 10
elif self.paddle.bottom >= display_h - 10:
self.paddle.bottom = display_h - 10
def update(self) -> None:
self.draw_paddle()
self.check_collision()
if self.player:
self.player_input()
elif not self.player and ball.round_active:
self.follow_ball()
class Ball(pygame.sprite.Sprite):
def __init__(self) -> None:
super().__init__()
self.x = display_w/2
self.y = display_h/2
self.x_movement = 8
self.y_movement = random.randint(-10, 10)
self.radius = 10
self.round_active = True
self.object = pygame.draw.circle(display, (66, 214, 255), (self.x, self.y), self.radius)
def draw_ball(self):
self.object = pygame.draw.circle(display, (66, 214, 255), (self.x, self.y), self.radius)
def movement(self):
self.x += self.x_movement
self.y += self.y_movement
def check_collision(self):
if self.object.colliderect(player.paddle):
pygame.mixer.Channel(1).play(bounce_sound)
self.x_movement = -self.x_movement
if self.object.colliderect(opp.paddle):
pygame.mixer.Channel(1).play(bounce_sound)
self.x_movement = -self.x_movement
if self.object.y <= 10:
pygame.mixer.Channel(1).play(bounce_sound)
self.y_movement = -self.y_movement
if self.object.y >= display_h - 20:
pygame.mixer.Channel(1).play(bounce_sound)
self.y_movement = -self.y_movement
def reset(self):
self.x = display_w/2
self.y = display_h/2
self.x_movement = 0
self.y_movement = 0
self.round_active = False
def restart_movement(self):
self.y_movement = random.randint(-10, 10)
self.x_movement = 8
self.round_active = True
def check_win(self):
if self.object.x > display_w + self.radius:
player.score += 1
pygame.mixer.Channel(2).play(win_sound)
self.reset()
if self.object.x < 0 - self.radius:
opp.score += 1
pygame.mixer.Channel(2).play(win_sound)
self.reset()
def update(self) -> None:
self.draw_ball()
self.check_collision()
self.check_win()
self.movement()
def draw_bg():
offwhite = (207, 59, 29)
border_top = pygame.draw.line(display, offwhite, (0,0), (display_w,0), 15)
border_bottom = pygame.draw.line(display, offwhite, (0, display_h), (display_w, display_h), 15)
divider = pygame.draw.rect(display, offwhite, (display_w/2, 0, 10, display_h))
def render_menu():
title_font = pygame.font.Font('fonts/PixelifySans-VariableFont_wght.ttf', 48)
instr_font = pygame.font.Font('fonts/PixelifySans-VariableFont_wght.ttf', 24)
bg = pygame.Surface.fill(display, "BLACK")
menu_title = title_font.render("PONG", False, "White")
menu_title_rect = menu_title.get_rect(center = (display_w/2, 150))
instr_text = instr_font.render("PRESS SPACE TO START", False, ("White"))
instr_rect = instr_text.get_rect(center = (display_w/2, display_h - 150))
display.blit(menu_title, menu_title_rect)
display.blit(instr_text, instr_rect)
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
return True
def score_board():
score_font = pygame.font.Font('fonts/PixelifySans-VariableFont_wght.ttf', 24)
player_score_text = score_font.render(f"Player: {player.score}", False, "WHITE")
player_score_rect = player_score_text.get_rect(center = (display_w/4, 350))
cpu_score_text = score_font.render(f"CPU: {opp.score}", False, "WHITE")
cpu_score_rect = cpu_score_text.get_rect(center = (display_w * 3/4, 350))
display.blit(player_score_text, player_score_rect)
display.blit(cpu_score_text, cpu_score_rect)
def reset_prompt():
prompt_font = pygame.font.Font('fonts/PixelifySans-VariableFont_wght.ttf', 20)
prompt_text = prompt_font.render(f"Press SPACE to start round", False, "WHITE")
prompt_rect = prompt_text.get_rect(center = (display_w/2, display_h/2))
pygame.draw.rect(display, "BLACK", prompt_rect)
prompt_text2 = prompt_font.render(f"First to 5 wins!", False, "WHITE")
prompt_rect2 = prompt_text2.get_rect(center = (display_w/2 , display_h/2 + 50))
display.blit(prompt_text, prompt_rect)
if player.score < 1 and opp.score < 1:
pygame.draw.rect(display, "BLACK", prompt_rect2)
display.blit(prompt_text2, prompt_rect2)
def win_screen(winning_player:Player):
pygame.Surface.fill(display, 'BLACK')
prompt_font = pygame.font.Font('fonts/PixelifySans-VariableFont_wght.ttf', 24)
winner = winning_player
if winning_player.player:
player_type = "Player"
else:
player_type = "Computer"
prompt_text = prompt_font.render(f"{player_type} wins!", False, "WHITE")
prompt_rect = prompt_text.get_rect(center = (display_w/2, display_h/2))
pygame.draw.rect(display, "BLACK", prompt_rect)
prompt_text2 = prompt_font.render(f"Press ESC to start a new game!", False, "WHITE")
prompt_rect2 = prompt_text2.get_rect(center = (display_w/2, display_h/2 + 50))
pygame.draw.rect(display, "BLACK", prompt_rect2)
display.blit(prompt_text, prompt_rect)
display.blit(prompt_text2, prompt_rect2)
player = Player(True)
opp = Player(False)
ball = Ball()
game_active = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
game_active = False
player.score = 0
opp.score = 0
if event.key == pygame.K_SPACE and not ball.round_active:
ball.restart_movement()
if game_active == True:
display.fill((0,0,0))
draw_bg()
player.update()
opp.update()
ball.update()
score_board()
if not ball.round_active:
reset_prompt()
if player.score == 5:
win_screen(player)
if opp.score == 5:
win_screen(opp)
else:
game_active = render_menu()
ball.reset()
pygame.display.update()
clock.tick(60)