软件使用
EasyX、VS2013
效果图
具体实现
画5*5个圆,每个圆都是一个结构体,储存圆的坐标和半径。每个圆中都有一个指针,初始时都指向上方(即90°)。鼠标点击某个圈,该圈的指针会顺时针旋转90°,旋转后指向的下一个圆也要进行旋转直到没有圆可以旋转为止。
#include <conio.h>
#include <stdio.h>
#include <graphics.h>
#include <math.h>
#define PI 3.14159
struct Round
{
float x, y; //圆的坐标
float r; //圆的半径
int angleNum;
};
int index[2] = {};
int step, total_angle;
Round rounds[5][5];
void start();
void draw_map();
void updateWithInput();
void rotate(int i, int j);
bool GetNextIndex(int index[2]);
int main()
{
start();
while (1)
{
draw_map();
updateWithInput();
}
_getch();
return 0;
}
void start()
{
//初始化面板
initgraph(600, 700);
setbkcolor(RGB(50, 50, 50));
setlinestyle(PS_SOLID, 3);
cleardevice();
BeginBatchDraw();
//初始化数值
step = 10;
total_angle = 0;
//绘制每个圆
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
rounds[i][j].x = 100 + j * 100;
rounds[i][j].y = 200 + i * 100;
rounds[i][j].r = 30;
rounds[i][j].angleNum = 1;
}
}
}
void draw_map()
{
cleardevice(); //注意清屏
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
//绘制圆
setlinecolor(RGB(200, 200, 200));
circle(rounds[i][j].x, rounds[i][j].y, rounds[i][j].r);
//绘制指针
setlinecolor(RED);
float angle = rounds[i][j].angleNum * PI / 2;
line(rounds[i][j].x, rounds[i][j].y, rounds[i][j].x + rounds[i][j].r * cos(-angle), rounds[i][j].y + rounds[i][j].r * sin(-angle));
}
}
//输出文字
TCHAR s[20];
setbkmode(TRANSPARENT);
_stprintf_s(s, _T("%d 步 %d 度"), step, total_angle);
settextstyle(50, 0, _T("宋体"));
outtextxy(150, 30, s);
settextstyle(20, 0, _T("宋体"));
outtextxy(15, 100, _T("点击一个圆圈 其指针顺时针旋转90度之后 指向的指针依次旋转"));
FlushBatchDraw();
FlushBatchDraw();
}
void updateWithInput()
{
MOUSEMSG m;
if (MouseHit()) //如果有鼠标点击
{
m = GetMouseMsg();
if (m.uMsg == WM_LBUTTONDOWN && step > 0)
{
step--;
//找出鼠标点击的圆的位置
int click_i = int(m.y - 150) / 100;
int click_j = int(m.x - 50) / 100;
rotate(click_i, click_j); //旋转该圆的指针
draw_map(); //重新绘制面板
Sleep(300);
int index[2] = { click_i, click_j };
//循环操作指针指向的圆,直到找不到下一个圆
while (GetNextIndex(index))
{
rotate(index[0], index[1]);
draw_map();
Sleep(800);
}
}
}
}
void rotate(int i, int j)
{
rounds[i][j].angleNum -= 1;
if (rounds[i][j].angleNum < 0)
rounds[i][j].angleNum = 3;
total_angle += 90; //记录总体旋转的角度
}
bool GetNextIndex(int index[2])
{
int i = index[0];
int j = index[1];
if (rounds[i][j].angleNum == 0) //angleNum为0表示指向右,j++
j++;
else if (rounds[i][j].angleNum == 1) //angleNum为1表示指向上,i--
i--;
else if (rounds[i][j].angleNum == 2) //angleNum为2表示指向左,j--
j--;
else if (rounds[i][j].angleNum == 3) //angleNum为3表示指向下,i++
i++;
index[0] = i;
index[1] = j;
//判断是否出界
if (i >= 0 && i < 5 && j >= 0 && j < 5)
return true;
else
return false;
}