打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
别碰白块(游戏(笔记)4

得分和游戏完善 避免重复起跳

#include<graphics.h>

#include<conio.h>

#include<stdio.h>

int main()

{

int width, height;  // 游戏画面大小 

width = 600;  // 宽

height = 400; // 高

initgraph(width,height); // 重建一个画布

float ball_x,ball_y,radius; //  圆心坐标 半径

float ball_vy,gravity; //y方向速度,重力加速度

radius = 20;

ball_x = width/4;  //  小球x 坐标

ball_y = height-radius;  //  小球y坐标

ball_vy = 0;     // 初始y方向速度为0

gravity = 0.6; // 重力加速度

float rect_left_x,rect_top_y,rect_width,rect_height;

float rect_vx;  //  方块障碍物相关参数

rect_height = 100;  //方块高度

rect_width = 20;   //方块宽度

rect_left_x = width*3/4;  //方块左边x坐标

rect_top_y = height - rect_height;  //方块顶部y坐标

rect_vx = -3;  //方块x方向速度

int score = 0;  //  得分

int isBallOnFloor = 1 ;  // 小球是否在地面上,避免重复起跳

while (1)  //  一直循环

{

if(kbhit())  // 当按键时

{

char input = _getch();  //获得输入字符

if (input==' ' && isBallOnFloor == 1) //当按下空格键并且小球在地面时时

{

ball_vy = -16; // 给小球一个向上的加速度

isBallOnFloor=0;// 以起跳在空中了

}

}

ball_vy = ball_vy + gravity;    // 根据重力速度更新小球y方向速度

ball_y = ball_y + ball_vy;    //  根据小球y方向速度更新其y坐标 

if (ball_y>=height-radius)  // 如果小球落到地面上

{

ball_y = 0;    //  y速度为0

ball_y = height-radius; // 规范其y坐标,避免落到地面上

isBallOnFloor = 1;  //  表示小球在地面上

}

rect_left_x = rect_left_x + rect_vx; // 方块往左运动

if (rect_left_x<=0)    // 如果方块跑到最左边

{

score = score + 1;    //  得分加1

rect_left_x=width;  // 在最右边重新出现

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))

{

score = 0;  //  得分清零

Sleep(100); // 慢动作效果

}

/* (rect_left_x<=ball_x+radius) //  方块最左边和小球最右边接触

(rect_left_x+rect_width>=ball_x-radius) // 方块最右边和小球最左边接触

(height-rect_height<=ball_y+radius) //  方块最上边边和小球最下边接触 */

cleardevice();  // 清空画面

fillrectangle(rect_left_x,height - rect_height,rect_left_x+rect_width,height); // 绘制方块

fillcircle(ball_x,ball_y,radius); // 绘制小球

//得分

TCHAR s[20];  // 定义字符串数组

_stprintf(s, _T("%d"),score); // 将score转换为文子串

settextstyle(40,0, _T("宋体")); // 设置文字大小、字体

outtextxy(50,30,s); // 输出得分文字

Sleep(10); // 暂停10毫秒

}

_getch();  // 等待按键

closegraph();  // 关闭窗口

return 0;

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
html5弹跳球示例代码
高级动画
新手入门『五』使用结构体, 让多个图形同时运动 – Easy Graphics Engine
《每周一点canvas动画》——圆周运动
Flash AS3.0制作飘动的气泡内含详细注释
as3 来回(上下)移动
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服