Código - Pong V2
Código - Pong V2
import sys
# Configurações iniciais
pygame.init()
WIDTH, HEIGHT = 1100, 500
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
LIGHT_BLUE = (173, 216, 230)
DARK_RED = (139, 0, 0)
FPS = 60
# Configuração da bola
BALL_RADIUS = 15
ball_speed_x = 5
ball_speed_y = 5
ball_pos = [WIDTH // 2, HEIGHT // 2]
max_ball_speed = 25 # Velocidade máxima da bola
# Pontuação e velocidade
left_score = 0
right_score = 0
max_points = 15
game_paused = False
# Tela
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong Game V2")
# Fontes
font = pygame.font.Font(None, 74)
font_small = pygame.font.Font(None, 36)
# Funções do jogo
def reset_ball():
global ball_pos, ball_speed_x, ball_speed_y
ball_pos = [WIDTH // 2, HEIGHT // 2]
ball_speed_x = 5 if ball_speed_x > 0 else -5
ball_speed_y = 5
def move_ball():
global ball_pos, ball_speed_x, ball_speed_y, left_score,
right_score
ball_pos[0] += ball_speed_x
ball_pos[1] += ball_speed_y
def check_paddle_collision():
global ball_speed_x, ball_speed_y
def adjust_ball_speed():
global ball_speed_x, ball_speed_y
# Aumenta a velocidade da bola a cada colisão com uma raquete, até
o máximo de 25
if abs(ball_speed_x) < max_ball_speed:
ball_speed_x += 2 if ball_speed_x > 0 else -2
ball_speed_y += 2 if ball_speed_y > 0 else -2
def adjust_ball_angle(paddle_pos):
global ball_speed_y
paddle_center = paddle_pos[1] + PADDLE_HEIGHT / 2
distance_from_center = ball_pos[1] - paddle_center
def move_paddles():
keys = pygame.key.get_pressed()
# Movimento da raquete esquerda
if keys[pygame.K_w] and left_paddle_pos[1] > 0:
left_paddle_pos[1] -= paddle_speed
if keys[pygame.K_s] and left_paddle_pos[1] < HEIGHT -
PADDLE_HEIGHT:
left_paddle_pos[1] += paddle_speed
# Movimento da raquete direita
if keys[pygame.K_UP] and right_paddle_pos[1] > 0:
right_paddle_pos[1] -= paddle_speed
if keys[pygame.K_DOWN] and right_paddle_pos[1] < HEIGHT -
PADDLE_HEIGHT:
right_paddle_pos[1] += paddle_speed
def draw_screen():
screen.fill(BLACK)
pygame.draw.circle(screen, WHITE, ball_pos, BALL_RADIUS)
pygame.draw.rect(screen, LIGHT_BLUE, (*left_paddle_pos,
PADDLE_WIDTH, PADDLE_HEIGHT))
pygame.draw.rect(screen, DARK_RED, (*right_paddle_pos,
PADDLE_WIDTH, PADDLE_HEIGHT))
pygame.display.flip()
def check_winner():
if left_score >= max_points:
return "Left Player Wins!"
elif right_score >= max_points:
return "Right Player Wins!"
return None
def show_start_screen():
screen.fill(BLACK)
title_text = font.render("PONG V2", True, WHITE)
prompt_text = font_small.render("Press any key to start", True,
WHITE)
screen.blit(title_text, (WIDTH // 2 - title_text.get_width() // 2,
HEIGHT // 2 - title_text.get_height() // 2))
screen.blit(prompt_text, (WIDTH // 2 - prompt_text.get_width() //
2, HEIGHT // 2 + title_text.get_height()))
pygame.display.flip()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
waiting = False
if not game_paused:
move_ball()
check_paddle_collision()
move_paddles()
draw_screen()
clock.tick(FPS)
pygame.quit()
sys.exit()