Frogs
Aliensunknown
abap
3 years ago
2.2 kB
7
Indexable
import pygame
import random
# Initialize pygame
pygame.init()
# Set the screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Set the caption
pygame.display.set_caption("Alien Invaders")
# Load the images
spaceship_img = pygame.image.load("spaceship.png")
alien_img = pygame.image.load("alien.png")
explosion_img = pygame.image.load("explosion.png")
# Set the game variables
player_x = SCREEN_WIDTH // 2
player_y = SCREEN_HEIGHT - 100
player_speed = 5
player_lives = 3
score = 0
aliens = []
alien_speed = 2
alien_frequency = 30
explosions = []
# Define the functions
def draw_player():
screen.blit(spaceship_img, (player_x, player_y))
def move_player(direction):
global player_x
if direction == "left":
player_x -= player_speed
elif direction == "right":
player_x += player_speed
if player_x < 0:
player_x = 0
elif player_x > SCREEN_WIDTH - spaceship_img.get_width():
player_x = SCREEN_WIDTH - spaceship_img.get_width()
def draw_aliens():
for alien in aliens:
screen.blit(alien_img, (alien["x"], alien["y"]))
def move_aliens():
for alien in aliens:
alien["y"] += alien_speed
if alien["y"] > SCREEN_HEIGHT:
player_lives -= 1
aliens.remove(alien)
def create_alien():
x = random.randint(0, SCREEN_WIDTH - alien_img.get_width())
y = -alien_img.get_height()
aliens.append({"x": x, "y": y})
def draw_explosions():
for explosion in explosions:
screen.blit(explosion_img, (explosion["x"], explosion["y"]))
def move_explosions():
for explosion in explosions:
explosion["frames_left"] -= 1
if explosion["frames_left"] == 0:
explosions.remove(explosion)
def create_explosion(x, y):
explosions.append({"x": x, "y": y, "frames_left": 30})
def detect_collisions():
global score
for alien in aliens:
alien_rect = pygame.Rect(alien["x"], alien["y"], alien_img.get_width(), alien_img.get_height())
for explosion in explosions:
explosion_rect = pygame.Rect(explosion["x"], explosion["y"], explosion_img.get_width(), explosion_img
800600210053030aliens:
alien1Editor is loading...