2D Game Dev – Part 3.2: Keyboard Input Part 2

This post is a portion of Part 3 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 will continue our look at keyboard input in Allegro 5. In the previous video, we did not have a way to track “holding” a button for continuous movement. You will see that using an enumeration and an array of boolean values that more in depth input it easy.

Source code 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 3, Tutorial
17 Comments » for 2D Game Dev – Part 3.2: Keyboard Input Part 2
  1. jim says:

    All tutorials are great! How did you learned allegro 5.0 so fast? Just from the manual?

  2. John says:

    Hey man, I appreciate Your tutorials alot. I have a half-term task to create a 2D game and Your works done here is really helpful for me.

    About the keyboard, I was wondering how to ged rid of the short “lag” when pushing 2nd button. I mean, when we hold down right arrow and during movement push e.g. upper arrow, the movement is not continuous, the squere stops for a moment. Will it be enought just to make our event depend on timer and FPS?

  3. John says:

    Yeah, I just wanted to write that I reached 4.2 tutorial and I found the answer ;)

  4. nocrej says:

    Hi Mike,

    I noticed that the video in this section is supposed to be Keyboard Input in Allegro 5 Part 2. But it’s showing Basic Mouse Input in Allegro 5 which I think belongs to the next section.

  5. Jaz says:

    I think this chapter is a huge jump for beginners for two reasons. The first is that you actually begin to learn at a more than basic level how the computer is processing all keyboard and mouse key presses/movements/clicks. I love the awe on eager faces at the realization that pressing a key and letting go are actually two different events. =) But more so I think it is important because for the first time programmers can see a reaction between what they are physically doing on keyboard and mouse, and what is happening on the screen. I can already see millions of games forming in my mind just after seeing this video. It really starts to build up from here on out for me. And I can honestly say that this is the best tutorial I have ever seen for it. Mike, you…are…amazing. Your students are incredibly lucky to have a teacher like you. You’re my idol. =) I really hope you make it to GDC. Good luck and THANK YOU. =D

  6. GD says:

    I got a problem….
    when ever i try to use al_draw_text(); in this program.. it builds perfectly but dont debug….Take a look at it…

  7. GD says:

    #include
    #include
    #include
    #include
    enum KEYS {UP,DOWN,LEFT,RIGHT};
    int main()
    {
    int x=1280;
    int y=720;
    int pos_x=x/2;
    int pos_y=y/2;
    bool keys[4]={false,false,false,false};
    bool done=false;
    ALLEGRO_DISPLAY *display=NULL;
    ALLEGRO_EVENT_QUEUE *ev_queue;
    al_init();
    display=al_create_display(x,y);
    al_init_font_addon();
    al_init_ttf_addon();
    al_draw_text(al_load_font(“alba.ttf”,62,NULL),al_map_rgb(255,255,255),pos_x,30,1,”GDROCKERS”);
    al_init_primitives_addon();
    al_install_keyboard();
    ALLEGRO_COLOR color = al_map_rgb(255,0,255);
    ev_queue=al_create_event_queue();
    al_register_event_source(ev_queue,al_get_keyboard_event_source());
    al_register_event_source(ev_queue,al_get_display_event_source(display));
    al_draw_filled_rectangle(pos_x-20,pos_y-20,pos_x+20,pos_y+20,color);
    al_flip_display();
    while(!done)
    {
    ALLEGRO_EVENT ev;
    al_wait_for_event(ev_queue, &ev);
    if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
    {
    switch(ev.keyboard.keycode)
    {
    case ALLEGRO_KEY_UP:
    keys[UP]=true;
    break;
    case ALLEGRO_KEY_DOWN:
    keys[DOWN]=true;
    break;
    case ALLEGRO_KEY_LEFT:
    keys[LEFT]=true;
    break;
    case ALLEGRO_KEY_RIGHT:
    keys[RIGHT]=true;
    break;
    }
    }
    else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
    done=true;
    else if(ev.type = ALLEGRO_EVENT_KEY_UP)
    {
    switch(ev.keyboard.keycode)
    {
    case ALLEGRO_KEY_UP:
    keys[UP]=false;
    break;
    case ALLEGRO_KEY_DOWN:
    keys[DOWN]=false;
    break;
    case ALLEGRO_KEY_LEFT:
    keys[LEFT]=false;
    break;
    case ALLEGRO_KEY_RIGHT:
    keys[RIGHT]=false;
    break;
    case ALLEGRO_KEY_ESCAPE:
    done=true;
    break;
    }
    }
    pos_y -= keys[UP] *20;
    pos_y += keys[DOWN] *20;
    pos_x -= keys[LEFT]*20;
    pos_x += keys[RIGHT]*20;
    al_draw_text(al_load_font(“alba.ttf”,62,NULL),al_map_rgb(255,255,255),pos_x,32,1,”GDROCKERS”);
    al_draw_filled_rectangle(pos_x-20,pos_y-20,pos_x+20,pos_y+20,color);
    al_flip_display();
    al_clear_to_color(al_map_rgb(0,0,0));
    }
    al_destroy_display(display);
    return 0;
    }

  8. Brock says:

    So, total idiot question. I know my initial thoughts are wrong, but I’d like for someone to explain why they’re wrong pretty quickly…
    It looks to me like the position update equations are handled within the same loop that is waiting for events… So why does it continue to run and update their position without additional events occurring? I understand the keyboard status is tracked, but it still looks to me like it shouldn’t reach those commands unless al_wait_for_event isn’t a true wait?

    • kprime says:

      I was wondering the same thing, could someone please clarify =]

    • Calcface says:

      I’m actually wondering the same thing. At least according to the manual(http://www.allegro.cc/manual/5/al_wait_for_event), I undestand that the function would wait until a new event comes, so the loop should get stuck there. Is it reciving any other type of event?
      It would be great if someone could clarify this.

  9. swapnil says:

    hey mike,i need an explanation over this thing..:the block when pressed down/left/right/up continuously it moves in the specified direction and moves across the specified window boundary.Also it returns back when u press the arrow keys accordingly…so where does this block go actually?i mean it should not cross the window boundary and enter into it rather stop whenever it encounters the boundary…any clues..thanxs for the great tut anyways..!

    • Mike says:

      It just goes off screen. The coordinates keep being changes and Allegro is smart enough to not render things that can’t be seen.

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>