import pygame
import random
# Initialize Pygame
[Link]()
# Define constants
WIDTH = 800
HEIGHT = 600
BALL_RADIUS = 10
PAD_WIDTH = 8
PAD_HEIGHT = 80
HALF_PAD_WIDTH = PAD_WIDTH // 2
HALF_PAD_HEIGHT = PAD_HEIGHT // 2
BALL_POS = [WIDTH // 2, HEIGHT // 2]
BALL_VEL = [0, 0]
LEFT = False
RIGHT = True
PADDLE_SPEED = 5
SCORE1 = 0
SCORE2 = 0
FONT = [Link](None, 36)
# Set up the display
screen = [Link].set_mode((WIDTH, HEIGHT))
[Link].set_caption("Pong Game")
# Define the ball spawn function
def ball_spawn(direction):
global BALL_POS, BALL_VEL # Update global variables
BALL_POS = [WIDTH // 2, HEIGHT // 2]
horz = [Link](120, 240) / 60.0
vert = [Link](60, 180) / 60.0
if direction == LEFT:
horz = -horz
BALL_VEL = [horz, -vert]
# Define the Paddle class
class Paddle:
def __init__(self, x):
self.y = HEIGHT // 2
[Link] = (255, 255, 255)
[Link] = 0
[Link] = [Link](x, self.y - HALF_PAD_HEIGHT, PAD_WIDTH, PAD_HEIGHT)
def move_up(self):
[Link] = -PADDLE_SPEED
def move_down(self):
[Link] = PADDLE_SPEED
def stop(self):
[Link] = 0
def update(self):
self.y += [Link]
if self.y - HALF_PAD_HEIGHT < 0:
self.y = HALF_PAD_HEIGHT
elif self.y + HALF_PAD_HEIGHT > HEIGHT:
self.y = HEIGHT - HALF_PAD_HEIGHT
[Link] = self.y
def draw(self):
[Link](screen, [Link], [Link])
# Set up the game objects
left_paddle = Paddle(0)
right_paddle = Paddle(WIDTH - PAD_WIDTH)
ball_spawn(RIGHT)
# Set up the game clock
clock = [Link]()
# Start the game loop
while True:
# Handle events
for event in [Link]():
if [Link] == [Link]:
[Link]()
quit()
elif [Link] == [Link]:
if [Link] == pygame.K_w:
left_paddle.move_up()
elif [Link] == pygame.K_s:
left_paddle.move_down()
elif [Link] == pygame.K_UP:
right_paddle.move_up()
elif [Link] == pygame.K_DOWN:
right_paddle.move_down()
elif [Link] == [Link]:
if [Link] in (pygame.K_w, pygame.K_s):
left_paddle.stop()
elif [Link] in (pygame.K_UP, pygame.K_DOWN):
right_paddle.stop()
# Move the paddles and ball
left_paddle.update()
right_paddle.update()
BALL_POS[0] += int(BALL_VEL[0])
BALL_POS[1] += int(BALL_VEL[1])
# Handle ball collisions with top and bottom walls
if BALL_POS[1] <= BALL_RADIUS or BALL_POS[1] >= HEIGHT - BALL_VEL[1]:
# Handle ball collisions with paddles
if BALL_POS[0] <= BALL_RADIUS + PAD_WIDTH and
left_paddle.[Link](BALL_POS):
BALL_VEL[0] = -BALL_VEL[0] * 1.1
BALL_VEL[1] = BALL_VEL[1] * 1.1
elif BALL_POS[0] >= WIDTH - BALL_RADIUS - PAD_WIDTH and
right_paddle.[Link](BALL_POS):
BALL_VEL[0] = -BALL_VEL[0] * 1.1
BALL_VEL[1] = BALL_VEL[1] * 1.1
# Handle ball going off the screen
if BALL_POS[0] <= BALL_RADIUS + PAD_WIDTH:
SCORE2 += 1
ball_spawn(RIGHT)
elif BALL_POS[0] >= WIDTH - BALL_RADIUS - PAD_WIDTH:
SCORE1 += 1
ball_spawn(LEFT)
elif BALL_POS[1] <= BALL_RADIUS or BALL_POS[1] >= HEIGHT - BALL_RADIUS:
BALL_VEL[1] = -BALL_VEL[1]
# Draw the game objects
[Link]((0, 0, 0))
[Link](screen, (255, 255, 255), (WIDTH // 2, 0), (WIDTH // 2, HEIGHT))
[Link](screen, (255, 255, 255), (int(BALL_POS[0]), int(BALL_POS[1])),
BALL_RADIUS)
left_paddle.draw()
right_paddle.draw()
score_text = [Link](str(SCORE1) + " - " + str(SCORE2), True, (255, 255, 255))
[Link](score_text, (WIDTH // 2 - score_text.get_width() // 2, 20))
# Update the display
[Link]()
# Set the frame rate
[Link](60)
# Initialize Pygame
[Link]()
# Set the window size
WIDTH = 600
HEIGHT = 400
WINDOW_SIZE = (WIDTH, HEIGHT)
# Set the paddle and ball parameters
PAD_WIDTH = 8
PAD_HEIGHT = 80
BALL_RADIUS = 20
BALL_VEL = [5, -5]
# Set the colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# Set the font
FONT = [Link]("Arial", 36)
# Set the scores
SCORE1 = 0
SCORE2 = 0
# Set the paddle class
class Paddle:
def __init__(self, side):
[Link] = PAD_WIDTH
[Link] = PAD_HEIGHT
[Link] = (0, HEIGHT // 2 - [Link] // 2)
if side == "left":
[Link] = (0, HEIGHT // 2 - [Link] // 2)
elif side == "right":
[Link] = (WIDTH - [Link], HEIGHT // 2 - [Link] // 2)
[Link] = [Link]([Link][0], [Link][1], [Link], [Link])
def draw(self):
[Link](screen, WHITE, [Link])
def update(self, vel):
new_pos = [Link][1] + vel
if new_pos >= 0 and new_pos <= HEIGHT - [Link]:
[Link] = ([Link][0], new_pos)
[Link] = [Link]([Link][0], [Link][1], [Link],
[Link])
# Set the ball class
class Ball:
def __init__(self):
[Link] = (WIDTH // 2, HEIGHT // 2)
def update(self):
global BALL_POS, BALL_VEL
BALL_POS[0] += BALL_VEL[0]
BALL_POS[1] += BALL_VEL[1]
# Set the function to spawn the ball
def ball_spawn(direction):
global BALL_POS, BALL_VEL
BALL_POS = [WIDTH // 2, HEIGHT // 2]
if direction == LEFT:
BALL_VEL = [-5, -5]
elif direction == RIGHT:
BALL_VEL = [5, -5]
# Set the screen
screen = [Link].set_mode(WINDOW_SIZE)
[Link].set_caption("Pong")
# Set the clock
clock = [Link]()
# Spawn the paddles and ball
left_paddle = Paddle("left")
right_paddle = Paddle("right")
ball = Ball()
ball_spawn([Link]([LEFT, RIGHT]))
# Run the game loop
running = True
while running:
for event in [Link]():
if [Link] == [Link]:
running = False
elif [Link] == [Link]:
if [Link] == pygame.K_w:
left_paddle.update(-5)
elif [Link] == pygame.K_s:
left_paddle.update(5)
elif [Link] == pygame.K_UP:
right_paddle.update(-5)
elif [Link] == pygame.K_DOWN:
right_paddle.update(5)
# Update the paddles and ball
[Link]()
left_paddle.draw()
right_paddle.draw()
# Handle ball collisions with the top and bottom of the screen
if BALL_POS[1] <= BALL_RADIUS or BALL_POS[1] >= HEIGHT - BALL_RADIUS:
BALL_VEL[1] = -BALL_VEL[1]
# Handle ball collisions with the paddles
if ball_rect.colliderect(left_paddle.rect) or
ball_rect.colliderect(right_paddle.rect):
BALL_VEL[0] = -BALL_VEL[0]
# Handle ball going out of bounds
if BALL_POS[0] <= BALL_RADIUS:
SCORE2 += 1
ball_spawn(RIGHT)
elif BALL_POS[0] >= WIDTH - BALL_RADIUS:
SCORE1 += 1
ball_spawn(LEFT)
# Update the ball position
ball_rect = [Link](screen, WHITE, BALL_POS, BALL_RADIUS)
# Draw the scores
score_text = [Link](str(SCORE1) + " - " + str(SCORE2), True, WHITE)
[Link](score_text, (WIDTH // 2 - score_text.get_width() // 2, 10))
# Update the display
[Link]()
[Link](BLACK)
# Set the game speed
[Link](60)
# Quit the game
[Link]()
[Link]()