|
| 1 | +import pygame |
| 2 | +import sys |
| 3 | + |
| 4 | +# Initialize pygame |
| 5 | +pygame.init() |
| 6 | + |
| 7 | +# Screen setup |
| 8 | +WIDTH, HEIGHT = 800, 600 |
| 9 | +screen = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 10 | +pygame.display.set_caption("🏓 Pong Game") |
| 11 | + |
| 12 | +# Colors |
| 13 | +WHITE = (255, 255, 255) |
| 14 | +BLACK = (0, 0, 0) |
| 15 | + |
| 16 | +# Fonts |
| 17 | +font_large = pygame.font.Font(None, 100) |
| 18 | +font_medium = pygame.font.Font(None, 60) |
| 19 | +font_small = pygame.font.Font(None, 40) |
| 20 | + |
| 21 | +# Game settings |
| 22 | +PADDLE_WIDTH, PADDLE_HEIGHT = 10, 100 |
| 23 | +BALL_SIZE = 20 |
| 24 | +PADDLE_SPEED = 7 |
| 25 | +BALL_SPEED_X, BALL_SPEED_Y = 5, 5 |
| 26 | + |
| 27 | +# Create paddles and ball |
| 28 | +player = pygame.Rect(WIDTH - 20, HEIGHT//2 - 50, PADDLE_WIDTH, PADDLE_HEIGHT) |
| 29 | +opponent = pygame.Rect(10, HEIGHT//2 - 50, PADDLE_WIDTH, PADDLE_HEIGHT) |
| 30 | +ball = pygame.Rect(WIDTH//2 - 10, HEIGHT//2 - 10, BALL_SIZE, BALL_SIZE) |
| 31 | + |
| 32 | +# Score |
| 33 | +player_score = 0 |
| 34 | +opponent_score = 0 |
| 35 | + |
| 36 | +clock = pygame.time.Clock() |
| 37 | + |
| 38 | +# Game state |
| 39 | +menu_active = True |
| 40 | +mode_selected = None # "pve" or "pvp" |
| 41 | + |
| 42 | +def draw_text(text, font, color, surface, x, y): |
| 43 | + """Helper function to draw text centered.""" |
| 44 | + textobj = font.render(text, True, color) |
| 45 | + textrect = textobj.get_rect(center=(x, y)) |
| 46 | + surface.blit(textobj, textrect) |
| 47 | + |
| 48 | +def main_menu(): |
| 49 | + """Display start menu.""" |
| 50 | + global menu_active, mode_selected |
| 51 | + while menu_active: |
| 52 | + screen.fill(BLACK) |
| 53 | + draw_text("PONG", font_large, WHITE, screen, WIDTH/2, HEIGHT/4) |
| 54 | + draw_text("1. Player vs Computer", font_medium, WHITE, screen, WIDTH/2, HEIGHT/2 - 30) |
| 55 | + draw_text("2. Player vs Player", font_medium, WHITE, screen, WIDTH/2, HEIGHT/2 + 40) |
| 56 | + draw_text("ESC to Quit", font_small, WHITE, screen, WIDTH/2, HEIGHT - 50) |
| 57 | + |
| 58 | + pygame.display.flip() |
| 59 | + |
| 60 | + for event in pygame.event.get(): |
| 61 | + if event.type == pygame.QUIT: |
| 62 | + pygame.quit() |
| 63 | + sys.exit() |
| 64 | + if event.type == pygame.KEYDOWN: |
| 65 | + if event.key == pygame.K_1: |
| 66 | + mode_selected = "pve" |
| 67 | + menu_active = False |
| 68 | + elif event.key == pygame.K_2: |
| 69 | + mode_selected = "pvp" |
| 70 | + menu_active = False |
| 71 | + elif event.key == pygame.K_ESCAPE: |
| 72 | + pygame.quit() |
| 73 | + sys.exit() |
| 74 | + |
| 75 | +def game_loop(mode): |
| 76 | + """Main game loop.""" |
| 77 | + global player_score, opponent_score |
| 78 | + ball_speed_x = BALL_SPEED_X |
| 79 | + ball_speed_y = BALL_SPEED_Y |
| 80 | + player_score = 0 |
| 81 | + opponent_score = 0 |
| 82 | + |
| 83 | + # Reset positions |
| 84 | + player.centery = HEIGHT // 2 |
| 85 | + opponent.centery = HEIGHT // 2 |
| 86 | + ball.center = (WIDTH // 2, HEIGHT // 2) |
| 87 | + |
| 88 | + running = True |
| 89 | + while running: |
| 90 | + for event in pygame.event.get(): |
| 91 | + if event.type == pygame.QUIT: |
| 92 | + pygame.quit() |
| 93 | + sys.exit() |
| 94 | + if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: |
| 95 | + running = False # Return to menu |
| 96 | + |
| 97 | + # Paddle movement |
| 98 | + keys = pygame.key.get_pressed() |
| 99 | + # Player 1 |
| 100 | + if keys[pygame.K_UP] and player.top > 0: |
| 101 | + player.y -= PADDLE_SPEED |
| 102 | + if keys[pygame.K_DOWN] and player.bottom < HEIGHT: |
| 103 | + player.y += PADDLE_SPEED |
| 104 | + |
| 105 | + # Opponent / Player 2 |
| 106 | + if mode == "pve": |
| 107 | + # Simple AI: follow the ball |
| 108 | + if opponent.centery < ball.centery: |
| 109 | + opponent.y += PADDLE_SPEED |
| 110 | + if opponent.centery > ball.centery: |
| 111 | + opponent.y -= PADDLE_SPEED |
| 112 | + else: # PvP |
| 113 | + if keys[pygame.K_w] and opponent.top > 0: |
| 114 | + opponent.y -= PADDLE_SPEED |
| 115 | + if keys[pygame.K_s] and opponent.bottom < HEIGHT: |
| 116 | + opponent.y += PADDLE_SPEED |
| 117 | + |
| 118 | + # Ball movement |
| 119 | + ball.x += ball_speed_x |
| 120 | + ball.y += ball_speed_y |
| 121 | + |
| 122 | + # Collision with top/bottom |
| 123 | + if ball.top <= 0 or ball.bottom >= HEIGHT: |
| 124 | + ball_speed_y *= -1 |
| 125 | + |
| 126 | + # Collision with paddles |
| 127 | + if ball.colliderect(player) or ball.colliderect(opponent): |
| 128 | + ball_speed_x *= -1 |
| 129 | + |
| 130 | + # Score update |
| 131 | + if ball.left <= 0: |
| 132 | + player_score += 1 |
| 133 | + ball.center = (WIDTH//2, HEIGHT//2) |
| 134 | + ball_speed_x *= -1 |
| 135 | + if ball.right >= WIDTH: |
| 136 | + opponent_score += 1 |
| 137 | + ball.center = (WIDTH//2, HEIGHT//2) |
| 138 | + ball_speed_x *= -1 |
| 139 | + |
| 140 | + # Drawing |
| 141 | + screen.fill(BLACK) |
| 142 | + pygame.draw.rect(screen, WHITE, player) |
| 143 | + pygame.draw.rect(screen, WHITE, opponent) |
| 144 | + pygame.draw.ellipse(screen, WHITE, ball) |
| 145 | + pygame.draw.aaline(screen, WHITE, (WIDTH//2, 0), (WIDTH//2, HEIGHT)) |
| 146 | + |
| 147 | + # Scores |
| 148 | + player_text = font_medium.render(str(player_score), True, WHITE) |
| 149 | + opponent_text = font_medium.render(str(opponent_score), True, WHITE) |
| 150 | + screen.blit(player_text, (WIDTH//2 + 40, 20)) |
| 151 | + screen.blit(opponent_text, (WIDTH//2 - 80, 20)) |
| 152 | + |
| 153 | + # Escape hint |
| 154 | + draw_text("ESC to return to menu", font_small, WHITE, screen, WIDTH/2, HEIGHT - 40) |
| 155 | + |
| 156 | + pygame.display.flip() |
| 157 | + clock.tick(60) |
| 158 | + |
| 159 | +# Run the game |
| 160 | +while True: |
| 161 | + main_menu() |
| 162 | + game_loop(mode_selected) |
| 163 | + menu_active = True |
0 commit comments