r/C_Programming • • Jul 09 '26

Code of the first game

This is the first code I wrote. What do you think?

#include "raylib.h"

int main(void) { InitWindow(800, 600, "Color Changing Ball Clicker"); SetTargetFPS(60);

int money = 0;
Color color = RED;

while (!WindowShouldClose())
{
    if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
    {
        money = money + 1;
    }

    if (money < 100)
    {
        color = RED;
    }
    else if (money < 200)
    {
        color = BLUE;
    }
    else if (money < 300)
    {
        color = GREEN;
    }
    else if (money < 400)
    {
        color = GOLD;
    }
    else if (money < 500)
    {
        color = PURPLE;
    }
    else
    {
        color = MAROON;
    }

    BeginDrawing();

    ClearBackground(RAYWHITE);

    DrawCircle(400, 300, 80, color);

    DrawText(TextFormat("money: %d", money), 20, 20, 30, BLACK);
    DrawText("Click to change the ball color!", 20, 60, 20, DARKGRAY);

    EndDrawing();
}

CloseWindow();

return 0;

}

2 Upvotes

15 comments sorted by

View all comments

Show parent comment

-1

u/chalkflavored Jul 10 '26

why to its own function? seems like moving code around just to move it around

0

u/Fujinn981 Jul 10 '26

Because then you can call it from anywhere else it needs to be called from in the code, instead of copying and pasting it should you want it elsewhere. This goes with the rest of my suggestions as well, as there's no need for that code to be ran every time the loop runs. It only ever needs to run when money is incremented or decremented. Otherwise you're constantly checking variables that haven't changed and wasting CPU cycles on that.