Mastering C Language: Creating a Pulsating Heart Pattern with C Language is So Simple

“From today on, study hard and make progress every day”

Repetition is the best method for memory; spend one minute every day to remember the basics of C language.

“Mastering C Language:C Language Graphics Programming and Game Development

Mastering C Language: Creating a Pulsating Heart Pattern with C Language is So Simple

Preface of this Series

Why learn C language graphical interface programming? Because it is more fun, allowing us to create better-looking interfaces, more entertaining effects, and even develop fun games and tools. Ultimately, after mastering C language graphical interface programming, we can write more advanced and interesting software, giving us more creativity and imagination!

Here, let’s reiterate some key points about graphical interfaces:

First, it is essential to understand that any graphical interface software must run on an operating system and relies on the operating system or third-party graphics libraries.

So, can we write C language graphical interfaces from scratch? The answer is certainly yes; you can think of the operating system as a super-large C language graphical interface program. However, because it is too low-level, it is difficult to use. Therefore, the operating system provides us with libraries that simplify graphical programming, making it easier for us to write graphical interface programs.

Seeing is Believing, Let’s Look at the Program Effect Achieved Today

Source Code and Comments Attached

#include "raylib.h"
#include <math.h>

// Function to draw a heart
void DrawHeart(int centerX, int centerY, float size, Color color)
{
    // Heart drawing algorithm based on parametric equations
    for (float angle = 0; angle < 2 * PI; angle += 0.01f)
    {
        // Heart parametric equations
        float x = 16 * powf(sinf(angle), 3);
        float y = -(13 * cosf(angle) - 5 * cosf(2 * angle) - 2 * cosf(3 * angle) - cosf(4 * angle));

        // Scaling and positioning
        Vector2 pos = {
            centerX + x * size,
            centerY + y * size};

        // Draw pixel
        DrawPixelV(pos, color);
    }
}

int main(void)
{
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "C Language Learning Notes Public Account: Pulsating Heart - raylib");
    SetTargetFPS(60);

    float heartSize = 5.0f;
    float sizeDelta = 0.05f;
    float hue = 0.0f;

    while (!WindowShouldClose())
    {
        // Update heart size 
        heartSize += sizeDelta;
        if (heartSize > 6.0f || heartSize < 4.0f)
        {
            sizeDelta = -sizeDelta;
        }

        // Update color 
        hue += 0.5f;
        if (hue >= 360.0f)
            hue = 0.0f;
        Color heartColor = ColorFromHSV(hue, 0.9f, 0.9f);

        // Draw
        BeginDrawing();
        ClearBackground(BLACK);

        // Draw heart
        DrawHeart(screenWidth / 2, screenHeight / 2, heartSize, heartColor);

        // Draw description text
        DrawText("This is a Love for C and raylib!", 180, 30, 30, RAYWHITE);
        DrawText("Press ESC to exit. Enjoy!", 300, 400, 20, LIGHTGRAY);

        EndDrawing();
    }

    CloseWindow();
    return 0;
}

Some students contacted me, wanting to have a study and exchange group. Previously, I hesitated to create a group due to concerns about advertisements, but considering that having a group is indeed convenient, I will try to create one this time.

If you need it, hurry to join; the validity period is 7 days.

Mastering C Language: Creating a Pulsating Heart Pattern with C Language is So Simple

———- End ———-

[Special Statement: All articles in this public account are original or authorized by the author, please feel free to consume, and the views are for learning reference only~~]

Mastering C Language: Creating a Pulsating Heart Pattern with C Language is So Simple

Mastering C Language: Creating a Pulsating Heart Pattern with C Language is So Simple

“If you like C, please like it”Mastering C Language: Creating a Pulsating Heart Pattern with C Language is So Simple Click the bottom right corner to seeMastering C Language: Creating a Pulsating Heart Pattern with C Language is So Simple

Leave a Comment