本文作者:C++橙羊
本文代码适合编译环境:DEV-C++
✨温馨提示:此文转载于codebus
最近橙羊在Easyx官网的codebus里随便逛逛的时候,发现了一个有意思的小游戏:挑战六秒。
说实话,我的手速真差,好几次都没按到六秒整。
游戏规则:
按“空格键”开始,再按暂停,继续按将置零!准确暂停到六秒整挑战成功!
其实这个小游戏在商场特别常见,上一次我逛商场碰到了一个挑战九秒。
效果:
下面吧代码和注释贴出,大家可以试一试。
(DEV-C++和VS都能运行)
#include
#include
#include
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, 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 占用率
}
那么本篇就到这了。我是橙羊,拜拜~