import pygame import random import sys # Constants WIDTH, HEIGHT = 800, 600 PADDLE_WIDTH, PADDLE_HEIGHT = 10, 100 BALL_SIZE = 18 PADDLE_SPEED = 4 BALL_SPEED_X = 5 BALL_SPEED_Y = 5 # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) # Initialize Pygame pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Ping Pong") # Create Paddles player_paddle = pygame.Rect(50, HEIGHT//2 - PADDLE_HEIGHT//2, PADDLE_WIDTH, PADDLE_HEIGHT) opponent_paddle = pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT//2 - PADDLE_HEIGHT//2, PADDLE_WIDTH, PADDLE_HEIGHT) # Create Ball- ball = pygame.Rect(WIDTH//2 - BALL_SIZE//2, HEIGHT//2 - BALL_SIZE//2, BALL_SIZE, BALL_SIZE) # Ball movement ball_speed_x = BALL_SPEED_X * random.choice((1, -1)) ball_speed_y = BALL_SPEED_Y * random.choice((1, -1)) # Score player_score = 0 opponent_score = 0 font = pygame.font.Font(None, 50) def draw_objects(): screen.fill(BLACK) pygame.draw.rect(screen, WHITE, player_paddle) pygame.draw.rect(screen, WHITE, opponent_paddle) pygame.draw.ellipse(screen, WHITE, ball) # Display scores player_text = font.render(str(player_score), True, WHITE) opponent_text = font.render(str(opponent_score), True, WHITE) screen.blit(player_text, (WIDTH//4, 50)) screen.blit(opponent_text, (WIDTH*3//4, 50)) def move_ball(): global ball_speed_x, ball_speed_y, player_score, opponent_score, game_over ball.x += ball_speed_x ball.y += ball_speed_y # Ball collision with top/bottom walls if ball.top <= 0 or ball.bottom >= HEIGHT: ball_speed_y *= -1 # Ball collision with paddles if ball.colliderect(player_paddle) or ball.colliderect(opponent_paddle): ball_speed_x *= -1 # Ball goes out of bounds if ball.left <= 0: opponent_score += 1 reset_ball() elif ball.right >= WIDTH: player_score += 1 reset_ball() # Check if game over if player_score >= 5 or opponent_score >= 5: game_over = True def reset_ball(): global ball_speed_x, ball_speed_y ball.center = (WIDTH//2, HEIGHT//2) ball_speed_x *= random.choice((1, -1)) ball_speed_y *= random.choice((1, -1)) def move_player_paddle(): # Get the vertical position of the mouse mouse_x, mouse_y = pygame.mouse.get_pos() # Ensure the paddle stays within the screen boundaries if mouse_y > 0 and mouse_y < HEIGHT - PADDLE_HEIGHT: player_paddle.y = mouse_y def move_opponent_paddle(): # Simple AI for opponent paddle if ball.centery < opponent_paddle.centery and opponent_paddle.top > 0: opponent_paddle.y -= PADDLE_SPEED elif ball.centery > opponent_paddle.centery and opponent_paddle.bottom < HEIGHT: opponent_paddle.y += PADDLE_SPEED def display_game_over(): screen.fill(BLACK) if player_score >= 5: text = font.render("Player Wins!", True, WHITE) else: text = font.render("Opponent Wins!", True, WHITE) text_rect = text.get_rect(center=(WIDTH//2, HEIGHT//2)) screen.blit(text, text_rect) # Add a delay before closing the window pygame.display.flip() pygame.time.delay(2000) sys.exit() clock = pygame.time.Clock() # Game loop running = True game_over = False while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if not game_over: move_player_paddle() move_opponent_paddle() move_ball() else: display_game_over() draw_objects() pygame.display.flip() clock.tick(60) pygame.quit()