Advanced Axe Game

I’m trying to run Axe Game using Delta time so the engine is running independently from the FPS. Similarly; the resolution and scaling is also adjustable. Here is my question. Why does the player speed adhere to the time delta but the axe speed does not?

#include "raylib.h"
int main()
{                                                   //----------------------//
    // INIT VARFIELD -------------------------------// INITIALIZE VARIABLES //
                                                    //----------------------//
    int iDefSize{100};
    int iPrevTime{};                                // Last Frame Time
    int iFrameTime{};                               // Engine Framerate
    int iDelta{};                                   // Engine Movement Speed
    int iMainFPS{};                                 // Display Framerate

    int iResX{1920};                                // Window Dimension: Width
    int iResY{1080};                                // Window Dimension: Height
    
    int iPlayerX{iResX/4};                          // Player Location X-Axis
    int iPlayerY{iResY/2};                          // Player Location Y-Axis
    
    int iAxeOffset{iDefSize/2};
    int iAxeX{iResX/4*3-(iAxeOffset)};              // Axe Location X-Axis
    int iAxeY(iResY/2-(iAxeOffset));                // Axe Location Y-Axis

    int iPlayerMove{};                              // Player Current Speed
    int iAxeXMove{};                                // Axe Movement X-Axis
    int iAxeYMove{};                                // Axe Movement Y-Axis
    
    float fSprint{2.0};                             // Movement Sprint Multiplier
    // END PRELOOP VARFIELD ------------------------

    
    
    
    
    
    // Display Initilization
    InitWindow(iResX, iResY, "Raulwynn's Window");
    SetTargetFPS(165);

    while (!WindowShouldClose())                    // Detect Window Close or ESC Key 
    {
        if (IsKeyPressed(KEY_F11))                  // Detect F11 to Enter Full-Screen
        {
            ToggleFullscreen();
        }
                                                    
                                                    //-------------------//
        BeginDrawing(); //--------------------------// INITIALIZE WINDOW //
        ClearBackground(WHITE);                     //-------------------//

        // Frame by Frame Logic
        iPrevTime = iFrameTime;                     // Marks the Last Frame in Memory
        iFrameTime = GetTime() * 60;                // Marks the Current Frame in Memory
        iDelta = (iFrameTime - iPrevTime) * 10;     // Last Frame Time - Current Frame Time = Delta
        iMainFPS = GetFPS();
        iPlayerMove = iDelta;

        // Debug Info
        DrawText(TextFormat("Debugging\niMainFPS(%i)\niFrameTime(%i)\niDelta(%i)", iMainFPS, iFrameTime, iDelta), 10, 10, 20, LIGHTGRAY);

        // Game Objects
        DrawCircle(iPlayerX, iPlayerY, iDefSize/2, BLUE);
        DrawRectangle(iAxeX, iAxeY, iDefSize, iDefSize, RED);

        iAxeX += iAxeXMove;
        iAxeY += iAxeYMove;

        // Determining Axe Movement
        if (iAxeXMove == 0)                         //Init Axe X-Axis Movement
        {
            iAxeXMove = iDelta;                     //Set Axe  X-Axis speed to iDelta
        }
        if (iAxeX <= 0 || iAxeX >= iResX)           // If Axe X-Axis hits 0 or iResX
        {
            iAxeXMove = -iAxeXMove;                 // Then Reverse X-Axis Direction
        }
        
        if (iAxeYMove == 0)                         //Init Axe Y-Axis Movement
        {
            iAxeYMove = iDelta;                     //Set speed to iDelta
        }        
        if (iAxeY <= 0 || iAxeY > iResY)            // If Axe Y-Axis hits 0 or iResY
        {
            iAxeYMove = -iAxeYMove;                 // Then Reverse Y-Axis Direction
        }

        //Determining Player Movement

        if (IsKeyDown(KEY_LEFT_SHIFT))              //If Player Holding Shift
        {
            iPlayerMove = iDelta*fSprint;           //Set iPlayerMove to iDelta multiplied by fSprint
        }
        if (!IsKeyDown(KEY_LEFT_SHIFT))             //If Player is not Holding Shift
        {
            iPlayerMove = iDelta;                   //Set iPlayerMove to iDelta
        }

        // Logic for moving Right
        if (IsKeyDown(KEY_D) && (iPlayerX < iResX))
        {  
            iPlayerX += iPlayerMove;
        }
        if (IsKeyDown(KEY_D) && (iPlayerX >= iResX))
        {
            iPlayerX = 0;
        }

        // Logic for moving Left
        if (IsKeyDown(KEY_A) && (iPlayerX > 0))
        {  
            iPlayerX -= iPlayerMove;
        }
        if (IsKeyDown(KEY_A) && (iPlayerX <= 0))
        {
            iPlayerX = iResX;
        }

        // Logic for moving Up
        if (IsKeyDown(KEY_W) && (iPlayerY > 0))
        {  
            iPlayerY -= iPlayerMove;
        }
        if (IsKeyDown(KEY_W) && (iPlayerY <= 0))
        {
            iPlayerY = iResY;
        }

        // Logic for moving Down
        if (IsKeyDown(KEY_S) && (iPlayerY < iResY))
        {  
            iPlayerY += iPlayerMove;
        }
        if (IsKeyDown(KEY_S) && (iPlayerY >= iResY))
        {
            iPlayerY = 0;
        }


        

        
        // Gamelogic End
        EndDrawing();
    }
}

I know there are too many comments in my code. I have a bad memory and it helps when I return to remember how I did something before.

You’re effectively setting iAxeXMove and iAxeYMove only once at the start of the game, where iDelta is going to be really high. Rather than setting your movement every frame.

Have you looked at the other sections yet? They’d help give a better understanding on how deltaTime works and how it’s utilized.

1 Like

Yeah I just added an bool that runs an init for the axe that I can flag off during the first loop. I just started learning C++ a couple days ago so I’m not too familiar with it.

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

Privacy & Terms