首页 > 其他分享 >基于EasyX和Raylib的自由落体小球

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

时间:2023-01-25 20:45:22浏览次数:44  
标签:600 自由落体 300 void EasyX 50 Raylib int vy

这个简陋的小游戏,在 《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;
}

标签:600,自由落体,300,void,EasyX,50,Raylib,int,vy
From: https://www.cnblogs.com/zjutzz/p/17067258.html

相关文章

  • 基于EasyX和Raylib的十字消除
    基于EasyX//根据《C和C++游戏趣味编程》第10章十字消除写出#include<graphics.h>#include<conio.h>//_kbhit()#include<stdio.h>#include<stdlib.h>#inc......
  • 基于EasyX和Raylib的别碰方块
    基于EasyX//根据《C和C++游戏趣味编程》第三章别碰方块写出#include<graphics.h>#include<conio.h>//_kbhit()#include<stdio.h>//检测按下了空格键voi......
  • 基于EasyX和Raylib的推箱子
    基于EasyX//根据《C和C++游戏趣味编程》第九章推箱子写出#include<graphics.h>#include<conio.h>//_kbhit()#include<stdio.h>#include<stdlib.h>//玩......
  • 基于EasyX和Raylib的坚持100秒
    EasyX//根据《C和C++游戏趣味编程》第12章坚持100秒写出#include<graphics.h>#include<conio.h>//_kbhit()#include<stdio.h>#include<stdlib.h>#include......
  • 基于EasyX和Raylib的字符雨
    思路按如下顺序尝试:绘制一个字符下落绘制4个字符(一列)的下落绘制20个字符(一列)的下落,并封装其位置更新、绘制的过程为Rain类的成员函数绘制多个雨滴每个雨滴在更新......
  • 基于EasyX和Raylib的贪吃蛇
    基于EasyX//根据《C和C++游戏趣味编程》第七章贪吃蛇写出#include<graphics.h>#include<conio.h>//_kbhit()#include<stdio.h>#include<stdlib.h>//全......
  • EasyX——概要
    EasyX之绘图(配置与显示图片)EasyX之鼠标   ......
  • C++利用easyX实现一个简单图形化窗口
    在实现这个图形化窗口过程中遇到了一些琐碎的问题,不过还是解决了首先easyX下载地址https://easyx.cn/download下载之后安装到VS上或者自己想使用的软件上就行1#incl......
  • EasyX入门笔记
    基于EasyX的C++图形化界面实现什么是EasyX?EasyX是针对C++的图形库,可以帮助C/C++初学者快速上手图形和游戏编程。比如,可以基于EasyX图形库很快的用几何图形画一......
  • EasyX绘制多边形
    引言:在Easyx中,专门给了一个函数绘制多边形——polygon函数一、打印较简单的多边形像长方形、正方形、三角形、梯形这些多边形较容易打印,因为他们的顶点坐标较容易求出。比如......