More Sprites... Lots More!

Okay, open up the fourth file, and let's get nuts!

Now we've added three new sprite classes. Heavy! Well, not really. They do the same thing as the PlayerSprite class, they're defining new "stickers" that we can place on our screen.

What are the differences? Well, they load up different images and, therefore, are different sizes. The area that each takes up is called its bounding box--that will help us figure out how they will react to each other. More on that later.

They also have different update functions. The enemy will move around in a given direction until it hits a boundary, then it will bounce back in the reverse direction. It will do this for every frame--keeping moving one way until it can't, then move the opposite. Dumb AI, but it works.

The other classes, the player and enemy "lasers", will go up or down respectively, for each frame, until they reach the screen boundary. That's it!

Now look at the main function. While things might seem like they're getting complicated, they're just more of the same. We create three enemies to start off with. Every 200 frames, we add another enemy. And for every group of sprites (players, enemies, and each of their sets of laser blasts) we do exactly what we did before. We erase. We ask them to update. We draw them again. And round and round we go.

Copyright Mike Edwards 2006-2009. All content available under the Creative Commons Attribution ShareAlike license, unless otherwise noted.