Sign in to follow this  
X19OV1

[C++]Key input question...

Recommended Posts

X19OV1    118
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?
???

Share this post


Link to post
Share on other sites
Captain P    1092
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).

Share this post


Link to post
Share on other sites
rip-off    10976
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.

Share this post


Link to post
Share on other sites
X19OV1    118
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;//x
int y = 472;//y

int life = 1;//Currently not used

int jumping = 0;//Used to check whether the character is jumping or not
int falling = 0;//Used to check whether the character is falling or not
bool fBool = 0;//Used to make the character start falling
int gy = 480;

int blockLeft1 = 300;//Left of block 1; x
int blockRight1 = 400;//Right of block 1; x
int blockTop1 = 440;//Top of block 1; y
int blockBottom1 = 480;//Bottom of block 1; y

bool xlc=0;//Determines if a left collision is true
bool xrc=0;//Determines if a right collision is true
bool yuc=0;//Determines if an upper collision is true
bool ylc=0;//Determines if a lower collison is true

BITMAP* 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();



Share this post


Link to post
Share on other sites
ncsu121978    1344
maintain the present state of the keys and the state of the keys from last frame

[code]
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this