2D Game Dev – Part 5.2: Our First Game – Timing and Movement

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

Building off of the last video, we now are adding timing and keyboard inputs to our game

Full source can be found here.

Notice that the source code is slightly different from the video? Here is why.

Posted in 2D Game Dev, Allegro, C++, Code, Game Dev, Part 5, Tutorial
13 Comments » for 2D Game Dev – Part 5.2: Our First Game – Timing and Movement
  1. John says:

    I don’t know if you’ll see this any time soon, but I keep trying to compile and VS 2010 keeps giving me a popup window that says “Unhandled exception at 0x0f7d4a36 in FBPgame.exe: 0xC0000005: Access violation reading location 0×00000000.”. In the error log it lists about 30 dll’s with the error message “Cannot find or open the PDB file”

    Any idea what in god’s name is happening?

  2. John says:

    I’ve already gotten rid of my code. I just redid the whole thing following you step by step. I’ll try doing things differently sometime when I can find a way to use allegro with a different IDE. I don’t like VC++ too much. Anyway thanks for offering help, and thanks for the videos. You’re the only one that explains it this well

  3. Mario says:

    You can install it on Dev-C, its really easy
    1.-Download “allegro-5.0.5-1cmw.DevPak”
    2.-Execute the file and clic next,next…
    3.-Done
    4.-Create a new project “Allegro5 aplication”

    I am using Dev-C and all runs perfect :)

  4. Nathan says:

    once i entered the if(ev.type == ALLEGRO_EVENT_TIMER)code my game loaded but no movement, i dont get an error or anything just no movement

    can i send u my code and can you take a look please?

  5. Zepher53 says:

    I’m kinda having a similar issue just like the person above, the window shows for a few seconds then it disappears with all the .dlls not executing which they never do but I never had a problem and I know for sure that I typed everything right. It could just be the laptop I’m on or the compiler I guess. Im using VC++ which I never had any problems with. here is the .cpp i believe, thanks for the tutorials on this, I really enjoy and study this every day for hours: http://www.mediafire.com/?qb55r5e84mn2vtd

    I left my email just in case, thanks Mike.

  6. Zepher53 says:

    Nevermind, I found out that it might have been a missing } or too many of them. I think thats what the problem was. I made FPS a const int since i forgot too and then I rewrote all the if statements in a different order of: ALLEGRO_EVENT_KEY_DOWN, ALLEGRO_EVENT_KEY_UP, ALLEGRO_EVENT_DISPLAY_CLOSE, ALLEGRO_EVENT_TIMER, and lastly the(redraw && al_is_event_queue_empty(event_queue)

    hope this works for anyone having a problem with the window not showing up. I added the al_rest() function and noticed that the window stayed longer so it could have been something but idk, I just did the above and it worked.

  7. lars says:

    stupid stupid me, i was wondering why my ship didn’t move… i was going over my text for two weeks and let my IT teacher have a few looks but we both could’t figure it out. today i opened the project and my eye suddenly caught the problem, i had typed “if(ev.type == ALLEGRO_KEY_DOWN)” istead of “if(ev.type == ALLEGRO_EVENT_KEY_DOWN)”. what a rookie mistake. luckely i type al my own code so i remember the comments better. and the funiest part was that from the beginning i knew something was wrong with the if statement inside the gameloop. but still needed 2 weeks to figure it out: it was waiting for the down key and not for a key to go down :P

  8. Brutal says:

    Can’t you just do

    bool keys[5] = false; to define them all at once instead of going through them all?

  9. Cody says:

    The answer to that question is no. If you declare the array
    bool keys[5] = false; wouldn’t build first of all. Secondly, it’s good programming practice to DEFAULT ALL VALUES. I believe if you did the code
    bool keys[5] = {false}; which I can only assume you are asking, it will only assign the first value to false as default, IIRC. Anyway, it’s best just to follow the instructor’s examples if you aren’t sure.

  10. Cody says:

    I may want to add that bool keys[5] is an array, which is actually 5 different locations in memory, starting with 0 to 4. if you just set the first the others may have values in them like this
    bool keys[5] = {false,NULL,NULL,NULL,NULL};
    you could also do
    bool keys[5] = {false, 0, 0, 0, 0}; and get a similar result.
    when you allocate memory for an array, sometimes there are values there, that you may not know about. it’s best just to format it to something that you KNOW is there, rather than just taking a chance at it. i have built an array in the past and output it to the console and found some strange numbers just sitting around in there, like -80000000000 or something crazy. Which I had to spend hours trying to debug to get my program to run.

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>