Here's a complete Python implementation using Pygame for a sci-fi themed ping pong game with AI and special bonuses:
python Copy code
import pygame
import sys
import random
import math
from pygame import gfxdraw
# Initialize pygame
pygame.init()
pygame.mixer.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Sci-Fi Pong")
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
CYAN = (0, 255, 255)
PURPLE = (128, 0, 255)
BLUE = (0, 100, 255)
NEON_GREEN = (57, 255, 20)
NEON_PINK = (255, 20, 147)
# Game elements
PADDLE_WIDTH, PADDLE_HEIGHT = 15, 100
BALL_SIZE = 12
PADDLE_SPEED = 8
AI_PADDLE_SPEED = 6
INITIAL_BALL_SPEED = 5
MAX_BALL_SPEED = 12
# Fonts
font = pygame.font.Font(None, 36)
small_font = pygame.font.Font(None, 24)
# Sound effects
try:
paddle_sound = pygame.mixer.Sound("paddle.wav")
wall_sound = pygame.mixer.Sound("wall.wav")
score_sound = pygame.mixer.Sound("score.wav")
bonus_sound = pygame.mixer.Sound("bonus.wav")
except:
# Create silent sounds if files not found
paddle_sound = pygame.mixer.Sound(buffer=bytearray(44))
wall_sound = pygame.mixer.Sound(buffer=bytearray(44))
score_sound = pygame.mixer.Sound(buffer=bytearray(44))
bonus_sound = pygame.mixer.Sound(buffer=bytearray(44))
# Game variables
player_score = 0
ai_score = 0
game_paused = False
bonus_active = False
bonus_type = ""
bonus_timer = 0
bonus_duration = 5000 # 5 seconds in milliseconds
darkness_effect = False
inverted_controls = False
super_bounce = False
ball_speed_boost = False
# Create paddles and ball
player_paddle = pygame.Rect(50, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
ai_paddle = pygame.Rect(WIDTH - 50 - PADDLE_WIDTH, HEIGHT // 2 - PADDLE_HEIGHT // 2, PADDLE_WIDTH, PADDLE_HEIGHT)
ball = pygame.Rect(WIDTH // 2 - BALL_SIZE // 2, HEIGHT // 2 - BALL_SIZE // 2, BALL_SIZE, BALL_SIZE)
# Ball direction
ball_speed_x = INITIAL_BALL_SPEED * random.choice((1, -1))
ball_speed_y = INITIAL_BALL_SPEED * random.choice((1, -1))
# Particle effects
particles = []
# Starfield background
stars = [(random.randint(0, WIDTH), random.randint(0, HEIGHT), random.uniform(0.1, 0.5)) for _ in range(200)]
# Game clock
clock = pygame.time.Clock()
last_time = pygame.time.get_ticks()
def reset_ball():
"""Reset ball to center with random direction"""
global ball_speed_x, ball_speed_y
ball.center = (WIDTH // 2, HEIGHT // 2)
ball_speed_x = INITIAL_BALL_SPEED * random.choice((1, -1))
ball_speed_y = INITIAL_BALL_SPEED * random.choice((1, -1))
if ball_speed_boost:
ball_speed_x *= 1.5
ball_speed_y *= 1.5
def draw_paddle(paddle, color):
"""Draw a paddle with a sci-fi effect"""
# Main paddle
pygame.draw.rect(screen, color, paddle, border_radius=5)
# Glow effect
glow_rect = paddle.inflate(10, 10)
for alpha in range(20, 0, -2):
temp_surf = pygame.Surface((glow_rect.width, glow_rect.height), pygame.SRCALPHA)
pygame.draw.rect(temp_surf, (*color[:3], alpha), (0, 0, glow_rect.width, glow_rect.height), border_radius=8)
screen.blit(temp_surf, (glow_rect.x, glow_rect.y))
# Energy lines
for i in range(3):
line_y = paddle.y + (i + 1) * (paddle.height // 4)
pygame.draw.line(screen, WHITE, (paddle.x, line_y), (paddle.x + paddle.width, line_y), 1)
def draw_ball():
"""Draw the ball with a sci-fi effect"""
# Glowing core
pygame.draw.circle(screen, NEON_GREEN, ball.center, BALL_SIZE)
# Outer glow
for radius in range(BALL_SIZE + 2, BALL_SIZE + 10, 2):
alpha = 255 - (radius - BALL_SIZE) * 25
if alpha > 0:
temp_surf = pygame.Surface((radius * 2, radius * 2), pygame.SRCALPHA)
pygame.draw.circle(temp_surf, (*NEON_GREEN[:3], alpha), (radius, radius), radius)
screen.blit(temp_surf, (ball.centerx - radius, ball.centery - radius))
# Add particles when moving
if abs(ball_speed_x) > 0 or abs(ball_speed_y) > 0:
particles.append({
'pos': [ball.centerx, ball.centery],
'color': random.choice([NEON_GREEN, CYAN, NEON_PINK]),
'radius': random.randint(1, 3),
'life': 20
})
def draw_particles():
"""Draw and update particles"""
for i, particle in enumerate(particles[:]):
pygame.draw.circle(screen, particle['color'],
(int(particle['pos'][0]), int(particle['pos'][1])),
particle['radius'])
particle['life'] -= 1
if particle['life'] <= 0:
particles.remove(particle)
def draw_starfield():
"""Draw the starfield background"""
screen.fill(BLACK)
current_time = pygame.time.get_ticks()
for i, (x, y, speed) in enumerate(stars):
# Update star position (parallax effect)
new_x = (x - speed * (current_time / 100)) % WIDTH
stars[i] = (new_x, y, speed)
# Draw star
brightness = int(255 * speed)
pygame.draw.circle(screen, (brightness, brightness, brightness), (int(new_x), int(y)), int(speed * 2))
def draw_light_around_paddle():
"""Draw lighting effect around player's paddle"""
if not darkness_effect:
return
# Create a surface for the light
light_surf = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
# Calculate light radius based on paddle position
radius = 150
center_x = player_paddle.x + player_paddle.width // 2
center_y = player_paddle.centery
# Draw gradient light
for r in range(radius, 0, -5):
alpha = int(30 * (1 - r/radius))
pygame.draw.circle(light_surf, (255, 255, 255, alpha), (center_x, center_y), r)
screen.blit(light_surf, (0, 0))
def draw_bonus_indicator():
"""Draw current active bonus indicator"""
if not bonus_active:
return
remaining_time = max(0, bonus_duration - (pygame.time.get_ticks() - bonus_timer))
progress = remaining_time / bonus_duration
# Draw progress bar background
pygame.draw.rect(screen, (50, 50, 50), (WIDTH // 2 - 100, 10, 200, 20), border_radius=5)
# Draw progress bar
color = NEON_PINK if "inversion" in bonus_type else NEON_GREEN
pygame.draw.rect(screen, color, (WIDTH // 2 - 100, 10, int(200 * progress), 20), border_radius=5)
# Draw bonus text
text = small_font.render(bonus_type, True, WHITE)
screen.blit(text, (WIDTH // 2 - text.get_width() // 2, 35))
def spawn_bonus():
"""Randomly spawn a bonus effect"""
global bonus