const int jumpHeight = 10;
//Non-Const variables
bool running = true;
bool isJumping = false;
bool isFalling = false;
int jump = 0;
Uint8 *keystate = NULL;
//Structs
typedef struct entity {
SDL_Rect hitbox;
} playerType, enemyType;
playerType player;
enemyType basicEnemy[10];
//Main Function
int main( int argc, char* argv[] )
{
while( running )
{
keystate = SDL_GetKeyState(NULL);
if( keystate[SDLK_UP] )
{
if(isJumping != true)
{
isJumping = true;
}
}
if( keystate[SDLK_LEFT] ) player.hitbox.x -= 1;
if( keystate[SDLK_RIGHT] ) player.hitbox.y += 1;
//Window collision
if( player.hitbox.x < 0 ) {player.hitbox.x = 0;}
else if( player.hitbox.x > SCREEN_WIDTH - player.hitbox.w ) {player.hitbox.x = SCREEN_WIDTH - player.hitbox.w;}
if( player.hitbox.y < 0 ) {player.hitbox.y = 0;}
else if( player.hitbox.y > SCREEN_HEIGHT - player.hitbox.h ) {player.hitbox.y = SCREEN_HEIGHT - player.hitbox.h;}
//Jumping
if( isJumping == true )
{
if( jump >= jumpHeight || isFalling == true )
{
jump--;
player.hitbox.y--;
isFalling = true;
}else if( jump <= 0 && isFalling == true )
{
jump - 0;
isFalling = false;
isJumping = false;
}else {
jump++;
player.hitbox.y++;
}
}
}
}
这是我在游戏中的当前代码(与跳跃相关的部分)。当我按下向上键时,我的角色会走到窗口的顶部并停留在那里。我哪里出错了?我用g++ -o myprogram.exe mysource.cpp -lmingw32 -lSDLmain -lSDL -lSDL_image -static-libgcc -static-libstdc++
. 引导您了解代码应该如何工作:按向上:isJumping 变为真,然后将播放器提升到最大高度,然后再次下降到它开始的位置(稍后我将添加碰撞检查)