-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtennis.py
More file actions
226 lines (182 loc) · 7.38 KB
/
Copy pathtennis.py
File metadata and controls
226 lines (182 loc) · 7.38 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
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
import pygame
import sys
import random
pygame.init()
WIDTH, HEIGHT = 800, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong vs Bot")
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
PADDLE_WIDTH, PADDLE_HEIGHT = 15, 100
BALL_RADIUS = 10
FONT = pygame.font.SysFont("Arial", 32)
FPS = 60
class Paddle:
def __init__(self, x, y):
self.rect = pygame.Rect(x, y, PADDLE_WIDTH, PADDLE_HEIGHT)
self.speed = 6
def draw(self):
pygame.draw.rect(WIN, WHITE, self.rect)
def move(self, up=True):
if up and self.rect.top > 0:
self.rect.y -= self.speed
elif not up and self.rect.bottom < HEIGHT:
self.rect.y += self.speed
class Ball:
def __init__(self, x, y):
self.rect = pygame.Rect(x, y, BALL_RADIUS * 2, BALL_RADIUS * 2)
self.base_speed = 5
self.speed_multiplier = 1.0
self.x_vel = random.choice([-self.base_speed, self.base_speed])
self.y_vel = random.choice([-3, 3])
def move(self):
self.rect.x += int(self.x_vel * self.speed_multiplier)
self.rect.y += int(self.y_vel * self.speed_multiplier)
if self.rect.top <= 0 or self.rect.bottom >= HEIGHT:
self.y_vel *= -1
def draw(self):
pygame.draw.ellipse(WIN, WHITE, self.rect)
def reset(self):
self.rect.center = (WIDTH // 2, HEIGHT // 2)
self.speed_multiplier = 1.0
self.x_vel = random.choice([-self.base_speed, self.base_speed])
self.y_vel = random.choice([-3, 3])
def bounce(self, paddle):
# Increase ball speed slightly each hit
self.speed_multiplier = min(self.speed_multiplier + 0.1, 2.5)
# Adjust angle depending on where it hits the paddle
hit_pos = (self.rect.centery - paddle.rect.centery) / (PADDLE_HEIGHT // 2)
self.y_vel = hit_pos * 5
self.x_vel *= -1
def bot_ai(bot, ball, difficulty):
if difficulty == "easy":
if random.random() < 0.95: # 5% chance to "ignore"
if bot.rect.centery < ball.rect.centery - 15:
bot.rect.y += 3
elif bot.rect.centery > ball.rect.centery + 15:
bot.rect.y -= 3
elif difficulty == "medium":
if bot.rect.centery < ball.rect.centery - 10:
bot.rect.y += 5
elif bot.rect.centery > ball.rect.centery + 10:
bot.rect.y -= 5
elif difficulty == "hard":
if bot.rect.centery < ball.rect.centery - 5:
bot.rect.y += 7
elif bot.rect.centery > ball.rect.centery + 5:
bot.rect.y -= 7
if bot.rect.top < 0:
bot.rect.top = 0
if bot.rect.bottom > HEIGHT:
bot.rect.bottom = HEIGHT
def main(difficulty):
clock = pygame.time.Clock()
player = Paddle(20, HEIGHT//2 - PADDLE_HEIGHT//2)
bot = Paddle(WIDTH - 40, HEIGHT//2 - PADDLE_HEIGHT//2)
ball = Ball(WIDTH//2, HEIGHT//2)
player_score, bot_score = 0, 0
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
menu()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
menu()
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
player.move(up=True)
if keys[pygame.K_s]:
player.move(up=False)
if pygame.mouse.get_pressed()[0]: # if screen is touched / mouse clicked
mouse_x, mouse_y = pygame.mouse.get_pos()
if mouse_y < HEIGHT // 2:
player.move(up=True)
else:
player.move(up=False)
ball.move()
bot_ai(bot, ball, difficulty)
if ball.rect.colliderect(player.rect):
ball.rect.left = player.rect.right
ball.bounce(player)
if ball.rect.colliderect(bot.rect):
ball.rect.right = bot.rect.left
ball.bounce(bot)
if ball.rect.left <= 0:
bot_score += 1
ball.reset()
elif ball.rect.right >= WIDTH:
player_score += 1
ball.reset()
WIN.fill(BLACK)
player.draw()
bot.draw()
ball.draw()
score_text = FONT.render(f"{player_score} {bot_score}", True, WHITE)
WIN.blit(score_text, (WIDTH//2 - score_text.get_width()//2, 20))
if player_score >= 5 or bot_score >= 5:
winner = "Player" if player_score > bot_score else "Bot"
end_text = FONT.render(f"{winner} Wins! Press R/Touch Top to Restart or Q to Menu", True, WHITE)
WIN.blit(end_text, (WIDTH//2 - end_text.get_width()//2, HEIGHT//2))
pygame.display.flip()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
menu()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
main(difficulty)
if event.key == pygame.K_q:
menu()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
if y < HEIGHT // 2:
main(difficulty)
else:
menu()
pygame.display.flip()
def menu():
clock = pygame.time.Clock()
while True:
clock.tick(FPS)
WIN.fill(BLACK)
title = FONT.render("Pong vs Bot", True, WHITE)
WIN.blit(title, (WIDTH//2 - title.get_width()//2, HEIGHT//4))
easy = FONT.render("1. Easy", True, WHITE)
medium = FONT.render("2. Medium", True, WHITE)
hard = FONT.render("3. Hard", True, WHITE)
quit_text = FONT.render("Q. Quit", True, WHITE)
WIN.blit(easy, (WIDTH//2 - easy.get_width()//2, HEIGHT//2 - 60))
WIN.blit(medium, (WIDTH//2 - medium.get_width()//2, HEIGHT//2 - 20))
WIN.blit(hard, (WIDTH//2 - hard.get_width()//2, HEIGHT//2 + 20))
WIN.blit(quit_text, (WIDTH//2 - quit_text.get_width()//2, HEIGHT//2 + 60))
pygame.display.flip()
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_1:
main("easy")
if event.key == pygame.K_2:
main("medium")
if event.key == pygame.K_3:
main("hard")
if event.key == pygame.K_q:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
if HEIGHT//2 - 70 < y < HEIGHT//2 - 40:
main("easy")
elif HEIGHT//2 - 30 < y < HEIGHT//2:
main("medium")
elif HEIGHT//2 + 10 < y < HEIGHT//2 + 40:
main("hard")
elif HEIGHT//2 + 50 < y < HEIGHT//2 + 80:
pygame.quit()
sys.exit()
if __name__ == "__main__":
menu()