Search code examples
crandomraylib

In my C raylib project, something strange is happening


In this code, the dog is meant to "eat" the bone, but instead of the bone being eaten, the dog moves away from the bone! I think this game is trolling me.

#include "raylib.h"
#include <stdio.h>
#include <stdlib.h>
#include "raymath.h"
#include <time.h>

#define HEIGHT 500
#define WIDTH 700

Color extraDarkGreen = {10,30,20,255};

int main(void)
{

    InitWindow(WIDTH, HEIGHT, "Random");
    Image doge = LoadImage("/home/neraan/C/rand/Doge.png");
    ImageResize(&doge, 75, 75);
    Texture2D dogeTex = LoadTextureFromImage(doge);

    Image Bone = LoadImage("/home/neraan/C/rand/Bone.png");
    ImageResize(&Bone, 75, 75);
    Texture2D BoneTex = LoadTextureFromImage(Bone);

    Vector2 DogePos = {50,50};

    Vector2 BonePos = {GetRandomValue(10, WIDTH-10),GetRandomValue(10, HEIGHT-10)};

    SetTargetFPS(60);

    while (!WindowShouldClose())
    {
        Rectangle DogeHit = {DogePos.x,DogePos.y,20,20};
        Rectangle BoneHit = {BonePos.x,BonePos.y,20,20};

        BeginDrawing();
        ClearBackground(extraDarkGreen);
        DrawTexture(dogeTex, DogePos.x, DogePos.y,GREEN);
        DrawTexture(BoneTex, BonePos.x, BonePos.y,GREEN);

        EndDrawing();

        DogePos.x += GetRandomValue(1.0f,2.0f);
        DogePos.x -= GetRandomValue(1.0f,2.0f);
        DogePos.y -= GetRandomValue(1.0f,2.0f);
        DogePos.y += GetRandomValue(1.0f,2.0f);

        if (IsKeyDown(KEY_RIGHT)) DogePos.x += 4.0f;
        if (IsKeyDown(KEY_LEFT)) DogePos.x -= 4.0f;
        if (IsKeyDown(KEY_UP)) DogePos.y -= 4.0f;
        if (IsKeyDown(KEY_DOWN)) DogePos.y += 4.0f;

         if (CheckCollisionRecs(DogeHit,BoneHit))
        {
            SetRandomSeed((unsigned int)time(NULL));
            Vector2 BonePos = {GetRandomValue(10, WIDTH-10),GetRandomValue(10, HEIGHT-10)};
            puts("Lets GOOOO");
        }


    }




    CloseWindow();

    return 0;
}

Here's an image of the output:

enter image description here

I have absolutely no idea what even to try. Stack Overflow wants me to add more text, as "there's only code here!" but I literally don't know what to add. So, let me just tell you that I'm unsure EXACTLY what is wrong here. To me, all the logic checks out! It SHOULD work, and, for goodness sake, WHY DOES THE DOG MOVE, NOT THE BONE!


Solution

  • Vector2 BonePos = ... inside the if block creates a new, local variable representing its own object within that scope. It does not affect the earlier definition. The position used to render the bone is never altered.

    Turn your compiler warnings up to catch unused variables.

    Aside from that, reseeding the pRNG upon collision is questionable. Usually you call SetRandomSeed once near the start of the program, or simply allow InitWindow to implicitly seed the pRNG.

    Also, it is unclear what the purpose of these jitters are

    DogePos.x += GetRandomValue(1.0f,2.0f);
    DogePos.x -= GetRandomValue(1.0f,2.0f);
    DogePos.y -= GetRandomValue(1.0f,2.0f);
    DogePos.y += GetRandomValue(1.0f,2.0f);
    

    but they mean the position of the "doge" is randomly shifting each frame.


    Overall, this appears to be a Snake-like game, with free movement. A refactored example follows.

    #include <stdio.h>
    #include <stdlib.h>
    #include "raylib.h"
    
    #define WIDTH 300
    #define HEIGHT 200
    #define BOUND_RNG(X,Y) (GetRandomValue(10, (X) - (Y) - 10))
    
    int main(void)
    {
        InitWindow(WIDTH, HEIGHT, "Random");
        Rectangle doge = { .x = 50, .y = 50, .width = 20, .height = 20 };
        Rectangle bone = {
            .x = BOUND_RNG(WIDTH, 20),
            .y = BOUND_RNG(HEIGHT, 20),
            .width = 20,
            .height = 20
        };
    
        SetTargetFPS(60);
    
        while (!WindowShouldClose()) {
            if (IsKeyDown(KEY_RIGHT)) doge.x += 4.0f;
            if (IsKeyDown(KEY_LEFT)) doge.x -= 4.0f;
            if (IsKeyDown(KEY_UP)) doge.y -= 4.0f;
            if (IsKeyDown(KEY_DOWN)) doge.y += 4.0f;
    
            if (CheckCollisionRecs(doge, bone)) {
                bone.x = BOUND_RNG(WIDTH, bone.width);
                bone.y = BOUND_RNG(HEIGHT, bone.height);
            }
    
            BeginDrawing();
            ClearBackground(DARKGRAY);
            DrawRectangleRec(doge, YELLOW);
            DrawRectangleRec(bone, RED);
            EndDrawing();
        }
    
        CloseWindow();
    }
    

    program as a gif

    Note that this example does not handle the event where the "bone" randomly moves to an overlapped position with the "doge", or prevent the "doge" from moving outside the play area. It does prevent the "bone" from clipping outside the play area when respawning.