Top Down Game Classy Clash - code errors

Course: C++ Fundamentals: Game Programming For Beginners

#include “raylib.h”

#include “raymath.h”

int main()

{

const int windowWidth{384};

const int windowHeight{384};

InitWindow(windowWidth, windowHeight, "I'm on top, You're on bottom");

Texture2D map = LoadTexture("nature_tileset/Dion Nature RPG map.png");

Vector2 mapPos{0.0, 0.0};

float speed(4.0);

Texture2D knight = LoadTexture("characters/knight_idle_spritesheet.png");

Vector2 knightPos

{

    (float)windowWidth/2.0f - 4.0f * 0.5f * (float)knight.width/6.0f};

    (float)windowHeight/2.0f - 4.0f * (0.5f * (float)knight.height);

}

SetTargetFPS(60);



while (!WindowShouldClose())

{

    BeginDrawing();

    ClearBackground(WHITE);

    Vector2 direction{};

   if (IsKeyDown(KEY_A)) direction.x -= 1.0;

   if (IsKeyDown(KEY_D)) direction.x += 1.0;

   if (IsKeyDown(KEY_W)) direction.y -= 1.0;

   if (IsKeyDown(KEY_S)) direction.y += 1.0;

   if (Vector2Length(direction) != 0.0)

   {

        // set mapPos = mapPos - direction

       

       mapPos = Vector2Subtract(mapPos, Vector2Scale(Vector2Normalize(direction), speed));

   }

    // draw the map

    DrawTextureEx(map, mapPos, 0.0, 4.0, WHITE);

    //draw the character

    Rectangle source{0.f, 0.f, (float)knight.weight/6.f, (float)knight.height};

    Rectangle dest{knightPos.x, knight.Pos.y, 4.0f * (float)knight.width/6.0f, 4.0f * (float)knight.height};

    DrawTexturePro(knight, source, dest, Vector2{}, 0.f, WHITE);

    EndDrawing();

}

CloseWindow();

}

Hello IAA_Agent,

There are a couple of issues with your code. The first group of errors, your Vector2 knightPos declaration has an extra } that is telling the compiler that your code’s scope is ending before it should (it thinks that the main function ends when it shouldn’t). You should also be using a comma to separate that two initializing values for Vectors, instead of semi-colons. Lastely, you need to add a semi-colon to the closing bracket of this initialization. The result should be this:

Vector2 knightPos{
        (float)windowWidth/2.0f - 4.0f * (0.5f * (float)knight.width/6.0f),
        (float)windowHeight/2.0f - 4.0f * (0.5f * (float)knight.height)
};

The next group of errors is with these two lines:

    Rectangle source{0.f, 0.f, (float)knight.weight/6.f, (float)knight.height};

    Rectangle dest{knightPos.x, knight.Pos.y, 4.0f * (float)knight.width/6.0f, 4.0f * (float)knight.height};

Texture2D does not have a property called weight, and I believe you had meant width. Texture2D also does not have a Pos property, which again I believe you meant knightPos.y. The resulting changes would look like this:

Rectangle source{0.f, 0.f, (float)knight.width/6.f, (float)knight.height};
Rectangle dest{knightPos.x, knightPos.y, 4.0f * (float)knight.width/6.0f, 4.0f * (float)knight.height};

Your code tested with these changes produces no errors. I hope this helps!

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

Privacy & Terms