In this game, you control a paddle and you and your opponent controls a paddle. You try and prevent the ball from getting past the paddle. If your opponent scores your opponent gets +1 points.

In order to play the game you must copy the code below and paste it into your python file. After that, run the code and enjoy!

Copy the code for FREE!:

import pygame
import random

# Initialize Pygame
pygame.init()

# Set up the game window
width, height = 800, 400
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pong")

# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# Set up the game clock
clock = pygame.time.Clock()

# Define the game elements
ball_radius = 10
ball_pos = [width // 2, height // 2]
ball_vel = [random.choice([-2, 2]), random.choice([-2, 2])]
paddle_width, paddle_height = 10, 60
paddle_vel = 5

# Define the paddles' initial positions
left_paddle_pos = [10, height // 2 - paddle_height // 2]
right_paddle_pos = [width - paddle_width - 10, height // 2 - paddle_height // 2]

# Define the scoreboard variables
score_left = 0
score_right = 0
font = pygame.font.Font(None, 36)

# Function to display the scoreboard
def display_score():
score_text = font.render(str(score_left) + " - " + str(score_right), True, WHITE)
screen.blit(score_text, (width // 2 - score_text.get_width() // 2, 10))

# Game loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Move the paddles
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and left_paddle_pos[1] > 0:
left_paddle_pos[1] -= paddle_vel
if keys[pygame.K_s] and left_paddle_pos[1] < height - paddle_height:
left_paddle_pos[1] += paddle_vel
if keys[pygame.K_UP] and right_paddle_pos[1] > 0:
right_paddle_pos[1] -= paddle_vel
if keys[pygame.K_DOWN] and right_paddle_pos[1] < height - paddle_height:
right_paddle_pos[1] += paddle_vel

# Update the ball position
ball_pos[0] += ball_vel[0]
ball_pos[1] += ball_vel[1]

# Check for collisions with walls
if ball_pos[1] <= 0 or ball_pos[1] >= height - ball_radius:
ball_vel[1] = -ball_vel[1]

# Check for collisions with paddles
if (
ball_pos[0] <= left_paddle_pos[0] + paddle_width
and left_paddle_pos[1] <= ball_pos[1] <= left_paddle_pos[1] + paddle_height
):
ball_vel[0] = -ball_vel[0]
if (
ball_pos[0] >= right_paddle_pos[0] - ball_radius
and right_paddle_pos[1] <= ball_pos[1] <= right_paddle_pos[1] + paddle_height
):
ball_vel[0] = -ball_vel[0]

# Check if the ball goes past the paddles
if ball_pos[0] <= 0:
score_right += 1
ball_pos = [width // 2, height // 2]
ball_vel = [random.choice([-2, 2]), random.choice([-2, 2])]
if ball_pos[0] >= width:
score_left += 1
ball_pos = [width // 2, height // 2]
ball_vel = [random.choice([-2, 2]), random.choice([-2, 2])]

# Clear the screen
screen.fill(BLACK)

# Draw the ball
pygame.draw.circle(screen, WHITE, ball_pos, ball_radius)

# Draw the paddles
pygame.draw.rect(screen, WHITE, (left_paddle_pos[0], left_paddle_pos[1], paddle_width, paddle_height))
pygame.draw.rect(screen, WHITE, (right_paddle_pos[0], right_paddle_pos[1], paddle_width, paddle_height))

# Display the score
display_score()

# Update the display
pygame.display.flip()

# Limit the frame rate
clock.tick(60)

# Quit the game
pygame.quit()

Leave a Reply

Your email address will not be published. Required fields are marked *