0% found this document useful (0 votes)
14 views2 pages

Pong

Uploaded by

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

Pong

Uploaded by

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

import pygame

import sys

# Initialize pygame
pygame.init()

# Set up the display


WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong")

# Set up the colors


WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Set up the clock


clock = pygame.time.Clock()

# Paddle settings
PADDLE_WIDTH, PADDLE_HEIGHT = 15, 100
PADDLE_SPEED = 10

# Ball settings
BALL_SIZE = 20
BALL_SPEED_X, BALL_SPEED_Y = 5, 5

# Create paddles and ball


paddle_a = pygame.Rect(30, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH,
PADDLE_HEIGHT)
paddle_b = pygame.Rect(WIDTH - 30 - PADDLE_WIDTH, HEIGHT // 2 - PADDLE_HEIGHT // 2,
PADDLE_WIDTH, PADDLE_HEIGHT)
ball = pygame.Rect(WIDTH // 2 - BALL_SIZE // 2, HEIGHT // 2 - BALL_SIZE // 2,
BALL_SIZE, BALL_SIZE)

# Initialize scores
score_a, score_b = 0, 0

# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

# Get key states


keys = pygame.key.get_pressed()
if keys[pygame.K_w] and paddle_a.top > 0:
paddle_a.y -= PADDLE_SPEED
if keys[pygame.K_s] and paddle_a.bottom < HEIGHT:
paddle_a.y += PADDLE_SPEED
if keys[pygame.K_UP] and paddle_b.top > 0:
paddle_b.y -= PADDLE_SPEED
if keys[pygame.K_DOWN] and paddle_b.bottom < HEIGHT:
paddle_b.y += PADDLE_SPEED

# Move the ball


ball.x += BALL_SPEED_X
ball.y += BALL_SPEED_Y
# Ball collision with top and bottom
if ball.top <= 0 or ball.bottom >= HEIGHT:
BALL_SPEED_Y = -BALL_SPEED_Y

# Ball collision with paddles


if ball.colliderect(paddle_a) or ball.colliderect(paddle_b):
BALL_SPEED_X = -BALL_SPEED_X

# Ball out of bounds (scoring)


if ball.left <= 0:
score_b += 1
ball.x = WIDTH // 2 - BALL_SIZE // 2
ball.y = HEIGHT // 2 - BALL_SIZE // 2
BALL_SPEED_X = -BALL_SPEED_X
if ball.right >= WIDTH:
score_a += 1
ball.x = WIDTH // 2 - BALL_SIZE // 2
ball.y = HEIGHT // 2 - BALL_SIZE // 2
BALL_SPEED_X = -BALL_SPEED_X

# Fill the screen with black


screen.fill(BLACK)

# Draw paddles and ball


pygame.draw.rect(screen, WHITE, paddle_a)
pygame.draw.rect(screen, WHITE, paddle_b)
pygame.draw.ellipse(screen, WHITE, ball)
pygame.draw.aaline(screen, WHITE, (WIDTH // 2, 0), (WIDTH // 2, HEIGHT))

# Draw scores
font = pygame.font.Font(None, 74)
text = font.render(f"{score_a} {score_b}", True, WHITE)
screen.blit(text, (WIDTH // 2 - text.get_width() // 2, 10))

# Update the display


pygame.display.flip()

# Cap the frame rate


clock.tick(60)

You might also like