软件使用
EasyX、VS2013
效果图

个人最高纪录(滑稽
具体实现
顾名思义,找缝插针,插到别的针上游戏结束。主要难点是如何让针转起来,其实就是让每根针的度数都慢慢增加,每次变化都刷新、更新针的位置
源代码
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
int main()
{
const float PI = 3.14159;
int width = 800, height = 600, score = 0;
initgraph(width, height);
setbkcolor(WHITE);
//设置针样式
int lineNum = 0;
float angles[1000];
float lineLen = 160, lineEndX, lineEndY, angle = 0;
float rotateSpeed = PI / 360; //每次所有针都旋转这个角度
setlinestyle(PS_SOLID, 2);
BeginBatchDraw();
while (1)
{
cleardevice();
setlinecolor(BLACK);
line(0, height / 2, lineLen, height / 2); // 左边的针
//让针旋转
for (int i = 0; i < lineNum; i++)
{
angles[i] += rotateSpeed;
if (angles[i] > 2 * PI) //防止角度无限增大
angles[i] -= 2 * PI;
//更新针终点坐标
//因为这里y轴和平时的y轴方向相反,所以角度是负的
lineEndX = lineLen * cos(-angles[i]) + width / 2;
lineEndY = lineLen * sin(-angles[i]) + height / 2;
setlinecolor(BLUE);
if (i == lineNum - 1) //刚插入的针设置为红色
setlinecolor(RED);
//画出针
line(width / 2, height / 2, lineEndX, lineEndY);
}
//按下空格射出针
if (_kbhit() && rotateSpeed != 0)
{
char input = _getch();
if (input == ' ')
{
lineNum++; //针数加1
angles[lineNum - 1] = PI; //因为是横着射出的,所以初始角度是PI
lineEndX = lineLen * cos(-angles[lineNum - 1]) + width / 2;
lineEndY = lineLen * sin(-angles[lineNum - 1]) + height / 2;
line(width / 2, height / 2, lineEndX, lineEndY);
//判断是否插到同一个位置,这里判断方式是两针夹角小于PI / 60
bool flag = true;
for (int i = 0; i < lineNum - 1; i++)
{
if (abs(angles[lineNum - 1] - angles[i]) < PI / 60)
{
rotateSpeed = 0;
flag = false;
break;
}
}
if (flag)
score += 1;
}
}
//设置中心圆盘样式
setlinecolor(RGB(255, 0, 0));
setfillcolor(RGB(255, 0, 0));
fillcircle(width / 2, height / 2, 60);
//输出分数
TCHAR s[20];
_stprintf_s(s, _T("%d"), score);
settextstyle(50, 0, _T("Times"));
settextcolor(RGB(50, 50, 50));
outtextxy(65, 200, s);
FlushBatchDraw();
Sleep(10);
}
_getch();
closegraph();
return 0;
}
版权声明:本文为weixin_43318827原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。