Arcade Game Blend: Space Invaders meets Pong
Here's a Python script that blends two classic arcade games - Space Invaders and Pong - into one functional game using Pygame. The game features elements from both games with a cohesive gameplay experience.
import pygame
import random
import sys
import math
from pygame import mixer
# Initialize pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Space Pong Invaders")
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Game variables
clock = pygame.time.Clock()
FPS = 60
score = 0
lives = 3
game_over = False
level = 1
paddle_speed = 8
ball_speed_x = 5
ball_speed_y = 5
invader_speed = 1
invader_direction = 1
invader_move_down = False
# Load sounds
mixer.init()
try:
shoot_sound = mixer.Sound('shoot.wav')
explosion_sound = mixer.Sound('explosion.wav')
bounce_sound = mixer.Sound('bounce.wav')
except:
# Create dummy sound functions if files not found
def dummy_sound():
pass
shoot_sound = dummy_sound
explosion_sound = dummy_sound
bounce_sound = dummy_sound
# Paddle (from Pong)
paddle_width = 100
paddle_height = 15
paddle_x = (WIDTH - paddle_width) // 2
paddle_y = HEIGHT - 50
# Ball (from Pong)
ball_radius = 10
ball_x = WIDTH // 2
ball_y = HEIGHT // 2
# Invaders (from Space Invaders)
invader_width = 40
invader_height = 30
invader_rows = 4
invader_cols = 8
invader_gap = 20
invaders = []
# Create invaders
for row in range(invader_rows):
for col in range(invader_cols):
invader_x = col * (invader_width + invader_gap) + 50
invader_y = row * (invader_height + invader_gap) + 50
invaders.append([invader_x, invader_y, True]) # x, y, alive
# Player bullet
bullet_width = 5
bullet_height = 15
bullet_speed = 10
bullet_state = "ready" # ready - can't see bullet, fire - bullet is moving
bullet_x = 0
bullet_y = paddle_y - bullet_height
# Invader bullets
invader_bullets = []
# Font
font = pygame.font.SysFont('Arial', 24)
def draw_paddle(x, y):
pygame.draw.rect(screen, WHITE, (x, y, paddle_width, paddle_height))
def draw_ball(x, y):
pygame.draw.circle(screen, WHITE, (x, y), ball_radius)
def draw_invader(x, y):
# Draw a simple invader shape
pygame.draw.rect(screen, GREEN, (x, y, invader_width, invader_height))
pygame.draw.rect(screen, BLACK, (x + 5, y + 5, invader_width - 10, invader_height - 10))
pygame.draw.rect(screen, GREEN, (x + 10, y + 10, invader_width - 20, invader_height - 20))
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
shoot_sound()
return x, y
def draw_bullet(x, y):
pygame.draw.rect(screen, RED, (x, y, bullet_width, bullet_height))
def is_collision(x1, y1, x2, y2, distance):
return math.sqrt((x2 - x1)**2 + (y2 - y1)**2) < distance
def show_score():
score_text = font.render(f"Score: {score}", True, WHITE)
lives_text = font.render(f"Lives: {lives}", True, WHITE)
level_text = font.render(f"Level: {level}", True, WHITE)
screen.blit(score_text, (10, 10))
screen.blit(lives_text, (WIDTH - 120, 10))
screen.blit(level_text, (WIDTH // 2 - 50, 10))
def show_game_over():
game_over_text = font.render("GAME OVER", True, RED)
restart_text = font.render("Press R to Restart", True, WHITE)
screen.blit(game_over_text, (WIDTH // 2 - 100, HEIGHT // 2 - 30))
screen.blit(restart_text, (WIDTH // 2 - 120, HEIGHT // 2 + 20))
def reset_game():
global score, lives, level, game_over, paddle_speed, ball_speed_x, ball_speed_y, invader_speed
global bullet_state, invaders, invader_bullets, paddle_x, paddle_y, ball_x, ball_y
score = 0
lives = 3
level = 1
game_over = False
paddle_speed = 8
ball_speed_x = 5
ball_speed_y = 5
invader_speed = 1
paddle_x = (WIDTH - paddle_width) // 2
paddle_y = HEIGHT - 50
ball_x = WIDTH // 2
ball_y = HEIGHT // 2
bullet_state = "ready"
invaders.clear()
for row in range(invader_rows):
for col in range(invader_cols):
invader_x = col * (invader_width + invader_gap) + 50
invader_y = row * (invader_height + invader_gap) + 50
invaders.append([invader_x, invader_y, True])
invader_bullets.clear()
def spawn_invader_bullet():
# Find alive invaders
alive_invaders = [invader for invader in invaders if invader[2]]
if alive_invaders:
# Randomly select one to shoot
shooter = random.choice(alive_invaders)
bullet_x = shooter[0] + invader_width // 2 - 2
bullet_y = shooter[1] + invader_height
invader_bullets.append([bullet_x, bullet_y])
def update_invader_bullets():
for bullet in invader_bullets[:]:
bullet[1] += 7 # Move bullet down
# Remove bullets that go off screen
if bullet[1] > HEIGHT:
invader_bullets.remove(bullet)
# Check collision with paddle
if (paddle_x < bullet[0] < paddle_x + paddle_width and
paddle_y < bullet[1] < paddle_y + paddle_height):
invader_bullets.remove(bullet)
return True # Paddle was hit
return False
def draw_invader_bullets():
for bullet in invader_bullets:
pygame.draw.rect(screen, RED, (bullet[0], bullet[1], 4, 10))
# Main game loop
running = True
while running:
# Fill the screen with black
screen.fill(BLACK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Keyboard events
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
paddle_x -= paddle_speed
if event.key == pygame.K_RIGHT:
paddle_x += paddle_speed
if event.key == pygame.K_SPACE and bullet_state == "ready":
bullet_x, bullet_y = fire_bullet(paddle_x + paddle_width // 2 - bullet_width // 2, paddle_y - bullet_height)
if event.key == pygame.K_r and game_over:
reset_game()
if not game_over:
# Paddle movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and paddle_x > 0:
paddle_x -= paddle_speed
if keys[pygame.K_RIGHT] and paddle_x < WIDTH - paddle_width:
paddle_x += paddle_speed
# Ball movement (Pong element)
ball_x += ball_speed_x
ball_y += ball_speed_y
# Ball collision with walls
if ball_x <= ball_radius or ball_x >= WIDTH - ball_radius:
ball_speed_x *= -1
bounce_sound()
if ball_y <= ball_radius:
ball_speed_y *= -1
bounce_sound()
# Ball collision with paddle
if (paddle_x < ball_x < paddle_x + paddle_width and
paddle_y - ball_radius < ball_y < paddle_y + paddle_height + ball_radius):
ball_speed_y *= -1
# Add some randomness to the bounce
ball_speed_x += random.uniform(-1, 1)
bounce_sound()
# Ball out of bounds (lose a life)
if ball_y > HEIGHT:
lives -= 1
ball_x = WIDTH // 2
ball_y = HEIGHT // 2
ball_speed_x = 5 * (1 if random.random() > 0.5 else -1)
ball_speed_y = 5
# Bullet movement
if bullet_state == "fire":
draw_bullet(bullet_x, bullet_y)
bullet_y -= bullet_speed
# Check if bullet went off screen
if bullet_y <= 0:
bullet_state = "ready"
# Check for bullet-invader collisions
for invader in invaders[:]:
if invader[2]: # If invader is alive
collision = is_collision(bullet_x, bullet_y, invader[0] + invader_width // 2, invader[1] + invader_height // 2, 20)
if collision and bullet_state == "fire":
explosion_sound()
bullet_state = "ready"
invader[2] = False # Mark invader as dead
score += 10
# Invader movement (Space Invaders element)
move_down = False
for invader in invaders:
if invader[2]: # Only move alive invaders
invader[0] += invader_speed * invader_direction
# Check if any invader hits the edge
if invader[0] <= 0 or invader[0] >= WIDTH - invader_width:
move_down = True
if move_down:
invader_direction *= -1
for invader in invaders:
if invader[2]:
invader[1] += 20 # Move down
# Check if invaders reached bottom
bottom_invader = max(invader[1] for invader in invaders if invader[2])
if bottom_invader + invader_height >= paddle_y - 20:
lives -= 1
# Reset invaders position
for i, invader in enumerate(invaders):
row = i // invader_cols
col = i % invader_cols
invader[0] = col * (invader_width + invader_gap) + 50
invader[1] = row * (invader_height + invader_gap) + 50
invader[2] = True # Revive all invaders
# Randomly spawn invader bullets
if random.random() < 0.02: # 2% chance each frame
spawn_invader_bullet()
# Update invader bullets and check for paddle hits
if update_invader_bullets():
lives -= 1
# Check if all invaders are dead
if all(not invader[2] for invader in invaders):
# Level up
level += 1
invader_speed += 0.5
paddle_speed += 0.5
ball_speed_x += 0.5 if ball_speed_x > 0 else -0.5
ball_speed_y += 0.5
# Reset invaders
for i, invader in enumerate(invaders):
row = i // invader_cols
col = i % invader_cols
invader[0] = col * (invader_width + invader_gap) + 50
invader[1] = row * (invader_height + invader_gap) + 50
invader[2] = True # Revive all invaders
# Check for game over
if lives <= 0:
game_over = True
# Draw game elements
draw_paddle(paddle_x, paddle_y)
draw_ball(ball_x, ball_y)
for invader in invaders:
if invader[2]: # Only draw alive invaders
draw_invader(invader[0], invader[1])
if bullet_state == "fire":
draw_bullet(bullet_x, bullet_y)
draw_invader_bullets()
show_score()
if game_over:
show_game_over()
# Update the display
pygame.display.update()
clock.tick(FPS)
pygame.quit()
sys.exit()
Game Features:
-
Blended Gameplay:
- Pong elements: Paddle at the bottom, bouncing ball
- Space Invaders elements: Alien invaders moving across the top, shooting mechanics
-
Game Mechanics:
- Control the paddle with left/right arrow keys
- Shoot bullets with spacebar to destroy invaders
- Bounce the ball to keep it in play
- Avoid invader bullets and don't let the ball fall off the bottom
-
Scoring System:
- Earn points by destroying invaders
- Level up when all invaders are destroyed
- Game gets progressively harder with each level
-
Game States:
- Score, lives, and level tracking
- Game over screen with restart option
- Sound effects for shooting, explosions, and ball bounces
-
Visual Elements:
- Clean, retro arcade-style graphics
- Clear UI showing game status
How to Play:
- Use left and right arrow keys to move the paddle
- Press spacebar to shoot bullets at the invaders
- Bounce the ball with your paddle to keep it in play
- Destroy all invaders to advance to the next level
- Avoid invader bullets and don't let the ball fall off the bottom
- If you lose all lives, press R to restart
To run this game, you'll need to have Pygame installed (pip install pygame). The game includes placeholder sound functions that will work even if the sound files aren't present.