基于easyx的小游戏,每一个函数模块都添加了对应的注释,涉及到基本的编程语法。

使用c++中类,继承,成员函数,构造,析构函数等。

代码如下

// rolling ball.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"/*
项目名称:
RollingBall
作者:
tzdhu.z@qq.com (1926594835)
编译环境:
Visual Studio 2017,EasyX_20190219(beta)
发布日期:
2019-2-2
最后修改:
2019-2-23
项目介绍:
本程序由单例设计模式 RollingBall 公有继承 BallAndPlank
protected 便于派生类访问基类数据成员
模板颜色随机,位置随机
界面由初始化界面大小控制,初学者可自行更改
版权声明:
本程序完全由作者所创,不涉及任何侵权行为,仅用于学习
*/// 头文件
#include <graphics.h>
#include <conio.h>
#include <ctime>// 全局变量
const COLORREF BKCOLOR = BLACK;                                                            // 绘图窗口背景颜色
const int max_x = 640;                                                                 // 绘图窗口像素宽度
const int max_y = 480;                                                                 // 绘图窗口像素高度
const int max_smx = GetSystemMetrics(SM_CXSCREEN);                                     // 电脑屏幕像素宽度
const int max_smy = GetSystemMetrics(SM_CYSCREEN);                                     // 电脑屏幕像素高度
const int inverse_operate_score = 600;                                                 // 反向操作启动分数
static IMAGE img_start(80, 40), id_start(80, 40), img_exit(80, 40), id_exit(80, 40);    // 用于按钮效果的 IMAGE// 小球和木板类
class BallAndPlank
{//小球结构体typedef struct BALL{int ball_x;                     // 小球球心位置 xint ball_y;                      // 小球球心位置 y}Ball;//木板结构体typedef struct PLANK{int plank_x;                   // 木板左端位置 xint plank_y;                 // 木板位置 yint plank_len;                 // 木板长度COLORREF plank_color;            // 木板颜色int thorn_x;                 // 尖刺左端位置 xbool is_thorn;                   // 木板是否有尖刺}Plank;public:// 构造函数初始化参数BallAndPlank(){// 小球ball_r = 4;ball_ddx = 1;      //小球加速度ball_dx_min = 0;   //x方向最小步长ball_dx_max = 8;left_right = STOP;ball_dx = ball_dx_min;  //初始化小球速度ball_dy = ball_dx_min;ball_color = RGB(255, 0, 0);//颜色// 木板plank_dy = 1;plank_len_min = 50;plank_len_max = 150;thorn_len = 32;thorn_h = 4;plank_gap = (max_y - 1) / plank_num;//}~BallAndPlank(){// 未定义,默认析构}// 小球颜色绘制小球void DrawBall(int x, int y){setfillcolor(ball_color);solidcircle(x, y, ball_r);}// 背景颜色清除小球void CleanBall(int x, int y){setfillcolor(BKCOLOR);solidcircle(x, y, ball_r);}bool IsThorn(){return (rand() % 1000 > 600) ? true : false;//?}// 木板颜色绘制木板void DrawPlank(){for (int i = 0; i < plank_num; i++){setlinecolor(plank[i].plank_color);line(plank[i].plank_x, plank[i].plank_y, plank[i].plank_x + plank[i].plank_len, plank[i].plank_y);if (plank[i].is_thorn == true){/**********************初始化荆棘****************************/for (int j = plank[i].thorn_x; j < plank[i].thorn_x + thorn_len; j += 2 * thorn_h){line(j, plank[i].plank_y, j + thorn_h, plank[i].plank_y - thorn_h - 1);line(j + thorn_h, plank[i].plank_y - thorn_h - 1, j + 2 * thorn_h, plank[i].plank_y);}}}}// 背景颜色清除木板void CleanPlank(){setlinecolor(BKCOLOR);for (int i = 0; i < plank_num; i++){line(plank[i].plank_x, plank[i].plank_y, plank[i].plank_x + plank[i].plank_len, plank[i].plank_y);if (plank[i].is_thorn == true){for (int j = plank[i].thorn_x; j < plank[i].thorn_x + thorn_len; j += 2 * thorn_h){line(j, plank[i].plank_y, j + thorn_h, plank[i].plank_y - thorn_h - 1);line(j + thorn_h, plank[i].plank_y - thorn_h - 1, j + 2 * thorn_h, plank[i].plank_y);}}}}protected:                                              // 保护用于派生类访问数据成员// 小球属性enum Left_Right { STOP, LEFT, RIGHT };               // 枚举小球左右方向int ball_r;                                          // 小球半径int ball_ddx;                                        // 可视为小球加速度int ball_dx_min;                                 // 小球 x 方向最小步长int ball_dx_max;                                  // 小球 x 方向最大步长int left_right;                                       // 小球左右方向int ball_dx;                                       // 可视为小球 x 方向速度int ball_dy;                                     // 可视为小球 y 方向速度COLORREF ball_color;                             // 小球颜色Ball ball;                                           // 小球结构对象// 木板属性enum Plank_Num { plank_num = 7 };                  // 枚举初始化木板数量int plank_dy;                                       // 可视为木板速度int plank_len_min;                                    // 木板最小长度int plank_len_max;                                 // 木板最大长度int plank_gap;                                     // 木板间隔int thorn_len;                                       // 尖刺长度int thorn_h;                                     // 尖刺高度Plank plank[plank_num];                              // 木板结构对象数组
};// 单例设计模式 RollingBall派生类
class RollingBall : public BallAndPlank
{
public:~RollingBall(){// 未定义}// 获取单例指针static RollingBall *GetInstance(){static RollingBall RB;return &RB;    //返回引用}// 开始前介绍界面void Introduce(){setbkcolor(BKCOLOR);cleardevice();settextcolor(LIGHTMAGENTA);settextstyle(50, 0, _T("黑体"));outtextxy((max_x - textwidth(_T("RollingBall"))) / 2, max_y / 5, _T("RollingBall"));settextcolor(GREEN);settextstyle(25, 0, _T("黑体"));outtextxy((max_x - textwidth(_T("ESC退出,空格暂停"))) / 2, max_y / 5 * 2 + 20, _T("ESC退出,空格暂停"));outtextxy((max_x - textwidth(_T("控制方向:左右方向键,AD,鼠标左右键"))) / 2, max_y / 5 * 2 + 60, _T("控制方向:左右方向键,AD,鼠标左右键"));SetWorkingImage(&img_start);setbkcolor(LIGHTGRAY);cleardevice();settextcolor(BROWN);settextstyle(25, 0, _T("黑体"));outtextxy((80 - textwidth(_T("开始"))) / 2, (40 - textheight(_T("开始"))) / 2, _T("开始"));SetWorkingImage(&id_start);setbkcolor(DARKGRAY);cleardevice();settextcolor(BROWN);settextstyle(25, 0, _T("黑体"));outtextxy((80 - textwidth(_T("开始"))) / 2, (40 - textheight(_T("开始"))) / 2, _T("开始"));SetWorkingImage(&img_exit);setbkcolor(LIGHTGRAY);cleardevice();settextcolor(BROWN);settextstyle(25, 0, _T("黑体"));outtextxy((80 - textwidth(_T("退出"))) / 2, (40 - textheight(_T("退出"))) / 2, _T("退出"));SetWorkingImage(&id_exit);setbkcolor(DARKGRAY);cleardevice();settextcolor(BROWN);settextstyle(25, 0, _T("黑体"));outtextxy((80 - textwidth(_T("退出"))) / 2, (40 - textheight(_T("退出"))) / 2, _T("退出"));SetWorkingImage();int yy = max_y / 4 * 3;int exit_x = max_x / 2 - 200;int start_x = max_x / 2 + 120;putimage(start_x, yy, &img_start);putimage(exit_x, yy, &img_exit);// 检测是否点击相关按钮及按键MOUSEMSG msg;bool selected = false;while (!selected){while (MouseHit()){msg = GetMouseMsg();if ((msg.x >= start_x && msg.x <= start_x + 80 && msg.y >= yy && msg.y <= yy + 40 && msg.uMsg == WM_LBUTTONDOWN) || GetAsyncKeyState(VK_RETURN) & 0x8000){putimage(start_x, yy, &id_start);Sleep(200);putimage(start_x, yy, &img_start);Sleep(100);selected = true;break;}else if ((msg.x >= exit_x && msg.x <= exit_x + 80 && msg.y >= yy && msg.y <= yy + 40 && msg.uMsg == WM_LBUTTONDOWN) || GetAsyncKeyState(VK_ESCAPE) & 0x8000){putimage(exit_x, yy, &id_exit);Sleep(200);putimage(exit_x, yy, &img_exit);Sleep(100);exit(0);}}Sleep(16);}}// 初始化游戏界面void Initialize(){setbkcolor(BKCOLOR);cleardevice();setlinecolor(DARKGRAY);line(0, 0, 0, max_y - 1);line(max_y, 0, max_y, max_y - 1);line(0, 0, max_y - 1, 0);line(0, max_y - 1, max_y - 1, max_y - 1);
/*
顶部尖刺位置*/for (int i = 0; i < max_y; i += 2 * die_top){line(i, 0, i + die_top, die_top);line(i + die_top, die_top, i + 2 * die_top, 0);}for (int i = 0; i < plank_num; i++){plank[i].plank_y = (i + 1) * plank_gap;plank[i].plank_len = rand() % (plank_len_max - plank_len_min) + plank_len_min + 1;plank[i].plank_x = rand() % (max_y - plank[i].plank_len);plank[i].plank_color = HSVtoRGB(float(rand() % 360), float(1.0), float(1.0));plank[i].is_thorn = IsThorn();if (plank[i].is_thorn == true)plank[i].thorn_x = plank[i].plank_x + rand() % (plank[i].plank_len - thorn_len);}plank[3].is_thorn = false;ball.ball_x = plank[3].plank_x + plank[3].plank_len / 2;ball.ball_y = plank[3].plank_y - 1 - ball_r;DrawBall(ball.ball_x, ball.ball_y);DrawPlank();Sleep(sleep_time);}// 检测是否死亡bool IsDead(){if (ball.ball_y <= die_top + ball_r || ball.ball_y >= max_y - 1 - ball_r || is_dead){is_dead = true;return true;}elsereturn false;}// 打印分数,速度及是否开启反向操作void PrintScore(){settextcolor(RED);settextstyle(16, 0, _T("黑体"));score = time_num / 5;TCHAR str[20];_stprintf_s(str, _T("当前得分: %d"), score);outtextxy(max_y + 5, max_y / 6 * 5 + 10, str);
/*
木板移动速度变化
***********
******************/if (score < 50)   plank_dy = 1;else if (score < 200)  plank_dy = 2;else if (score < 500)  plank_dy = 3;else if (score < 1000) plank_dy = 4;else if (score < 1500) plank_dy = 5;else                  plank_dy = 6;_stprintf_s(str, _T("当前速度: %d/6"), plank_dy);outtextxy(max_y + 5, max_y / 2 - 10, str);/*增加难度,反向操作************************************/if (score > inverse_operate_score)outtextxy(max_y + 5, max_y / 11, _T("反向操作 已开启"));elseouttextxy(max_y + 5, max_y / 11, _T("反向操作 未开启"));}// 非ESC结束时显示最终分数void Finish(){if (is_dead){TCHAR str[50];_stprintf_s(str, _T("\t     您的最终得分为: %d\t\t"), score);MessageBox(GetHWnd(), str, _T("游戏结束"), MB_OK);}}// 游戏运行处理void GameRunning(){// 清除小球和木板CleanBall(ball.ball_x, ball.ball_y);CleanPlank();// 计算木板位置for (int i = 0; i < plank_num; i++)plank[i].plank_y -= plank_dy;// 顶木板是否消失,是 。则生成尾木板if (plank[0].plank_y < die_top + ball_r + 1){for (int i = 0; i < plank_num - 1; i++)plank[i] = plank[i + 1];plank[plank_num - 1].plank_y = (plank_num)* plank_gap;plank[plank_num - 1].plank_len = rand() % (plank_len_max - plank_len_min) + plank_len_min + 1;plank[plank_num - 1].plank_x = rand() % (max_y - plank[plank_num - 1].plank_len);plank[plank_num - 1].plank_color = HSVtoRGB(float(rand() % 360), float(1.0), float(1.0));plank[plank_num - 1].is_thorn = IsThorn();if (plank[plank_num - 1].is_thorn == true)plank[plank_num - 1].thorn_x = plank[plank_num - 1].plank_x + rand() % (plank[plank_num - 1].plank_len - thorn_len);}// 计算小球球心 x 位置(加减速效果)if ((GetAsyncKeyState(VK_LEFT) & 0x8000) || (GetAsyncKeyState('A') & 0x8000) || (GetAsyncKeyState(VK_LBUTTON) & 0x8000)){if (score < inverse_operate_score){if (left_right == LEFT)ball_dx = (ball_dx += ball_ddx) > ball_dx_max ? ball_dx_max : ball_dx;else{ball_dx = ball_dx_min;left_right = LEFT;}ball.ball_x = (ball.ball_x -= ball_dx) < ball_r ? ball_r : ball.ball_x;//左移}else{if (left_right == RIGHT)                           /* 反向设置*/ball_dx = (ball_dx += ball_ddx) > ball_dx_max ? ball_dx_max : ball_dx;else{ball_dx = ball_dx_min;left_right = RIGHT;}ball.ball_x = (ball.ball_x += ball_dx) > (max_y - 1 - ball_r) ? max_y - 1 - ball_r : ball.ball_x;}}else if ((GetAsyncKeyState(VK_RIGHT) & 0x8000) || (GetAsyncKeyState('D') & 0x8000) || (GetAsyncKeyState(VK_RBUTTON) & 0x8000)){if (score > inverse_operate_score){if (left_right == LEFT)ball_dx = (ball_dx += ball_ddx) > ball_dx_max ? ball_dx_max : ball_dx;else{ball_dx = ball_dx_min;left_right = LEFT;}ball.ball_x = (ball.ball_x -= ball_dx) < ball_r ? ball_r : ball.ball_x;}else{if (left_right == RIGHT)ball_dx = (ball_dx += ball_ddx) > ball_dx_max ? ball_dx_max : ball_dx;else{ball_dx = ball_dx_min;left_right = RIGHT;}ball.ball_x = (ball.ball_x += ball_dx) > (max_y - 1 - ball_r) ? max_y - 1 - ball_r : ball.ball_x;}}else{ball_dx -= ball_ddx;if (ball_dx > ball_dx_min){if (left_right == LEFT)ball.ball_x = (ball.ball_x -= ball_dx) < ball_r ? ball_r : ball.ball_x;else if (left_right == RIGHT)ball.ball_x = (ball.ball_x += ball_dx) > (max_y - 1 - ball_r) ? max_y - 1 - ball_r : ball.ball_x;}else{ball_dx = ball_dx_min;left_right = STOP;}}// 计算小球球心 y 位置(加速效果)int ii = 0;   // 用于确定小球位于哪块木板上方while (ball.ball_y - plank_dy > plank[ii].plank_y - 1 - ball_r)ii++;if (ii < plank_num &&ball.ball_x >= plank[ii].plank_x && ball.ball_x <= plank[ii].plank_x + plank[ii].plank_len&& (ball.ball_y - plank_dy == plank[ii].plank_y - 1 - ball_r || ball.ball_y >= plank[ii].plank_y - 1 - ball_r)){ball.ball_y = plank[ii].plank_y - 1 - ball_r;ball_dy = ball_dx_min;}else{ball_dy = (ball_dy += ball_ddx) > ball_dx_max ? ball_dx_max : ball_dy;ball.ball_y += ball_dy;if (ii < plank_num &&ball.ball_x >= plank[ii].plank_x && ball.ball_x <= plank[ii].plank_x + plank[ii].plank_len && ball.ball_y >= plank[ii].plank_y - 1 - ball_r){ball.ball_y = plank[ii].plank_y - 1 - ball_r;ball_dy = ball_dx_min;}else if (ball.ball_y > max_y - 1 - ball_r)ball.ball_y = max_y - 1 - ball_r;}// 判断小球是否触碰尖刺if (ball.ball_x >= plank[ii].thorn_x - ball_r / 2 && ball.ball_x <= plank[ii].thorn_x + thorn_len + ball_r / 2&& ball.ball_y == plank[ii].plank_y - 1 - ball_r && plank[ii].is_thorn)is_dead = true;// 绘制木板和小球DrawPlank();DrawBall(ball.ball_x, ball.ball_y);FlushBatchDraw();time_num++;// 打印分数,速度及是否开启反向操作PrintScore();Sleep(sleep_time);}private:// 构造函数初始化参数RollingBall(){score = 0;die_top = 5;time_num = 0;sleep_time = 20;is_dead = false;}RollingBall(const RollingBall &rb) {}                         // 禁止拷贝构造RollingBall &operator = (const RollingBall &rb) {}                // 禁止赋值重载int sleep_time;                                                    // 游戏刷新时间间隔int time_num;                                                    // 记录游戏刷新次数int die_top;                                                 // 顶部尖刺位置int score;                                                     // 记录分数bool is_dead;                                                    // 是否死亡
};// main 主函数
int main()
{initgraph(max_x, max_y, NOMINIMIZE);srand((unsigned)time(NULL));// 获取单例指针RollingBall *rb = RollingBall::GetInstance();rb->Introduce();rb->Initialize();BeginBatchDraw();while (!(GetAsyncKeyState(VK_ESCAPE) & 0x8000)){rb->GameRunning();if (rb->IsDead())break;if (_kbhit() && _getwch() == ' ')_getwch();}EndBatchDraw();rb->Finish();closegraph();return 0;
}

计算机项目 rolling ball相关推荐

  1. 计算机项目教学法探讨,【计算机教学论文】项目教学法在计算机教学中的应用(共3594字)...

    摘要:计算机教学质量的高低决定着技工院校学生计算机专业水平和实践能力的高低,将项目教学法应用于技工院校计算机教学中,能够弥补现有教学模式和教学方法的不足,对提高计算机教学质量有着积极促进作用.因此,笔 ...

  2. 计算机项目教学法探讨,项目教学法在计算机教学中应用计算机教学论文计算机论文...

    <项目教学法在计算机教学中应用计算机教学论文计算机论文>由会员分享,可在线阅读,更多相关<项目教学法在计算机教学中应用计算机教学论文计算机论文(7页珍藏版)>请在人人文库网上搜 ...

  3. 积分学在计算机行业的应用,浅谈积分激励在计算机项目教学中的应用.doc

    PAGE PAGE 1 浅谈积分激励在计算机项目教学中的应用 摘要:爱因斯坦曾说过:"兴趣是最好的老师".如何提高学生的学习兴趣?调动学生的学习积极性,进而提高学生的专业技能水平, ...

  4. 计算机运维相关专业知识考试,计算机项目运维人员考什么证书

    计算机项目是一种属于典型项目的科技工程.在计算机项目有许多的分支,包括软件.系统集成.计算机系统等.在一个计算机项目中,项目运维人员是一个项目的末端,承担着项目的最终测试工作,对于一个项目也是有一定的 ...

  5. 计算机公司招聘应该考什么证书,计算机项目经理可以考哪些证

    计算机项目经理可以考哪些证 项目经理是各行各业中比较重要的一个岗位,项目经理是为企业提供产品的人员.项目有工程项目与商业活动之分.无论是工程项目经理,还是商业活动项目经理,都是为企业提供产品或提供服务 ...

  6. Unity3d项目入门之Rolling Ball

    下面通过分析制作一个简单的收集特定物体的滚球游戏来入门unity,包括操作面板和C#脚本的编写导入,创建Game Object和给Object添加组件等等. 一 初始设置 在Assert下创建主场景M ...

  7. 计算机word实训项目任务说明,计算机项目实训报告怎么写啊

    我用的2000 Win2000中关于页面文件的管理项目在控制面板中,进入"控制面板"后选"系统",然后在"高级/性能选项"中选择" ...

  8. 计算机项目教学法探讨,基于项目教学法的非计算机专业计算机教学的设计和探讨...

    摘 要:非计算机专业计算机教学课程的开设,主要是为了培养学生的计算机应用能力,使学生通过学习计算机解决生活和工作中的实际问题.计算机教学中运用项目教学法,不仅提高了学生在学习过程中的学习热情和积极性, ...

  9. 李三立院士逝世:中国计算机体系结构先驱,曾为国防计算机项目总负责人

    鱼羊 发自 凹非寺 量子位 | 公众号 QbitAI 据清华大学消息,著名计算机专家李三立院士因病医治无效,于4月23日在京去世,享年87岁. 李三立是中国工程院院士.清华大学计算机科学与技术系教授, ...

最新文章

  1. 以远程桌面访问Windows Azure虚拟机(转+译)
  2. angular artDialog未及时更新
  3. flex+php截图Demo
  4. 如何设计一个短URL地址系统
  5. 清橙 A1120 拦截导弹 -- 动态规划(最长上升子序列)
  6. html判断是安卓还是苹果手机,网页能够自己判断是pc端首页还是手机android和苹果。...
  7. Nuc972使用NandFlash时,uboot所需要的改动
  8. xshell连接Linux Server
  9. python学习之最常用的内置函数
  10. 微课|中学生可以这样学Python(5.7节):序列解包
  11. 设置hash后导致的返回问题的解决方案
  12. JAVA基础:从一道面试题看逻辑运算符与、|与||的区别
  13. windows安装composer总结
  14. 三、VueJs 填坑日记之项目文件认识
  15. 初体验scrapy-爬取豆瓣250电影数据
  16. IMU惯导相关开源项目整理
  17. 计算机考研评分标准,考研复试评分标准来啦!
  18. 从零开始的openGL--cs游戏(11)3种常用shader
  19. 信息学奥赛第九节 —— 贪心算法(需要安排几位师傅加工零件 + 排队打水问题)
  20. centos路由查看命令_centos查看ip

热门文章

  1. Springboot+Vue实现在线聊天(通用版)
  2. java gmail smtp_使用Javamail连接到Gmail smtp服务器会忽略指定的端口,并尝试使用25...
  3. 刚刚从GitHub 上扒下来,标星 75k,超牛的《Java面试突击版》,这么高标星果真有原因的
  4. 我如何从月薪1800到年薪百万的饿了么技术总监到自由职业?
  5. F5 iAPP 配置自动备份
  6. 浅谈Springcloud中的几个主流熔断器
  7. 气体全自动切换汇流排液晶控制系统
  8. [并发并行]_[线程模型]_[Pthread线程使用模型之一管道Pipeline]
  9. 3D图形:矩阵的相关知识
  10. Ubuntu/Window下X2Go安装连接同步/上传文件夹(一次性成功)