学无先后,达者为师

网站首页 编程语言 正文

Easyx实现窗口自动碰撞的小球_C 语言

作者:shi_xiaobin   更新时间: 2022-03-28 编程语言

本文实例为大家分享了Easyx实现窗口自动碰撞的小球的具体代码,供大家参考,具体内容如下

代码:

#include<easyx.h>
#include<stdlib.h>
#include<time.h>
 
int main()
{
    //创建窗口
    initgraph(640, 480);
 
    //定义小球的数据
    int bx = getwidth() / 2;
    int by = getheight() / 2;
    int br = 20;
    int xSpeed = 5;//速度
    int ySpeed = 5;
    
    //处理消息
    while (true)
    {
        int startTime = clock();//获取当前的毫秒数(程序启动到调用clock的时间)
        //双缓冲
        BeginBatchDraw();//开始双缓冲
        //清屏
        cleardevice();
        //绘制小球
        setfillcolor(GREEN);
        solidcircle(bx, by, br);
 
        //移动小球
        bx += xSpeed;
        by += ySpeed;
 
        //如果碰撞到边界就反弹
        if (bx+br>getwidth()||bx - br < 0)
        {
            xSpeed = -xSpeed;
        }
        if (by + br > getheight() || by - br < 0)
        {
            ySpeed = -ySpeed;
        }
        
        static ExMessage msg;//每次循环的时候,不要重新定义
        while (peekmessage(&msg,EM_MOUSE | EM_KEY))
        {
 
        }
        EndBatchDraw();//把内存中的图片显示到窗口上
 
        //fps帧数  一般游戏是24帧数或60帧数  怎么控制帧率  1000毫秒/60帧=16.666
        int frameTime = clock() - startTime;//获取当前帧执行了多少毫秒
        //如果当前帧执行时间小于美珍应该执行的时间(提前执行完毕)
        if (frameTime < 1000 / 60)
        {
            Sleep(1000 / 60 - frameTime);//多余的时间睡觉
            //Sleep(16);
        }
        
    }
 
    return 0;
}

原文链接:https://blog.csdn.net/shi_xiaobin/article/details/122448763

栏目分类
最近更新