Sure! I'll help you create a simplified version of a game inspired by Vampire Survivors using Pygame. This game will feature:
- A player character that can move around the screen.
- Enemies that spawn in waves and move towards the player.
- Power-ups that the player can collect to enhance abilities.
- Basic attack mechanics where the player automatically attacks nearby enemies.
Since we're not using any external images, we'll use simple shapes for all game elements.
Let's start by setting up the basic structure. Here's the complete code:
import pygame
import random
import math
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
# Create the display surface
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Vampire Survivors Clone')
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
# Clock object to control FPS
clock = pygame.time.Clock()
FPS = 60
# Player properties
PLAYER_SIZE = 20
PLAYER_COLOR = BLUE
PLAYER_SPEED = 5
PLAYER_HEALTH = 100
PLAYER_MAX_HEALTH = 100
# Enemy properties
ENEMY_SIZE = 15
ENEMY_COLOR = RED
ENEMY_SPEED = 1
# Power-up properties
POWERUP_SIZE = 10
POWERUP_COLOR = GREEN
POWERUP_DURATION = 300 # Frames
# Attack properties
ATTACK_RANGE = 50
ATTACK_DAMAGE = 10
ATTACK_COOLDOWN = 60 # Frames
# Fonts
font = pygame.font.SysFont(None, 24)
# Game variables
player_pos = [WIDTH // 2, HEIGHT // 2]
enemies = []
powerups = []
projectiles = []
enemy_spawn_timer = 0
enemy_spawn_interval = 90 # Frames
attack_timer = 0
powerup_active = False
powerup_timer = 0
def spawn_enemy():
# Spawn enemy at a random edge of the screen
edge = random.choice(['top', 'bottom', 'left', 'right'])
if edge == 'top':
x = random.randint(0, WIDTH)
y = 0
elif edge == 'bottom':
x = random.randint(0, WIDTH)
y = HEIGHT
elif edge == 'left':
x = 0
y = random.randint(0, HEIGHT)
else: # 'right'
x = WIDTH
y = random.randint(0, HEIGHT)
enemies.append({'pos': [x, y], 'health': 20})
def spawn_powerup():
x = random.randint(POWERUP_SIZE, WIDTH - POWERUP_SIZE)
y = random.randint(POWERUP_SIZE, HEIGHT - POWERUP_SIZE)
powerups.append({'pos': [x, y]})
def draw_player():
pygame.draw.circle(screen, PLAYER_COLOR, player_pos, PLAYER_SIZE)
def draw_enemies():
for enemy in enemies:
pygame.draw.circle(screen, ENEMY_COLOR, enemy['pos'], ENEMY_SIZE)
def draw_powerups():
for powerup in powerups:
pygame.draw.rect(screen, POWERUP_COLOR, (powerup['pos'][0] - POWERUP_SIZE // 2,
powerup['pos'][1] - POWERUP_SIZE // 2,
POWERUP_SIZE, POWERUP_SIZE))
def move_enemies():
for enemy in enemies:
dx = player_pos[0] - enemy['pos'][0]
dy = player_pos[1] - enemy['pos'][1]
dist = math.hypot(dx, dy)
if dist != 0:
dx, dy = dx / dist, dy / dist
enemy['pos'][0] += dx * ENEMY_SPEED
enemy['pos'][1] += dy * ENEMY_SPEED
def check_collisions():
global PLAYER_HEALTH, powerup_active, powerup_timer
# Check collision with enemies
for enemy in enemies[:]:
dist = math.hypot(player_pos[0] - enemy['pos'][0], player_pos[1] - enemy['pos'][1])
if dist < PLAYER_SIZE + ENEMY_SIZE:
PLAYER_HEALTH -= 0.5 # Damage over time
if PLAYER_HEALTH <= 0:
pygame.quit()
print("Game Over!")
exit()
# Check collision with power-ups
for powerup in powerups[:]:
dist = math.hypot(player_pos[0] - powerup['pos'][0], player_pos[1] - powerup['pos'][1])
if dist < PLAYER_SIZE + POWERUP_SIZE:
powerups.remove(powerup)
powerup_active = True
powerup_timer = POWERUP_DURATION
def attack_enemies():
global attack_timer
if attack_timer == 0:
# Attack enemies within range
for enemy in enemies[:]:
dist = math.hypot(player_pos[0] - enemy['pos'][0], player_pos[1] - enemy['pos'][1])
if dist < ATTACK_RANGE:
enemy['health'] -= ATTACK_DAMAGE
if enemy['health'] <= 0:
enemies.remove(enemy)
attack_timer = ATTACK_COOLDOWN
else:
attack_timer -= 1
def draw_attack_radius():
pygame.draw.circle(screen, WHITE, player_pos, ATTACK_RANGE, 1)
def draw_health_bar():
# Draw background bar
pygame.draw.rect(screen, RED, (10, 10, 200, 20))
# Calculate health ratio
health_ratio = PLAYER_HEALTH / PLAYER_MAX_HEALTH
pygame.draw.rect(screen, GREEN, (10, 10, 200 * health_ratio, 20))
# Render health text
health_text = font.render(f'Health: {int(PLAYER_HEALTH)}', True, WHITE)
screen.blit(health_text, (10, 35))
# Game loop
running = True
while running:
clock.tick(FPS)
screen.fill(BLACK)
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Movement keys
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and player_pos[0] - PLAYER_SIZE > 0:
player_pos[0] -= PLAYER_SPEED
if keys[pygame.K_d] and player_pos[0] + PLAYER_SIZE < WIDTH:
player_pos[0] += PLAYER_SPEED
if keys[pygame.K_w] and player_pos[1] - PLAYER_SIZE > 0:
player_pos[1] -= PLAYER_SPEED
if keys[pygame.K_s] and player_pos[1] + PLAYER_SIZE < HEIGHT:
player_pos[1] += PLAYER_SPEED
# Spawn enemies over time
enemy_spawn_timer += 1
if enemy_spawn_timer >= enemy_spawn_interval:
spawn_enemy()
enemy_spawn_timer = 0
# Possibly spawn a power-up
if random.randint(0, 1000) < 5:
spawn_powerup()
# Move enemies
move_enemies()
# Check collisions
check_collisions()
# Handle power-up effect
if powerup_active:
PLAYER_SPEED = 8 # Increase speed as a power-up effect
powerup_timer -= 1
if powerup_timer <= 0:
powerup_active = False
PLAYER_SPEED = 5 # Reset speed
# Attack enemies
attack_enemies()
# Draw everything
draw_player()
draw_enemies()
draw_powerups()
draw_attack_radius()
draw_health_bar()
# Update the display
pygame.display.flip()
# Quit Pygame
pygame.quit()
Explanation
-
Initialization: We start by importing necessary modules and initializing Pygame. We set up the game window and define some basic colors.
-
Player Setup: We define the player's properties, including size, color, speed, health, and position.
-
Enemy Setup: Enemies have their own properties. Enemies will spawn at random edges of the screen and move towards the player.
-
Power-Up Setup: Power-ups appear randomly on the screen. When collected, they temporarily enhance the player's abilities.
-
Attack Mechanics: The player automatically attacks enemies within a certain range after a cooldown period.
-
Drawing Functions: We have functions to draw the player, enemies, power-ups, and the attack radius.
-
Game Loop: The main game loop handles event processing, movement, enemy spawning, collisions, and drawing.
-
Movement: Player movement is controlled with WASD keys.
-
Enemy Behavior: Enemies move towards the player by calculating the direction vector.
-
Collisions: We check for collisions between the player and enemies/power-ups to apply damage or activate power-ups.
-
Power-Up Effects: When a power-up is active, the player's speed increases for a duration.
-
Health Bar: We display the player's health at the top-left corner.
Running the Game
-
Install Pygame: Ensure you have Pygame installed. If not, install it using:
pip install pygame
-
Save the Code: Copy the code into a file named vampire_survivors_clone.py.
-
Run the Game:
python vampire_survivors_clone.py
Controls
- Move: Use the
W, A, S, D keys to move the player up, left, down, and right, respectively.
Gameplay Tips
-
Avoid Enemies: Keep moving to avoid being overwhelmed by enemies.
-
Collect Power-ups: Grab power-ups to gain temporary advantages.
-
Survive: The goal is to survive as long as possible against increasing waves of enemies.
Customization
Feel free to enhance the game by:
-
Adding Different Enemy Types: Introduce enemies with varying speeds and health.
-
Implementing Different Power-ups: Create power-ups that affect attack damage, range, or health regeneration.
-
Creating More Weapons: Add different attack patterns or projectile weapons.
Note
This is a simplified version of the game intended to resemble the core mechanics of Vampire Survivors. Full replication of all features would require a more extensive codebase.
Enjoy your game development journey!