Categories
Game Development

Space Invaders Clone Progress

I woke up late, which pretty much set the tone for the rest of the day. I didn’t spend nearly as much time working on the Space Invaders clone as I would have liked. Still, it was progress.

I ran into two bugs, one for each small update I made. The first bug appeared when I tried to replace one alien with multiple aliens. I basically swapped out a single pointer for a vector of pointers. I actually liked how easy it was to get the aliens to move back and forth since I didn’t even have to change the variable I used.

Unfortunately, my program would seg fault whenever the last alien was shot. It turned out that I was using a for loop when handling collisions, which incremented the iterator each time through. Well, if I erased the object the iterator references, then the new value of the iterator is one after that object. If it happens to equal vector::end(), then the loop should end.

Except that the iterator gets incremented, so now not only does it not equal vector::end(), it doesn’t refer to anything valid! I switched to using a while loop, and I only increment if I am not erasing, and it fixed the problem.

The second bug was simpler but more subtle. Yesterday I decided to add rows of aliens as well as columns. I found that if you shoot the rightmost or leftmost column of aliens, the rest of the aliens would sometimes move all the way over to the right or left, just like they should. The problem was that it didn’t happen consistently. Sometimes they would move that far, while other times they acted like the column of aliens I just shot still existed. What was going on?

After adding debugging statements and checking the position of the aliens, I found out what was happening. The code was actually working correctly. The problem was that I had an extra row of aliens that were too high to be seen on the screen. Doh! Once I moved the aliens down, I could see that they were moving as far left and as far right as they could as a unit.

I want people to pick up this game and think that it is as finished as any regular game they played. I know I will need a menu and an interface to continue or select levels, but I want to see what else I will need. I am basically looking through source code from various games, and I will document my adventures reading through source in a later post.