In this game, you shoot down enemies by clicking “E”. Once you shoot down a couple, you go on to the next round. Each round is harder than the round before it.

In order to play the game, copy the code below and paste it into your python file. After that, run the code and enjoy. (Also make sure you have pygame installed)

Copy the code for FREE!:

import pygame
import random

# Initialize Pygame
pygame.init()

# Set up the game window
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Shooter Game")

# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

# Player attributes
player_size = 50
player_x = screen_width // 2 - player_size // 2
player_y = screen_height - player_size - 10
player_speed = 5

# Enemy attributes
enemy_size = 50
enemy_x = random.randint(0, screen_width - enemy_size)
enemy_y = -enemy_size
enemy_speed = 2

# Laser attributes
laser_width = 2
laser_height = 10
laser_speed = 5
player_lasers = []
enemy_lasers = []

# Game variables
player_health = 100
current_round = 1
enemies_per_round = 3
enemies_killed = 0
game_over = False

# Clock to control the frame rate
clock = pygame.time.Clock()

# Function to generate enemy lasers
def generate_enemy_lasers():
for enemy in enemies:
if random.randint(0, 100) < 3:
laser_x = enemy['x'] + enemy_size // 2 - laser_width // 2
laser_y = enemy['y'] + enemy_size
enemy_lasers.append(pygame.Rect(laser_x, laser_y, laser_width, laser_height))

# Create enemies list
enemies = []

# Game loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_e:
# Shoot laser
laser_x = player_x + player_size // 2 - laser_width // 2
laser_y = player_y
player_lasers.append(pygame.Rect(laser_x, laser_y, laser_width, laser_height))

# Update player position
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_d] and player_x < screen_width - player_size:
player_x += player_speed

# Move and update lasers
for laser in player_lasers:
laser.y -= laser_speed
if laser.y < 0:
player_lasers.remove(laser)

for laser in enemy_lasers:
laser.y += laser_speed
if laser.y > screen_height:
enemy_lasers.remove(laser)

# Update enemy position
enemy_y += enemy_speed

# Check if enemy passes the player
if enemy_y > screen_height:
enemy_x = random.randint(0, screen_width - enemy_size)
enemy_y = -enemy_size
player_health -= 10

# Check for collision between lasers and enemies
for laser in player_lasers:
if laser.colliderect(pygame.Rect(enemy_x, enemy_y, enemy_size, enemy_size)):
player_lasers.remove(laser)
enemies_killed += 1
if enemies_killed == enemies_per_round:
current_round += 1
enemies_per_round += 2
enemies_killed = 0
enemy_speed += 1
player_health = 100
enemy_x = random.randint(0, screen_width - enemy_size)
enemy_y = -enemy_size

# Check for collision between enemy lasers and player
for laser in enemy_lasers:
if laser.colliderect(pygame.Rect(player_x, player_y, player_size, player_size)):
enemy_lasers.remove(laser)
player_health -= 5
if player_health <= 0:
game_over = True

# Clear the screen
screen.fill(WHITE)

# Draw the player
pygame.draw.rect(screen, GREEN, (player_x, player_y, player_size, player_size))

# Draw the enemy
pygame.draw.rect(screen, RED, (enemy_x, enemy_y, enemy_size, enemy_size))

# Draw player health
pygame.draw.rect(screen, GREEN, (10, 10, player_health, 10))

# Draw round information
font = pygame.font.Font(None, 30)
round_text = font.render(f"Round: {current_round}", True, RED)
screen.blit(round_text, (screen_width - round_text.get_width() - 10, 10))

# Draw lasers
for laser in player_lasers:
pygame.draw.rect(screen, GREEN, laser)

for laser in enemy_lasers:
pygame.draw.rect(screen, RED, laser)

# Update the display
pygame.display.flip()

# Generate enemy lasers
generate_enemy_lasers()

# Check if the game is over
if game_over:
running = False

# Limit the frame rate
clock.tick(60)

# Game over message
font = pygame.font.Font(None, 50)
game_over_text = font.render("Game Over", True, RED)
screen.blit(game_over_text, (screen_width // 2 - game_over_text.get_width() // 2, screen_height // 2))

# Update the display
pygame.display.flip()

# Wait for a few seconds before quitting
pygame.time.delay(3000)

# Quit the game
pygame.quit()

Leave a Reply

Your email address will not be published. Required fields are marked *