-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
127 lines (96 loc) · 5.98 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
import pygame, sys, random
def reset_ball():
global ball_speed_x, ball_speed_y
ball.x = screen_width/2 - 10
ball.y = random.randint(10,100)
ball_speed_x *= random.choice([-1,1])
ball_speed_y *= random.choice([-1,1])
def point_won(winner):
global cpu_points, player_points
if winner == "cpu":
cpu_points += 1
if winner == "player":
player_points += 1
reset_ball()
def animate_ball():
global ball_speed_x, ball_speed_y
ball.x += ball_speed_x
ball.y += ball_speed_y
if ball.bottom >= screen_height or ball.top <= 0:
ball_speed_y *= -1
if ball.right >= screen_width:
point_won("cpu")
if ball.left <= 0:
point_won("player")
if ball.colliderect(player) or ball.colliderect(cpu):
ball_speed_x *= -1
def animate_player():
player.y += player_speed
if player.top <= 0:
player.top = 0
if player.bottom >= screen_height:
player.bottom = screen_height
def animate_cpu():
global cpu_speed
cpu.y += cpu_speed
if ball.centery <= cpu.centery:
cpu_speed = -6
if ball.centery >= cpu.centery:
cpu_speed = 6
if cpu.top <= 0:
cpu.top = 0
if cpu.bottom >= screen_height:
cpu.bottom = screen_height
pygame.init()
screen_width = 1280
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("My Pong Game!")
clock = pygame.time.Clock()
ball = pygame.Rect(0,0,30,30)
ball.center = (screen_width/2, screen_height/2)
cpu = pygame.Rect(0,0,20,100)
cpu.centery = screen_height/2
player = pygame.Rect(0,0,20,100)
player.midright = (screen_width, screen_height/2)
ball_speed_x = 6
ball_speed_y = 6
player_speed = 0
cpu_speed = 6
cpu_points, player_points = 0, 0
score_font = pygame.font.Font(None, 100)
while True:
#Check for events
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_UP:
player_speed = -6
if event.key == pygame.K_DOWN:
player_speed = 6
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
player_speed = 0
if event.key == pygame.K_DOWN:
player_speed = 0
#Change the positions of the game objects
animate_ball()
animate_player()
animate_cpu()
#Clear the screen
screen.fill('black')
#Draw the score
cpu_score_surface = score_font.render(str(cpu_points), True, "white")
player_score_surface = score_font.render(str(player_points), True, "white")
screen.blit(cpu_score_surface,(screen_width/4,20))
screen.blit(player_score_surface,(3*screen_width/4,20))
#Draw the game objects
pygame.draw.aaline(screen,'white',(screen_width/2, 0), (screen_width/2, screen_height))
pygame.draw.ellipse(screen,'white',ball)
pygame.draw.rect(screen,'white',cpu)
pygame.draw.rect(screen,'white',player)
#Update the display
pygame.display.update()
clock.tick(60)