#include<stdio.h>
#include<graphics.h>
#define FPS 100
#define WIN_SIZE 640
#define MAP_SIZE 10//一行多少图片
#define IMG_SIZE //图片宽度
#define MAX_PICTURE_NUM 42//图片数量
#define WIN__SIZE MAP_SIZE*IMG_SIZE+2*IMG_SIZE
IMAGE img_total[2];//初始化数据
IMAGE img_picture[MAX_PICTURE_NUM 42] [2];
int map[MAP_SIZE + 2][MAP_SIZE + 2];//前后有边界+2
MOUSEMSG msg;
POINT begin = { -1,-1 }, end = { -1,-1 };//现代你记得坐标,后点击的坐标
enum STATE//表示点击的状态,第一次&第二次
{
m_begin,
m_end,
};
int flag = m_begin;
void GameInit()
{
srand(GetTickCount());
loadimage(&img_total[0], "/res/animal.bmp");
loadimage(&img_total[1], "/res/bk.jpg",WIN_SIZE,WIN_SIZE);
SetWorkingImage(&img_total[0]);//长图切片,保存数组;
for(int i=0;i<MAX_PICTURE_NUM;i++)//二维数组双重循环
{
for (int k = 0; k < 2; k++)
{
getimage(&img_picture[i][k], k*IMG_SIZE, i*IMG_SIZE, IMG_SIZE, IMG_SIZE);
}
}
SetWorkingImage();
int temp = 1, _count = 1;//_count记录已经初始化的个数
//每张图片10个
for (int i = 0; i < MAP_SIZE; i++)//去头去尾,消除边界
{
for (int k = 1; k <= MAP_SIZE; k++)
{
map[i][k] = temp;
if (_count % 10 == 0)
{
temp++;
}
_count++;
}
}
//打乱数组
for (int i = 1; i < MAP_SIZE; i++)
{
for (int k = 1; k <= MAP_SIZE; k++)
{
//也可以用i,k去与别的交换
int x = rand() % 10 + 1;
int y = rand() % 10 + 1;
int x1 = rand() % 10 + 1;
int y1 = rand() % 10 + 1;
int _swap = map[x][y];
map[x][y] = map[x1][y1];
map[x1][y1] = _swap;
}
}
}
void GameDraw()
{
cleardevice();//背景图
putimage(0, 0, &img_total[1]);//绘制动物图片
for(int i = 1; i <= MAP_SIZE, i++)
{
for (int k = 1; k <= MAP_SIZE; k++)
{
if (map[i][k] > 0)
{
putimage(k*IMG_SIZE, i*IMG_SIZE, &img_picture[i][k][1],SRCAND);
putimage(k*IMG_SIZE, i*IMG_SIZE, &img_picture[i][k][0],SRCPAINT);
}
}
}
}
//游戏控制,鼠标点击获得数组下标
void GameControl()
{
if (MouseHit())
{
msg = GetMouseMsg();
if (msg.uMsg == WM_LBUTTONDOWN && flag==m_begin)
{
begin.y=msg.x/IMG_SIZE;
begin.x = msg.y; /IMG_SIZE;
flag = m_end;
}
else if (msg.uMsg == WM_LBUTTONDOWN && flag == m_end)
{
end.y = msg.x / IMG_SIZE;
end.x = msg.y; / IMG_SIZE;
flag = m_begin;
}
printf("begin:(%d,%d)end:(%d,%d)\n", begin.x, begin.y, end.x, end.y);
}
}
//判断某一点是否有障碍
static int isBlockeed(int x,int y)
{
return map[x][y];
}
//同一水平是否能够消除
int horizon(POINT begin,POINT end)
{
//首先判断是否点击的同一个
if (begin.x == end.x&&begin.y == end.y)
{
return 0;
}
//是否在同一水平方向
if (begin.x!=end.x)
{
return 0;
}
//判断两者之间是否有障碍
int tx = min(begin.y, end.y);
int maxx = max(begin.y, end.y);
for (int i = minx + 1; i < maxx; i++)
{
if (isBlockeed( begin.y,i))
{
return 0;
}
}
return 1;
}
//垂直方向消除
woid vertical()
{
}
//一个拐点
void turn_once()
{
}
//两个拐点
void turn_two()
{
}
voidprintf()
{
for (int i = 0; i < MAP_SIZE + 2; i++)
{
for (int k = 0; k < MAP_SIZE + 2; k++)
{
printf("%d", map[i][k]);
}
printf("\n");
}
}
int mian()
{
initgraph(WIN_SIZE, WIN_SIZE,SHOWCONSOLE);
printf("WELCOME");
GameInit();
//BeginBatchDraw();//开启双缓冲绘图
while (true)
{
GameDraw();
FlushBatchDraw();
GameControl();
if (horizon(begin,end) == 1&&map[begin.x][begin.y]==map[end.x][end.y])
{
map[begin.x][begin.y] = 0;
map[end.x][end.y] = 0;
}
printf("%d", horizon(begin, end));
}
return 0;
}