基于EasyX和Raylib的自由落体小球

基于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;
}

原文链接: https://www.cnblogs.com/zjutzz/p/17067258.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    基于EasyX和Raylib的自由落体小球

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/313901

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 下午1:05
下一篇 2023年2月16日 下午1:06

相关推荐