Write a website - a 2D ping pong game with AI. The AI should NOT be the “king of the…
- User
Write a website - a 2D ping pong game with AI. The AI should NOT be the “king of the game,” giving the player the feeling that they are really playing with someone else. (the game should not be complicated) add “bonuses” for the ball to the field “ball acceleration”, “super bounce effect”, “inversion of rebounds”, “complete darkness on the field (only a small amount of lighting around the main player's paddle)”
You need to give me the full code in Python. If you are confident in your abilities, you can take care of the appearance! I prefer the sci-fi style.
Assistant
Sci-Fi 2D Ping Pong Game with AI and Bonuses
Here's a complete Python implementation of a sci-fi themed 2D ping pong game with AI opponent and special bonuses. The game uses Pygame for graphics and gameplay.
import pygame import sys import random import math from pygame import gfxdraw # Initialize pygame pygame.init() pygame.mixer.init() # Constants WIDTH, HEIGHT = 800, 600 PADDLE_WIDTH, PADDLE_HEIGHT = 15, 100 BALL_SIZE = 10 FPS = 60 WHITE = (255, 255, 255) BLACK = (0, 0, 0) BLUE = (0, 100, 255) RED = (255, 50, 50) GREEN = (0, 255, 0) PURPLE = (150, 0, 255) YELLOW = (255, 255, 0) CYAN = (0, 255, 255) # Create the screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Sci-Fi Ping Pong") clock = pygame.time.Clock() # Load fonts font = pygame.font.SysFont('Arial', 30) small_font = pygame.font.SysFont('Arial', 20) # Sound effects try: bounce_sound = pygame.mixer.Sound("bounce.wav") bonus_sound = pygame.mixer.Sound("bonus.wav") pygame.mixer.music.load("background.mp3") pygame.mixer.music.set_volume(0.3) pygame.mixer.music.play(-1) except: # If sound files are missing, create dummy sound objects class DummySound: def play(self): pass bounce_sound = DummySound() bonus_sound = DummySound() # Game objects class Paddle: def __init__(self, x, y, width, height, color): self.rect = pygame.Rect(x, y, width, height) self.color = color self.speed = 7 self.score = 0 self.original_color = color def move(self, up_key, down_key): keys = pygame.key.get_pressed() if keys[up_key] and self.rect.top > 0: self.rect.y -= self.speed if keys[down_key] and self.rect.bottom < HEIGHT: self.rect.y += self.speed def ai_move(self, ball): # Simple AI that follows the ball but makes some mistakes if random.random() < 0.1: # 10% chance to make a mistake if random.random() < 0.5: self.rect.y += self.speed else: self.rect.y -= self.speed else: # Follow the ball but not perfectly if self.rect.centery < ball.rect.centery - 10: self.rect.y += self.speed - 1 elif self.rect.centery > ball.rect.centery + 10: self.rect.y -= self.speed - 1 def draw(self): # Draw paddle with sci-fi glow effect pygame.draw.rect(screen, self.color, self.rect) # Glow effect for i in range(1, 6): alpha = 255 - (i * 40) s = pygame.Surface((self.rect.width + i*2, self.rect.height + i*2), pygame.SRCALPHA) s.fill((*self.color, alpha)) screen.blit(s, (self.rect.x - i, self.rect.y - i)) class Ball: def __init__(self, x, y, size, color): self.rect = pygame.Rect(x - size//2, y - size//2, size, size) self.color = color self.speed_x = 5 * random.choice((1, -1)) self.speed_y = 5 * random.choice((1, -1)) self.original_speed_x = self.speed_x self.original_speed_y = self.speed_y self.original_color = color self.ball_acceleration = False self.super_bounce = False self.inversion = False self.darkness = False def move(self): self.rect.x += self.speed_x self.rect.y += self.speed_y # Bounce off top and bottom if self.rect.top <= 0 or self.rect.bottom >= HEIGHT: if self.super_bounce: # Super bounce - don't change direction but speed up self.speed_y *= 1.2 else: self.speed_y *= -1 bounce_sound.play() def reset(self): self.rect.center = (WIDTH // 2, HEIGHT // 2) self.speed_x = self.original_speed_x * random.choice((1, -1)) self.speed_y = self.original_speed_y * random.choice((1, -1)) self.ball_acceleration = False self.super_bounce = False self.inversion = False self.darkness = False self.color = self.original_color def draw(self): # Draw ball with sci-fi glow effect pygame.draw.rect(screen, self.color, self.rect) # Glow effect for i in range(1, 4): alpha = 255 - (i * 60) s = pygame.Surface((self.rect.width + i*2, self.rect.height + i*2), pygame.SRCALPHA) s.fill((*self.color, alpha)) screen.blit(s, (self.rect.x - i, self.rect.y - i)) class Bonus: def __init__(self): self.rect = pygame.Rect(0, 0, 20, 20) self.active = False self.type = None self.spawn_time = 0 self.duration = 0 self.colors = { "acceleration": RED, "super_bounce": BLUE, "inversion": PURPLE, "darkness": BLACK } def spawn(self): self.active = True self.rect.x = random.randint(50, WIDTH - 50) self.rect.y = random.randint(50, HEIGHT - 50) self.spawn_time = pygame.time.get_ticks() self.type = random.choice(["acceleration", "super_bounce", "inversion", "darkness"]) self.duration = 10000 # 10 seconds def draw(self): if self.active: pygame.draw.rect(screen, self.colors[self.type], self.rect) # Glow effect for i in range(1, 3): alpha = 255 - (i * 80) s = pygame.Surface((self.rect.width + i*2, self.rect.height + i*2), pygame.SRCALPHA) s.fill((*self.colors[self.type], alpha)) screen.blit(s, (self.rect.x - i, self.rect.y - i)) def check_collision(self, paddle): if self.active and self.rect.colliderect(paddle.rect): self.active = False bonus_sound.play() return self.type return None # Create game objects player_paddle = Paddle(30, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT, CYAN) ai_paddle = Paddle(WIDTH - 30 - PADDLE_WIDTH, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT, RED) ball = Ball(WIDTH // 2, HEIGHT // 2, BALL_SIZE, WHITE) bonus = Bonus() # Game state game_active = True player_score = 0 ai_score = 0 bonus_active = False bonus_type = None bonus_end_time = 0 darkness_surface = None # Create sci-fi background def create_background(): bg = pygame.Surface((WIDTH, HEIGHT)) bg.fill(BLACK) # Draw stars for _ in range(100): x = random.randint(0, WIDTH) y = random.randint(0, HEIGHT) radius = random.randint(1, 3) color = (random.randint(200, 255), random.randint(200, 255), random.randint(200, 255)) pygame.draw.circle(bg, color, (x, y), radius) # Draw grid lines for i in range(0, WIDTH, 40): pygame.draw.line(bg, (30, 30, 50), (i, 0), (i, HEIGHT), 1) for i in range(0, HEIGHT, 40): pygame.draw.line(bg, (30, 30, 50), (0, i), (WIDTH, i), 1) # Draw center line pygame.draw.line(bg, (50, 50, 70), (WIDTH // 2, 0), (WIDTH // 2, HEIGHT), 2) return bg background = create_background() # Create darkness effect surface def create_darkness_surface(paddle): darkness = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA) darkness.fill((0, 0, 0, 200)) # Create a gradient mask around the paddle for i in range(1, 200, 5): alpha = 200 - i if alpha < 0: alpha = 0 s = pygame.Surface((paddle.rect.width + i*2, paddle.rect.height + i*2), pygame.SRCALPHA) s.fill((0, 0, 0, alpha)) darkness.blit(s, (paddle.rect.x - i, paddle.rect.y - i)) return darkness # Game loop def main(): global game_active, player_score, ai_score, bonus_active, bonus_type, bonus_end_time, darkness_surface last_bonus_spawn = pygame.time.get_ticks() bonus_spawn_interval = 15000 # 15 seconds while True: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() if event.key == pygame.K_r and not game_active: # Reset game player_score = 0 ai_score = 0 ball.reset() game_active = True bonus_active = False bonus_type = None player_paddle.color = player_paddle.original_color ai_paddle.color = ai_paddle.original_color ball.color = ball.original_color if game_active: # Move paddles player_paddle.move(pygame.K_w, pygame.K_s) ai_paddle.ai_move(ball) # Move ball ball.move() # Check for collisions with paddles if ball.rect.colliderect(player_paddle.rect) or ball.rect.colliderect(ai_paddle.rect): bounce_sound.play() if ball.inversion: # Inversion effect - ball goes in same direction but changes color if ball.rect.colliderect(player_paddle.rect): ball.color = ai_paddle.color else: ball.color = player_paddle.color else: # Normal bounce ball.speed_x *= -1 # Add some randomness to the bounce ball.speed_y += random.uniform(-1, 1) # Speed up slightly on each hit if ball.ball_acceleration: ball.speed_x *= 1.1 ball.speed_y *= 1.1 # Check for scoring if ball.rect.left <= 0: ai_score += 1 ball.reset() elif ball.rect.right >= WIDTH: player_score += 1 ball.reset() # Spawn bonuses current_time = pygame.time.get_ticks() if current_time - last_bonus_spawn > bonus_spawn_interval and not bonus.active: bonus.spawn() last_bonus_spawn = current_time # Check for bonus collisions bonus_type = bonus.check_collision(player_paddle) if bonus_type: if bonus_type == "acceleration": ball.ball_acceleration = True ball.color = RED elif bonus_type == "super_bounce": ball.super_bounce = True ball.color = BLUE elif bonus_type == "inversion": ball.inversion = True ball.color = PURPLE elif bonus_type == "darkness": ball.darkness = True ball.color = YELLOW darkness_surface = create_darkness_surface(player_paddle) bonus_active = True bonus_end_time = current_time + bonus.duration # Check if bonus effect should end if bonus_active and current_time > bonus_end_time: bonus_active = False ball.ball_acceleration = False ball.super_bounce = False ball.inversion = False ball.darkness = False ball.color = ball.original_color darkness_surface = None # Draw everything screen.blit(background, (0, 0)) # Draw paddles and ball player_paddle.draw() ai_paddle.draw() ball.draw() # Draw bonus if active bonus.draw() # Draw darkness effect if active if ball.darkness and darkness_surface: screen.blit(darkness_surface, (0, 0)) # Draw scores player_text = font.render(str(player_score), True, CYAN) ai_text = font.render(str(ai_score), True, RED) screen.blit(player_text, (WIDTH // 4, 20)) screen.blit(ai_text, (3 * WIDTH // 4, 20)) # Draw bonus timer if active if bonus_active: remaining = max(0, (bonus_end_time - current_time) // 1000) bonus_text = small_font.render(f"Bonus: {remaining}s", True, WHITE) screen.blit(bonus_text, (WIDTH // 2 - 50, 20)) # Draw instructions instructions = small_font.render("ESC to quit, R to restart", True, WHITE) screen.blit(instructions, (WIDTH // 2 - 100, HEIGHT - 30)) else: # Game over screen screen.blit(background, (0, 0)) # Draw final scores player_text = font.render(f"Player: {player_score}", True, CYAN) ai_text = font.render(f"AI: {ai_score}", True, RED) screen.blit(player_text, (WIDTH // 2 - 100, HEIGHT // 2 - 50)) screen.blit(ai_text, (WIDTH // 2 - 100, HEIGHT // 2)) # Draw winner if player_score > ai_score: winner_text = font.render("You Win!", True, GREEN) elif ai_score > player_score: winner_text = font.render("AI Wins!", True, RED) else: winner_text = font.render("It's a Tie!", True, WHITE) screen.blit(winner_text, (WIDTH // 2 - 80, HEIGHT // 2 + 50)) # Draw restart instruction restart_text = font.render("Press R to restart", True, WHITE) screen.blit(restart_text, (WIDTH // 2 - 100, HEIGHT // 2 + 100)) pygame.display.flip() clock.tick(FPS) if __name__ == "__main__": main()Game Features:
-
Sci-Fi Theme:
- Dark background with stars and grid lines
- Glowing effects on paddles and ball
- Futuristic color scheme
-
AI Opponent:
- Follows the ball but makes occasional mistakes (10% chance)
- Not perfect, giving the player a fair chance
-
Special Bonuses:
- Ball Acceleration: Makes the ball move faster with each hit
- Super Bounce: Ball doesn't change direction when hitting top/bottom but speeds up
- Inversion of Rebounds: Ball changes color and behaves differently when hitting paddles
- Complete Darkness: Only a small area around your paddle is visible
-
Game Mechanics:
- Simple controls (W/S keys to move your paddle)
- Score tracking
- Sound effects (if sound files are available)
- Restart functionality
-
Visual Effects:
- Glowing paddles and ball
- Darkness effect that follows your paddle
- Bonus indicators with timers
How to Run:
- Make sure you have Python and Pygame installed
- Copy this code into a Python file (e.g.,
ping_pong.py) - Run the file with Python
- Use W/S keys to move your paddle
- Press ESC to quit or R to restart
The game is designed to be simple but visually appealing with the sci-fi theme. The AI is challenging but not perfect, giving players a fair chance to win. The bonuses add variety to the gameplay without making it too complicated.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.