Today I worked on a Space Invaders game. This version includes sound effects, score tracking, multiple enemies, and a simple bullet system. I also added a background image and registered custom shapes for the player and invaders.
Screen Setup
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Space Invaders")
wn.bgpic("space_invaders_background.gif")
The game window now has a title and a background image. This helps define the play area visually and makes the sprites easier to see.
Border Drawing
border_pen.setposition(-300, -300)
for side in range(4):
border_pen.fd(600)
border_pen.lt(90)
A simple white border outlines the playable space. It keeps the movement of the player and enemies contained and gives the game a clear frame.
Score System
score = 0
score_pen.write("Score: %s" % score)
The score is displayed in the upper‑left corner. It increases whenever the player destroys an enemy. The score text is cleared and rewritten each time it changes.
Player Setup
player.shape("player.gif")
player.setposition(0, -250)
player.setheading(90)
The player uses a custom GIF shape and starts near the bottom of the screen. Movement is limited to left and right, which matches the classic Space Invaders style.
Enemy Setup
x = random.randint(-200, 200)
y = random.randint(100, 250)
enemy.setposition(x, y)
A group of enemies is created and placed randomly near the top of the screen. They move horizontally as a formation. When one reaches a boundary, all enemies shift downward and reverse direction.
Bullet System
if bulletstate == "ready":
os.system("afplay laser.wav&")
bulletstate = "fire"
The bullet uses a small triangle shape and fires straight upward. It can only be fired again once it either hits an enemy or leaves the top of the screen. A laser sound plays each time the bullet is launched.
Collision Detection
distance = math.sqrt((t1.xcor()-t2.xcor())**2 + (t1.ycor()-t2.ycor())**2)
if distance < 15:
return True
Collisions are checked using simple distance calculations. When the bullet hits an enemy, an explosion sound plays, the enemy resets to a new position, and the score increases.
Main Game Loop
x = enemy.xcor()
x += enemyspeed
enemy.setx(x)
The loop moves each enemy, handles direction changes, updates the bullet, checks for collisions, and updates the score. If an enemy collides with the player, both sprites are hidden and the game ends.

Leave a Reply