import pygame import sys import random # Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 800, 600 FPS = 50 WHITE = (255, 255, 255) BLACK = (0, 0, 0) # Paddle Constants PADDLE_WIDTH, PADDLE_HEIGHT = 15, 100 PADDLE_SPEED = 10 # Ball Constants BALL_SIZE = 20 BALL_SPEED_X = 5 BALL_SPEED_Y = 5 # Create game window screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Pong") clock = pygame.time.Clock() # Paddle Class class Paddle(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface((PADDLE_WIDTH, PADDLE_HEIGHT)) self.image.fill(WHITE) self.rect = self.image.get_rect(center=(x, y)) def update(self, keys): if keys[pygame.K_w] and self.rect.top > 0: self.rect.y -= PADDLE_SPEED if keys[pygame.K_s] and self.rect.bottom < HEIGHT: self.rect.y += PADDLE_SPEED # Ball Class class Ball(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.Surface((BALL_SIZE, BALL_SIZE), pygame.SRCALPHA) pygame.draw.circle(self.image, WHITE, (BALL_SIZE // 2, BALL_SIZE // 2), BALL_SIZE // 2) self.rect = self.image.get_rect(center=(WIDTH // 2, HEIGHT // 2)) self.speed_x = BALL_SPEED_X * random.choice([-1, 1]) self.speed_y = BALL_SPEED_Y * random.choice([-1, 1]) def update(self): self.rect.x += self.speed_x self.rect.y += self.speed_y # Ball bouncing off walls if self.rect.top <= 0 or self.rect.bottom >= HEIGHT: self.speed_y = -self.speed_y # Create sprites all_sprites = pygame.sprite.Group() paddles = pygame.sprite.Group() balls = pygame.sprite.Group() # Create paddles left_paddle = Paddle(20, HEIGHT // 2) right_paddle = Paddle(WIDTH - 20, HEIGHT // 2) all_sprites.add(left_paddle, right_paddle) paddles.add(left_paddle, right_paddle) # Create ball ball = Ball() all_sprites.add(ball) balls.add(ball) # Score score_left = 0 score_right = 0 # Font for displaying score font = pygame.font.Font(None, 36) # Game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() left_paddle.update(keys) right_paddle.update(keys) balls.update() # Ball bouncing off paddles if pygame.sprite.spritecollide(left_paddle, balls, False): ball.speed_x = -ball.speed_x elif pygame.sprite.spritecollide(right_paddle, balls, False): ball.speed_x = -ball.speed_x # Ball missed by left paddle if ball.rect.left <= 0: ball.rect.center = (WIDTH // 2, HEIGHT // 2) ball.speed_x = BALL_SPEED_X ball.speed_y = BALL_SPEED_Y score_right += 1 # Ball missed by right paddle elif ball.rect.right >= WIDTH: ball.rect.center = (WIDTH // 2, HEIGHT // 2) ball.speed_x = -BALL_SPEED_X ball.speed_y = BALL_SPEED_Y score_left += 1 # Draw everything screen.fill(BLACK) pygame.draw.line(screen, WHITE, (WIDTH // 2, 0), (WIDTH // 2, HEIGHT), 2) # Draw score score_text = font.render(f"{score_left} - {score_right}", True, WHITE) screen.blit(score_text, (WIDTH // 2 - 50, 20)) all_sprites.draw(screen) # Update display pygame.display.flip() # Cap the frame rate clock.tick(FPS) # Quit the game pygame.quit() sys.exit()