Creating a Galaga Game in Python

Here's a basic implementation of a Galaga-style game using Python and the Pygame library. This example includes a player ship, enemies, and the ability to shoot bullets.

Note: Make sure you have Pygame installed. You can install it using pip:

pip install pygame

Python Code:


import pygame
import random

# Initialize Pygame
pygame.init()

# Set up the game window
width = 800
height = 600
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Galaga")

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

# Player
player_width = 50
player_height = 50
player_x = width // 2 - player_width // 2
player_y = height - player_height - 10
player_speed = 5

# Bullet
bullet_width = 5
bullet_height = 10
bullet_speed = 7
bullets = []

# Enemy
enemy_width = 40
enemy_height = 40
enemy_speed = 2
enemies = []

# Create player
player = pygame.Rect(player_x, player_y, player_width, player_height)

# Game loop
clock = pygame.time.Clock()
running = True
score = 0

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_SPACE:
                bullet = pygame.Rect(player.centerx - bullet_width // 2, player.top, bullet_width, bullet_height)
                bullets.append(bullet)

    # Move player
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player.left > 0:
        player.x -= player_speed
    if keys[pygame.K_RIGHT] and player.right < width:
        player.x += player_speed

    # Move bullets
    for bullet in bullets[:]:
        bullet.y -= bullet_speed
        if bullet.bottom < 0:
            bullets.remove(bullet)

    # Create enemies
    if len(enemies) < 5 and random.randint(1, 60) == 1:
        enemy = pygame.Rect(random.randint(0, width - enemy_width), 0, enemy_width, enemy_height)
        enemies.append(enemy)

    # Move enemies
    for enemy in enemies[:]:
        enemy.y += enemy_speed
        if enemy.top > height:
            enemies.remove(enemy)

    # Check for collisions
    for enemy in enemies[:]:
        if player.colliderect(enemy):
            running = False
        for bullet in bullets[:]:
            if bullet.colliderect(enemy):
                bullets.remove(bullet)
                enemies.remove(enemy)
                score += 10

    # Clear the screen
    window.fill(BLACK)

    # Draw player
    pygame.draw.rect(window, WHITE, player)

    # Draw bullets
    for bullet in bullets:
        pygame.draw.rect(window, WHITE, bullet)

    # Draw enemies
    for enemy in enemies:
        pygame.draw.rect(window, RED, enemy)

    # Draw score
    font = pygame.font.Font(None, 36)
    score_text = font.render(f"Score: {score}", True, WHITE)
    window.blit(score_text, (10, 10))

    # Update the display
    pygame.display.flip()

    # Control the game speed
    clock.tick(60)

# Quit the game
pygame.quit()
  

Explanation:

  1. We start by importing the necessary libraries: Pygame for game development and random for generating random enemy positions.
  2. We initialize Pygame and set up the game window.
  3. We define colors and create rectangles for the player, bullets, and enemies.
  4. The main game loop handles events, moves game objects, checks for collisions, and updates the display.
  5. Player movement is controlled using the left and right arrow keys, and shooting is done with the spacebar.
  6. Enemies are randomly generated at the top of the screen and move downwards.
  7. Collisions between bullets and enemies increase the score, while collisions between the player and enemies end the game.
  8. The game runs at 60 frames per second, controlled by the clock.tick(60) call.

This basic implementation provides a foundation for a Galaga-style game. You can expand on this by adding graphics, sound effects, power-ups, different enemy types, and more complex gameplay mechanics to make it more engaging and closer to the original Galaga game.