Guest User

Untitled

a guest
Aug 16th, 2026
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. import pygame
  2. import sys
  3.  
  4. # 1. Setup Constants
  5. WIDTH = 800
  6. HEIGHT = 600
  7. FPS = 60
  8.  
  9. # Colors (RGB)
  10. WHITE = (255, 255, 255)
  11. BLACK = (0, 0, 0)
  12. RED = (255, 0, 0) # Added for a "Game Over" feel if desired, but staying with White
  13.  
  14. # Game Settings
  15. PADDLE_WIDTH = 15
  16. PADDLE_HEIGHT = 90
  17. BALL_SIZE = 15
  18. PLAYER_SPEED = 7
  19. AI_SPEED = 5
  20. BALL_SPEED_X = 5
  21. BALL_SPEED_Y = 5
  22. WINNING_SCORE = 11
  23.  
  24. # Initialize Pygame
  25. pygame.init()
  26. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  27. pygame.display.set_caption("Pong: Race to 11")
  28. clock = pygame.time.Clock()
  29. font = pygame.font.SysFont("Arial", 40) # Made font slightly larger for the win message
  30.  
  31. # Game Objects
  32. player1 = pygame.Rect(10, (HEIGHT - PADDLE_HEIGHT) // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
  33. computer_paddle = pygame.Rect(WIDTH - 10 - PADDLE_WIDTH, (HEIGHT - PADDLE_HEIGHT) // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
  34.  
  35. # Ball 1 Setup
  36. ball1 = pygame.Rect(WIDTH // 4, HEIGHT // 2, BALL_SIZE, BALL_SIZE)
  37. ball1_dx = BALL_SPEED_X
  38. ball1_dy = BALL_SPEED_Y
  39.  
  40. # Ball 2 Setup
  41. ball2 = pygame.Rect((WIDTH // 4) + 100, (HEIGHT // 2) - 50, BALL_SIZE, BALL_SIZE)
  42. ball2_dx = -BALL_SPEED_X
  43. ball2_dy = BALL_SPEED_Y
  44.  
  45. player1_score = 0
  46. computer_score = 0
  47. game_over = False # New flag to stop the game logic when someone wins
  48.  
  49. def main():
  50. global ball1_dx, ball1_dy, ball2_dx, ball2_dy, player1_score, computer_score, game_over
  51.  
  52. while True:
  53. # Event Handling
  54. for event in pygame.event.get():
  55. if event.type == pygame.QUIT:
  56. pygame.quit()
  57. sys.exit()
  58.  
  59. if not game_over:
  60. # --- Input Handling (Human Player) ---
  61. keys = pygame.key.get_pressed()
  62. if keys[pygame.K_w] and player1.top > 0:
  63. player1.y -= PLAYER_SPEED
  64. if keys[pygame.K_s] and player1.bottom < HEIGHT:
  65. player1.y += PLAYER_SPEED
  66.  
  67. # --- AI Logic (Computer Player) ---
  68. if ball1.centery < computer_paddle.centery:
  69. computer_paddle.y -= AI_SPEED
  70. elif ball1.centery > computer_paddle.centery:
  71. computer_paddle.y += AI_SPEED
  72.  
  73. # Boundary checks for Computer
  74. if computer_paddle.top < 0: computer_paddle.top = 0
  75. if computer_paddle.bottom > HEIGHT: computer_paddle.bottom = HEIGHT
  76.  
  77. # --- Ball 1 Logic ---
  78. ball1.x += ball1_dx
  79. ball1.y += ball1_dy
  80.  
  81. if ball1.top <= 0 or ball1.bottom >= HEIGHT:
  82. ball1_dy *= -1
  83.  
  84. if ball1.colliderect(player1):
  85. ball1_dx *= -1
  86. ball1.x = (player1.right + ball1.width) if ball1_dx > 0 else (player1.left - ball1.width)
  87.  
  88. if ball1.colliderect(computer_paddle):
  89. ball1_dx *= -1
  90. ball1.x = (computer_paddle.left - ball1.width) if ball1_dx < 0 else (computer_paddle.right + ball1.width)
  91.  
  92. # Ball 1 Scoring & Win Check
  93. if ball1.left <= 0:
  94. computer_score += 1
  95. ball1.center = (WIDTH // 2, HEIGHT // 2)
  96. ball1_dx *= -1
  97. elif ball1.right >= WIDTH:
  98. player1_score += 1
  99. ball1.center = (WIDTH // 2, HEIGHT // 2)
  100. ball1_dx *= -1
  101.  
  102. # --- Ball 2 Logic ---
  103. ball2.x += ball2_dx
  104. ball2.y += ball2_dy
  105.  
  106. if ball2.top <= 0 or ball2.bottom >= HEIGHT:
  107. ball2_dy *= -1
  108.  
  109. if ball2.colliderect(player1):
  110. ball2_dx *= -1
  111. ball2.x = (player1.right + ball2.width) if ball2_dx > 0 else (player1.left - ball2.width)
  112.  
  113. if ball2.colliderect(computer_paddle):
  114. ball2_dx *= -1
  115. ball2.x = (computer_paddle.left - ball2.width) if ball2_dx < 0 else (computer_paddle.right + ball2.width)
  116.  
  117. # Ball 2 Scoring & Win Check
  118. if ball2.left <= 0:
  119. computer_score += 1
  120. ball2.center = (WIDTH // 2, HEIGHT // 2)
  121. ball2_dx *= -1
  122. elif ball2.right >= WIDTH:
  123. player1_score += 1
  124. ball2.center = (WIDTH // 2, HEIGHT // 2)
  125. ball2_dx *= -1
  126.  
  127. # --- Check for Winner ---
  128. if player1_score >= WINNING_SCORE or computer_score >= WINNING_SCORE:
  129. game_over = True
  130.  
  131. # --- Drawing logic ---
  132. screen.fill(BLACK)
  133.  
  134. pygame.draw.rect(screen, WHITE, player1)
  135. pygame.draw.rect(screen, WHITE, computer_paddle)
  136. pygame.draw.ellipse(screen, WHITE, ball1)
  137. pygame.draw.ellipse(screen, WHITE, ball2)
  138.  
  139. # Draw Scoreboard
  140. score_text = font.render(f"{player1_score} - {computer_score}", True, WHITE)
  141. screen.blit(score_text, (WIDTH // 2 - 60, 20))
  142.  
  143. # Center Line
  144. pygame.draw.rect(screen, WHITE, (WIDTH // 2, 0, 2, HEIGHT), 1)
  145.  
  146. # If Game Over, display message
  147. if game_over:
  148. winner_text = ""
  149. if player1_score >= WINNING_SCORE:
  150. winner_text = "PLAYER 1 WINS!"
  151. else:
  152. winner_text = "COMPUTER WINS!"
  153.  
  154. # Render the big winning message in the middle of the screen
  155. win_surface = font.render(winner_text, True, WHITE)
  156. # Center text on screen (Subtract half width/height to perfectly center)
  157. rect = win_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2))
  158. screen.blit(win_surface, rect)
  159.  
  160. pygame.display.update()
  161. clock.tick(FPS)
  162.  
  163. if __name__ == "__main__":
  164. main()
  165.  
Advertisement
Add Comment
Please, Sign In to add comment