3 Keyboard Input

Video

http://youtu.be/rJ7OvdoAz-A


Table of Contents

  1. Introduction
  2. Initializing Allegro
  3. Keyboard Input
  4. Drawing Graphics
  5. Playing Sound
  6. Drawing Text
  7. Regulating FPS
  8. Enumerations and Random Numbers
  9. Bounding Box Collision Detection
  10. Planning the Game
  11. Super Basic Game Structure
  12. Wrapping up

Downloads

Download entire guide as ODT


 

Part 3 – Keyboard Input

Allegro Code:

key[KEY_(name)]

For example:

int main()
{
    System myAllegroGame;
    Player player;
    bool done = false;
    BITMAP *buffer = create_bitmap( 640, 480 );

    while ( !done )
    {
        /* Input */
        if ( key[KEY_ESC] )
            done = true;
        if ( key[KEY_F5] )
            myAllegroGame.ToggleFullscreen();

        if ( key[KEY_UP] )
            player.Move( UP );
        else if ( key[KEY_DOWN] )
            player.Move( DOWN );
        if ( key[KEY_LEFT] )
            player.Move( LEFT );
        else if ( key[KEY_RIGHT] )
            player.Move( RIGHT );

        /* Draw functions */
        blit( buffer, screen, 0, 0, 0, 0, 640, 480 );
        release_screen();
        clear_bitmap( buffer );
    }

    /* Free up memory or ELSE!! */
    destroy_bitmap( buffer );

    return 0;
}
END_OF_MAIN();

Small note:

if you have else if( key[KEY_LEFT] ) instead of just if( key[KEY_LEFT] ), then it will make it so you cannot move diagonally. The way I have it above, the player can move vertically AND horizontally in the same game cycle.

Allegro Keys

From the Allegro Documentation here:

http://alleg.sourceforge.net/latestdocs/en/alleg006.html

KEY_A ... KEY_Z,
KEY_0 ... KEY_9,
KEY_0_PAD ... KEY_9_PAD,
KEY_F1 ... KEY_F12,

KEY_ESC, KEY_TILDE, KEY_MINUS, KEY_EQUALS,
KEY_BACKSPACE, KEY_TAB, KEY_OPENBRACE, KEY_CLOSEBRACE,
KEY_ENTER, KEY_COLON, KEY_QUOTE, KEY_BACKSLASH,
KEY_BACKSLASH2, KEY_COMMA, KEY_STOP, KEY_SLASH,
KEY_SPACE,

KEY_INSERT, KEY_DEL, KEY_HOME, KEY_END, KEY_PGUP,
KEY_PGDN, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN,

KEY_SLASH_PAD, KEY_ASTERISK, KEY_MINUS_PAD,
KEY_PLUS_PAD, KEY_DEL_PAD, KEY_ENTER_PAD,

KEY_PRTSCR, KEY_PAUSE,

KEY_ABNT_C1, KEY_YEN, KEY_KANA, KEY_CONVERT, KEY_NOCONVERT,
KEY_AT, KEY_CIRCUMFLEX, KEY_COLON2, KEY_KANJI,

KEY_LSHIFT, KEY_RSHIFT,
KEY_LCONTROL, KEY_RCONTROL,
KEY_ALT, KEY_ALTGR,
KEY_LWIN, KEY_RWIN, KEY_MENU,
KEY_SCRLOCK, KEY_NUMLOCK, KEY_CAPSLOCK

KEY_EQUALS_PAD, KEY_BACKQUOTE, KEY_SEMICOLON, KEY_COMMAND

© Rachel J. Morris, 2009

Print Friendly