Sign in to follow this  
X19OV1

[C++]Key press

Recommended Posts

X19OV1    118
So I'm trying to figure out how to make the computer only detect a key being pressed, but not held down. You see, in the script I'm working on, the character(more like the box) jumps and lands on platforms correctly, but jumping around while holding the UP key presents some problems... Here is the code(Yes, it's long for a forum.): ---------------------------------------------------------------------- #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=blockTop1; } else{ ylc=0; gy=480; } } void moveCharacter(){ 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]) { 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
Cornstalks    7030
Quote:
Original post by X19OV1
Here is the code(Yes, it's long for a forum.

If you use [source][/source] tags, it'll be a lot better! [smile] And can you tell us specifically what those jumping around problems are? It's a lot easier to debug code if we can 1) read it (meaning source tags are your friend), and 2) if we know what's wrong (i.e. what you're expecting and what you're getting).

Share this post


Link to post
Share on other sites
X19OV1    118
Thanks, I'll try those tags.^^ The main problem I'm concerned about is, while holding the Up arrow key when you jump off the platform I made, the box will appear as if it was bouncing on air. I know it's hard to understand, but it's the best way I could say it.

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