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/MysticPlasma Jul 10 '26

Isn't this premature optimization? Since the game is still in the design phase (and considering the skill level), it should probably be something more flexible and comprehensive, where changes can be done easily.

What if they decide they want a color for negative values? - Here it would be UB and potentially hard to track error for a beginner.

What if they want to change the limits during testing? - Here it would require a total rewrite of the mapping.

2

u/sciencekm Jul 11 '26

I believe it is best practice to design software so that it lends to patterns that lead to simpler implementation. Irregular logic is both hard to implement and maintain.