2D Game Dev – Part 14.1 – Adding Game States to our Allegro 5 Game

This post is a portion of Part 14 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.

In this video we take the game we have been playing before and add different states. We new have a title screen, and a “lost” screen as well as the ability to play again if we want to.

Full source code can be found here.

 

Posted in 2D Game Dev, Allegro, C++, Code, Game Dev, Part 14, Tutorial
12 Comments » for 2D Game Dev – Part 14.1 – Adding Game States to our Allegro 5 Game
  1. Rwinz Cyruz says:

    Hi Mike,

    Your tutorial are awesome, and really helpful for me to complete my project.
    But, I am wondering about how to make the game system PAUSE and CONTINUE again from the last session. Could you please give me some clue how to accomplish this?
    thank so much before

    • Mike says:

      Pause is really no different than any other state. Basically, you just have a state where you don’t update (and depending on your desire) where you don’t draw anything

      • Rwinz Cyruz says:

        thanks for replying, Mike
        I am so glad that you notice my question

        In my code, I add two states to accomplish this, one is PAUSE and just put some bitmap on it, and other is CONTINUE, which update the bullet and explosion, is this effective?
        But I meet some problems on the sound.
        I can’t pause the ambience sound when it is in the pause state.
        Do you have a solution for this?

        and the last, I put a line transition on the title’s bitmap, but although the whole bitmap doesn’t loaded completely, look like we can give a command on it.
        I notice this when the bitmap is still loaded I try to press space bar, and when the bitmap has loaded completely it just directly go on to the game and the game look like has got some update…
        I hope you understand my question, and sorry if my english is bad…

        thank you so much

        • Mike says:

          You can do a 2 state system like you are using. There is nothing wrong with that.

          As far as pausing sound goes, in your “state machine” whenever you leave the playing state you can stop music. Whenever you enter the playing state you can start music. Then is works for pauses and game overs.

          I don’t understand the third question. Can you maybe rephrase it?

          • Rwinz Cyruz says:

            But if I use the stop function “al_stop_sample_instance(…)”, it seems that the sound is playing at the start again. What I want is the sound continues playing at the section when I press the pause button, can it be like that?

            The third question is about giving a transition to the title’s bitmap. It will be better if you can serve a tutorial about it. I think I just put it at the wrong place or section. I am confuse about it.

            Thank you so much before…

          • Mike says:

            You can “pause” the music by saving it’s position before stopping it, and then setting it’s position before starting it. http://www.allegro.cc/manual/5/al_get_sample_instance_position

            I still don’t know what you mean by “giving a transition to the title’s bitmap”. Are you talking about moving an image? Or are you trying to fade the “Super Space Shooter” title screen out?

          • Rwinz Cyruz says:

            Sorry to make you confused. Yes, I want to fade in and fade out the title screen before it gets into the playing state. But I think something is misplaced.

          • Rwinz Cyruz says:

            And thanks so much, Mike. I have successfully implemented the pause section for the bgm.

  2. Jacob says:

    hey mike i was implementing game states myself and i did not follow your videos on game state implementation but from what ive seen they are pretty similar, for some reason everything works the main menu, the game, and the gameover screen which actually has an option to where you can go back to the main menu and play another game. But one thing is bothering me i also have a pause implemented but when i change the gamestate to or from paused for some reason it terminates the program i have searched for a clue on how to fix this for a looong time (about three hours :) ) but still havent found anything wrong and i cant seem to find out why it is doing this so please help me.

  3. Jacob says:

    nm i found the contact me button

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>