0
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成真,然後玩家升高到最大高度,然後再次下降到它開始的位置(稍後我將添加碰撞檢查)SDL C++跳轉
隨着y值的增加,我原本就是這樣做的,但由於某種奇怪的原因,它要求我按照原來的方式進行。當我實施你的修復程序時,我把它們恢復到原來的方式,並且它工作正常! – lewisjb