Today I added several improvements to my SpaceWar project. The most noticeable change is a new explosion effect built from a simple particle system. I also added a starfield background image, set a window title, and adjusted the shapes of the player and missile so they read better on screen.
Sprite Class
class Sprite(turtle.Turtle):
def move(self):
self.fd(self.speed)
# boundary checks...
The Sprite class remains the base for all moving objects. It handles forward movement, turning when hitting the screen boundaries, and basic collision detection. Since every other class inherits from it, the movement behavior stays consistent across the player, enemies, allies, missile, and particles.
Player Class
class Player(Sprite):
def accelerate(self):
self.speed += 1
The player ship now uses a stretched triangle shape, which makes its orientation clearer. It still turns in 45 degree increments and can speed up or slow down. The new background image helps the white player ship stand out, which makes it easier to track during fast movement.
Enemy Class
class Enemy(Sprite):
def __init__(...):
self.setheading(random.randint(0,360))
Enemies move at a steady speed and start with random headings. With many of them active at once, they create constant motion across the play area. Colliding with one lowers the score and triggers a sound effect, so the player has to stay alert.
Ally Class
class Ally(Sprite):
def move(self):
self.fd(self.speed)
# turns left on boundary
Allies move faster than enemies and turn in the opposite direction when they hit a boundary. Multiple allies move around the screen at once, and hitting one with a missile lowers the score.
Missile Class
class Missile(Sprite):
def fire(self):
os.system("afplay laser.mp3&")
The missile now uses a smaller stretched shape that looks more like a projectile. When fired, it plays a laser sound and travels straight ahead until it hits something or leaves the play area.
Particle Class
class Particle(Sprite):
def explode(self, x, y):
self.goto(x, y)
self.frame = 1
This is the new feature. Each particle is a tiny sprite that moves outward for a short number of frames before disappearing. When the missile hits an enemy, all particles are triggered at the impact point.
Game Class
class Game():
def show_status(self):
self.pen.write("Score: %s" % self.score)
The Game class draws the border and displays the score. The score updates immediately whenever the player hits or collides with something.
Main Game Loop
for enemy in enemies:
if missile.is_collision(enemy):
os.system("afplay explosion.mp3&")
game.score += 100
The loop updates all sprites, checks for collisions, plays sounds, adjusts the score, moves sprites to new positions, and triggers the explosion effect when needed. The particle system runs alongside everything else without interrupting the flow of the game.

Leave a Reply