I'm trying to make a snake game in Raylib. I've gotten to the stage of writing a cell (food) to the screen, this is my code:
#include "raylib.h"
#define CELLSIZE 30
#define CELLCOUNT 25
Color green = {173, 204, 96, 255};
Color darkGreen = {43, 51, 24, 255};
int main(void){
Vector2 foodPos = {GetRandomValue(0, CELLCOUNT - 1),GetRandomValue(0, CELLCOUNT - 1)};
InitWindow(CELLSIZE * CELLCOUNT, CELLSIZE * CELLCOUNT, "retro snake");
SetTargetFPS(60);
BeginDrawing();
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(green);
DrawRectangle(foodPos.x, foodPos.y, CELLSIZE, CELLSIZE, darkGreen);
EndDrawing();
}
CloseWindow();
return 0;
}
When I run this code the food is always in the top left corner - it just spawns the food at 0,0 every time.
In your code, you are calling
GetRandomValuewithout first callingSetRandomSeed.As of Raylib 5, there are two possible pRNGs: C's standard
rand(srand), andrprand- an implementation of xoshiro128**.If the standard C pRNG is in use, calling
rand()without first callingsrand(seed)behaves as thoughsrand(1)was called during program startup. In this situation, it is extremely likely you would see non-zero values - but since the seed is always the same (1), they would be the same values on every execution of the program.If
rprandis in use, calling the internalrprand_xoshirowithout first callingrprand_set_seedresults in a pRNG with a zero-state: all numbers generated will be zero. I would hazard a guess that this is what is occurring on your system (in effect: when you callGetRandomValuebeforeSetRandomSeed).InitWindowcallsas the last thing it does, and is often the first Raylib library function called in an application, so you can simply reorder the code:
As pointed out by @pmacfarlane, your coordinates, which are in the range
[0, CELLSIZE)must be multiplied byCELLSIZEfor the rectangle to be drawn at the correct offset.Additionally note that the first
BeginDrawing();(outside the loop) is erroneous.A working example (press SPACE to move the rectangle to a random position):