key[KEY_SPACE]{
(Do this);
(Do this);
}
???
key[KEY_SPACE]{
(Do this);
(Do this);
}
???
bool oldSpace = false;while(running){ if(!oldSpace && key[SPACE]) { jumping = true; } oldSpace = key[SPACE]; if(jumping) { // move the character. }}
#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();
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]