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/Lost-Initial2874 Jul 09 '26

Nah just create an array where ceil(money/100) - 1 as an index matches the correct color. This makes the algorithm O(1) in terms of colors where your solution iterates over every color and performs a comparison making it O(n) where n is each color. This also drastically reduces the amount of logic and can probably just be inlined into the main function.

1

u/mysticreddit Jul 10 '26

That's ASSUMING that mapping holds. If it does then using a hashmap is even faster.

The problem with hard-coded solution(s) is that it isn't flexible. i.e. How would your system handle 250?