Problem with my code - Intro to C++ course

I followed the videos line per line but my workspace is riddled with errors and won’t make the nebulae like it’s shown in the video

instructor: Stephen Ulibarri
Course: C++ Fundamentals: Game Programming For Beginners

i’ll paste my code here

#include “raylib.h”

struct AnimData

{

Rectangle rec;

Vector2 pos;

int frame;

float updateTime;

float runningTime;

};

int main()

{

int windowDimensions[2];

windowDimensions[0] = 512;

windowDimensions[1] = 380;

// initialize the window

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

// acceleration due to gravity (pixel/s/s

const int gravity{1'000};

// nebula variables

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

const int sizeofNebulae{6};

AnimData nebulae[sizeofNebulae]{};

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 = windowDimensions[1] - nebula.height/8;

    nebulae[i].frame = 0;

    nebulae[i].runningTime = 0.0;

    nebulae[i].updateTime = 0.0;

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

}

// nebula x velocity (pixels/second)

int nebVel{-200};

// scarfy variables

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

AnimData scarfyData;

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

scarfyData.rec.height = scarfy.height;

scarfyData.rec.x = 0;

scarfyData.rec.y = 0;

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

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

scarfyData.frame = 0;

scarfyData.updateTime = 1.0/12.0;

scarfyData.runningTime = 0.0;

// jump velocity (pixels/second)

const int jumpVel{-600};



// rectangle is in the air?

bool isInAir{};

int velocity{0};

SetTargetFPS(60);

while (!WindowShouldClose())

{

    // delta time (time since last frame)

    const float dT{GetFrameTime()};

   

    // start drawing

    BeginDrawing();

    ClearBackground(WHITE);

   

    if (scarfyData.pos.y >= windowDimensions[1] - scarfyData.rec.height )

    {

        // rectangle is on the ground

        velocity = 0;

        isInAir = false;

    }

    else

    {

        // rectangle is in the air

        velocity += gravity * dT;

        isInAir = true;

    }

    // check for jumping

    if (IsKeyPressed(KEY_SPACE) && !isInAir)

    {

        velocity += jumpVel;

    }

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

    {

        //update the position of each nebula

        nebulae[i].pos.x += nebVel * dT;

    }

    // update scarfy position

    scarfyData.pos.y += velocity * dT;

    // update scarfy's animation frame

    if (!isInAir)

    {

          // update running time

        runningTime += dT;

        if(scarfyData.runningTime >= scarfyData.updateTime)

        {

            scarfyData.runningTime = 0.0;

            // update animation frame

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

            scarfyData.frame++;

            if (scarfyData.frame > 5)

            {

                scarfyData.frame = 0;

            }

        }

    }

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

    {

         // update nebula animation frame

        nebulae[i].RunningTime += dT;

        if (nebulae[i].RunningTime >= nebulae[i].UpdateTime)

        {

            nebulae[i].RunningTime = 0.0;

            nebulae[i].rec.x = nebulae[i].Frame * nebRec.width;

            nebulae[i].Frame++;

            if (nebulae[i].Frame > 7)

            {

                nebulae[i].Frame = 0;

            }

        }

    }



    for (int i = 0; i < sizeofNebulae, i++)

    {

         // draw nebula

         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);

CloseWindow();

}

Hi IAA_Agent,

variable names are case-sensitive, if you name a variable “rec” or “scarfyData” then you need to use that spelling and case exactly. Accessing that variable by using “Rec” or “scarfydata” will result in a compiler error.

You also have a reference to “runningTime” on its own, which is likely left over from a previous lecture. Just make sure to replace it with “scrafyData.runningTime”

Thank you, Tuomo. I’ll try that.

Hey Tuomo_T,
I did those fixes and it got rid of most of the errors, but I still have one error and now it won’t summon the nebulae at all.

I’m just confused why it won’t show any now, it’s supposed to show multiple and for some reason the window looks smaller but maybe that’s just me.

#include “raylib.h”

struct AnimData

{

Rectangle rec;

Vector2 pos;

int frame;

float updateTime;

float runningTime;

};

int main()

{

int windowDimensions[2];

windowDimensions[0] = 512;

windowDimensions[1] = 380;

// initialize the window

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

// acceleration due to gravity (pixel/s/s

const int gravity{1'000};

// nebula variables

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

const int sizeofNebulae{6};

AnimData nebulae[sizeofNebulae]{};

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 = windowDimensions[1] - nebula.height/8;

    nebulae[i].frame = 0;

    nebulae[i].runningTime = 0.0;

    nebulae[i].updateTime = 0.0;

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

}

// nebula x velocity (pixels/second)

int nebVel{-200};

// scarfy variables

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

AnimData scarfyData;

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

scarfyData.rec.height = scarfy.height;

scarfyData.rec.x = 0;

scarfyData.rec.y = 0;

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

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

scarfyData.frame = 0;

scarfyData.updateTime = 1.0/12.0;

scarfyData.runningTime = 0.0;

// jump velocity (pixels/second)

const int jumpVel{-600};



// rectangle is in the air?

bool isInAir{};

int velocity{0};

SetTargetFPS(60);

while (!WindowShouldClose())

{

    // delta time (time since last frame)

    const float dT{GetFrameTime()};

   

    // start drawing

    BeginDrawing();

    ClearBackground(WHITE);

   

    if (scarfyData.pos.y >= windowDimensions[1] - scarfyData.rec.height )

    {

        // rectangle is on the ground

        velocity = 0;

        isInAir = false;

    }

    else

    {

        // rectangle is in the air

        velocity += gravity * dT;

        isInAir = true;

    }

    // check for jumping

    if (IsKeyPressed(KEY_SPACE) && !isInAir)

    {

        velocity += jumpVel;

    }

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

    {

        //update the position of each nebula

        nebulae[i].pos.x += nebVel * dT;

    }

    // update scarfy position

    scarfyData.pos.y += velocity * dT;

    // update scarfy's animation frame

    if (!isInAir)

    {

          // update running time

        scarfyData.runningTime += dT;

        if(scarfyData.runningTime >= scarfyData.updateTime)

        {

            scarfyData.runningTime = 0.0;

            // update animation frame

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

            scarfyData.frame++;

            if (scarfyData.frame > 5)

            {

                scarfyData.frame = 0;

            }

        }

    }

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

    {

         // update nebula animation frame

        nebulae[i].runningTime += dT;

        if (nebulae[i].runningTime >= nebulae[i].updateTime)

        {

            nebulae[i].runningTime = 0.0;

            nebulae[i].rec.x = nebulae[i].frame * nebulae[0].rec.width;

            nebulae[i].frame++;

            if (nebulae[i].frame > 7)

            {

                nebulae[i].frame = 0;

            }

        }

    }



    for (int i = 0; i < sizeofNebulae, i++;)

    {

         // draw nebula

         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);

CloseWindow();

}

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

Privacy & Terms