#include<graphics.h>
#include<conio.h>
#include<iostream>
#define maxnumber 14 //单边格子最大数目
#define G_length 50 //格子边长
void welcome();
void PaintGrid(int mid_x, int mid_y, int num, int color);
void left_key(MOUSEMSG m, int num);
void right_key(int num);
int judgefull(int num, int array[maxnumber][maxnumber]);
int mouse(int num);
void nextlevel(int num);
//定义结构体
struct Grid
{
int left;
int right;
int up;
int down;
int array[maxnumber][maxnumber];
int num; //单边格子数目
}
grid;
int main()
{
int end = 0;
grid.num = 4;
welcome();
PaintGrid(320, 240, grid.num, RGB(0, 255, 0));
MOUSEMSG m;
while (end != 1)
{
end = mouse(grid.num);
}
closegraph();
return 0;
}
//欢迎来到点灯游戏
void welcome() {
initgraph(640, 480);
cleardevice();
settextstyle(64, 0, L"黑体");
settextcolor(GREEN);
outtextxy(160, 50, L"亮灯程序");
settextstyle(15, 0, L"宋体");
outtextxy(100, 200, L"每点一个格子,上下左右的的格子也会做出与现状相反的动作");
outtextxy(200, 240, L"总共11关,左键填色,右键清空");
int c = 255;
while (!_kbhit())
{
settextcolor(RGB(0, c, 0));
outtextxy(280, 400, L"请按任意键继续");
c =c- 8;
if (c < 0)
c = 255;
Sleep(30);
}
cleardevice();
}
/*mid_x 中心X的坐标,mid_y 中心Y的坐标, num 单边格子数目, color 格子和线条颜色*/
void PaintGrid(int mid_x, int mid_y, int num, int color)
{
int x, y, nx, ny;
//游戏区域大小
grid.left = mid_x - num * G_length / 2;
grid.right = mid_x + num * G_length / 2;
grid.up = mid_y - num * G_length / 2;
grid.down = mid_y + num * G_length / 2;
//画格子
setlinecolor(color);
for (x = grid.left;x <= grid.right;x += G_length)
line(x, grid.up, x, grid.down);
for (y = grid.up;y<= grid.down;y += G_length)
line(grid.left,y,grid.right,y);
//外边框
for (x = 20; x > 10; x--)
{
line(grid.left - x, grid.up - x, grid.right + x, grid.up - x);
line(grid.left - x, grid.down + x, grid.right + x, grid.down + x);
line(grid.left - x, grid.up - x, grid.left - x, grid.down + x);
line(grid.right + x, grid.up - x, grid.right + x, grid.down + x);
}
//清空单元格
for (x = 0;x < num;x++)
for (y = 0;y < num;y++)
grid.array[x][y] = -1;
for(nx=0;nx<num;nx++)
for (ny = 0;ny < num;ny++)
{
setfillcolor(BLACK);
x = nx * G_length + grid.left;
y = ny * G_length + grid.up;
solidrectangle(x+1,y+1,x+G_length-1,y+G_length-1);
}
}
//鼠标左键点击
void left_key(MOUSEMSG m, int num)
{
int nx, ny, x, y;
if (m.x > grid.left&& m.x<grid.right && m.y>grid.up&& m.y < grid.down)
{
nx = (int)(m.x - grid.left) / G_length;
ny = (int)(m.y - grid.up) / G_length;
grid.array[nx][ny] = -grid.array[nx][ny];
//转换格子颜色
if (nx >= 0 && nx < num - 1)
grid.array[nx + 1][ny] = -grid.array[nx + 1][ny];
if (nx > 0 && nx < num )
grid.array[nx -1][ny] = -grid.array[nx - 1][ny];
if (ny >= 0 && ny < num - 1)
grid.array[nx][ny+1] = -grid.array[nx][ny+1];
if (ny > 0 && ny < num)
grid.array[nx][ny - 1] = -grid.array[nx][ny - 1];
for (nx = 0; nx < num; nx++)
for (ny = 0; ny < num; ny++) {
if (grid.array[nx][ny] == 1)
setfillcolor(GREEN);
else
setfillcolor(BLACK);
x = nx * G_length + grid.left;
y = ny * G_length + grid.up;
solidrectangle(x + 1, y + 1, x + G_length - 1, y + G_length - 1);
}
}
}
//右键清空
void right_key(int num)
{
int x, y, nx, ny;
for (x = 0;x < num;x++)
for (y = 0;y < num;y++)
grid.array[x][y] = -1;
for (nx = 0;nx < num;nx++)
for (ny = 0;ny < num;ny++)
{
setfillcolor(BLACK);
x = nx * G_length + grid.left;
y = ny * G_length + grid.up;
solidrectangle(x + 1, y + 1, x + G_length - 1, y + G_length - 1);
}
}
//判断是否填满
int judgefull(int num, int array[maxnumber][maxnumber])
{
int c = -1;
int nx = 0, ny = 0;
while (nx < num && ny < num)
{
for (nx = 0;nx < num;nx++)
for (ny = 0;ny < num;ny++)
if (array[nx][ny] == 1)
continue;
else
return c;
}
c = 1;
return c;
}
//下一关
void nextlevel(int num)
{
//批量绘图
BeginBatchDraw();
for (int y = 0;y <= 480;y += 5)
{
setlinecolor(RGB(0, 255, 0));
line(0, y, 640, y);
line(0, 480 - y, 640, 480 - y);
outtextxy(300, y - 8,L"再接再厉");
FlushBatchDraw();
Sleep(20);
setfillcolor(BLACK);
solidrectangle(0, y, 640, y - 10);
solidrectangle(0,480- y, 640, 480-y+1);
}
EndBatchDraw();
PaintGrid(320, 240, num, RGB(0, 255, 0));
}
//传递鼠标
int mouse(int num)
{
MOUSEMSG m;
while (1)
{
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
left_key(m, grid.num);
if (judgefull(grid.num, grid.array) == 1)
{
grid.num++;
if (grid.num > maxnumber)
{
return 1;
break;
}
else
nextlevel(grid.num);
}
break;
case WM_RBUTTONDOWN:
right_key(grid.num);
break;
default:
break;
}
}
}
最少步数怎么弄啊