This post is a portion of Part 7 in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.

Images

Up until this point, we have been working predominately with static images. Whether they were graphical primitives or loaded bitmaps, the items appearing on screen have been solid, unmoving (mostly), and unchanging. In this part, we change that by introducing something called a Sprite.

Sprites, not the drink

Sprites refer to any graphical screen element that moves or changes (for the most part). Therefore, our Space Ship from part 5 could be considered a sprite, while the background (if there was one) would not be. In reality, what being a “sprite” means is having the complex framework that allows our game image to do something meaningful. A little bit we started this framework. In our Space Ship struct we had the ships position, bounding box, speed, and a few other attributes. That is already a good start for sprite support. A couple things that our ship didn’t have that we are going to look at adding in this section is a self contained image and animation.

Classy Structs

Like I mentioned above, the key to utilizing the power of sprites is to have well thought out code supporting your game actions. It is important for a sprite object to know where is it, what animation frame it is on, if it is collide-able, and what image represents itself on the screen. Normally that means that I would program a Sprite class and build all of the foundational functionality into it. As you will recall, however, I have vowed to do this whole series with as little “advanced” coding as possible. What this means is that we will be doing all of our work with structs. What structs lose in power, they gain in simplicity. Remember that if you would to tackle larger scale projects, it is a good idea to learn classes and OOP (object oriented programming), but for now, using structs will suffice.