【项目实战】C/C++实现自制游戏:挑战六秒!你能挑战成功吗?

每天一个编程小项目,提升你的编程能力!

挑战六秒的说明

编写这个程序的想法来源于生活中的这个小游戏,一般商场等地方就会有这样的一个供路人来玩的“挑战六秒”的小小的装置。看谁能够准确的暂停到六秒整,感觉这个挺有意思,而且很锻炼人的反应能力和预判能力。

其实编写这个“挑战六秒”程序的想法很早就产生了,但是由于各种原因,只能等到放假空闲下来编写。编写过程还算顺利,几乎是一气呵成,用了半天时间就编写成功了。

程序编写过程

首先我需要将程序的框架构建成功,主要过程就是按键后程序能够结束循环并暂停。这个实现后接下来就是设计程序界面。以前设计界面我都是用尺子在纸上进行勾绘。最近我发现 CAD 其实更适用。使用 CAD 可以准确的设计布局,并且可以找准坐标。为了更加真实的模仿“挑战六秒”,我需要按照电子数字字体绘制数字。我将电子数字字体分成 7 个模块。并将每个模块单独的编写成函数,每个模块的绘制是比较耗费时间的,这 7 各模块可以根据不同的组合组成需要的数字。为了防止出现闪屏的情况,我使用覆盖的方法将每次产生的数字进行覆盖。为了更加精确,我使用了精确延时。至于颜色搭配和布局设计这种,这可能就是小时候办手抄小报时培养的吧,每个人的审美因人而异。

效果图

简单了解游戏后我们就来试试吧!(直接上源码,大家可以看注释)

#include<graphics.h> #include<conio.h> #include<time.h> void name(); // 绘制标题提示等 void one(int x, int y); void two(int x, int y); void three(int x, int y); void four(int x, int y); void five(int x, int y); void six(int x, int y); void seven(int x, int y); // one seven 是组成电子数字的 7 个模块 void HpSleep(int ms); // 精确延时 void OUTNUMBER(int number, int x, int y); // 7 个模块组成数字 0 9 void OUTPUT(int i, int j, int k); // 在屏幕上显示 int main() { initgraph(800, 600); setbkcolor(RGB(6, 31, 62)); cleardevice(); name(); while (true) { OUTPUT(0, 0, 0); _getch(); bool T = false; for (int i = 0; i <= 9; i++) { if (T) { break; } for (int j = 0; j <= 9; j++) { if (T) { break; } for (int k = 0; k <= 9; k++) { if (_kbhit()) { T = true; break; } OUTPUT(i, j, k); HpSleep(10); } } } _getch(); _getch(); } return 0; } void name() { RECT r = { 240, 80, 560, 160 }; settextcolor(RGB(163, 212, 255)); settextstyle(80, 0, _T(“楷体”)); drawtext(_T(“挑战六秒”), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE); settextcolor(RGB(254, 218, 168)); settextstyle(20, 0, _T(“楷体”)); outtextxy(160,450,_T(“提示:按“空格键”开始,再按暂停,继续按将置零!”)); settextcolor(RGB(252, 191, 116)); outtextxy(160, 480, _T(“准确暂停到六秒整挑战成功!”)); } void one(int x, int y) { int pts[] = { 36 + x, 14 + y, 59 + x, 39 + y, 118 + x, 39 + y, 138 + x, 15 + y, 131 + x, 9 + y, 41 + x, 9 + y }; setfillcolor(RGB(176, 215, 254)); solidpolygon((POINT*)pts, 6); } void two(int x, int y) { int pts[] = { 32 + x, 17 + y, 53 + x, 40 + y, 47 + x, 96 + y, 26 + x, 113 + y, 18 + x, 102 + y, 25 + x, 27 + y }; setfillcolor(RGB(184, 249, 251)); solidpolygon((POINT*)pts, 6); } void three(int x, int y) { int pts[] = { 143 + x, 20 + y, 152 + x, 31 + y, 144 + x, 105 + y, 132 + x, 115 + y, 115 + x, 98 + y, 122 + x, 41 + y, }; setfillcolor(RGB(187, 250, 255)); solidpolygon((POINT*)pts, 6); } void four(int x, int y) { int pts[] = { 46 + x, 101 + y, 113 + x, 101 + y, 127 + x, 116 + y, 111 + x, 131 + y, 45 + x, 131 + y, 29 + x, 117 + y }; setfillcolor(RGB(187, 254, 219)); solidpolygon((POINT*)pts, 6); } void five(int x, int y) { int pts[] = { 25 + x, 120 + y, 43 + x, 136 + y, 38 + x, 199 + y, 15 + x, 223 + y, 6 + x, 209 + y, 15 + x, 127 + y }; setfillcolor(RGB(245, 255, 192)); solidpolygon((POINT*)pts, 6); } void six(int x, int y) { int pts[] = { 132 + x, 119 + y, 144 + x, 128 + y, 136 + x, 213 + y, 129 + x, 222 + y, 107 + x, 201 + y, 115 + x, 134 + y }; setfillcolor(RGB(245, 255, 190)); solidpolygon((POINT*)pts, 6); } void seven(int x, int y) { int pts[] = { 40 + x, 203 + y, 104 + x, 203 + y, 126 + x, 226 + y, 120 + x, 231 + y, 24 + x, 231 + y, 18 + x, 226 + y }; setfillcolor(RGB(251, 221, 185)); solidpolygon((POINT*)pts, 6); } void OUTNUMBER(int number, int x, int y) { setfillcolor(RGB(6, 31, 62)); solidrectangle(x, y, x + 160, y + 240); switch (number) { case 0:one(x, y); three(x, y); six(x, y); seven(x, y); five(x, y); two(x, y); break; case 1:three(x, y); six(x, y); break; case 2:one(x, y); three(x, y); four(x, y); five(x, y); seven(x, y); break; case 3:one(x, y); three(x, y); four(x, y); six(x, y); seven(x, y); break; case 4:two(x, y); three(x, y); four(x, y); six(x, y); break; case 5:one(x, y); two(x, y); four(x, y); six(x, y); seven(x, y); break; case 6:one(x, y); two(x, y); four(x, y); six(x, y); seven(x, y); five(x, y); break; case 7:one(x, y); three(x, y); six(x, y); break; case 8:one(x, y); three(x, y); six(x, y); seven(x, y); five(x, y); two(x, y); four(x, y); break; case 9:one(x, y); three(x, y); six(x, y); seven(x, y); two(x, y); four(x, y); break; } } void OUTPUT(int i, int j, int k) { OUTNUMBER(i, 80, 180); setfillcolor(RGB(64, 157, 254)); solidcircle(320, 240, 15); setfillcolor(RGB(163, 212, 255)); solidcircle(320, 360, 15); OUTNUMBER(j, 400, 180); OUTNUMBER(k, 560, 180); } // 精确延时函数(可以精确到 1ms,精度 ±1ms) // 记得加头文件 time.h // by yangw80<yw80@qq.com>, 2011-5-4 void HpSleep(int ms) { static clock_t oldclock = clock(); // 静态变量,记录上一次 tick oldclock += ms * CLOCKS_PER_SEC / 1000; // 更新 tick if (clock() > oldclock) // 如果已经超时,无需延时 oldclock = clock(); else while (clock() < oldclock) // 延时 Sleep(1); // 释放 CPU 控制权,降低 CPU 占用率 }

大家赶紧去动手试试吧!

此外,我也给大家分享我收集的其他资源,帮助大家在学习C语言的道路上披荆斩棘!

整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)最重要的是你可以在群里面交流提问编程问题哦!

对于C/C++感兴趣可以关注小编在后台私信我:【编程交流】一起来学习哦!可以领取一些C/C++的项目学习视频资料哦!已经设置好了关键词自动回复,自动领取就好了!

© 版权声明
THE END