Building Space Invaders with Python Turtle Graphics
One of my favorite projects has been recreating the classic Space Invaders game using Python’s Turtle Graphics library. In this post, I’ll walk through the development process and share what I learned.
Why Space Invaders?
Space Invaders is a perfect project for intermediate programmers because it involves:
- Object-oriented programming
- Game loops and event handling
- Collision detection
- State management
- User input processing
The Tech Stack
Language: Python 3
Graphics Library: Turtle Graphics
Repository: github.com/DeanJ93/space_invaders
Project Architecture
Core Components
- Player Ship: Controlled by keyboard input
- Enemies: Multiple alien ships in formation
- Bullets: Projectiles fired by both player and enemies
- Collision Detection: Detecting hits between objects
- Score System: Tracking player performance
Game Loop
The main game loop handles:
# Pseudocode structure
while game_running:
update_player_position()
update_enemies()
update_bullets()
check_collisions()
update_score()
refresh_screen()
Key Challenges
1. Smooth Movement
Getting smooth, responsive movement with Turtle Graphics required:
- Using
tracer(0)to disable automatic screen updates - Manually calling
update()after position changes - Implementing velocity-based movement
2. Collision Detection
Implementing accurate collision detection between objects:
def is_collision(obj1, obj2, distance):
dx = obj1.xcor() - obj2.xcor()
dy = obj1.ycor() - obj2.ycor()
return (dx ** 2 + dy ** 2) ** 0.5 < distance
3. Enemy Movement Patterns
Creating the classic Space Invaders movement pattern where enemies:
- Move horizontally across the screen
- Drop down when reaching the edge
- Increase speed as their numbers decrease
4. Performance Optimization
Turtle Graphics isn’t the fastest library, so optimization was crucial:
- Limiting the number of bullets on screen
- Efficient collision detection
- Removing destroyed objects from memory
What I Learned
Object-Oriented Design
This project reinforced OOP principles:
- Creating classes for different game entities
- Encapsulating behavior within objects
- Managing object lifecycles
Game Development Concepts
- Game state management
- Frame rate considerations
- User input handling
- Balancing difficulty
Python Specifics
- Working with Turtle Graphics library
- Event-driven programming with
onkey() - List comprehensions for managing multiple objects
- Python’s
randommodule for enemy behavior
Code Highlights
Player Class
class Player:
def __init__(self):
self.player = turtle.Turtle()
self.player.speed(0)
self.player.shape("triangle")
self.player.color("blue")
self.player.penup()
self.player.setheading(90)
self.player.goto(0, -250)
def move_left(self):
x = self.player.xcor()
if x > -280:
self.player.setx(x - 15)
def move_right(self):
x = self.player.xcor()
if x < 280:
self.player.setx(x + 15)
Bullet System
class Bullet:
def __init__(self, x, y):
self.bullet = turtle.Turtle()
self.bullet.speed(0)
self.bullet.shape("circle")
self.bullet.color("yellow")
self.bullet.penup()
self.bullet.goto(x, y)
self.state = "fire"
def move(self):
if self.state == "fire":
y = self.bullet.ycor()
self.bullet.sety(y + 20)
Future Improvements
Ideas for enhancing the game:
- Power-ups: Add special abilities and bonuses
- Multiple Levels: Increasing difficulty with each level
- Sound Effects: Adding audio for shooting and explosions
- High Score System: Persistent leaderboard
- Better Graphics: Custom sprites instead of Turtle shapes
Try It Yourself
The full source code is available on GitHub. Feel free to:
- Clone the repository
- Experiment with the code
- Add your own features
- Share your improvements
Conclusion
Building Space Invaders was a fantastic learning experience that combined programming fundamentals with game development concepts. It’s a project I recommend to anyone looking to:
- Practice Python and OOP
- Learn basic game development
- Build something fun and interactive
- Have a portfolio piece to showcase
The best part? It’s genuinely fun to play once you’ve built it!
Have you built any games with Python? What was your experience? Let me know in the comments!
Check out the live project on GitHub and give it a try!