Game Development 15 min read

Modular Alien Invasion Game Tutorial with Python and Pygame

This tutorial walks through building a complete Alien Invasion arcade game in Python using Pygame, explaining the modular design of settings, ship, alien, bullet, button, game statistics, and scoreboard modules, and demonstrates how to implement game loops, event handling, collision detection, and scoring logic.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Modular Alien Invasion Game Tutorial with Python and Pygame

This article presents a step‑by‑step tutorial for creating the classic "Alien Invasion" arcade game in Python with the Pygame library, focusing on a clean, modular architecture that separates configuration, sprites, and game logic into distinct files.

The main entry point run_game() initializes Pygame, creates a Settings instance, sets up the display, and instantiates the ship, bullet group, alien group, and UI elements such as the Play button and scoreboard. The game loop continuously processes events, updates the ship, bullets, and aliens, and refreshes the screen.

def run_game():
"""Initialize background settings"""
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption('Alien Invasion')
ship = Ship(ai_settings, screen)
bullets = Group()
aliens = Group()
gf.creat_fleet(ai_settings, screen, ship, aliens)
stats = GameStats(ai_settings)
sb = Scoreboard(ai_settings, screen, stats)
play_button = Button(ai_settings, screen, "Play")
while True:
gf.check_events(ai_settings, screen, ship, aliens, bullets, stats, play_button, sb)
if stats.game_active:
ship.update()
gf.update_bullet(ai_settings, screen, ship, aliens, bullets, stats, sb)
gf.update_aliens(ai_settings, screen, ship, aliens, stats, bullets, sb)
gf.update_screen(ai_settings, screen, ship, aliens, bullets, stats, play_button, sb)

Key gameplay functions are defined in game_function.py , such as fire_bullet() which creates new Bullet objects while respecting the maximum allowed on screen, and check_events() which handles keyboard and mouse input, including starting the game when the Play button is clicked.

def fire_bullet(ai_settings, screen, ship, bullets, stats):
"""Fire a bullet if limit not reached"""
for i in range(int(stats.level / 5) + 1):
if len(bullets) < ai_settings.bullet_allowed:
new_bullet = Bullet(ai_settings, screen, ship)
bullets.add(new_bullet)

The supporting modules settings.py , ship.py , alien.py , bullet.py , button.py , game_stats.py , and scoreboard.py each encapsulate related data and behavior, illustrating the benefits of modular programming for readability, testing, and future extensions.

Throughout the guide the author reflects on the learning experience, emphasizing the value of modular thinking, class design, and extensive use of functions to manage game complexity.

Pythongame developmentPygameModular ProgrammingAlien Invasion
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

login 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.