Below is an example of a complete bullet hell game built using Python's Pygame library. The game features a player that can move and shoot, while waves of enemies appear one after the other. Each enemy periodically fires a radial spread of bullets toward the play area. Feel free to copy the code into a file (for example, bullet_hell.py) and run it (make sure you have Python and Pygame installed).
Note: This sample game is a simplified bullet hell implementation. You can expand upon it by tweaking enemy patterns, adding sound effects, graphics, and more refined collision handling.
import pygame
import random
import math
import sys
# Initialize pygame
pygame.init()
# Screen dimensions and game settings
WIDTH, HEIGHT = 480, 640
FPS = 60
PLAYER_SPEED = 5
PLAYER_SHOOT_DELAY = 250 # milliseconds between shots
BULLET_SPEED = 7
ENEMY_SPEED = 1
ENEMY_SHOOT_DELAY = 1500 # enemy shooting interval in milliseconds
WAVE_DELAY = 1500 # Delay in milliseconds between waves
# Colors
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
# Setup display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Bullet Hell - Infinite Waves")
clock = pygame.time.Clock()
# Sprite groups
all_sprites = pygame.sprite.Group()
player_bullets = pygame.sprite.Group()
enemy_bullets = pygame.sprite.Group()
enemies = pygame.sprite.Group()
# Player sprite
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
# Using a simple rectangle for the player sprite
self.image = pygame.Surface((30, 30))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH // 2
self.rect.bottom = HEIGHT - 10
self.last_shot = pygame.time.get_ticks()
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
self.rect.x -= PLAYER_SPEED
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
self.rect.x += PLAYER_SPEED
if keys[pygame.K_UP] or keys[pygame.K_w]:
self.rect.y -= PLAYER_SPEED
if keys[pygame.K_DOWN] or keys[pygame.K_s]:
self.rect.y += PLAYER_SPEED
# Keep player within screen bounds
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > HEIGHT:
self.rect.bottom = HEIGHT
# Auto-fire if space is held down
if keys[pygame.K_SPACE]:
self.shoot()
def shoot(self):
now = pygame.time.get_ticks()
if now - self.last_shot > PLAYER_SHOOT_DELAY:
self.last_shot = now
# Create a bullet moving upward from the center of the player.
bullet = Bullet(self.rect.centerx, self.rect.top, 0, -BULLET_SPEED, BLUE, owner="player")
all_sprites.add(bullet)
player_bullets.add(bullet)
# Bullet sprite (can be used for both player and enemy bullets)
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, dx, dy, color, owner="enemy"):
super().__init__()
self.image = pygame.Surface((6, 6))
self.image.fill(color)
self.rect = self.image.get_rect(center=(x, y))
self.dx = dx
self.dy = dy
self.owner = owner # "player" or "enemy"
def update(self):
self.rect.x += self.dx
self.rect.y += self.dy
# Remove the bullet if it goes off screen
if (self.rect.bottom < 0 or self.rect.top > HEIGHT or
self.rect.right < 0 or self.rect.left > WIDTH):
self.kill()
# Enemy sprite
class Enemy(pygame.sprite.Sprite):
def __init__(self, x, y, shoot_delay=ENEMY_SHOOT_DELAY):
super().__init__()
self.image = pygame.Surface((28, 28))
self.image.fill(RED)
self.rect = self.image.get_rect(center=(x, y))
self.speed = ENEMY_SPEED
self.last_shot = pygame.time.get_ticks()
self.shoot_delay = shoot_delay
def update(self):
# Simple movement: slowly move downward
self.rect.y += self.speed
# If enemy goes off screen (bottom), remove it.
if self.rect.top > HEIGHT:
self.kill()
# Shoot bullets in a circular spread at intervals
now = pygame.time.get_ticks()
if now - self.last_shot > self.shoot_delay:
self.last_shot = now
self.shoot_pattern()
def shoot_pattern(self):
# Fire a circular spread of bullets (for example, 12 bullets evenly spaced)
num_bullets = 12
angle_step = 360 / num_bullets
for i in range(num_bullets):
angle = math.radians(i * angle_step)
dx = BULLET_SPEED * math.cos(angle)
dy = BULLET_SPEED * math.sin(angle)
bullet = Bullet(self.rect.centerx, self.rect.centery, dx, dy, WHITE, owner="enemy")
all_sprites.add(bullet)
enemy_bullets.add(bullet)
def spawn_wave(wave_number):
"""
Spawn a new wave of enemies. The number and speed of enemies increases with the wave number.
"""
# For simplicity, we spawn a row of enemies at the top.
enemy_count = 5 + wave_number # increase enemy count with waves
spacing = WIDTH // (enemy_count + 1)
for i in range(enemy_count):
x = (i + 1) * spacing
y = random.randint(-100, -40) # spawn off the screen (above)
enemy = Enemy(x, y, shoot_delay=max(500, ENEMY_SHOOT_DELAY - wave_number * 50))
all_sprites.add(enemy)
enemies.add(enemy)
def draw_text(surf, text, size, x, y, color=WHITE):
font = pygame.font.SysFont("Arial", size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect(midtop=(x, y))
surf.blit(text_surface, text_rect)
# Create player instance
player = Player()
all_sprites.add(player)
# Game state variables
running = True
wave_number = 0
wave_cooldown = pygame.time.get_ticks() + 1000 # delay before first wave
# Game loop
while running:
clock.tick(FPS)
# Process input/events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Spawn new wave if there are no enemies and cooldown has expired.
if not enemies and pygame.time.get_ticks() > wave_cooldown:
wave_number += 1
spawn_wave(wave_number)
wave_cooldown = pygame.time.get_ticks() + WAVE_DELAY
# Update all sprites
all_sprites.update()
# Check collision: if enemy bullet hits player, game over.
hits = pygame.sprite.spritecollide(player, enemy_bullets, True)
if hits:
print("Game Over! You reached Wave:", wave_number)
running = False
# Check collision: if player bullet hits enemy, remove enemy.
enemy_hits = pygame.sprite.groupcollide(enemies, player_bullets, True, True)
# Clear screen
screen.fill(BLACK)
# Draw all sprites
all_sprites.draw(screen)
# Draw wave number text at the top of the screen.
draw_text(screen, f"Wave: {wave_number}", 20, WIDTH // 2, 10)
# Flip the display buffer to the screen.
pygame.display.flip()
pygame.quit()
sys.exit()
How to Run
-
Install Pygame:
If you don’t have Pygame installed yet, install it via pip:
pip install pygame
-
Save the Code:
Save the code into a file, e.g., bullet_hell.py.
-
Run the Game:
Execute the script using:
python bullet_hell.py
Use the arrow keys (or WASD) to move your player, and hold the spacebar to shoot. Avoid the enemy bullets filling the screen! Each wave brings more enemies and faster shooting patterns.
Enjoy the game and feel free to modify or expand the code to add new features and polish. Happy coding!