forked from PeaceTheeCoder/PongAI-Master
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpong_play.py
68 lines (47 loc) · 1.82 KB
/
pong_play.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
import pygame
from pong import *
def show_end_game_message(window, winner):
font = pygame.font.SysFont(None, 60)
text = font.render(f"Game ended! {winner.get_name()} won!", True, WHITE)
text_rect = text.get_rect(center=(WIDTH//2, HEIGHT//2))
window.blit(text, text_rect)
pygame.display.update()
pygame.time.delay(5000)
def end_game(win, left_player, right_player):
total_chances = 5 #Just have 5 chances per player
if right_player.get_score() >= total_chances or left_player.get_score() >= total_chances:
if right_player.get_score() >= total_chances:
show_end_game_message(win, right_player)
return True
if left_player.get_score() >= total_chances:
show_end_game_message(win, left_player)
return True
return False
if __name__ =="__main__":
pygame.init()
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("PongAI Master")
run = True
clock = pygame.time.Clock()
gm = Game(WINDOW, WIDTH, HEIGHT)
while run:
clock.tick(FRAMES_PER_SECOND)
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
gm.move_paddle(left=True ,up = True)
if keys[pygame.K_s]:
gm.move_paddle(left=True ,up = False)
if keys[pygame.K_UP]:
gm.move_paddle(left=False ,up = True)
if keys[pygame.K_DOWN]:
gm.move_paddle(left=False ,up = False)
gm.draw()
info = gm.loop()
pygame.display.update()
for event in pygame.event.get():
#checking if the user pressed the red button to close the game
if event.type == pygame.QUIT:
run = False
if end_game(WINDOW ,info.left_player, info.right_player):
gm.reset()
pygame.quit()