C/C++ 拼图游戏 代码简单 鼠标点击版拼图游戏 带有graphics图形库

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<graphics.h>
#include<string.h>
int map[4][3];
int num = 0;
int x, y;
IMAGE image1, image2, image3, image4, image5, image6, image7, image8, image9, image10, image11, image12, image13;
void Initmap()
{
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			map[i][j] = rand() % 100;
		}
	}
	map[2][3] = 100;

}
void PaintInit()
{
	loadimage(&image1, L"./image/1.jpg", 100, 100);
	loadimage(&image2, L"./image/2.jpg", 100, 100);
	loadimage(&image3, L"./image/3.jpg", 100, 100);
	loadimage(&image4, L"./image/4.jpg", 100, 100);
	loadimage(&image5, L"./image/5.jpg", 100, 100);
	loadimage(&image6, L"./image/6.jpg", 100, 100);
	loadimage(&image7, L"./image/7.jpg", 100, 100);
	loadimage(&image8, L"./image/8.jpg", 100, 100);
	loadimage(&image9, L"./image/9.jpg", 100, 100);
	loadimage(&image10, L"./image/10.jpg", 100, 100);
	loadimage(&image11, L"./image/11.jpg", 100, 100);
	loadimage(&image12, L"./image/back.jpg", 400, 200);
	loadimage(&image13, L"./image/border.jpg", 50, 300);

}
int finMin(int a)
{
	int flag=1;
	
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			if (a > map[i][j])
			{
				flag++;
			}
		}
	}
	return flag;
}
void GameInit()
{
	
	int min=map[0][0];
	putimage(450, 0, &image12);
	putimage(400, 0, &image13);
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
				if (finMin(map[i][j]) == 1)
				{
					putimage(i * 100, j * 100, &image1);
				}
				if (finMin(map[i][j]) == 2)
				{
					putimage(i * 100, j * 100, &image2);
				}
				if (finMin(map[i][j]) == 3)
				{
					putimage(i * 100, j * 100, &image3);
				}
				if (finMin(map[i][j]) == 4)
				{
					putimage(i * 100, j * 100, &image4);
				}
				if (finMin(map[i][j]) == 5)
				{
					putimage(i * 100, j * 100, &image5);
				}
				if (finMin(map[i][j]) == 6)
				{
					putimage(i * 100, j * 100, &image6);
				}
				if (finMin(map[i][j]) == 7)
				{
					putimage(i * 100, j * 100, &image7);
				}
				if (finMin(map[i][j]) == 8)
				{
					putimage(i * 100, j * 100, &image8);
				}
				if (finMin(map[i][j]) == 9)
				{
					putimage(i * 100, j * 100, &image9);
				}
				if (finMin(map[i][j]) == 10)
				{
					putimage(i * 100, j * 100, &image10);
				}
				if (finMin(map[i][j]) == 11)
				{
					putimage(i * 100, j * 100, &image11);
				}
				
		}
	}
	
}
void PlayGame()
{
	
	MOUSEMSG m;
	int newA;
	char ch;
	int r, c;  //空白块的下标  //
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			if (map[i][j] == 100)
			{
				r = i;
				c = j;
			}
		}
	}
	
		m = GetMouseMsg();//鼠标控制
		switch (m.uMsg)
		{
		case WM_LBUTTONDOWN:
			{
				x = m.x / 100;
				y = m.y / 100;
				if (x < 4 && y < 3)
				{
					if (map[x][y + 1] == 100)
					{
						newA = map[r][c];
						map[r][c] = map[r][c - 1];
						map[r][c - 1] = newA;
						num++;
					}
					if (map[x][y - 1] == 100)
					{
						newA = map[r][c];
						map[r][c] = map[r][c + 1];
						map[r][c + 1] = newA;
						num++;
					}
					if (map[x - 1][y] == 100)
					{
						newA = map[r][c];
						map[r][c] = map[r + 1][c];
						map[r + 1][c] = newA;
						num++;
					}
					if (map[x + 1][y] == 100)
					{
						
						newA = map[r][c];
						map[r][c] = map[r - 1][c];
						map[r - 1][c] = newA;
						num++;
					}
				}
			}
		
		
	}
		
		/*//键盘控制
	ch = getch();
	switch (ch)
	{
		
	case 'W':case 'w' :
	{
		
		if (c - 1<0)return;
		newA = map[r][c];
		map[r][c] = map[r][c-1];
		map[r][c-1] = newA;
		num++;
		break;
	}
	case 'S':case 's':
	{
		if (c + 1>2)return;
		newA = map[r][c];
		map[r][c] = map[r][c+1];
		map[r][c+1] = newA;
		num++;
		break;
	}
	case 'A':case 'a':
	{
		if (r - 1<0)return;
		newA = map[r][c];
		map[r][c] = map[r-1][c];
		map[r-1][c] = newA;
		num++;
		break;
	}
	case 'd':case 'D':
	{
		if(r+1>3)return;
		newA = map[r][c];
		map[r][c] = map[r+1][c];
		map[r+1][c] = newA;
		num++;
		break;
	}
	}
	*/
}
int main()
{
	TCHAR sql1[200];
	TCHAR sql2[200];
	initgraph(850, 300);
	Initmap();
	PaintInit();
	
	while (1)
	{
		cleardevice();
		BeginBatchDraw();
		GameInit();
		outtextxy(650, 210, _T("原图"));
		_stprintf(sql1, _T("走了:%d 步"), num);
		_stprintf(sql2, _T("坐标X:%d Y:%d"), x,y);
		outtextxy(500, 250, sql2);
		outtextxy(600, 250, sql1);
		EndBatchDraw();
		PlayGame();
	}
	getch();
	closegraph();
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值