基於EasyX和Raylib的自由落體小球

這個簡陋的小遊戲,在 《C和C++遊戲趣味編程》 第三章, 是逐次迭代寫成的。這裏貼出基於 easyx 和 raylib 的各自實現。

基於 EasyX

// 根據《C和C++遊戲趣味編程》第二章 仿真“自由落體小球” 寫出

#include <graphics.h>
#include <conio.h> // _kbhit()
#include <stdio.h>

int mainxx()
{
    const int screen_width = 600;
    const int screen_height = 800;

    initgraph(screen_width, screen_height, SHOWCONSOLE);

    int y = 50;
    int step = 1;
    while (true)
    {
        y += step;
        if (y > 620)
        {
            y = -20;
        }
        cleardevice();
        fillcircle(300, y, 20);
        Sleep(10);
    }

    getchar();
    closegraph();

    return 0;
}

// 顯示窗口
void demo_2_1()
{
    initgraph(600, 600, SHOWCONSOLE);
    getchar();
    closegraph();
}

// 顯示一個靜止小球
void demo_2_2_1()
{
    initgraph(600, 600, SHOWCONSOLE);
    fillcircle(300, 300, 100);
    getchar();
    closegraph();
}

// 顯示多個小球
void demo_2_2_2()
{
    initgraph(600, 800, SHOWCONSOLE);
    fillcircle(300, 100, 50);
    fillcircle(300, 250, 50);
    fillcircle(300, 400, 50);
    getchar();
    closegraph();
}

// 利用變量修改小球位置
void demo_2_4_4()
{
    int height = 800;
    initgraph(600, height, SHOWCONSOLE);
    fillcircle(300, 1 * height / 4, 50);
    fillcircle(300, 2 * height / 4, 50);
    fillcircle(300, 3 * height / 4, 50);
    getchar();
    closegraph();
}

// 小球下落動畫
void demo_2_5_2()
{
    int y = 100;
    int step = 100;
    initgraph(600, 600, SHOWCONSOLE);
    
    for (int i = 0; i < 5; i++)
    {
        cleardevice();
        fillcircle(300, y, 20);
        Sleep(200);
        y += step;
    }

    getchar();
    closegraph();
}

// 利用if語句實現小球重複下落
void demo_2_7_1()
{
    int y = 50;
    initgraph(600, 600);
    while (1)
    {
        y = y + 1;
        if (y > 620)
        {
            y = -20;
        }
        BeginBatchDraw();
        {
            cleardevice();
            fillcircle(300, y, 20);
            Sleep(10);
        }
        EndBatchDraw();
    }
    closegraph();
}

// 小球落地反彈
void demo_2_8_2()
{
    int y = 50;
    int vy = 3;
    initgraph(600, 600);
    while (1)
    {
        y = y + vy;
        if (y >= 620)
        {
            vy = -vy;
        }
        BeginBatchDraw();
        cleardevice();
        fillcircle(300, y, 20);
        Sleep(10);
        EndBatchDraw();
    }
    getchar();
    closegraph();
}

// 小球加速下落
void demo_2_9_3()
{
    float y = 100;
    float vy = 0;
    float g = 0.5;
    initgraph(600, 600);
    while (1)
    {
        vy = vy + g;
        y = y + vy;
        if (y >= 580)
        {
            vy = -0.95 * vy;
        }
        if (y > 580)
        {
            y = 580;
        }
        BeginBatchDraw();
        cleardevice();
        fillcircle(300, y, 20);
        Sleep(10);
        EndBatchDraw();
    }
    getchar();
    closegraph();
}

// 拋物線運動的小球
void ex_2_8()
{
    float x = 100;
    float y = 200;
    float vx = 8;
    float vy = 0;
    float g = 0.5;
    initgraph(600, 600);
    while (1)
    {
        vy = vy + g;
        x = x + vx;
        y = y + vy;
        if (y >= 580)
        {
            vx = 0.98 * vx; // x方向速度受阻尼影響變小
            vy = -0.95 * vy; // y方向速度改變方向,並受阻尼影響變小
        }
        if (y > 580)
        {
            y = 580;
        }

        if (x >= 580)
        {
            vx = -vx;
        }
        if (x <= 20)
        {
            vx = -vx;
        }
        BeginBatchDraw();
        cleardevice();
        fillcircle(x, y, 20);
        Sleep(10);
        EndBatchDraw();
    }
    getchar();
    closegraph();
}

int main()
{
    //demo_2_1();
    //demo_2_2_1();
    //demo_2_2_2();
    //demo_2_4_4();
    //demo_2_5_2();
    //demo_2_8_2();
    //demo_2_9_3();
    ex_2_8();
    
    return 0;
}

基於 Raylib

// 根據《C和C++遊戲趣味編程》第二章 仿真“自由落體小球” 寫出

#include "raylib.h"
#include <stdio.h>
#include <stdlib.h>

// 顯示窗口
void demo_2_1()
{
    InitWindow(600, 600, "free falling ball");
    SetTargetFPS(60);
    while (!WindowShouldClose())
    {
        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
        }
        EndDrawing();
    }
    CloseWindow();
}

// 顯示一個靜止小球
void demo_2_2_1()
{
    InitWindow(600, 600, "free falling ball");
    SetTargetFPS(60);
    while (!WindowShouldClose()) {
        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
            DrawCircle(300, 300, 100, WHITE);
        }
        EndDrawing();
    }
    CloseWindow();
}

// 顯示多個小球
void demo_2_2_2()
{
    InitWindow(600, 800, "free falling ball");
    SetTargetFPS(60);
    while (!WindowShouldClose()) {
        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
            DrawCircle(300, 100, 50, WHITE);
            DrawCircle(300, 250, 50, WHITE);
            DrawCircle(300, 400, 50, WHITE);
        }
        EndDrawing();
    }
    CloseWindow();
}

// 利用變量修改小球位置
void demo_2_4_4()
{
    int height = 800;
    InitWindow(600, height, "free falling ball");
    SetTargetFPS(60);
    while (!WindowShouldClose())
    {
        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
            DrawCircle(300, 1 * height / 4, 50, WHITE);
            DrawCircle(300, 2 * height / 4, 50, WHITE);
            DrawCircle(300, 3 * height / 4, 50, WHITE);
        }
        EndDrawing();
    }
    CloseWindow();
}

// 小球下落動畫
void demo_2_5_2()
{
    int y = 100;
    InitWindow(600, 600, "free falling ball");
    SetTargetFPS(5);

    int step = 100;
    int cnt = 0;
    while (!WindowShouldClose())
    {
        // Update
        cnt++;

        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
            DrawCircle(300, y, 20, WHITE);
            if (cnt < 6)
            {
                y += step;
            }
        }
        EndDrawing();
    }
    
    CloseWindow();
}

// 小球下落動畫
void demo_2_7_1()
{
    InitWindow(600, 600, "free falling ball");
    SetTargetFPS(60);

    int y = 50;
    while (!WindowShouldClose())
    {
        // Update
        y = y + 1;
        if (y > 620)
        {
            y = -20;
        }

        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
            DrawCircle(300, y, 20, WHITE);
        }
        EndDrawing();
    }

    CloseWindow();
}

// 小球落地反彈
void demo_2_8_2()
{
    InitWindow(600, 600, "free falling ball");
    SetTargetFPS(100);

    int y = 50;
    int vy = 3;
    while (!WindowShouldClose())
    {
        // Update
        y = y + vy;
        if (y >= 620)
        {
            vy = -vy;
        }

        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
            DrawCircle(300, y, 20, WHITE);
        }
        EndDrawing();
    }

    CloseWindow();
}

// 小球加速下落
void demo_2_9_3()
{
    InitWindow(600, 600, "free falling ball");
    SetTargetFPS(60);

    float y = 100;
    float vy = 0;
    float g = 0.5;
    while (!WindowShouldClose())
    {
        // Update
        vy = vy + g;
        y = y + vy;
        if (y >= 580)
        {
            vy = -0.95 * vy;
        }
        if (y > 580)
        {
            y = 580;
        }

        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
            DrawCircle(300, y, 20, WHITE);
        }
        EndDrawing();
    }

    CloseWindow();
}

// 拋物線運動的小球
void ex_2_8()
{
    InitWindow(600, 600, "free falling ball");
    SetTargetFPS(60);

    float x = 100;
    float y = 200;
    float vx = 8;
    float vy = 0;
    float g = 0.5;
    while (!WindowShouldClose())
    {
        // Update
        vy = vy + g;
        x = x + vx;
        y = y + vy;
        if (y >= 580)
        {
            vx = 0.98 * vx; // x方向速度受阻尼影響變小
            vy = -0.95 * vy; // y方向速度改變方向,並受阻尼影響變小
        }
        if (y > 580)
        {
            y = 580;
        }

        if (x >= 580)
        {
            vx = -vx;
        }
        if (x <= 20)
        {
            vx = -vx;
        }

        // Draw
        BeginDrawing();
        {
            ClearBackground(BLACK);
            DrawCircle(x, y, 20, WHITE);
        }
        EndDrawing();
    }

    CloseWindow();
}

int main()
{
    //demo_2_1();
    //demo_2_2_1();
    //demo_2_2_2();
    //demo_2_4_4();
    //demo_2_5_2();
    //demo_2_7_1();
    //demo_2_8_2();
    //demo_2_9_3();
    ex_2_8();

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章