0% found this document useful (0 votes)
18 views5 pages

Código - Pong V2

This document is a Python script for a Pong game using the Pygame library. It includes configurations for the game window, ball, paddles, scoring system, and game mechanics such as movement, collision detection, and scoring. The game features a main loop that handles user input, updates the game state, and renders the graphics on the screen.

Uploaded by

natanm39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Código - Pong V2

This document is a Python script for a Pong game using the Pygame library. It includes configurations for the game window, ball, paddles, scoring system, and game mechanics such as movement, collision detection, and scoring. The game features a main loop that handles user input, updates the game state, and renders the graphics on the screen.

Uploaded by

natanm39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

import pygame

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

# Configuração das raquetes


PADDLE_WIDTH, PADDLE_HEIGHT = 10, 100
paddle_speed = 12
left_paddle_pos = [50, HEIGHT // 2 - PADDLE_HEIGHT // 2]
right_paddle_pos = [WIDTH - 50 - PADDLE_WIDTH, HEIGHT // 2 -
PADDLE_HEIGHT // 2]

# 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

# Colisão com o topo e a base da tela


if ball_pos[1] <= BALL_RADIUS or ball_pos[1] >= HEIGHT -
BALL_RADIUS:
ball_speed_y *= -1

# Colisão com o lado direito (ponto para o jogador esquerdo)


if ball_pos[0] >= WIDTH - BALL_RADIUS:
left_score = min(left_score + 2, max_points)
right_score = max(right_score - 1, 0)
reset_ball()

# Colisão com o lado esquerdo (ponto para o jogador direito)


if ball_pos[0] <= BALL_RADIUS:
right_score = min(right_score + 2, max_points)
left_score = max(left_score - 1, 0)
reset_ball()

def check_paddle_collision():
global ball_speed_x, ball_speed_y

# Colisão com a raquete esquerda


if (left_paddle_pos[0] < ball_pos[0] < left_paddle_pos[0] +
PADDLE_WIDTH and
left_paddle_pos[1] < ball_pos[1] < left_paddle_pos[1] +
PADDLE_HEIGHT):
ball_speed_x *= -1
adjust_ball_speed()
adjust_ball_angle(left_paddle_pos)

# Colisão com a raquete direita


if (right_paddle_pos[0] < ball_pos[0] < right_paddle_pos[0] +
PADDLE_WIDTH and
right_paddle_pos[1] < ball_pos[1] < right_paddle_pos[1] +
PADDLE_HEIGHT):
ball_speed_x *= -1
adjust_ball_speed()
adjust_ball_angle(right_paddle_pos)

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

# Se a bola bater no centro da raquete, mantém o ângulo atual


if abs(distance_from_center) < PADDLE_HEIGHT / 4:
ball_speed_y *= 1
# Se bater na parte superior ou inferior da raquete, ajusta o
ângulo
else:
ball_speed_y = distance_from_center * 0.15

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))

left_text = font.render(str(left_score), True, WHITE)


right_text = font.render(str(right_score), True, WHITE)
screen.blit(left_text, (WIDTH // 4, 20))
screen.blit(right_text, (3 * WIDTH // 4, 20))

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

# Loop principal do jogo


clock = pygame.time.Clock()
show_start_screen()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p: # Sistema de pause
game_paused = not game_paused

if not game_paused:
move_ball()
check_paddle_collision()
move_paddles()
draw_screen()

# Verifica se algum jogador venceu


winner = check_winner()
if winner:
screen.fill(BLACK)
text = font.render(winner, True, WHITE)
screen.blit(text, (WIDTH // 2 - text.get_width() // 2,
HEIGHT // 2 - text.get_height() // 2))
pygame.display.flip()
pygame.time.delay(3000)
running = False

clock.tick(FPS)

pygame.quit()
sys.exit()

You might also like