Space Invaders Game

Created on June 27th, 2014. Built on the MacBook Air (so it currently plays really slowly on the Raspberry Pi).

This was our biggest challenge yet. Can we recreate the classic Space Invaders game? Unlike the other games, we built this one directly on my laptop.  Devon was sitting on my lap, typing out variable names one character at a time (God give me patience!).

He kept wanting to be able to control the alien ship, but I explained to him how the game works and how the aliens will come down and we’ll have to fight them off. He really wanted it to be two player though so he convinced me to add another space ship. I’m glad he did! We’ve had so much fun playing this game collaboratively so even if one of us dies, the other can keep going. I never would have thought of doing that. 🙂

I added some extra touches and styled out the sprites a bit more when he was asleep. I had a lot of fun building this one.

This one has a lot of complexity and when we moved it to Devon’s Raspberry Pi, it barely ran… so it probably needs some rework there.

Here’s the stage code.

stage_code

We again created multiple costumes for various stages of the game.

stage_costumes

We have a lot of sprites, but started with just one alien and one ship and then later added the others.

sprites

The ship code is pretty straightforward for handling movement and if the game is over or not.

ship_code

Each ship also has a rocket they can fire. It starts from the location of the ship and moves upward until it hits an alien or the edge of the screen. We used purple there because Aria, my 3 year old, insisted on it.

It’s funny how we find and fix various bugs. When we first started, even ships which had been blown up and were not currently visible could still fire missiles. We added a check against the number of lives left for the ship before firing.

ship1_fired_code

We set it up to have three lives per ship, but displaying the variable on the page got in the way so we created an indicator to show how many lives the ship has left.

lives_code
lives_costumes

If a ship gets hit by an alien shot, we display a little fire indicator.

ship_on_fire_code

Speaking of aliens, lets dive right in to the code there.

alien_code_1
alien_code_2

We created a fun little cycle where the aliens move right, down, left and down again. We keep track of he current direction the aliens are moving and adjust things as needed. We also use broadcast messages to let the system know which missiles hit. For a fun twist, we change the color each time an alien ship is hit until they reach the “alien hits before exploding” variable. If the alien reaches the bottom of the screen or touches one of the ships, the game is over.

The alien bullets are interesting as well because of the random delay we added. We found a need for a second if check because even if the alien ship was no longer visible, they could fire seconds later.

a1_bullets_code

As mentioned above, the current version of this game runs really slow on the Raspberry Pi. The best place for improvement is probably in the alien code. Instead of each one keeping track of movements, we could link them together a bit. We might also want to change the glide method to a simple movement per cycle.

Want to play? Download the full Scratch file for Space Invaders here.

Thanks for joining our fun! If you learned something, feel free to contribute to Devon’s bitcoin fund.

After finishing this post, Corinne reminded me of this video she took while we were building Space Invaders. This is a typical glimpse into what our time together building games looks like.

While growing up, I watched my dad program and I know some of this is sinking in. If he continues to enjoy building games, hopefully some day he’ll be building them on his own.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s