make a ping pong game in python

To create a ping pong game in Python, we will use the pygame library for game development. The game will involve two paddles that slide up and down the screen, a ball that bounces back and forth between the paddles, and collision detection to determine whether the ball hits a paddle or goes off the screen.

main.py
import pygame
from pygame.locals import *

# Define constants
WIDTH = 640
HEIGHT = 480
BALL_SIZE = 20
PADDLE_WIDTH = 15
PADDLE_HEIGHT = 80
PADDLE_SPEED = 5
BALL_SPEED_X = 5
BALL_SPEED_Y = 5

# Initialize pygame
pygame.init()

# Create the screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))

# Create the players' paddles
player1_paddle = pygame.Rect(50, HEIGHT/2 - PADDLE_HEIGHT/2, PADDLE_WIDTH, PADDLE_HEIGHT)
player2_paddle = pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT/2 - PADDLE_HEIGHT/2, PADDLE_WIDTH, PADDLE_HEIGHT)

# Create the ball
ball = pygame.Rect(WIDTH/2 - BALL_SIZE/2, HEIGHT/2 - BALL_SIZE/2, BALL_SIZE, BALL_SIZE)

# Set initial ball movement direction
ball_speed_x = BALL_SPEED_X
ball_speed_y = BALL_SPEED_Y

# Game loop
game_running = True
while game_running:

    # Handle events
    for event in pygame.event.get():
        if event.type == QUIT:
            game_running = False

    # Move the paddles
    keys = pygame.key.get_pressed()
    if keys[K_w] and player1_paddle.top > 0:
        player1_paddle.move_ip(0, -PADDLE_SPEED)
    if keys[K_s] and player1_paddle.bottom < HEIGHT:
        player1_paddle.move_ip(0, PADDLE_SPEED)
    if keys[K_UP] and player2_paddle.top > 0:
        player2_paddle.move_ip(0, -PADDLE_SPEED)
    if keys[K_DOWN] and player2_paddle.bottom < HEIGHT:
        player2_paddle.move_ip(0, PADDLE_SPEED)

    # Update the ball position
    ball.move_ip(ball_speed_x, ball_speed_y)

    # Handle ball collision with the paddles
    if ball.colliderect(player1_paddle) or ball.colliderect(player2_paddle):
        ball_speed_x = -ball_speed_x

    # Handle ball going off the top or bottom of the screen
    if ball.top <= 0 or ball.bottom >= HEIGHT:
        ball_speed_y = -ball_speed_y

    # Handle ball going off the left or right of the screen
    if ball.left <= 0 or ball.right >= WIDTH:
        ball_speed_x = -ball_speed_x

    # Fill the screen with black
    screen.fill((0, 0, 0))

    # Draw the paddles and the ball
    pygame.draw.rect(screen, (255, 255, 255), player1_paddle)
    pygame.draw.rect(screen, (255, 255, 255), player2_paddle)
    pygame.draw.ellipse(screen, (255, 255, 255), ball)

    # Update the display
    pygame.display.update()

    # Set the frame rate
    pygame.time.Clock().tick(60)

# Quit pygame
pygame.quit()
2306 chars
82 lines

This code creates a basic ping pong game in Python using the pygame library. The game loop handles events, updates the position of the paddles and ball, handles collision detection between the ball and paddles, and draws the game elements on the screen. With additional features such as keeping score or adding sound effects, this game could be expanded upon to make an even more engaging game.

gistlibby LogSnag