Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

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.

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.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Pythongame-developmentModular ProgrammingAlien Invasion

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion