Here's a simple implementation of the Pong game using Python and Pygame. This code creates a basic version of Pong with two paddles controlled by the players and a ball that bounces between them. The game keeps score and resets the ball after a point is scored.
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
PADDLE_WIDTH, PADDLE_HEIGHT = 15, 100
BALL_SIZE = 15
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
FPS = 60
# Create the game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong")
clock = pygame.time.Clock()
# 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)
# Ball
ball = pygame.Rect(WIDTH // 2 - BALL_SIZE // 2, HEIGHT // 2 - BALL_SIZE // 2, BALL_SIZE, BALL_SIZE)
ball_speed_x = 7 * random.choice((1, -1))
ball_speed_y = 7 * random.choice((1, -1))
# Scores
player1_score = 0
player2_score = 0
font = pygame.font.Font(None, 36)
def reset_ball():
ball.center = (WIDTH // 2, HEIGHT // 2)
return 7 * random.choice((1, -1)), 7 * random.choice((1, -1))
ball_speed_x, ball_speed_y = reset_ball()
def main():
global ball_speed_x, ball_speed_y, player1_score, player2_score
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Handle paddle movement
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and player1_paddle.top > 0:
player1_paddle.y -= 7
if keys[pygame.K_s] and player1_paddle.bottom < HEIGHT:
player1_paddle.y += 7
if keys[pygame.K_UP] and player2_paddle.top > 0:
player2_paddle.y -= 7
if keys[pygame.K_DOWN] and player2_paddle.bottom < HEIGHT:
player2_paddle.y += 7
# 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 *= -1
# Ball collision with paddles
if ball.colliderect(player1_paddle) or ball.colliderect(player2_paddle):
ball_speed_x *= -1
# Scoring
if ball.left <= 0:
player2_score += 1
ball_speed_x, ball_speed_y = reset_ball()
if ball.right >= WIDTH:
player1_score += 1
ball_speed_x, ball_speed_y = reset_ball()
# Rendering
screen.fill(BLACK)
pygame.draw.rect(screen, WHITE, player1_paddle)
pygame.draw.rect(screen, WHITE, player2_paddle)
pygame.draw.ellipse(screen, WHITE, ball)
pygame.draw.aaline(screen, WHITE, (WIDTH // 2, 0), (WIDTH // 2, HEIGHT))
# Display scores
player1_text = font.render(f"{player1_score}", True, WHITE)
player2_text = font.render(f"{player2_score}", True, WHITE)
screen.blit(player1_text, (WIDTH // 4, 20))
screen.blit(player2_text, (3 * WIDTH // 4, 20))
pygame.display.flip()
clock.tick(FPS)
if __name__ == "__main__":
main()
How to Run the Game
- Save the code above to a file named
pong.py.
- Make sure you have Python and Pygame installed. If not, install Pygame using pip:
pip install pygame
- Run the game by executing the Python script:
python pong.py
Controls
- Player 1 (left paddle): Use the W and S keys to move up and down.
- Player 2 (right paddle): Use the UP and DOWN arrow keys to move up and down.
Features
- The game keeps score for both players.
- The ball resets to the center after a point is scored.
- The ball bounces off the paddles and the top and bottom walls.
Enhancements
To make the game more interesting, you can add features like sound effects, a start screen, a game over screen, an AI opponent, difficulty levels, and power-ups. These enhancements can make the game more engaging and fun to play.
This code provides a solid foundation for a Pong game, and you can build upon it to add more features and improve the gameplay experience.