Game Development 10 min read

Build a Simple Alien Shooter Game with Python and Pygame – Step‑by‑Step Tutorial

This tutorial walks you through creating a basic alien‑invader shooter using Python's Pygame library, covering essential game libraries, class design for Game, Alien, Hero, Generator, and Rocket, collision detection, win/lose logic, and provides the complete source code with explanations.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Build a Simple Alien Shooter Game with Python and Pygame – Step‑by‑Step Tutorial

Game Development Python Libraries

Popular Python libraries for game development include Pygame , Pyglet , PyOpenGL , Arcade , and Panda3D . These libraries provide rendering, input handling, and physics support for building 2D and 3D games.

Basic Game Setup

The core of the game is the Game class, which initializes Pygame, creates the display, and manages the main loop at 60 FPS. It tracks aliens, rockets, and the player's state.

import pygame

class Game:
    screen = None
    aliens = []
    rockets = []
    lost = False

    def __init__(self, width, height):
        pygame.init()
        self.width = width
        self.height = height
        self.screen = pygame.display.set_mode((width, height))
        self.clock = pygame.time.Clock()
        done = False
        hero = Hero(self, width / 2, height - 20)
        generator = Generator(self)
        while not done:
            if len(self.aliens) == 0:
                self.displayText("VICTORY ACHIEVED")
            pressed = pygame.key.get_pressed()
            if pressed[pygame.K_LEFT]:
                hero.x -= 2
            elif pressed[pygame.K_RIGHT]:
                hero.x += 2
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = True
                if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and not self.lost:
                    self.rockets.append(Rocket(self, hero.x, hero.y))
            pygame.display.flip()
            self.clock.tick(60)
            self.screen.fill((0, 0, 0))
            for alien in self.aliens:
                alien.draw()
                alien.checkCollision(self)
                if alien.y > height:
                    self.lost = True
                    self.displayText("YOU DIED")
            for rocket in self.rockets:
                rocket.draw()
            if not self.lost:
                hero.draw()

    def displayText(self, text):
        pygame.font.init()
        font = pygame.font.SysFont('Arial', 50)
        textsurface = font.render(text, False, (44, 0, 62))
        self.screen.blit(textsurface, (110, 160))

Alien Class

The Alien class stores position, size, and rendering logic. Each frame the alien moves downward slightly, and it checks for collisions with rockets.

class Alien:
    def __init__(self, game, x, y):
        self.x = x
        self.game = game
        self.y = y
        self.size = 30

    def draw(self):
        pygame.draw.rect(self.game.screen, (81, 43, 88), pygame.Rect(self.x, self.y, self.size, self.size))
        self.y += 0.05

    def checkCollision(self, game):
        for rocket in game.rockets:
            if (rocket.x < self.x + self.size and
                rocket.x > self.x - self.size and
                rocket.y < self.y + self.size and
                rocket.y > self.y - self.size):
                game.rockets.remove(rocket)
                game.aliens.remove(self)

Hero Class

The player‑controlled Hero is drawn as a small rectangle and moves left or right based on arrow keys.

class Hero:
    def __init__(self, game, x, y):
        self.x = x
        self.game = game
        self.y = y

    def draw(self):
        pygame.draw.rect(self.game.screen, (210, 250, 251), pygame.Rect(self.x, self.y, 8, 5))

Generator Class

The Generator populates the upper half of the screen with a grid of aliens using configurable margins and spacing.

class Generator:
    def __init__(self, game):
        margin = 30
        width = 50
        for x in range(margin, game.width - margin, width):
            for y in range(margin, int(game.height / 2), width):
                game.aliens.append(Alien(game, x, y))

Rocket Class

Rockets are created when the player presses the space bar. They travel upward at a constant speed and are removed upon collision with an alien.

class Rocket:
    def __init__(self, game, x, y):
        self.x = x
        self.y = y
        self.game = game

    def draw(self):
        pygame.draw.rect(self.game.screen, (254, 52, 110), pygame.Rect(self.x, self.y, 2, 4))
        self.y -= 2

Running the Game

When the script is executed, an instance of Game with a 600×400 window is created, launching the alien shooter.

if __name__ == '__main__':
    game = Game(600, 400)

Win condition: all aliens are destroyed, displaying "VICTORY ACHIEVED". Lose condition: any alien reaches the bottom of the screen, displaying "YOU DIED".

Game screenshot
Game screenshot
Alien rendering
Alien rendering
Hero movement animation
Hero movement animation
Alien class illustration
Alien class illustration
Hero sprite
Hero sprite
Rocket sprite
Rocket sprite
Pythongame developmentcode-exampletutorialPygameAlien Shooter
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.