> ## Content Index
> Fetch the complete content index at: https://evil-penguin.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# MLX42 - Game Development
- URL: https://evil-penguin.com/mlx42-game-development/
- Published: 2024-02-02T04:56:31.000Z
- Updated: 2026-11-16T23:03:07.000Z
- Description: I will guide you through some game dynamics. This is the continuation of MLX42 - Intro
- Author: evilpenguin
- Tags: 42, MLX, C

This is the continuation of my post: [MLX42 - INTRO](https://pulgamecanica.herokuapp.com/posts/mlx42-intro?ref=evil-penguin.com)  
  
Last time, we were left with the game screen.   
  
A few important points: 

- We will create the game finally.
- We will recycle the foreground to draw the game dynamics.
- We must implement another animation for the color selections, this is similar to the previous one but it's smaller, it's **32x32px**.

The game background and the animation are the following:

![](https://evil-penguin.com/content/images/2026/11/game_bg.png)

![](https://evil-penguin.com/content/images/2026/11/small_select_sprite_sheet.png)

Let's modify a little our game structure:  
  
#define COLOR\_SELECTION\_LEN 3  
  
enum color\_selection {  
 RED,  
 GREEN,  
 BLUE  
**};**  
  
typedef struct s\_color\_game {  
 mlx\_t \* mlx;  
 mlx\_image\_t \* menu\_bg;  
 mlx\_image\_t \* game\_bg;  
 mlx\_image\_t \* foreground;  
 mlx\_image\_t \* difficulty\_imgs\[DIFFICULTY\_LEN\];  
 t\_animation \* select\_animation;  
**t\_animation \* small\_select\_animation;**  
 enum menu\_selection menu\_selection;  
 enum game\_status game\_status;  
 enum game\_difficulty game\_difficulty;  
 enum color\_selection color\_selection;  
 **int selected\_colors\[COLOR\_SELECTION\_LEN\];**  
} t\_color\_game;  
And on the ***init\_game*** function you should also modify accordingly:init\_game() {  
...  
 t\_animation\* small\_anim;  
  
 // Sprite & Animation (small select)  
 sprite = new\_sprite("./images/small\_select\_sprite\_sheet.png", mlx);  
 small\_anim = slice\_sprite(&sprite, (sprite\_slice){0, 0, 32, 32, 0, 0}, false, 4, 120);  
 destroy\_sprite(&sprite);  
...  
 return (t\_color\_game){mlx, img, img2, foreground\_img,  
 {difficulty\_imgs\[0\], difficulty\_imgs\[1\], difficulty\_imgs\[2\]},  
 anim, **small\_anim,** SELECT\_PLAY, MENU, EASY, **RED,**  
 **{255, 255, 255}**};  
**}**  
And of course, don't forget to free the animation on the main function:  
  
ft\_lstclear(&cg.small\_select\_animation->frames, bait);  
**free(cg.small\_select\_animation);**Now we can implement the selection in the foreground just like we did with the menu!  
  
Let's get our hands dirty with animations! :D  
  
So on the update function let's implement the animation, we will use static coordinates to place the animation in the correct spot!  
  
static int color\_selection\_coords\[COLOR\_SELECTION\_LEN\]\[2\] = {{320, 256}, {416, 256}, {512, 256}};  
...  
if (cg->game\_status == PLAYING) {  
 mlx\_image\_t \* frame = (mlx\_image\_t \*)ft\_lstget(cg->small\_select\_animation->frames, cg->small\_select\_animation->current\_frame\_num)->content;  
 if (!frame)  
 error();  
 put\_img\_to\_img(cg->foreground, frame,  
 color\_selection\_coords\[cg->color\_selection\]\[0\],  
 color\_selection\_coords\[cg->color\_selection\]\[1\]);  
 update\_animation(cg->small\_select\_animation, cg->mlx->delta\_time);  
} else if (cg->game\_status == MENU) {  
...  
Now we can implement a key hook, to change the color selection whenever the user press the left or right keys!  
  
void key\_update(mlx\_key\_data\_t keydata, void\* ptr) {  
 t\_color\_game\* cg = (t\_color\_game\*)ptr;  
  
 if (cg->game\_status == PLAYING && keydata.action == MLX\_PRESS) {  
 if (keydata.key == MLX\_KEY\_LEFT) {  
 cg->color\_selection--;  
 if (cg->color\_selection == -1)  
 cg->color\_selection = COLOR\_SELECTION\_LEN - 1;  
 } else if (keydata.key == MLX\_KEY\_RIGHT) {  
 cg->color\_selection++;  
 cg->color\_selection %= COLOR\_SELECTION\_LEN;  
 }  
 **}** else if (cg->game\_status == MENU && keydata.action == MLX\_RELEASE) {  
...  
Now your game should look something like this:  
  
![](https://evil-penguin.com/content/images/2026/11/Screencastfrom2024-01-3113-19-23-ezgif.com-video-to-gif-converter.gif)

# Colors

Now we are going to implement the colors, you might have had noticed, I added a member **selected\_colors\[COLOR\_SELECTION\_LEN\]** member to our game.  
  
This is a data array which will hold the user selections, it is initialized to white {255, 255, 255}  
  
The user will need to control each int to generate a color similar to the color of the game in order to win.  
When the user is playing, and press (or holds) the up or down key, the selected color should be affected, so we can implement that in the key hook!  
  
void key\_update(mlx\_key\_data\_t keydata, void\* ptr) {  
 t\_color\_game\* cg = (t\_color\_game\*)ptr;  
  
 if (cg->game\_status == PLAYING && (keydata.action == MLX\_REPEAT || keydata.action == MLX\_PRESS)) {  
 if (keydata.key == MLX\_KEY\_DOWN) {  
 if (cg->selected\_colors\[cg->color\_selection\] > 0)  
 cg->selected\_colors\[cg->color\_selection\]--;  
 } else if (keydata.key == MLX\_KEY\_UP) {  
 if (cg->selected\_colors\[cg->color\_selection\] < 255)  
 cg->selected\_colors\[cg->color\_selection\]++;  
 }  
 }  
 if (cg->game\_status == PLAYING && keydata.action == MLX\_PRESS) {  
 if (keydata.key == MLX\_KEY\_LEFT) {  
 cg->color\_selection--;  
**...**  
It's very important to realize that it's an independent if, the color selection should not affect the color value (it's not an if - else if).  
  
We have to represent somehow the amount of each color (Red, Green & Blue).  
There are many ways you could do this, we could even try to a custom font and put the number which is represented... but it's tooooo complicated.  
So we can just use ***mlx\_put\_string*** this is a function from MLX42!  
  
The function is very basic and not super handful, mainly because we can't choose to put a string to an image, the library creates an extra image with the text. The image is just big enough to hold the text.  
This is very inconvenient, but we can create some code make it work.  
  
So we should draw three images one for each value. Let's do that then:  
And because we have the color values as numbers, we will need to use our ***ft\_itoa*** function to convert the number to a string which we can represent!  
  
Let's modify our game structure, we will need 3 font images:  
  
typedef struct s\_color\_game {  
 mlx\_t \* mlx;  
 mlx\_image\_t \* menu\_bg;  
 mlx\_image\_t \* game\_bg;  
 mlx\_image\_t \* foreground;  
 mlx\_image\_t \* difficulty\_imgs\[DIFFICULTY\_LEN\];  
 **mlx\_image\_t \* font\_img\[COLOR\_SELECTION\_LEN\];**  
 t\_animation \* select\_animation;  
 t\_animation \* small\_select\_animation;  
 enum menu\_selection menu\_selection;  
 enum game\_status game\_status;  
 enum game\_difficulty game\_difficulty;  
 enum color\_selection color\_selection;  
 int selected\_colors\[COLOR\_SELECTION\_LEN\];  
} t\_color\_game;  
As usual, also modify the ***init\_game*** function and initialize each member to NULL  
  
...   
 return (t\_color\_game){mlx, img, img2, foreground\_img,  
 {difficulty\_imgs\[0\], difficulty\_imgs\[1\], difficulty\_imgs\[2\]},  
**{0},** anim, small\_anim, SELECT\_PLAY, MENU, EASY, RED,  
 {255, 255, 255}};  
}  
Now let's go to the update function to draw each color selection with its value!  
  
We had a condition for when we are playing, where we draw the playing background. After this we will need to create an image with the text.  
  
In total we have 3 colors, for each of the colors we will need to draw an image. So the code needs to be inside a loop.  
  
The pseudo-code could look something like this:  
  
COLORS\_LEN 3  
  
for n in COLOR\_LEN ---> Iterate through each color (Red, Green & Blue)  
 color\_amount = get\_color\_amoun(n) ---> Get the color value \[0..255\]  
 color\_str = num\_to\_str(color\_amount) ---> Get number as string  
 img = generate\_string\_image(color\_str) ---> Generate an image from string  
 copy\_image\_to\_image(foregroun, img) ---> Put the image to the foreground  
 delete\_img(img) ---> Delete the image  
  
void update(void \* ptr) {  
...  
 if (cg->game\_status == PLAYING) {  
...  
 for (int n = 0; n < COLOR\_SELECTION\_LEN; n++) {  
 char \* num\_str = ft\_itoa(cg->selected\_colors\[n\]);  
 cg->font\_imgs\[n\] = mlx\_put\_string(cg->mlx, num\_str, 0, 0);  
 free(num\_str);  
 put\_img\_to\_img(cg->foreground, cg->font\_imgs\[n\], color\_selection\_coords\[n\]\[0\], color\_selection\_coords\[n\]\[1\]);  
 mlx\_delete\_image(cg->mlx, cg->font\_imgs\[n\]);  
 **}**  
} else if (cg->game\_status == MENU) {  
...  
If we run the code as it is, you will realize there are two problems, let's look at the picture from the code generated:

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2024-02-01_21-46-38.png)

# Font Color

The mlx\_put\_string will generate white text, which will be problematic because our background is also white, we need to change the font color.  
We can change the color very easily, before we copy the font image to the foreground, let's change all the pixels which were drawn to another color!  
I will use the color (#424242)  
  
for (int i = 0; i < cg->font\_imgs\[n\]->width; i++) {  
 for (int j = 0; j < cg->font\_imgs\[n\]->height; j++) {  
 if (mlx\_get\_pixel(cg->font\_imgs\[n\], i, j) != 0)  
 mlx\_put\_pixel(cg->font\_imgs\[n\], i, j, 0x424242FF);  
 }  
}  
Now you will realize the color is gray instead of white, but still, the number is not alight at the center and it covers the animation.  
We can just move the animation logic bellow in order to draw the animation on top of the text.  
In order to center the string, we can use a padding for the horizontal and a padding for the vertical.  
I recommend a fixed padding of **\+ 5** pixels for the vertical (y) and for the horizontal padding it would be something like this:

> **+9px** if amount < 10, **+5px** if amount < 100 & **+0 pixels** else.

The code would look something like this:  
  
void update(void \* ptr) {  
 static int menu\_selection\_coords\[SELECTION\_LEN\]\[2\] = {{256, 160}, {256, 256}};  
 static int color\_selection\_coords\[COLOR\_SELECTION\_LEN\]\[2\] = {{320, 256}, {416, 256}, {512, 256}};  
  
 t\_color\_game\* cg = (t\_color\_game\*)ptr;  
  
 memset(cg->foreground->pixels, 0xFF000000, cg->foreground->width \* cg->foreground->height \* BPP);  
 if (cg->game\_status == PLAYING) {  
 // Put the number (amount) of each color selection  
 for (int n = 0; n < COLOR\_SELECTION\_LEN; n++) {  
 char \* num\_str = ft\_itoa(cg->selected\_colors\[n\]);  
 int padding\_left = 0;  
 if (cg->selected\_colors\[n\] < 100)  
 padding\_left += 6;  
 if (cg->selected\_colors\[n\] < 9)  
 padding\_left += 5;  
cg->font\_imgs\[n\] = mlx\_put\_string(cg->mlx, num\_str, 0, 0);  
 free(num\_str);  
 for (int i = 0; i < cg->font\_imgs\[n\]->width; i++) {  
 for (int j = 0; j < cg->font\_imgs\[n\]->height; j++) {  
 if (mlx\_get\_pixel(cg->font\_imgs\[n\], i, j) != 0)  
 mlx\_put\_pixel(cg->font\_imgs\[n\], i, j, 0x424242FF);  
 }  
 }  
 put\_img\_to\_img(cg->foreground, cg->font\_imgs\[n\], color\_selection\_coords\[n\]\[0\] **+** **padding\_left**, color\_selection\_coords\[n\]\[1\] **\+ 5**);  
 mlx\_delete\_image(cg->mlx, cg->font\_imgs\[n\]);   
 }  
 // Draw the color selection animation  
 mlx\_image\_t \* frame = (mlx\_image\_t \*)ft\_lstget(cg->small\_select\_animation->frames, cg->small\_select\_animation->current\_frame\_num)->content;  
 if (!frame)  
 error();  
 put\_img\_to\_img(cg->foreground, frame,  
 color\_selection\_coords\[cg->color\_selection\]\[0\],  
 color\_selection\_coords\[cg->color\_selection\]\[1\]);  
 update\_animation(cg->small\_select\_animation, cg->mlx->delta\_time);  
 } else if (cg->game\_status == MENU) {  
...  
}

![](https://evil-penguin.com/content/images/2026/11/Screencastfrom2024-02-0122-03-05-ezgif.com-video-to-gif-converter.gif)

Now it's time to put the color that the user is generating to the screen, I decided to use a set of circles to do this. The result will look something like this:  
  
For the example I am using a fixed color: **rgb(66, 135, 245)** or **#4287f5**  
To win you would need to input a color that is very similar to that one!

![](https://evil-penguin.com/content/images/2026/11/Screencastfrom2024-02-0122-08-10-ezgif.com-video-to-gif-converter_1_.gif)

In this tutorial I won't be explaining how the draw\_circle function works but you can read about it [here](https://en.wikipedia.org/wiki/Midpoint%5Fcircle%5Falgorithm?ref=evil-penguin.com)  
There are many ways to draw a circle, in this case I choose to implement the Bresenham\`s Algorithm (Mid Point Circle Algo).  
Basically for each iteration you choose 8 points in the circle and draw a horizontal line between each pair of points. It really is not very complicated.  
This might be an overkill but I also implemented a function to draw a line, using once again... [Bresenham's line algorithm](http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html?ref=evil-penguin.com) (yes by the same guy, Jack Bresenham, he's very cool guy)  
  
***draw\_utils.c***  
  
#include <stdlib.h>  
#include "MLX42/MLX42.h"  
  
void draw\_line(mlx\_image\_t \* img, int x0, int y0, int x1, int y1, uint32\_t color) {  
 int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;  
 int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1;   
 int err = (dx>dy ? dx : -dy)/2, e2;  
  
 for(;;){  
 mlx\_put\_pixel(img, x0, y0, color);  
 if (x0==x1 && y0==y1)  
 break;  
 e2 = err;  
 if (e2 >-dx) {  
 err -= dy;  
 x0 += sx;  
 }  
 if (e2 < dy) {  
 err += dx;  
 y0 += sy;  
 }  
 }  
}   
  
static void draw\_circle\_coords(mlx\_image\_t \* img, int xc, int yc, int x, int y, uint32\_t color) {  
 draw\_line(img, xc+x, yc+y, xc-x, yc+y, color);  
 draw\_line(img, xc+x, yc-y, xc-x, yc-y, color);  
 draw\_line(img, xc+y, yc+x, xc-y, yc+x,   
 draw\_line(img, xc+y, yc-x, xc-y, yc-x, color);  
}  
  
void draw\_circle(mlx\_image\_t \* img, int xc, int yc, int r, uint32\_t color) {  
 int x = 0, y = r;  
 int d = 3 - 2 \* r;  
 while (y >= x) {  
 /\*for each pixel we will draw all eight pixels \*/  
 draw\_circle\_coords(img, xc, yc, x, y, color);  
 x++;  
 /\*check for decision parameter and correspondingly update d, x, y\*/  
 if (d > 0) {  
 y--;  
 d = d + 4 \* (x - y) + 10;  
 } else {  
 d = d + 4 \* x + 6;  
 }  
 draw\_circle\_coords(img, xc, yc, x, y, color);  
 }  
}Now you can draw the 3 circles to make the effect that you see on the scree. The biggest circle should be drawn first, and the smaller at the end.  
So you will be drawing one circle on top on the other, which will make the screen effect.  
  
 // Draw the circles  
 int user\_color = get\_rgba(cg->selected\_colors\[RED\], cg->selected\_colors\[GREEN\], cg->selected\_colors\[BLUE\], 255);  
 draw\_circle(cg->foreground, (WIDTH / 20) \* 13.5, (HEIGHT / 5) \* 2, 72, 0xFF);  
 draw\_circle(cg->foreground, (WIDTH / 20) \* 13.5, (HEIGHT / 5) \* 2, 70, 0x4287f5FF);  
 draw\_circle(cg->foreground, (WIDTH / 20) \* 13.5, (HEIGHT / 5) \* 2, 42, user\_color);  
So far we are almost finished, now we only need a screen to show the score. We will need to modify our game status and add an extra status, SCORE.  
  
enum game\_status {  
 MENU,  
 PLAYING,  
 **SCORE**  
};  
Now on the update we can implement the score screen.  
When the user decides to submit his color choice, we must calculate his score and show him  
  
First, let's start by adding a score\_bg image  
  
typedef struct s\_color\_game {  
 mlx\_t \* mlx;  
 mlx\_image\_t \* menu\_bg;  
 mlx\_image\_t \* game\_bg;  
 **mlx\_image\_t \* score\_bg;**  
 mlx\_image\_t \* foreground;  
 mlx\_image\_t \* difficulty\_imgs\[DIFFICULTY\_LEN\];  
 mlx\_image\_t \* font\_imgs\[COLOR\_SELECTION\_LEN\];  
 t\_animation \* select\_animation;  
 t\_animation \* small\_select\_animation;  
 enum menu\_selection menu\_selection;  
 enum game\_status game\_status;  
 enum game\_difficulty game\_difficulty;  
 enum color\_selection color\_selection;  
 int selected\_colors\[COLOR\_SELECTION\_LEN\];  
} t\_color\_game;t\_color\_game init\_game() {  
 mlx\_t\* mlx;  
 mlx\_image\_t\* img;  
 mlx\_image\_t\* img2;  
 mlx\_image\_t\* img3;  
 mlx\_image\_t\* foreground\_img;  
 mlx\_image\_t\* difficulty\_imgs\[DIFFICULTY\_LEN\];  
 t\_animation\* anim;  
 t\_animation\* small\_anim;  
 t\_sprite sprite;  
  
 mlx = mlx\_init(WIDTH, HEIGHT, "Color Game", false);  
 if (!mlx)  
 error();  
 // Load the Background and Menu images  
 img = my\_load\_png("./images/menu\_bg.png", mlx);  
 img2 = my\_load\_png("./images/game\_bg.png", mlx);  
 **img3 = my\_load\_png("./images/score\_bg.png", mlx);**  
 img2->instances\[0\].enabled = false; // disable game background for now  
**img3->instances\[0\].enabled = false; // disable score background for now**  
 difficulty\_imgs\[0\] = my\_load\_png("./images/menu\_easy.png", mlx);  
 difficulty\_imgs\[1\] = my\_load\_png("./images/menu\_medium.png", mlx);  
 difficulty\_imgs\[2\] = my\_load\_png("./images/menu\_hard.png", mlx);  
 // Sprite & Animation  
 sprite = new\_sprite("./images/select\_sprite\_sheet.png", mlx);  
 anim = slice\_sprite(&sprite, (sprite\_slice){0, 0, 128, 32, 0, 0}, false, 5, 120);  
 destroy\_sprite(&sprite);  
 // Sprite & Animation (small select)  
 sprite = new\_sprite("./images/small\_select\_sprite\_sheet.png", mlx);  
 small\_anim = slice\_sprite(&sprite, (sprite\_slice){0, 0, 32, 32, 0, 0}, false, 4, 120);  
 destroy\_sprite(&sprite);  
 // Foreground  
 foreground\_img = mlx\_new\_image(mlx, WIDTH, HEIGHT);  
 if (!foreground\_img)  
 error();  
 return (t\_color\_game){mlx, img, img2, **img3**, foreground\_img,  
 {difficulty\_imgs\[0\], difficulty\_imgs\[1\], difficulty\_imgs\[2\]},  
 {0}, anim, small\_anim, SELECT\_PLAY, MENU, EASY, RED,  
 {255, 255, 255}};  
}  
Now, change the key hook, when the user is **PLAYING** and he decides to submit then enable the score screen and disable the game screen.  
  
void key\_update(mlx\_key\_data\_t keydata, void\* ptr) {  
 t\_color\_game\* cg = (t\_color\_game\*)ptr;  
  
 if (cg->game\_status == PLAYING && (keydata.action == MLX\_REPEAT || keydata.action == MLX\_PRESS)) {  
 if (keydata.key == MLX\_KEY\_ENTER) {  
 cg->game\_bg->instances\[0\].enabled = false;  
 cg->score\_bg->instances\[0\].enabled = true;  
 cg->game\_status = SCORE;  
 **} else** if (keydata.key == MLX\_KEY\_DOWN) {  
 if (cg->selected\_colors\[cg->color\_selection\] > 0)  
 cg->selected\_colors\[cg->color\_selection\]--;  
 } else if (keydata.key == MLX\_KEY\_UP) {  
 if (cg->selected\_colors\[cg->color\_selection\] < 255)  
 cg->selected\_colors\[cg->color\_selection\]++;  
 }  
 }  
 ...  
}

![](https://evil-penguin.com/content/images/2026/11/score_bg.png)

Awesome, there are a few things we are going to add to the score screen.  
We will evaluate the user selection with 0, 1, 2 or 3 starts, it will follow this formula:  
**0stars** < 50%, **1star** < 90%, **2stars** < 96% & **3stars** \> 96%  
  
We will develop a function which can compare two colors and return the similarity percentage.  
  
This information will be right bellow "SCORE"  
  
Then we will fill the information under the "Prediction" & "Real Value"  
The first Blank will be a circle of the given color followed by it's hexadecimal representation.  
Then under each line of the color, it will fill each value for each color.  
  
Let's implement a few animations, one for the stars, and another to decorate in the background.  
  
To decorate the background I want to have some dinosaurs doing some random actions in the background.  
I will implement biped (walking in two legs) dinosours.  
  
If you ask me why we must do this, the answer is **42!**  
  
Of course this is not relevant, but it's a nice detail, and we can get much experience using linked lists from this example! 

![](https://evil-penguin.com/content/images/2026/11/Screencastfrom2024-02-0201-17-23-ezgif.com-video-to-gif-converter.gif)

Let's start by adding a list of dinos to our game:  
  
typedef struct s\_color\_game {  
 mlx\_t\* mlx;  
 mlx\_image\_t\* menu\_bg;  
 mlx\_image\_t\* game\_bg;  
 mlx\_image\_t\* score\_bg;  
 mlx\_image\_t\* foreground;  
 mlx\_image\_t\* difficulty\_imgs\[DIFFICULTY\_LEN\];  
 mlx\_image\_t\* font\_imgs\[COLOR\_SELECTION\_LEN\];  
 t\_animation\* select\_animation;  
 t\_animation\* small\_select\_animation;  
**t\_list\* random\_dinos;**  
 enum menu\_selection menu\_selection;  
 enum game\_status game\_status;  
 enum game\_difficulty game\_difficulty;  
 enum color\_selection color\_selection;  
 int selected\_colors\[COLOR\_SELECTION\_LEN\];  
} t\_color\_game;  
And as we've done always, modify the **init\_game** retrun, I will initialize the list to **NULL**.  
  
return (t\_color\_game){mlx, img, img2, img3, foreground\_img,  
 {difficulty\_imgs\[0\], difficulty\_imgs\[1\], difficulty\_imgs\[2\]},  
 {0}, anim, small\_anim, **NULL**, SELECT\_PLAY, MENU, EASY, RED,  
 {255, 255, 255}};  
Now after we've initialized the game, we must initialize the dinos, so on our main, we should initilize our dinos  
  
int32\_t main(void)  
{  
 t\_color\_game cg;  
  
 cg = init\_game();  
**init\_dinos(&cg);**  
  
 if (mlx\_image\_to\_window(cg.mlx, cg.foreground, 0, 0) == -1)  
 error();  
  
 mlx\_loop\_hook(cg.mlx, update, &cg);  
 mlx\_key\_hook(cg.mlx, key\_update, &cg);  
 mlx\_loop(cg.mlx);  
  
 ft\_lstclear(&cg.select\_animation->frames, bait);  
 free(cg.select\_animation);  
 ft\_lstclear(&cg.small\_select\_animation->frames, bait);  
 free(cg.small\_select\_animation);  
 // Free random dinos  
 ft\_lstclear(&cg.random\_dinos, destroy\_dino);  
 **free(cg.random\_dinos);**  
  
 mlx\_terminate(cg.mlx);  
 return (EXIT\_SUCCESS);  
}  
Because the dinos are represented with a linked list (**t\_list**) we need to clean so we don't have any memory leaks.  
You should notice that I called two functions that don't exist so far ***init\_dinos & destroy\_dino***   
  
Before we get to those functions let's define the dinos.  
The dinos are an entity in our game, a dino will have a unique position in the game, it will als have a set of actions.  
A dino might be jumping, running, iddle, or exploding (those are the available animations)

![](https://evil-penguin.com/content/images/2026/11/dino_vita.png)

We should use an enum to represent the dino actions, but the enum should start at 1 instead of 0.  
This will help up later to gather the index for the correct animation. Each animation has a "mirrored" version.  
The dino can be running to the left or to the right, each action can be to the left or right (mirrored).  
In total it's 8 animations. (iddle, running, exploding, jumping, iddle mirrored, running mirrored, exploding mirrored & jumping mirrored)  
  
Each action needs it's own animation  
  
We can easily get the animation index based on the action; Let's visualize the enum for the actions:  
  
1. IDLE
2. RUNNING
3. EXPLODING
4. JUMPING

And the list of animations let's consider to be this one:  
  
animations(actions) \[8\] = {  
\[0\] IDLE,  
\[1\] RUNNING,  
\[2\] EXPLODING,  
\[3\] JUMPING,  
\[4\] IDLE MIRRORED,  
\[5\] RUNNING MIRRORED,  
\[6\] EXPLODING MIRRORED,  
\[7\] JUMPING MIRRORED  
}  
  
We can get the index based on this formula:  
  
> index = (enum action - 1) + (4 \* mirrored)

When **mirrored** is false (**0**), the formula becomes this:  
  
> index = (enum action - 1) + (4 \* 0) <==>   
> index = (enum action - 1) + 0 <==>  
> index = (enum action - 1)

When **mirrored** is true (**1**), the formula becomes this:  
  
> index = (enum action - 1) + (4 \* 1) <==>  
> index = (enum action - 1) + (4)

This should make sense, look how for each animation, the mirrored version is **4** animations beyond.  
  
Finally let's start to define our dinooooooos!  
  
#ifndef \_\_DINOS\_H\_\_  
\# define \_\_DINOS\_H\_\_  
  
#include <stdbool.h>  
#include "MLX42/MLX42.h"  
#include "libft.h"  
  
enum dino\_action {  
 IDLE = 1,  
 RUNNING = 2,  
 EXPLODING = 3,  
 JUMPING = 4  
};  
  
typedef struct s\_dino {  
 int x;  
 int y;  
 enum dino\_action dino\_action;  
 bool mirrored;  
 t\_list \* actions; // List of all the dino actions, 8 total animations  
} t\_dino;  
  
void destroy\_dino(void\* ptr);  
t\_dino \* create\_dino(char\* file\_path, mlx\_t\* mlx);  
  
#endif  
  
  
Let's start by implementing the **create\_dino** function.  
  
***dinos.c***  
  
#include "dinos.h"  
#include "color\_game.h"  
  
t\_dino \* create\_dino(char\* file\_path, mlx\_t\* mlx) {  
 t\_sprite sprite;  
 t\_animation \* animation;  
 t\_dino\* dino;  
  
 sprite\_slice slices\[4\] = {  
 (sprite\_slice){0, 0, 24, 24, 0, 0}, // IDLE \[4 frames\]  
 (sprite\_slice){4 \* 24, 0, 24, 24, 0, 0}, // JUMP \[4 frames\]  
 (sprite\_slice){13 \* 24, 0, 24, 24, 0, 0}, // EXPLODE \[4 frames\]  
 (sprite\_slice){17 \* 24, 0, 24, 24, 0, 0} // RUN \[5 frames\]  
 };  
 **int num\_frames\[4\] = {4, 4, 4, 5};**  
  
 dino = (t\_dino \*)calloc(sizeof(t\_dino), 1);  
 if (!dino)  
 error();  
 // Choose Random starting dino action  
 dino->dino\_action = 1 + (rand() % 4);  
 // Choose random dino spawn  
 dino->y = HEIGHT - 24 - (rand() % 10);  
 dino->x = rand() % WIDTH;  
 // Load sprite and cut all the animations  
 sprite = new\_sprite(file\_path, mlx);  
 for (int i = 0; i < 8; i++) {  
 bool mirrored = i >= 4;  
 animation = slice\_sprite(&sprite, slices\[i % 4\], mirrored, num\_frames\[i % 4\], 300);  
 ft\_lstadd\_back(&dino->actions, ft\_lstnew(animation));  
 }   
 destroy\_sprite(&sprite);  
 return dino;  
}  
It might look like a lot of code, but it's very simple really.  
I highlighted the slices and num frames because I prepared those cuts. All the dino sprites are cut the same.  
In order to save some time, and avoid repetition, I've created that static list for any dino which is created. The slices were calculated beforehand, each dino is **24x24px** and also the frames are pre-calculated.  
  
We are also choosing a random action for the dino and a random spawn (x & y) but in the lower part of the game. All dinos will by default have **mirrored = false**, because calloc initialized everything to zero.  
  
Now to destroy a dino, we should clean the linked list. The dino list is a list of **animations**, that is why for each element of the list we should also clean the list of **frames** of the animation.  
It's almost like a two level recursive function...  
  
***dinos.c***  
  
static void destroy\_animation(void\* ptr) {  
 t\_animation\* a = (t\_animation\*)ptr;  
  
 ft\_lstclear(&a->frames, bait);  
 free(a);  
}  
  
void destroy\_dino(void\* ptr) {  
 t\_dino \* dino = (t\_dino\*)ptr;  
  
 ft\_lstclear(&dino->actions, **destroy\_animation**);  
 free(dino);  
}  
Now just to be able to compile the code, let's implement the ***init\_dinos*** function on our main!  
  
***main.c***  
  
void init\_dinos(t\_color\_game \* cg) {  
 ft\_lstadd\_back(&cg->random\_dinos, ft\_lstnew(create\_dino("./images/dino\_doux.png", cg->mlx)));  
 ft\_lstadd\_back(&cg->random\_dinos, ft\_lstnew(create\_dino("./images/dino\_mort.png", cg->mlx)));  
 ft\_lstadd\_back(&cg->random\_dinos, ft\_lstnew(create\_dino("./images/dino\_tard.png", cg->mlx)));  
 ft\_lstadd\_back(&cg->random\_dinos, ft\_lstnew(create\_dino("./images/dino\_vita.png", cg->mlx)));  
}  
Super simple! We've initialized 4 dinos, you can initialize as many as you want :D  
  
Make sure to compile, because it was so much code at this point!  
  
Finally we just have to animate the dinos!  
  
# Dinos Animation

Let's go to the **update** function, we should add a condition for the score, and draw ALL our dinos!  
  
void update(void\* ptr) {  
 ...  
 if (cg->game\_status == SCORE) {  
 // Update the dinos!  
 ft\_lstiter\_param(cg->random\_dinos, update\_dinos, cg);  
 **} else** if (cg->game\_status == PLAYING) {   
 // Put the number (amount) of each color selection  
 ...  
What is **ft\_lstiter\_param**!? Well, it's not an official libft function but I find it very useful and I've always kept it with me: its basically just ft\_lstiter but you can send an extra parameter. So it's equivalent to this:  
  
void ft\_lstiter\_param(t\_list \*lst, void (\*f)(void \*, void \*), void \* **ptr**)  
{  
 t\_list \*temp;  
  
 temp = lst;  
 while (temp != NULL) {  
 f(temp->content, **ptr**);  
 temp = temp->next;  
 }  
}  
With this function we can iterate through each dino, and also access the game through the parameter.  
  
Finally we just have to implement **update\_dinos*.*  
The update dinos function will be in charge of:  
  
- Updating the animation throughout time
- Drawing the correct frame to the foreground
- Changing the dino orientation (mirrored) **0.5%** probability
- Moving the dino to the left or right, depending on the orientation **50%** probability
- Changing the dino action (IDLE, RUNNING, EXPLODING, JUMPING) **1%** probability

If the walks out of the screen, it should spaw on the other side, giving an infinite effect.void update\_dinos(void\* ptr1, void\* ptr2) {  
 t\_dino\* dino = (t\_dino\*) ptr1;  
 t\_color\_game\* cg = (t\_color\_game\*) ptr2;  
 t\_animation \* action\_animation;  
 int dino\_action\_index;  
  
 dino\_action\_index = **(dino->dino\_action - 1) + (4 \* dino->mirrored);**  
 action\_animation = (t\_animation \*)ft\_lstget(dino->actions, dino\_action\_index)->content;  
  
 mlx\_image\_t \* frame = (mlx\_image\_t \*)ft\_lstget(action\_animation->frames, action\_animation->current\_frame\_num)->content;  
 if (!frame)  
 error();  
 put\_img\_to\_img(cg->foreground, frame, dino->x, dino->y);  
 update\_animation(action\_animation, cg->mlx->delta\_time);  
  
 // Change direction 0.5% probable  
 if (rand() % 1000 < 5)  
 dino->mirrored = !dino->mirrored;  
 // Change dino action 1% probable  
 if (rand() % 100 < 1)  
 dino->dino\_action = 1 + (rand() % 4);  
 // Update dino movement 50% probable  
 if (dino->dino\_action == RUNNING || dino->dino\_action == JUMPING) {  
 if (rand() % 2) {  
 if (dino->mirrored)  
 dino->x--;  
 else  
 dino->x++;  
 if (dino->x < 0)  
 dino->x == WIDTH - 24;  
 dino->x %= WIDTH;  
 }  
 }  
}  
Finally this code should be enough to make your dinos run aroun!  
  
Now we can get to what really matters, calculate the score! 

## Calculating The Score

Before getting started, I want to go back and modify a little bit how we are puting strings to our game.  
We created a list of images with the fonts, but this is not necessary, we can get rid of this, and only create the font images temporarly and delete them after dawing the pixels to the foreground. At the same time, let's add a new member, this is the game colors, the color which the user must guess!  
We also want a member to represent the score which the user had.  
So let's go right ahead:  
  
typedef struct s\_color\_game {  
 mlx\_t\* mlx;  
 mlx\_image\_t\* menu\_bg;  
 mlx\_image\_t\* game\_bg;  
 mlx\_image\_t\* score\_bg;  
 mlx\_image\_t\* foreground;  
 mlx\_image\_t\* difficulty\_imgs\[DIFFICULTY\_LEN\];  
 **mlx\_image\_t\* font\_imgs\[COLOR\_SELECTION\_LEN\];**  
 t\_animation\* select\_animation;  
 t\_animation\* small\_select\_animation;  
 t\_list\* random\_dinos;  
 enum menu\_selection menu\_selection;  
 enum game\_status game\_status;  
 enum game\_difficulty game\_difficulty;  
 enum color\_selection color\_selection;  
 int selected\_colors\[COLOR\_SELECTION\_LEN\];  
 int game\_colors\[COLOR\_SELECTION\_LEN\];  
 **int score;**  
} t\_color\_game;  
And also remove it from ***init\_game***  
  
 ...  
 return (t\_color\_game){mlx, img, img2, img3, foreground\_img,  
 {difficulty\_imgs\[0\], difficulty\_imgs\[1\], difficulty\_imgs\[2\]},  
 **{0},** anim, small\_anim, NULL, SELECT\_PLAY, MENU, EASY, RED,  
 {255, 255, 255}, **{rand() % 256, rand() % 256, rand() % 256}, -1**};  
}  
Now, on the update function, we can simply use a temporary image. But even better than that, we could implement our very own put string!  
  
**my\_mlx\_put\_string**#define GAME\_TEXT\_COLOR 0x424242FF  
  
void my\_mlx\_put\_string(char \* str, t\_color\_game \* cg, int x, int y) {  
 mlx\_image\_t\* tmp\_text;  
  
 tmp\_text = mlx\_put\_string(cg->mlx, str, 0, 0);  
 for (int i = 0; i < tmp\_text->width; i++) {  
 for (int j = 0; j < tmp\_text->height; j++) {  
 if (mlx\_get\_pixel(tmp\_text, i, j) != 0)  
 mlx\_put\_pixel(tmp\_text, i, j, GAME\_TEXT\_COLOR);  
 }  
 }  
 put\_img\_to\_img(cg->foreground, tmp\_text, x, y);  
 mlx\_delete\_image(cg->mlx, tmp\_text);  
}  
This is a very specific function because it puts directly to the foreground, if you prefer, you can implment somehting more generic which takes an mlx\_image\_t instead of t\_color\_game. Up to you!  
  
Now on the update function we can reduce the code a lot! For the logic when **PLAYING** to draw the strings for the color values, we can utilize our brand new funciton!  
  
...  
 } else if (cg->game\_status == PLAYING) {   
 // Put the number (amount) of each color selection  
 for (int n = 0; n < COLOR\_SELECTION\_LEN; n++) {  
 char \* num\_str = ft\_itoa(cg->selected\_colors\[n\]);  
 int padding\_left = 0;  
 if (cg->selected\_colors\[n\] < 100)  
 padding\_left += 6;  
 if (cg->selected\_colors\[n\] < 9)  
 padding\_left += 5;  
 **my\_mlx\_put\_string(num\_str, cg, color\_selection\_coords\[n\]\[0\] + padding\_left, color\_selection\_coords\[n\]\[1\] + 5);**  
 free(num\_str);  
 }  
 ...  
And finally, we have reached the part where we start calculating the score.  
  
At the beggining of this section we added the **game\_colors*,*this is the color which the user has to guess.  
This color should be random, but because **rand()** will always return the same values unless we use a seed, we must implement this.  
  
On the main, at the very top, let's add the following:  
  
#include <time.h>  
  
int32\_t main(void) {  
 srand( time( NULL ) );  
 t\_color\_game cg;  
  
 cg = init\_game();  
 init\_dinos(&cg);  
 ...  
Your update function should now use the game color instead of a fixed number:  
  
void update(void\* ptr) {  
 static int menu\_selection\_coords\[SELECTION\_LEN\]\[2\] = {{256, 160}, {256, 256}};  
 static int color\_selection\_coords\[COLOR\_SELECTION\_LEN\]\[2\] = {{320, 256}, {416, 256}, {512, 256}};  
 **int user\_color, game\_color;**  
  
 t\_color\_game\* cg = (t\_color\_game\*)ptr;  
  
 memset(cg->foreground->pixels, 0xFF000000, cg->foreground->width \* cg->foreground->height \* BPP);  
 user\_color = get\_rgba(cg->selected\_colors\[RED\], cg->selected\_colors\[GREEN\], cg->selected\_colors\[BLUE\], 255);  
 **game\_color = get\_rgba(cg->game\_colors\[RED\], cg->game\_colors\[GREEN\], cg->game\_colors\[BLUE\], 255);**  
 if (cg->game\_status == SCORE) {  
 // Update the dinos!  
 ft\_lstiter\_param(cg->random\_dinos, update\_dinos,   
 } else if (cg->game\_status == PLAYING) {   
 // Put the number (amount) of each color selection  
 for (int n = 0; n < COLOR\_SELECTION\_LEN; n++) {  
 char \* num\_str = ft\_itoa(cg->selected\_colors\[n\]);  
 int padding\_left = 0;  
 if (cg->selected\_colors\[n\] < 100)  
 padding\_left += 6;  
 if (cg->selected\_colors\[n\] < 9)  
 padding\_left += 5;  
 **my\_mlx\_put\_string(num\_str, cg, color\_selection\_coords\[n\]\[0\] + padding\_left, color\_selection\_coords\[n\]\[1\] + 5);**  
 free(num\_str);  
 }  
 // Draw the circles  
 draw\_circle(cg->foreground, (WIDTH / 20) \* 13.5, (HEIGHT / 5) \* 2, 72, 0xFF);  
 draw\_circle(cg->foreground, (WIDTH / 20) \* 13.5, (HEIGHT / 5) \* 2, 70, **game\_color**);  
 draw\_circle(cg->foreground, (WIDTH / 20) \* 13.5, (HEIGHT / 5) \* 2, 42, user\_color);  
 // Draw the color selection animation  
 mlx\_image\_t \* frame = (mlx\_image\_t \*)ft\_lstget(cg->small\_select\_animation->frames, cg->small\_select\_animation->current\_frame\_num)->content;  
 if (!frame)  
 error();  
 put\_img\_to\_img(cg->foreground, frame,  
 color\_selection\_coords\[cg->color\_selection\]\[0\],  
 color\_selection\_coords\[cg->color\_selection\]\[1\]);  
 update\_animation(cg->small\_select\_animation, cg->mlx->delta\_time);  
 } else if (cg->game\_status == MENU) {  
 // Paint the select animation on the foreground  
 mlx\_image\_t \* frame = (mlx\_image\_t \*)ft\_lstget(cg->select\_animation->frames, cg->select\_animation->current\_frame\_num)->content;  
 if (!frame)  
 error();  
 put\_img\_to\_img(cg->foreground, frame,  
 menu\_selection\_coords\[cg->menu\_selection\]\[0\],  
 menu\_selection\_coords\[cg->menu\_selection\]\[1\]);  
 update\_animation(cg->select\_animation, cg->mlx->delta\_time);  
 // Draw the dificulty image to the menu (Easy, Medium or Hard)  
 for (int i = 0; i < DIFFICULTY\_LEN; i++) {  
 cg->difficulty\_imgs\[i\]->instances\[0\].enabled = false;  
 }  
 cg->difficulty\_imgs\[cg->game\_difficulty\]->instances\[0\].enabled = true;  
 // Logic for the menu  
 if (mlx\_is\_key\_down(cg->mlx, MLX\_KEY\_DOWN)) {  
 cg->menu\_selection = SELECT\_DIFFICULTY;  
 }  
 if (mlx\_is\_key\_down(cg->mlx, MLX\_KEY\_UP)) {  
 cg->menu\_selection = SELECT\_PLAY;  
 }  
 }  
}  
And we can finally work on the score!  
  
When the score is **\-1** it means that it has not been calculated, the score is a number \[0,100\] (it represents a percentage)  
  
It's simple to calculcate the score if we concider that the color can be a coordinate in a 3D plane (x,y,z).  
We can calculate the distance between two points in a plane with the formula, that is, how distant is one point from another.  
We can also calculcate the **MAX** possible distance to calculate the percentage (because the max is the 100%) then we can do a simple rule of 3.  
  
#include <math.h>  
  
static double colors\_dist(int r1, int g1, int b1, int r2, int g2, int b2) {  
 return (sqrt(  
 pow((r2 - r1), 2) +  
 pow((g2 - g1), 2) +  
 pow((b2 - b1), 2)));  
}  
  
static int colors\_diff(int r1, int g1, int b1, int r2, int g2, int b2) {  
 double d = colors\_dist(r1, g1, b1, r2, g2, b2);  
 return 100 - (d \* 100) / 441.672956;  
}  
Now thhe **colors\_diff** will return the difference between two colors as a percentage, which is just what we need.  
Let's put all the data to the screen now, we will need to put the following strings:  
  
- Guess Color as hexa
- Circle with guess color
- Game Color as hexa
- Circle with guess color
- Guess Colors (Red, Green, Blue)
- Game Colors (Red, Green, Blue)
- Percentage of difference.

void update(void\* ptr) {  
 static int fps;  
 static int menu\_selection\_coords\[SELECTION\_LEN\]\[2\] = {{256, 160}, {256, 256}};  
 static int color\_selection\_coords\[COLOR\_SELECTION\_LEN\]\[2\] = {{320, 256}, {416, 256}, {512, 256}};  
 int user\_color, game\_color;  
  
 t\_color\_game\* cg = (t\_color\_game\*)ptr;  
  
 fps = 1000 / cg->mlx->delta\_time;  
 // printf("\\e\[1;1H\\e\[2Jfps \[%d\]\\n\\n", fps);  
 // Clean the foreground  
 memset(cg->foreground->pixels, 0xFF000000, cg->foreground->width \* cg->foreground->height \* BPP);  
 user\_color = get\_rgba(cg->selected\_colors\[RED\], cg->selected\_colors\[GREEN\], cg->selected\_colors\[BLUE\], 255);  
 game\_color = get\_rgba(cg->game\_colors\[RED\], cg->game\_colors\[GREEN\], cg->game\_colors\[BLUE\], 255);  
 if (cg->game\_status == SCORE) {  
 // Update the dinos!  
 ft\_lstiter\_param(cg->random\_dinos, update\_dinos, cg);  
 // Update the score if needed  
 if (cg->score < 0) {  
 cg->score = colors\_diff(cg->selected\_colors\[RED\], cg->selected\_colors\[GREEN\], cg->selected\_colors\[BLUE\],  
 cg->game\_colors\[RED\], cg->game\_colors\[GREEN\], cg->game\_colors\[BLUE\]);  
 **}**  
  
 {  
 // Put the score to the screen  
 char buf\[5\] = {0};  
 sprintf(buf, "%d%%", cg->score);  
 my\_mlx\_put\_string(buf, cg, 385, 120);  
 **}**  
 {  
 // Put user hexa color   
 char buf1\[11\] = {0};  
 char buf2\[7\] = {0};  
 sprintf(buf1, "%X", user\_color);  
 ft\_strlcat(buf2, buf1, 7);  
 my\_mlx\_put\_string(buf2, cg, 210, 200);  
 draw\_circle(cg->foreground, 200, 209, 6, user\_color);  
 **}**  
 {  
 // Put game hexa color   
 char buf1\[11\] = {0};  
 char buf2\[7\] = {0};  
 sprintf(buf1, "%X", game\_color);  
 ft\_strlcat(buf2, buf1, 7);  
 my\_mlx\_put\_string(buf2, cg, 370, 200);  
 draw\_circle(cg->foreground, 360, 209, 6, game\_color);  
 **}**  
 {  
 // Draw User Red, Green & Blue  
 int coords\[6\]\[2\] = {  
 {194, 260}, {224, 260}, {258, 260},  
 {354, 260}, {384, 260}, {418, 260},  
 };  
 for (int i = 0; i < 3; i++) {  
 char buf1\[4\] = {0};  
 char buf2\[4\] = {0};  
 sprintf(buf1, "%d", cg->selected\_colors\[i\]);  
 my\_mlx\_put\_string(buf1, cg, coords\[i\]\[0\], coords\[i\]\[1\]);  
 sprintf(buf2, "%d", cg->game\_colors\[i\]);  
 my\_mlx\_put\_string(buf2, cg, coords\[i + 3\]\[0\], coords\[i + 3\]\[1\]);  
 }  
 **}**  
  
 } else if (cg->game\_status == PLAYING) {   
 ...  
I use a lot of buffers, that is because I know for sure the size of the string, and it would not be convenient right now to allocate memory and free for something so pointless.  
  
The score now should look something like this. The only missing thing are the stars! :D

![](https://evil-penguin.com/content/images/2026/11/Screencastfrom2024-02-0204-39-57-ezgif.com-video-to-gif-converter.gif)

Let's add our star animation to the game structure:  
  
typedef struct s\_color\_game {  
 mlx\_t\* mlx;  
 mlx\_image\_t\* menu\_bg;  
 mlx\_image\_t\* game\_bg;  
 mlx\_image\_t\* score\_bg;  
 mlx\_image\_t\* foreground;  
 mlx\_image\_t\* difficulty\_imgs\[DIFFICULTY\_LEN\];  
 t\_animation\* select\_animation;  
 t\_animation\* small\_select\_animation;  
 **t\_animation\* star\_animation;**  
 t\_list\* random\_dinos;  
 enum menu\_selection menu\_selection;  
 enum game\_status game\_status;  
 enum game\_difficulty game\_difficulty;  
 enum color\_selection color\_selection;  
 int selected\_colors\[COLOR\_SELECTION\_LEN\];  
 int game\_colors\[COLOR\_SELECTION\_LEN\];  
 int score;  
} t\_color\_game;  
And also to the **init\_game**  
  
 ...  
 // Star animation  
 sprite = new\_sprite("./images/star.png", mlx);  
 star\_anim = slice\_sprite(&sprite, (sprite\_slice){0, 0, 32, 32, 0, 0}, false, 13, 320);  
 **destroy\_sprite(&sprite);**  
 ...  
 return (t\_color\_game){mlx, img, img2, img3, foreground\_img,  
 {difficulty\_imgs\[0\], difficulty\_imgs\[1\], difficulty\_imgs\[2\]},  
 anim, small\_anim, **star\_anim**, NULL, SELECT\_PLAY, MENU, EASY, RED,  
 {rand() % 256, rand() % 256, rand() % 256},  
 {rand() % 256, rand() % 256, rand() % 255}, -1};  
}  
The animation is **32x32px** and it's 13 frames.

![](https://evil-penguin.com/content/images/2026/11/star.png)

Now we can finally put the star to the screen.  
  
Depending on the score, we might need to put more stars! On the update function :  
  
***update***  
  
...  
 if (cg->game\_status == SCORE) {  
 // Update the dinos!  
 ft\_lstiter\_param(cg->random\_dinos, update\_dinos, cg);  
 // Update the score if needed  
 if (cg->score < 0) {  
 cg->score = colors\_diff(cg->selected\_colors\[RED\], cg->selected\_colors\[GREEN\], cg->selected\_colors\[BLUE\],  
 cg->game\_colors\[RED\], cg->game\_colors\[GREEN\], cg->game\_colors\[BLUE\]);  
 }  
  
 {  
 // Calculate the stars needed  
 mlx\_image\_t \* frame = (mlx\_image\_t \*)ft\_lstget(cg->star\_animation->frames, cg->star\_animation->current\_frame\_num)->content;  
 if (!frame)  
 error();  
 if (cg->score > 50)  
 put\_img\_to\_img(cg->foreground, frame, (WIDTH / 2) - 48, 100);  
 if (cg->score > 90)  
 put\_img\_to\_img(cg->foreground, frame, (WIDTH / 2) - 14, 80);  
 if (cg->score > 96)  
 put\_img\_to\_img(cg->foreground, frame, (WIDTH / 2) + 16, 100);  
  
 update\_animation(cg->star\_animation, cg->mlx->delta\_time);  
 }  
 ...  
 }The game should be playable now!  
  
The only missing thing now is to implement the dificulty, of course, I didn't forget !  
  
I want to go easy... For the easy mode, we can block 2 colors, so the user only has to modify one color, medium, you lock 1 color, and hard mode all colors are available!  
  
Let's implement that, it's really simple, we can just modify the key hook so it wont allow the user to move to the other colors.  
  
void key\_update(mlx\_key\_data\_t keydata, void\* ptr) {  
 ...  
 if (cg->game\_status == PLAYING && keydata.action == MLX\_PRESS) {  
**int available\_colors = COLOR\_SELECTION\_LEN;**  
 if (cg->game\_difficulty == EASY)  
 available\_colors = 1;  
 else if (cg->game\_difficulty == MEDIUM)  
 **available\_colors = 2;**  
 if (keydata.key == MLX\_KEY\_LEFT) {  
 cg->color\_selection--;  
 if (cg->color\_selection == -1)  
 cg->color\_selection = **available\_colors** \- 1;  
 } else if (keydata.key == MLX\_KEY\_RIGHT) {  
 cg->color\_selection++;  
 cg->color\_selection %= **available\_colors**;  
 }  
Now, when easy mode is selected, we must give away the **GREEN** & **BLUE** (the user must only guess one color)  
When medium is selected, we give away **BLUE** (The user must guess RED & GREEN)  
On hard mode, the user must guess all colors.  
  
For this we must also modify the code, when the user decides to start playing, then we must analize the game mode and fill the colors accordingly.  
  
 } else if (cg->game\_status == MENU && keydata.action == MLX\_RELEASE) {  
 if (keydata.key == MLX\_KEY\_ENTER) {  
 if (cg->game\_difficulty == EASY) {  
 cg->selected\_colors\[GREEN\] = cg->game\_colors\[GREEN\];  
 cg->selected\_colors\[BLUE\] = cg->game\_colors\[BLUE\];  
 } else if (cg->game\_difficulty == MEDIUM) {  
 cg->selected\_colors\[BLUE\] = cg->game\_colors\[BLUE\];  
 **}**  
 for (int i = 0; i < DIFFICULTY\_LEN; i++)  
 cg->difficulty\_imgs\[i\]->instances\[0\].enabled = false;  
 cg->menu\_bg->instances\[0\].enabled = false;  
 cg->game\_bg->instances\[0\].enabled = true;  
 cg->game\_status = PLAYING;  
 }  
 ...  
We can implement a lock in order to tell the user that it wont be possible to visit that color.

![](https://evil-penguin.com/content/images/2026/11/Screencastfrom2024-02-0205-24-48-ezgif.com-video-to-gif-converter.gif)

To add the lock, just add an image as we did before with the background images.  
If you want to change the lock color, you can also acomplish this by doing the same trick we did to change the font color.  
  
Finally when you are playing put the image of the lock in the foreground.  
It could look something like this:  
  
 } else if (cg->game\_status == PLAYING) {  
 // Draw the lock on the selection if needed   
 if (cg->game\_difficulty == EASY || cg->game\_difficulty == MEDIUM) {  
 put\_img\_to\_img(cg->foreground, cg->lock\_img,  
 color\_selection\_coords\[2\]\[0\],  
 color\_selection\_coords\[2\]\[1\] + 8);  
 **}**  
 if (cg->game\_difficulty == EASY) {  
 put\_img\_to\_img(cg->foreground, cg->lock\_img,  
 color\_selection\_coords\[1\]\[0\],  
 color\_selection\_coords\[1\]\[1\] + 8);  
 **}**  
 // Put the number (amount) of each color selection  
 for (int n = 0; n < COLOR\_SELECTION\_LEN; n++) {  
 char \* num\_str = ft\_itoa(cg->selected\_colors\[n\]);  
 int padding\_left = 0;  
 ...  
 ...  
The final product will look like this:  
  
![](https://evil-penguin.com/content/images/2026/11/Screencastfrom2024-02-0205-47-49-ezgif.com-video-to-gif-converter.gif)

I hope you liked this tutorial!  
It was super easy to use mlx42, you should totally thank them over in Github!  
Also if you want to see my official repo with this project feel free: (very useful to get the images)  
  
[https://github.com/pulgamecanica/42Course/tree/main/42Documentation/mlx42\_intro](https://github.com/pulgamecanica/42Course/tree/main/42Documentation/mlx42%5Fintro?ref=evil-penguin.com)  
  
  
Farewell and until next time friends!