May of gone a bit overboard

I got a little distracted and went a bit overboard and wrote a run handler and return a slower speed if strafing (vertical and horizontal movement at the same time) xD

#include "raylib.h"

int moveSpeed{3};
int runMultiplier{3};

bool isStrafing() {
    return ((IsKeyDown(KEY_W) || IsKeyDown(KEY_S)) && (IsKeyDown(KEY_A) || IsKeyDown(KEY_D)));
}

int getSpeed() {
    if(IsKeyDown(KEY_LEFT_SHIFT)) { // Running?
        return isStrafing() ? (moveSpeed * runMultiplier) * 0.8 : moveSpeed * runMultiplier;
    } else {
        return isStrafing() ? moveSpeed * 0.8 : moveSpeed;
    }
}

int main() {
    int width{500};
    int height{350};
    InitWindow(width, height, "My Game");

    int circlePosX{100};
    int circlePosY{100};

    SetTargetFPS(60);
    while(!WindowShouldClose()) {
        BeginDrawing();
        ClearBackground(RED);
        DrawCircle(circlePosX, circlePosY, 50, BLUE);

        if(IsKeyDown(KEY_D)) {
            circlePosX = circlePosX + getSpeed();
        }
        if(IsKeyDown(KEY_A)) {
            circlePosX = circlePosX - getSpeed();
        }
        if(IsKeyDown(KEY_W)) {
            circlePosY = circlePosY - getSpeed();
        } 
        if(IsKeyDown(KEY_S)) {
            circlePosY = circlePosY + getSpeed();
        }

        EndDrawing();
    }
}
3 Likes

Experimenting with ideas is the best way to learn. I adopted your run functionality and added directional keys to the mix.

Privacy & Terms