-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
151 lines (134 loc) · 4.58 KB
/
main.py
File metadata and controls
151 lines (134 loc) · 4.58 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
import pygame
import sys
import random
# Functions
def player_ani():
global player_speed
player.x += player_speed
if player.right >= screen_width:
player.right = screen_width
if player.left <= 0:
player.left = 0
def ball_ani():
global ball_speed_x, ball_speed_y, player, score, game_over
ball.x += ball_speed_x
ball.y += ball_speed_y
if ball.right >= screen_width:
pygame.mixer.Sound.play(pong_sound)
ball_speed_x *= -1
if ball.left <= 0:
pygame.mixer.Sound.play(pong_sound)
ball_speed_x *= -1
if ball.top <= 0:
pygame.mixer.Sound.play(pong_sound)
ball_speed_y *= -1
if ball.colliderect(player):
pygame.mixer.Sound.play(pong_sound)
if ball_speed_x > 0:
if abs(ball.right - player.left) < 10:
ball_speed_x *= -1
score += 1
elif abs(ball.left - player.right) < 10:
ball_speed_x *= -1
score += 1
if ball_speed_y > 0:
if abs(ball.bottom - player.top) < 10:
ball_speed_y *= -1
score += 1
elif abs(ball.top - player.bottom) < 10:
ball_speed_y *= -1
score += 1
if ball.bottom >= screen_height:
pygame.mixer.Sound.play(game_over_sound)
game_over = True
# General setup
pygame.mixer.pre_init(44100, -16, 2, 512)
pygame.init()
clock = pygame.time.Clock()
# Main window
screen_width = 480
screen_height = 650
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Ping Pong')
# Recs
player = pygame.Rect(screen_width / 2 - 40, screen_height - 30, 80, 20)
ball = pygame.Rect(screen_width / 2 - 7.5, 200, 25, 25)
player_speed = 0
ball_speed_x = 5 * random.choice((1, -1))
ball_speed_y = 5
# Colors
bg_color = pygame.Color('yellow')
sc_color = pygame.Color('black')
# Text
score = 0
high_score = 0
font = pygame.font.Font('freesansbold.ttf', 30)
font2 = pygame.font.Font('freesansbold.ttf', 15)
# Timer
game_over = False
menu = True
# Sounds
pong_sound = pygame.mixer.Sound('pong.ogg')
game_over_sound = pygame.mixer.Sound('gameover.ogg')
# Main loop
while True:
# Input
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_SPACE and menu:
menu = False
if event.key == pygame.K_SPACE and game_over:
game_over = False
score = 0
ball.center = (screen_width / 2, screen_height / 2)
if event.key == pygame.K_RIGHT:
player_speed += 6
if event.key == pygame.K_LEFT:
player_speed -= 6
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
player_speed -= 6
if event.key == pygame.K_LEFT:
player_speed += 6
# Menu screen
if menu:
screen.fill(sc_color)
text = font.render('Ping Pong', False, pygame.Color('white'))
screen.blit(text, (screen_width / 2 - 80, screen_height / 2))
text_2 = font2.render('press space to start', False, pygame.Color('white'))
screen.blit(text_2, (screen_width / 2 - 80, screen_height / 2 + 40))
pygame.display.flip()
continue
# Functions
player_ani()
ball_ani()
# Visuals
screen.fill(bg_color)
pygame.draw.rect(screen, (255, 0, 0), player)
pygame.draw.ellipse(screen, (255, 0, 0), ball)
# Text
score_text = font.render(f'{score}', False, sc_color)
screen.blit(score_text, (screen_width / 2 - 10, screen_height / 2))
# Game over screen
if game_over:
screen.fill(sc_color)
text = font.render('Game Over!', False, pygame.Color('white'))
screen.blit(text, (screen_width / 2 - 80, screen_height / 2))
text_2 = font.render(f'Score: {score}', False, pygame.Color('white'))
screen.blit(text_2, (screen_width / 2 - 50, screen_height / 2 + 40))
text_3 = font.render(f'High Score: {high_score}', False, pygame.Color('white'))
screen.blit(text_3, (screen_width / 2 - 80, screen_height / 2 + 80))
text_4 = font2.render('press space to try again', False, pygame.Color('white'))
screen.blit(text_4, (screen_width / 2 - 80, screen_height / 2 + 120))
if score > high_score:
high_score = score
pygame.display.flip()
continue
# Update high score
if score > high_score:
high_score = score
pygame.display.flip()
clock.tick(80)