Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- # 1. Setup Constants
- WIDTH = 800
- HEIGHT = 600
- FPS = 60
- # Colors (RGB)
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- RED = (255, 0, 0) # Added for a "Game Over" feel if desired, but staying with White
- # Game Settings
- PADDLE_WIDTH = 15
- PADDLE_HEIGHT = 90
- BALL_SIZE = 15
- PLAYER_SPEED = 7
- AI_SPEED = 5
- BALL_SPEED_X = 5
- BALL_SPEED_Y = 5
- WINNING_SCORE = 11
- # Initialize Pygame
- pygame.init()
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("Pong: Race to 11")
- clock = pygame.time.Clock()
- font = pygame.font.SysFont("Arial", 40) # Made font slightly larger for the win message
- # Game Objects
- player1 = pygame.Rect(10, (HEIGHT - PADDLE_HEIGHT) // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
- computer_paddle = pygame.Rect(WIDTH - 10 - PADDLE_WIDTH, (HEIGHT - PADDLE_HEIGHT) // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
- # Ball 1 Setup
- ball1 = pygame.Rect(WIDTH // 4, HEIGHT // 2, BALL_SIZE, BALL_SIZE)
- ball1_dx = BALL_SPEED_X
- ball1_dy = BALL_SPEED_Y
- # Ball 2 Setup
- ball2 = pygame.Rect((WIDTH // 4) + 100, (HEIGHT // 2) - 50, BALL_SIZE, BALL_SIZE)
- ball2_dx = -BALL_SPEED_X
- ball2_dy = BALL_SPEED_Y
- player1_score = 0
- computer_score = 0
- game_over = False # New flag to stop the game logic when someone wins
- def main():
- global ball1_dx, ball1_dy, ball2_dx, ball2_dy, player1_score, computer_score, game_over
- while True:
- # Event Handling
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- if not game_over:
- # --- Input Handling (Human Player) ---
- keys = pygame.key.get_pressed()
- if keys[pygame.K_w] and player1.top > 0:
- player1.y -= PLAYER_SPEED
- if keys[pygame.K_s] and player1.bottom < HEIGHT:
- player1.y += PLAYER_SPEED
- # --- AI Logic (Computer Player) ---
- if ball1.centery < computer_paddle.centery:
- computer_paddle.y -= AI_SPEED
- elif ball1.centery > computer_paddle.centery:
- computer_paddle.y += AI_SPEED
- # Boundary checks for Computer
- if computer_paddle.top < 0: computer_paddle.top = 0
- if computer_paddle.bottom > HEIGHT: computer_paddle.bottom = HEIGHT
- # --- Ball 1 Logic ---
- ball1.x += ball1_dx
- ball1.y += ball1_dy
- if ball1.top <= 0 or ball1.bottom >= HEIGHT:
- ball1_dy *= -1
- if ball1.colliderect(player1):
- ball1_dx *= -1
- ball1.x = (player1.right + ball1.width) if ball1_dx > 0 else (player1.left - ball1.width)
- if ball1.colliderect(computer_paddle):
- ball1_dx *= -1
- ball1.x = (computer_paddle.left - ball1.width) if ball1_dx < 0 else (computer_paddle.right + ball1.width)
- # Ball 1 Scoring & Win Check
- if ball1.left <= 0:
- computer_score += 1
- ball1.center = (WIDTH // 2, HEIGHT // 2)
- ball1_dx *= -1
- elif ball1.right >= WIDTH:
- player1_score += 1
- ball1.center = (WIDTH // 2, HEIGHT // 2)
- ball1_dx *= -1
- # --- Ball 2 Logic ---
- ball2.x += ball2_dx
- ball2.y += ball2_dy
- if ball2.top <= 0 or ball2.bottom >= HEIGHT:
- ball2_dy *= -1
- if ball2.colliderect(player1):
- ball2_dx *= -1
- ball2.x = (player1.right + ball2.width) if ball2_dx > 0 else (player1.left - ball2.width)
- if ball2.colliderect(computer_paddle):
- ball2_dx *= -1
- ball2.x = (computer_paddle.left - ball2.width) if ball2_dx < 0 else (computer_paddle.right + ball2.width)
- # Ball 2 Scoring & Win Check
- if ball2.left <= 0:
- computer_score += 1
- ball2.center = (WIDTH // 2, HEIGHT // 2)
- ball2_dx *= -1
- elif ball2.right >= WIDTH:
- player1_score += 1
- ball2.center = (WIDTH // 2, HEIGHT // 2)
- ball2_dx *= -1
- # --- Check for Winner ---
- if player1_score >= WINNING_SCORE or computer_score >= WINNING_SCORE:
- game_over = True
- # --- Drawing logic ---
- screen.fill(BLACK)
- pygame.draw.rect(screen, WHITE, player1)
- pygame.draw.rect(screen, WHITE, computer_paddle)
- pygame.draw.ellipse(screen, WHITE, ball1)
- pygame.draw.ellipse(screen, WHITE, ball2)
- # Draw Scoreboard
- score_text = font.render(f"{player1_score} - {computer_score}", True, WHITE)
- screen.blit(score_text, (WIDTH // 2 - 60, 20))
- # Center Line
- pygame.draw.rect(screen, WHITE, (WIDTH // 2, 0, 2, HEIGHT), 1)
- # If Game Over, display message
- if game_over:
- winner_text = ""
- if player1_score >= WINNING_SCORE:
- winner_text = "PLAYER 1 WINS!"
- else:
- winner_text = "COMPUTER WINS!"
- # Render the big winning message in the middle of the screen
- win_surface = font.render(winner_text, True, WHITE)
- # Center text on screen (Subtract half width/height to perfectly center)
- rect = win_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2))
- screen.blit(win_surface, rect)
- pygame.display.update()
- clock.tick(FPS)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment