栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

跳球过障碍游戏 C++

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

跳球过障碍游戏 C++

啥也不说了,直接上代码,在windows下运行

#include
#include
#include

int main(){
    float width, height, gravity; //游戏画面宽高,重力加速度
    float ball_x, ball_y, ball_vy, radius;//小球圆心坐标、y方向加速度、半径
    float rect_left_x, rect_top_y, rect_width, rect_height, rect_vx;
    int score = 0;
    int isBallOnFloor = 1;

    width = 600;//游戏画面宽度
    height = 400; //游戏画面高度
    gravity = 0.6;//重力加速度
    initgraph(width, height);//新建一个画布

    radius = 20;
    ball_x = width/4;
    ball_y = height - radius;
    ball_vy = 0;

    rect_height = 100;
    rect_width = 20;
    rect_left_x = width *3/4;
    rect_top_y = height - rect_height;
    rect_vx = -3;
    while(1){
        if(kbhit()){ //当按键时
            char input = _getch();
            if(input == ' ' && isBallOnFloor == 1){
                ball_vy = -17;
                isBallOnFloor = 0;
            }
        }
        ball_vy += gravity;
        ball_y += ball_vy;

        if(ball_y >= height - radius){
            ball_vy = 0;
            ball_y =height - radius;
            isBallOnFloor = 1;
        }
        rect_left_x = rect_left_x + rect_vx;
        if(rect_left_x <= 0){
            rect_left_x = width;
            score += 1;
            rect_height = rand() % int(height/4) + height/4;
            rect_vx = rand()/float(RAND_MAX)*4 - 7;
        }
        if((rect_left_x<=ball_x+radius)
            &&(rect_left_x + rect_width >= ball_x - radius)
            &&(height - rect_height <= ball_y + radius)){
                Sleep(100);
                score = 0;
        }
            
        cleardevice();
        fillcircle(ball_x, ball_y,radius);
        fillrectangle(rect_left_x, height-rect_height, rect_left_x+rect_width, height);
        TCHAR s[20];
        _stprintf(s,_T("%d"), score);
        settextstyle(40, 0, _T("宋体"));
        outtextxy(50, 30, s);
        Sleep(10);
    }
    closegraph();
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1029844.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号