Need Help with Game Over /Win Screen Prompt

When i start debugging the game it instantly says i won and then instantly says game over right after i dont know where i went wrong in the code .

#include "raylib.h"

struct AnimData

{

  Rectangle rec;

  Vector2 pos;

  int frame;

  float updateTime;

  float runningTime;

};

bool isOnGround(AnimData data, int windowHeight)

{

    return data.pos.y >= windowHeight-data.rec.height;

}

AnimData updateAnimData(AnimData data, float deltatime, int maxframe)

{

    //update running time

    data.runningTime += deltatime;

    if(data.runningTime >= data.updateTime)

    {

        data.runningTime = 0.0;

        //update animaiton frame

        data.rec.x = data.frame * data.rec.width;

        data.frame++;

        if(data.frame > maxframe )

        {

            data.frame = 0;

        }

    }

    return data;

}

int main ()

{

    int windowDimension[2];

    windowDimension[0]= 512; //window width in a array format

    windowDimension[1]= 380; //window height in a array format

   

    // Initalize the Window

InitWindow(windowDimension[0] ,windowDimension[1], "Dapper Dasher");

    //accleration due to gravity (pixels/s)/s

const int gravity{1'000};

const int JumpVelocity{-600}; //height of Jump . (pixels/second)

    //scarfy character variables .lines 17-25

Texture2D scarfy = LoadTexture("textures/scarfy.png");

    //AnimData for scarfy

AnimData scarfyData;

scarfyData.rec.width = scarfy.width/6;

scarfyData.rec.height = scarfy.height;

scarfyData.rec.x = 0;

scarfyData.rec.y = 0;

scarfyData.pos.x = windowDimension[0]/2 - scarfyData.rec.width /2;

scarfyData.pos.y = windowDimension[1] - scarfyData.rec.height;

scarfyData.frame = 0;

scarfyData.updateTime = 1.0/12.0;

scarfyData.runningTime = 0.0;

    //Nebula Hazard Variables

Texture2D nebula = LoadTexture("textures/12_nebula_spritesheet.png");

   

const int sizeOfNebulae {3};

AnimData nebulae [sizeOfNebulae] {};

    //loop thru nebulae array and assign data to each AnimData

for ( int i=0; i < sizeOfNebulae; i++)

{

    nebulae[i].rec.x = 0.0;

    nebulae[i].rec.y = 0.0;

    nebulae[i].rec.width = nebula.width/8;

    nebulae[i].rec.height = nebula.height / 8;

    nebulae[i].pos.y = windowDimension [1] - nebula.height / 8;

    nebulae[i].frame = 0;

    nebulae[i].runningTime= 0.0;

    nebulae[i].updateTime = 1.0/16.0;

   

 // update each nebula positon by 300 to spawn 300 pixels after each other

    nebulae[i].pos.x = windowDimension[0] +i*300;

}

float finishline {nebulae[sizeOfNebulae -1].pos.x};

    //nebula velocity on the x axis . Moving towards player

int nebulaVelocity{-200};

    //Jump Check

bool IsInAir{}; //false by default

    //velocity is zero when not jumping

int velocity {0};

Texture2D Background = LoadTexture("textures/far-buildings.png");

float backgroundxpos {};

Texture2D midground = LoadTexture("textures/back-buildings.png");

float midgroundx{};

Texture2D forground = LoadTexture("textures/foreground.png");

float forgroundx{};

bool collison {};

SetTargetFPS(60);

while (!WindowShouldClose())

{

    //delta time (time since last frame)

  const float dt{GetFrameTime()};

        //startDrawing

    BeginDrawing();

    ClearBackground(WHITE);

    // move background by 20 pixels per second on x- axis

    backgroundxpos -= 20 *dt;

    if(backgroundxpos <= -Background.width *2 )

    {

        backgroundxpos = 0.0;

    }

    // move midground by 40 pixels per second on x- axis

    midgroundx -= 40 * dt;

    if(midgroundx <= -Background.width *2 )

    {

        midgroundx = 0.0;

    }

     // move foreground by 80 pixels per second on x- axis

    forgroundx -= 80 * dt;

    if(forgroundx <= -Background.width *2)

    {

        forgroundx = 0.0;

    }

    //draw the background

    Vector2 background1pos{backgroundxpos,0.0};

    DrawTextureEx(Background,background1pos, 0.0,2.0,WHITE);

    Vector2 background2pos {backgroundxpos + Background.width *2, 0.0};

    DrawTextureEx(Background,background2pos, 0.0,2.0,WHITE);

    //draw midground

    Vector2 midground1pos {midgroundx,0.0};

    DrawTextureEx (midground,midground1pos,0.0,2.0, WHITE);

    Vector2 midground2pos {midgroundx + midground.width* 2, 0.0};

    DrawTextureEx (midground,midground2pos,0.0,2.0,WHITE);

    //draw forground

    Vector2 forground1pos {forgroundx,0.0};

    DrawTextureEx(forground,forground1pos,0.0,2.0,WHITE);

    Vector2 forground2pos {forgroundx + forground.width *2, 0.0};

    DrawTextureEx (forground,forground2pos,0.0,2.0,WHITE);

    //checks to see if we are on the ground

   if(isOnGround(scarfyData, windowDimension[1]))

   {

        //scarfy is on the ground  

    velocity = 0;

    IsInAir = false;

   }

   else   //scarfy is in the air

   {

 

    //apply gravity

   velocity +=  gravity *dt;

   IsInAir = true;

   }

        //  jumping  with spacebar

    if (IsKeyPressed(KEY_SPACE) && !IsInAir)

   {

    velocity += JumpVelocity;

   }

for(int i=0; i <sizeOfNebulae; i++)

{

    //update the position of each nebula

     nebulae [i].pos.x += nebulaVelocity *dt;

}

    //update position of finish line

finishline =+ nebulaVelocity *dt;

   //update scarfy y position

scarfyData.pos.y += velocity * dt;

    //update scarfy's animation frame

if(!IsInAir)

{

    scarfyData = updateAnimData(scarfyData,dt,5);

}

//update each animation frame of the nebula

for (int i=0; i < sizeOfNebulae; i++)

{

    //update nebula animation frame

nebulae[i]= updateAnimData( nebulae[i],dt,7);

}

for (AnimData nebula : nebulae)

{

        float pad{50};

    Rectangle nebRec{

        nebula.pos.x + pad,

        nebula.pos.y + pad,

        nebula.rec.width - 2 * pad,

        nebula.rec.height - 2 * pad

    };

    Rectangle scarfyrec {

        scarfyData.pos.x,

        scarfyData.pos.y,

        scarfyData.rec.width,

        scarfyData.rec.height

    };

    if (CheckCollisionRecs(nebRec,scarfyrec))

    {

        collison = true;

    }

}

if (collison)

{

   DrawText("Game Over", windowDimension[0]/4, windowDimension[1]/2, 40, RED);

   

}

else if (scarfyData.pos.x >= finishline)

{

 //win the game

  DrawText("You win", windowDimension[0]/4, windowDimension[1]/2, 40, RED);

}

else

{

        //update each textures of the nebulas

    for (int i = 0; i < sizeOfNebulae; i++)

    {

    DrawTextureRec(nebula,nebulae[i].rec,nebulae[i].pos, WHITE);

    }

    //draw scarfy

    DrawTextureRec(scarfy,scarfyData.rec,scarfyData.pos, WHITE);

}

    //Stop Drawing

    EndDrawing();

}

UnloadTexture(scarfy);

UnloadTexture(nebula);

UnloadTexture(Background);

UnloadTexture(midground);

UnloadTexture(forground);

CloseWindow();

}

nevermind i figured it out. for finshline line i had =+ nebulavelocity instead of += nebulavelocity. small mistake :slight_smile:

Iā€™m glad you found the solution, good job!

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms