[C++]Key input question...

Started by
7 comments, last by djsteffey 14 years, 11 months ago
Let's say that in a game that you are making, you press space to jump. So it would look like:

key[KEY_SPACE]{
               (Do this);
               (Do this);
              }

The object jumping would hop up and down as long as you're holding space. But what would the code look like if you only wanted the object to jump when you PRESS the key and only jump when you PRESS it again?

???

Advertisement
That depends on what library/API you're using to check for keyboard input. With some, it's as easy as reacting to a keyboard press event. With others, you may need to store button states, so you can check if a button is pressed (if it's down during this frame, but it wasn't last frame, then the button has been pressed).
Create-ivity - a game development blog Mouseover for more information.
Oh, yes. I forgot.
I'm using allegro. :)
key[KEY_SPACE]{
(Do this);
(Do this);
key[KEY_SPACE] = false;
}

...
Sorry, it doesn't make a difference.

You need to remember the old state of the key. This way you can include logic that works when the state changes rather than on instantaneous values:
bool oldSpace = false;while(running){    if(!oldSpace && key[SPACE])    {        jumping = true;    }    oldSpace = key[SPACE];    if(jumping)    {        // move the character.    }}

If you need to remember lots of keys, consider simply making a copy of the "keys" array every frame rather than declaring lots of boolean variables.
This is strange. It's still acting as if I didn't change it at all.
Let me show you the code. By the way, I'm using key[KEY_UP] instead of key[KEY_SPACE].
#include <allegro.h>#include <cstdlib>int x = 64;//xint y = 472;//yint life = 1;//Currently not usedint jumping = 0;//Used to check whether the character is jumping or notint falling = 0;//Used to check whether the character is falling or notbool fBool = 0;//Used to make the character start fallingint gy = 480;int blockLeft1 = 300;//Left of block 1; xint blockRight1 = 400;//Right of block 1; xint blockTop1 = 440;//Top of block 1; yint blockBottom1 = 480;//Bottom of block 1; ybool xlc=0;//Determines if a left collision is truebool xrc=0;//Determines if a right collision is truebool yuc=0;//Determines if an upper collision is truebool ylc=0;//Determines if a lower collison is trueBITMAP* buffer;void collisionCheck(){     if(x==blockLeft1&&x<blockRight1&&y>blockTop1){                                                  xrc=1;                                                  }     else{            xrc=0;            }     if(x>blockLeft1&&x==blockRight1+32&&y>blockTop1){                                                   xlc=1;                                                   }     else{          xlc=0;          }     if(x>blockLeft1&&x<blockRight1+32&&y>blockTop1-3){                                                                  ylc=1;                                                                  gy=440;                                                   }     else{          ylc=0;          gy=480;          }            }void moveCharacter(){                                   bool oldUp=false;                                                                                                                                                                                                                 rectfill(buffer,x,y,x-32,y-32,makecol(255,255,255));                        					    collisionCheck();                                                if(yuc==1){                                   jumping=40;                                   }                                                  if(falling>=0&&y<gy&&ylc==0){                                          y+=3;                                         }                           if(jumping>0&&jumping<41&&falling==0&&yuc==0){                                                jumping+=1;                                                y-=6;                                                }                        if(jumping==40){                                        fBool=1;                                        jumping=0;                                        }                        if(fBool==1){                                     falling+=1;                                     }                        if(falling==40){                                        jumping=0;                                        falling=0;                                        fBool=0;                                        }                                                if(jumping==0&&falling==0&&key[KEY_UP]&&!oldUp)                        {                        oldUp=key[KEY_UP];                        jumping+=1;                        }                        if(key[KEY_LEFT]&&x>32&&xlc==0)                        {                        --x;                        }                        if(key[KEY_RIGHT]&&x<640&&xrc==0)                        {                        ++x;                        }                                                                            acquire_screen();                        rectfill(buffer,x,y,x-32,y-32,makecol(0,255,0));                        draw_sprite(screen,buffer,0,0);                                                release_screen();                                                rest(10);                        }int main(){                        allegro_init();                        install_keyboard();                        set_color_depth(16);                        set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);                        buffer = create_bitmap(640,480);                        						acquire_screen();                        						clear_to_color(buffer,makecol(255,255,255));						    while( !key[KEY_ESC]){                      moveCharacter();           rectfill(buffer,blockLeft1,blockBottom1,blockRight1,blockTop1,makecol(0,0,0));                        }                                                                              return(0);                        }END_OF_MAIN();   
Actually, after changing a few things, this really worked! Thanks! :)
maintain the present state of the keys and the state of the keys from last frame


key_pressed_last_frame = key_pressed_this_frame;
key_pressed_this_frame = get_key_states();
if (key_pressed_this_frame[KEY_SPACE] && !key_pressed_last_frame[KEY_SPACE])
{
jump();
}
.
.
.
.
[/sode]

This topic is closed to new replies.

Advertisement