Make a Top-down Shooter in python. Add randomly spawned power-ups that lower shoting cooldown, add shoters, make bullets larger, homing bullets (not stackable unlike other power-ups). Increase enemy spawn rate as the score is increasing. Press ESC to quit the game, also shoting should be automatic by holding the mouse button, F11 for fullscreen mode.
Requirements: Minimal setup no assets needed. One Fully working code.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here is the complete code for the top-down shooter with all the requested features:
python
import pygame
import math
import random
# Initialize Pygame
pygame.init()
# Screen settings
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Top-Down Shooter with Power-Ups")
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
# Clock for frame rate
clock = pygame.time.Clock()
# Player class
class Player:
def __init__(self):
self.x = SCREEN_WIDTH // 2
self.y = SCREEN_HEIGHT // 2
self.radius = 20
self.speed = 5
self.shooting_cooldown = 10 # Frames between shots
self.current_cooldown = 0
self.shooters = 1 # Number of bullets fired at once
self.bullet_size = 5 # Default bullet size
self.homing_bullets = False # Whether bullets home in on enemies
self.power_up_duration = 300 # Duration in frames (e.g., 5 seconds at 60 FPS)
self.active_power_ups = {} # Track active power-ups and their timers
def move(self, keys):
if keys[pygame.K_w] and self.y > self.radius:
self.y -= self.speed
if keys[pygame.K_s] and self.y < SCREEN_HEIGHT - self.radius:
self.y += self.speed
if keys[pygame.K_a] and self.x > self.radius:
self.x -= self.speed
if keys[pygame.K_d] and self.x < SCREEN_WIDTH - self.radius:
self.x += self.speed
def update_power_ups(self):
# Decrement timers for active power-ups
to_remove = []
for power_type in self.active_power_ups:
self.active_power_ups[power_type] -= 1
if self.active_power_ups[power_type] <= 0:
to_remove.append(power_type)
# Reset the power-up effect
if power_type == "cooldown":
self.shooting_cooldown = 10
elif power_type == "shooters":
self.shooters = 1
elif power_type == "size":
self.bullet_size = 5
elif power_type == "homing":
self.homing_bullets = False
for power_type in to_remove:
del self.active_power_ups[power_type]
def apply_power_up(self, power_type):
if power_type == "cooldown":
self.shooting_cooldown = max(2, self.shooting_cooldown - 2) # Lower cooldown, but not below 2
self.active_power_ups[power_type] = self.power_up_duration
elif power_type == "shooters":
self.shooters += 1
self.active_power_ups[power_type] = self.power_up_duration
elif power_type == "size":
self.bullet_size += 2 # Increase bullet size
self.active_power_ups[power_type] = self.power_up_duration
elif power_type == "homing":
self.homing_bullets = True
# Homing bullets are non-stackable, so reset the timer if already active
self.active_power_ups[power_type] = self.power_up_duration
def draw(self):
pygame.draw.circle(screen, BLUE, (int(self.x), int(self.y)), self.radius)
def shoot(self, target_x, target_y):
if self.current_cooldown <= 0:
# Calculate angle between shooters if there are multiple
angle_step = 0
if self.shooters > 1:
angle_step = 0.2 # Radians between shooters
for i in range(self.shooters):
# Calculate direction for each shooter
angle_offset = (i - (self.shooters - 1) / 2) * angle_step
angle = math.atan2(target_y - self.y, target_x - self.x) + angle_offset
dx = math.cos(angle) * 10 # Speed
dy = math.sin(angle) * 10
bullets.append(Bullet(self.x, self.y, dx, dy, self.bullet_size, self.homing_bullets))
self.current_cooldown = self.shooting_cooldown
else:
self.current_cooldown -= 1
# Bullet class
class Bullet:
def __init__(self, x, y, dx, dy, size, homing):
self.x = x
self.y = y
self.radius = size
self.dx = dx
self.dy = dy
self.speed = 10
self.homing = homing
self.homing_strength = 0.1 # How strongly bullets turn towards enemies
def update(self, enemies):
if self.homing and enemies:
# Find the nearest enemy
nearest_enemy = None
min_distance = float('inf')
for enemy in enemies:
distance = math.sqrt((enemy.x - self.x)**2 + (enemy.y - self.y)**2)
if distance < min_distance:
min_distance = distance
nearest_enemy = enemy
if nearest_enemy:
# Calculate direction to nearest enemy
target_x, target_y = nearest_enemy.x + nearest_enemy.width / 2, nearest_enemy.y + nearest_enemy.height / 2
dx = target_x - self.x
dy = target_y - self.y
dist = math.sqrt(dx**2 + dy**2)
if dist != 0:
# Adjust bullet direction towards the enemy
self.dx += dx / dist * self.homing_strength
self.dy += dy / dist * self.homing_strength
# Normalize the direction vector to maintain speed
length = math.sqrt(self.dx**2 + self.dy**2)
if length != 0:
self.dx = self.dx / length * self.speed
self.dy = self.dy / length * self.speed
self.x += self.dx
self.y += self.dy
def draw(self):
pygame.draw.circle(screen, WHITE, (int(self.x), int(self.y)), self.radius)
def off_screen(self):
return self.x < 0 or self.x > SCREEN_WIDTH or self.y < 0 or self.y > SCREEN_HEIGHT
# Enemy class
class Enemy:
def __init__(self, player):
self.x = random.choice([0, SCREEN_WIDTH])
self.y = random.choice([0, SCREEN_HEIGHT])
self.width = 20
self.height = 20
self.speed = 2
self.target = player # Move towards player
def update(self):
dx = self.target.x - self.x
dy = self.target.y - self.y
dist = math.sqrt(dx**2 + dy**2)
if dist != 0:
self.x += dx / dist * self.speed
self.y += dy / dist * self.speed
def draw(self):
pygame.draw.rect(screen, RED, (int(self.x), int(self.y), self.width, self.height))
def collides_with(self, other):
if isinstance(other, Player):
return (other.x > self.x and other.x < self.x + self.width and
other.y > self.y and other.y < self.y + self.height)
elif isinstance(other, Bullet):
# Check if bullet collides with enemy (circle-rect collision)
# Simplified: check if bullet center is inside enemy rectangle
return (other.x > self.x and other.x < self.x + self.width and
other.y > self.y and other.y < self.y + self.height)
# PowerUp class
class PowerUp:
def __init__(self, x, y, power_type):
self.x = x
self.y = y
self.radius = 10
self.power_type = power_type # "cooldown", "shooters", "size", "homing"
self.color = {
"cooldown": (0, 255, 0), # Green
"shooters": (0, 0, 255), # Blue
"size": (255, 255, 0), # Yellow
"homing": (255, 0, 255) # Purple
}.get(power_type, (255, 255, 255)) # Default white
def draw(self):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
def collides_with(self, player):
distance = math.sqrt((self.x - player.x)**2 + (self.y - player.y)**2)
return distance < self.radius + player.radius
# Main game function
def main():
global screen, SCREEN_WIDTH, SCREEN_HEIGHT
player = Player()
bullets = []
enemies = []
power_ups = []
score = 0
enemy_spawn_timer = 0
power_up_spawn_timer = 0
running = True
game_over = False
fullscreen = False
while running:
screen.fill(BLACK)
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if event.key == pygame.K_F11:
fullscreen = not fullscreen
if fullscreen:
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.FULLSCREEN)
else:
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
mouse_pressed = pygame.mouse.get_pressed()
if mouse_pressed[0] and not game_over: # Left mouse button held down
mouse_x, mouse_y = pygame.mouse.get_pos()
player.shoot(mouse_x, mouse_y)
if not game_over:
# Update player
player.move(keys)
player.update_power_ups()
# Spawn enemies periodically, with increasing rate based on score
enemy_spawn_timer += 1
spawn_rate = max(10, 60 - score // 2) # Decrease spawn interval as score increases, but not below 10
if enemy_spawn_timer > spawn_rate:
enemies.append(Enemy(player))
enemy_spawn_timer = 0
# Spawn power-ups periodically
power_up_spawn_timer += 1
if power_up_spawn_timer > 300: # Every 5 seconds (300 frames at 60 FPS)
power_up_types = ["cooldown", "shooters", "size", "homing"]
power_type = random.choice(power_up_types)
x = random.randint(50, SCREEN_WIDTH - 50)
y = random.randint(50, SCREEN_HEIGHT - 50)
power_ups.append(PowerUp(x, y, power_type))
power_up_spawn_timer = 0
# Update bullets
for bullet in bullets[:]:
bullet.update(enemies)
if bullet.off_screen():
bullets.remove(bullet)
# Update enemies and check collisions
for enemy in enemies[:]:
enemy.update()
# Check bullet collisions
for bullet in bullets[:]:
if enemy.collides_with(bullet):
enemies.remove(enemy)
bullets.remove(bullet)
score += 1
break
# Check player collision
if enemy.collides_with(player):
game_over = True
break # Exit the loop if game over
# Check power-up collisions
for power_up in power_ups[:]:
if power_up.collides_with(player):
player.apply_power_up(power_up.power_type)
power_ups.remove(power_up)
# Draw everything
player.draw()
for bullet in bullets:
bullet.draw()
for enemy in enemies:
enemy.draw()
for power_up in power_ups:
power_up.draw()
# Draw score
font = pygame.font.SysFont(None, 36)
score_text = font.render(f"Score: {score}", True, WHITE)
screen.blit(score_text, (10, 10))
# Draw active power-ups
power_up_texts = []
for power_type in player.active_power_ups:
power_up_texts.append(power_type)
if power_up_texts:
power_up_text = ", ".join(power_up_texts)
power_up_display = font.render(f"Active Power-ups: {power_up_text}", True, WHITE)
screen.blit(power_up_display, (10, 50))
if game_over:
font = pygame.font.SysFont(None, 48)
game_over_text = font.render("Game Over! Final Score: " + str(score), True, WHITE)
screen.blit(game_over_text, (SCREEN_WIDTH // 2 - 200, SCREEN_HEIGHT // 2))
pygame.display.flip()
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()
Explanation:
PowerUps: The PowerUp class creates power-ups with different effects. They spawn randomly and are collected by the player.
Player Enhancements: The player can collect power-ups that enhance shooting capabilities (lower cooldown, more shooters, larger bullets, homing bullets).
Enemy Spawn Rate: The spawn rate of enemies increases as the score increases, making the game progressively harder.
Controls: The game can be exited with ESC, and F11 toggles fullscreen mode. Shooting is automatic when the mouse button is held down.
This script provides a complete, minimal top-down shooter with all the requested features. It uses basic shapes for graphics and requires no external assets. The game mechanics are straightforward but provide a fun and challenging experience.