I understand this is just a tutorial but if anyone is curious for a better method of handling bounds while taking into account the width and height of the window based on the values we set and the radius of the circle the ideal method would be to Clamp
the value. This is in the cpp standard library but it’s also in the raymath.h library. This is how I would handle it:
First making the circle radius a global variable and use it when drawing the circle.
Then I create a function that takes the desired position and the max width. Since the window always starts at 0 that can be inferred. Then because I don’t want to cut off the circle I add the radius to the min (0) and subtract it from the max (width/height of window) to give a true bounds between 50 and 450 (Since the window width for me is 500). Then if the number passed is greater than or less than those numbers it’ll clamp them to the closest within the range. Then for each direction’s keydown I pass the position plus or minus 3 and the max width OR heigh depending if I’m setting the vertical or horizontal position. Simplified Full code:
#include "raylib.h"
#include "raymath.h"
int circleRadius{50};
int limitBounds(int input, int max) {
return Clamp(input, 0 + circleRadius, max - circleRadius);
}
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, circleRadius, BLUE);
if(IsKeyDown(KEY_D)) {
circlePosX = limitBounds(circlePosX + 3, width);
}
if(IsKeyDown(KEY_A)) {
circlePosX = limitBounds(circlePosX - 3, width);
}
if(IsKeyDown(KEY_W)) {
circlePosY = limitBounds(circlePosY - 3, height);
}
if(IsKeyDown(KEY_S)) {
circlePosY = limitBounds(circlePosY + 3, height);
}
EndDrawing();
}
}
*Funny story, realized after the fact this completely avoids the intended topic for this lesson…