拼图过程中,移动的图片与鼠标点击的图片不一致,并且有时候会出现多张黑色的图。

0

拼图过程中,移动的图片与鼠标点击的图片不一致,并且有时候会出现多张黑色的图。

#include<stdio.h>
#include<stdlib.h>
#include<easyx.h>
#include<time.h>
#include<conio.h>
void loadResouce();//加载资源
void UserUI();//渲染
void drawMenu();//菜单
void PlayGame();//游戏过程
void DrawIMG(int black_a, int black_b, int now_a, int now_b);//交换图片
void swap1(int &a, int &b);//交换数组
int array2D[3][3] = { 0 };
int array1D[9] = { 1,2,3,4,5,6,7,8,9 };
int pos;
IMAGE beauty;
IMAGE black;
/*加载资源*/
void loadResouce() {
	int lenth = 9;
	srand((unsigned int)time(NULL));
	loadimage(&beauty, "", 600, 600);//此处添加游戏拼图
	loadimage(&black, "", 200, 200);//此处添加一个纯黑色的图片
	srand((unsigned int)time(NULL));
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			pos = rand() % lenth;
			array2D[i][j] = array1D[pos];
			for (int k = pos; k < lenth; k++) {
				array1D[k] = array1D[k + 1];
			}
			lenth--;
		}
	}
}
/*界面初始化(渲染)*/
void InitUI() {
	int choice;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			choice = array2D[i][j];
			switch (choice) {
			case 1:
				putimage(i * 200, j * 200, 200, 200, &beauty, 0, 0);
				break;
			case 2:
				putimage(i * 200, j * 200, 200, 200, &beauty, 200, 0);
				break;
			case 3:
				putimage(i * 200, j * 200, 200, 200, &beauty, 400, 0);
				break;
			case 4:
				putimage(i * 200, j * 200, 200, 200, &beauty, 0, 200);
				break;
			case 5:
				putimage(i * 200, j * 200, 200, 200, &beauty, 200, 200);
				break;
			case 6:
				putimage(i * 200, j * 200, 200, 200, &beauty, 400, 200);
				break;
			case 7:
				putimage(i * 200, j * 200, 200, 200, &beauty, 0, 400);
				break;
			case 8:
				putimage(i * 200, j * 200, 200, 200, &beauty, 200, 400);
				break;
			case 9:
				putimage(i * 200, j * 200, 200, 200, &black, 0, 0);
				break;
			}
		}
	}
}
/*菜单显示*/
void drawMenu()//菜单
{
	initgraph(600, 600);
	setbkcolor(YELLOW);
	cleardevice();
	setfillcolor(BLACK);
	settextstyle(30, 0, "宋体");
	setbkmode(TRANSPARENT);
	fillrectangle(220, 200, 380, 250);
	fillrectangle(220, 300, 380, 350);
	outtextxy(240, 220, "开始游戏");
	outtextxy(240, 320, "退出游戏");
	MOUSEMSG m;
	while (1)
	{
		m = GetMouseMsg();
		if (m.x >= 220 && m.x <= 380 && m.y >= 200 && m.y <= 250)
		{
			setlinecolor(BLACK);
			rectangle(220, 200, 380, 250);
			if (m.uMsg == WM_LBUTTONDOWN)
			{
				break;
			}
		}
		else if (m.x >= 220 && m.x <= 380 && m.y >= 300 && m.y <= 350)
		{
			setlinecolor(BLACK);
			rectangle(220, 300, 380, 350);
			if (m.uMsg == WM_LBUTTONDOWN)
			{
				exit(0);
			}
		}
		else
		{
			setlinecolor(WHITE);
			rectangle(220, 200, 380, 250);
			rectangle(220, 300, 380, 350);
		}
	}
}
/*交换图片*/
void DrawIMG(int black_a,int black_b,int now_a,int now_b) {
	IMAGE black;
	IMAGE now;
	getimage(&black,black_a,black_b,200,200);
	getimage(&now, now_a, now_b, 200, 200);
	putimage(now_a, now_b, 200, 200, &black, 0, 0);
	putimage(black_a, black_b, 200, 200, &now, 0, 0);
}
/*交换数组的值*/
void swap1(int &a, int &b) {
	int t;
	t = a;
	a = b;
	b = t;
}
/*开始游戏*/
void PlayGame() {
	MOUSEMSG Mouse;
	int x, y;//x,y是黑框左上角坐标  (y,x)
	while (1) {
		Mouse = GetMouseMsg();
		/*得到x,y的值,x,y是黑框的左上角坐标*/
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				if (array2D[i][j] == 9) {
					y = i;
					x = j;
				}
			}
		}
		/*黑框上方*/
		if (Mouse.x>0&&Mouse.x<600&&Mouse.y>0&&Mouse.y<600&&Mouse.x >(200 * y) && Mouse.x < (200 + (200*y)) && Mouse.y > ((x*200) - 200) && Mouse.y < (x*200)) {
			setlinecolor(BLACK);
			rectangle((x * 200), ((y * 200) -200), (200 + (x * 200)), (y * 200));
			/*如果捕捉到鼠标左键按下的信息,就将黑框与此部分交换*/
			if (Mouse.uMsg == WM_LBUTTONDOWN && (y-1)>=0) {
				swap1(array2D[y][x], array2D[y - 1][x]);
				DrawIMG(200 * x, 200 * y, 200 * x, 200 * y - 200);
			}
		}/*黑框右方*/
		else if (Mouse.x > 0 && Mouse.x < 600 && Mouse.y>0 && Mouse.y<600 && Mouse.x >(y * 200+200) && Mouse.x < (400 + (y * 200)) && Mouse.y >(x * 200) && Mouse.y < ((x * 200) + 200)) {
			setlinecolor(BLACK);
			rectangle((x * 200), ((y * 200) + 200), ((x * 200) + 200), ((y * 200) + 400));
			if (Mouse.uMsg == WM_LBUTTONDOWN && (x+1)<=3) {
				swap1(array2D[y][x], array2D[y][x + 1]);
				DrawIMG(200 * x, 200 * y, 200 * x + 200, 200 * y);
				continue;
			}
		}/*黑框下方*/
		else if (Mouse.x > 0 && Mouse.x < 600 && Mouse.y>0 && Mouse.y<600 && Mouse.x >(y * 200) && Mouse.x < (200 + (y * 200)) && Mouse.y >(x * 200+200) && Mouse.y < ((x * 200) + 400)) {
			setlinecolor(BLACK);
			rectangle(((x * 200) + 200), ((y * 200) + 200), ((x * 200) + 400), (y * 200));
			if (Mouse.uMsg == WM_LBUTTONDOWN && (y+1)<=3) {
				swap1(array2D[y][x], array2D[y + 1][x]);
				DrawIMG(200 * x, 200 * y, 200 * x, 200 * y + 200);
				continue;
			}
		}/*黑框左方*/
		else if (Mouse.x > 0 && Mouse.x < 600 && Mouse.y>0 && Mouse.y<600 && Mouse.x > ((y * 200) - 200) && Mouse.x < (y * 200) && Mouse.y > (x * 200) && Mouse.y < ((x * 200) + 200)) {
			setlinecolor(BLACK);
			rectangle(((x * 200) - 200), (y * 200), (x * 200), ((y * 200) + 200));
			if (Mouse.uMsg == WM_LBUTTONDOWN && (x-1)>=0) {
				swap1(array2D[y][x], array2D[y][x - 1]);
				DrawIMG(200 * x, 200 * y, 200 * x - 200, 200 * y);
				continue;
			}
		}
	}
}
int main()
{
	drawMenu();//显示菜单
	initgraph(600, 600);//调整控制台大小
	loadResouce();//加载图片资源
	InitUI();
	PlayGame();
	system("pause");
	closegraph();
	return 0;
}

ava
艾茶

2021-3-25

0

总的来说,是你在很多地方,把 x、y 混淆了。

还有许多小问题,比如:

多次 initgraph,多次 srand,putimage 参数错误等等。

我基本上没有对你的逻辑进行大的修改,只是加了若干调试信息,以及将图片改用数字来表示。在开发过程中,不要直接用图片,会方便排查问题。

以下是基于你当前逻辑的可以运行的拼图(你还需要对逻辑进行许多调整):

#include<stdio.h>
#include<stdlib.h>
#include<easyx.h>
#include<time.h>
#include<conio.h>
void loadResouce();//加载资源
void UserUI();//渲染
void drawMenu();//菜单
void PlayGame();//游戏过程
void DrawIMG(int black_a, int black_b, int now_a, int now_b);//交换图片
void swap1(int& a, int& b);//交换数组
int array2D[3][3] = { 0 };
int array1D[9] = { 1,2,3,4,5,6,7,8,9 };
int pos;
IMAGE beauty(600, 600);
IMAGE black(200, 200);

/*加载资源*/
void loadResouce()
{
	int lenth = 9;

//	loadimage(&beauty, "", 600, 600);//此处添加游戏拼图
	SetWorkingImage(&beauty);
	setfillcolor(LIGHTGRAY);
	settextstyle(96, 0, _T("Arial"));
	setbkmode(TRANSPARENT);
	int n = 1;
	for (int y = 2; y < 600; y += 200)
		for (int x = 2; x < 600; x += 200)
		{
			solidrectangle(x, y, x + 196, y + 196);
			outtextxy(x + (196 - textwidth('0' + n)) / 2, y + 50, '0' + n);
			n++;
		}

//	loadimage(&black, "", 200, 200);//此处添加一个纯黑色的图片
	SetWorkingImage(&black);
	setfillcolor(BLACK);
	cleardevice();

	SetWorkingImage();

	for (int x = 0; x < 3; x++)
		for (int y = 0; y < 3; y++)
		{
			pos = rand() % lenth;
			array2D[x][y] = array1D[pos];
			for (int k = pos; k < lenth; k++)
				array1D[k] = array1D[k + 1];
			lenth--;
		}
}

/*界面初始化(渲染)*/
void InitUI()
{
	int choice;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			choice = array2D[i][j];
			switch (choice) {
			case 1:
				putimage(i * 200, j * 200, 200, 200, &beauty, 0, 0);
				break;
			case 2:
				putimage(i * 200, j * 200, 200, 200, &beauty, 200, 0);
				break;
			case 3:
				putimage(i * 200, j * 200, 200, 200, &beauty, 400, 0);
				break;
			case 4:
				putimage(i * 200, j * 200, 200, 200, &beauty, 0, 200);
				break;
			case 5:
				putimage(i * 200, j * 200, 200, 200, &beauty, 200, 200);
				break;
			case 6:
				putimage(i * 200, j * 200, 200, 200, &beauty, 400, 200);
				break;
			case 7:
				putimage(i * 200, j * 200, 200, 200, &beauty, 0, 400);
				break;
			case 8:
				putimage(i * 200, j * 200, 200, 200, &beauty, 200, 400);
				break;
			case 9:
				putimage(i * 200, j * 200, 200, 200, &black, 0, 0);
				break;
			}
		}
	}
}

/*菜单显示*/
void drawMenu()//菜单
{
	setbkcolor(YELLOW);
	cleardevice();
	setfillcolor(BLACK);
	settextstyle(30, 0, _T("宋体"));
	setbkmode(TRANSPARENT);
	fillrectangle(220, 200, 380, 250);
	fillrectangle(220, 300, 380, 350);
	outtextxy(240, 220, _T("开始游戏"));
	outtextxy(240, 320, _T("退出游戏"));
	MOUSEMSG m;
	while (1)
	{
		m = GetMouseMsg();
		if (m.x >= 220 && m.x <= 380 && m.y >= 200 && m.y <= 250)
		{
			setlinecolor(BLACK);
			rectangle(220, 200, 380, 250);
			if (m.uMsg == WM_LBUTTONDOWN)
				break;
		}
		else if (m.x >= 220 && m.x <= 380 && m.y >= 300 && m.y <= 350)
		{
			setlinecolor(BLACK);
			rectangle(220, 300, 380, 350);
			if (m.uMsg == WM_LBUTTONDOWN)
				exit(0);
		}
		else
		{
			setlinecolor(WHITE);
			rectangle(220, 200, 380, 250);
			rectangle(220, 300, 380, 350);
		}
	}
}

/*交换图片*/
void DrawIMG(int now_a, int now_b, int black_a, int black_b)
{
	putimage(black_a, black_b, &black);
	int n = array2D[now_a / 200][now_b / 200] - 1;
	printf("%d", n);
	putimage(now_a, now_b, 200, 200, &beauty, 200 * (n % 3), 200 * (n / 3));
}

/*交换数组的值*/
void swap1(int& a, int& b)
{
	int t;
	t = a;
	a = b;
	b = t;
}

/*开始游戏*/
void PlayGame()
{
	MOUSEMSG Mouse;
	int x, y;//x,y是黑框左上角坐标  (y,x)
	while (1)
	{
		Mouse = GetMouseMsg();
		printf(".");
		if (Mouse.uMsg == WM_LBUTTONDOWN)
		{
			printf("*");
			/*得到x,y的值,x,y是黑框的左上角坐标*/
			for (int i = 0; i < 3; i++)
				for (int j = 0; j < 3; j++)
					if (array2D[i][j] == 9)
					{
						x = i;
						y = j;
					}

			printf("(%d,%d)", x, y);
			/*黑框上方*/
			if (Mouse.x > 0 && Mouse.x < 600 && Mouse.y>0 && Mouse.y<600 && Mouse.x >(200 * x) && Mouse.x < (200 + (200 * x)) && Mouse.y >((y * 200) - 200) && Mouse.y < (y * 200))
			{
				printf("U");
				setlinecolor(BLACK);
				rectangle((x * 200), ((y * 200) - 200), (200 + (x * 200)), (y * 200));
				/*如果捕捉到鼠标左键按下的信息,就将黑框与此部分交换*/
				if ((y - 1) >= 0)
				{
					swap1(array2D[x][y], array2D[x][y - 1]);
					DrawIMG(200 * x, 200 * y, 200 * x, 200 * y - 200);
				}
			}/*黑框右方*/
			else if (Mouse.x > 0 && Mouse.x < 600 && Mouse.y>0 && Mouse.y<600 && Mouse.x >(x * 200 + 200) && Mouse.x < (400 + (x * 200)) && Mouse.y >(y * 200) && Mouse.y < ((y * 200) + 200))
			{
				printf("R");
				setlinecolor(BLACK);
				rectangle((x * 200), ((y * 200) + 200), ((x * 200) + 200), ((y * 200) + 400));
				if ((x + 1) <= 3)
				{
					swap1(array2D[x][y], array2D[x + 1][y]);
					DrawIMG(200 * x, 200 * y, 200 * x + 200, 200 * y);
				}
			}/*黑框下方*/
			else if (Mouse.x > 0 && Mouse.x < 600 && Mouse.y>0 && Mouse.y<600 && Mouse.x >(x * 200) && Mouse.x < (200 + (x * 200)) && Mouse.y >(y * 200 + 200) && Mouse.y < ((y * 200) + 400))
			{
				printf("D");
				setlinecolor(BLACK);
				rectangle(((x * 200) + 200), ((y * 200) + 200), ((x * 200) + 400), (y * 200));
				if ((y + 1) <= 3)
				{
					swap1(array2D[x][y], array2D[x][y + 1]);
					DrawIMG(200 * x, 200 * y, 200 * x, 200 * y + 200);
				}
			}/*黑框左方*/
			else if (Mouse.x > 0 && Mouse.x < 600 && Mouse.y>0 && Mouse.y<600 && Mouse.x >((x * 200) - 200) && Mouse.x < (x * 200) && Mouse.y >(y * 200) && Mouse.y < ((y * 200) + 200))
			{
				printf("L");
				setlinecolor(BLACK);
				rectangle(((x * 200) - 200), (y * 200), (x * 200), ((y * 200) + 200));
				if ((x - 1) >= 0)
				{
					swap1(array2D[x][y], array2D[x - 1][y]);
					DrawIMG(200 * x, 200 * y, 200 * x - 200, 200 * y);
				}
			}
		}
	}
}

int main()
{
	srand((unsigned int)time(NULL));
	initgraph(600, 600, EW_SHOWCONSOLE);//调整控制台大小
	drawMenu();//显示菜单
	loadResouce();//加载图片资源
	InitUI();
	FlushMouseMsgBuffer();
	PlayGame();
	system("pause");
	closegraph();
	return 0;
}

我就不一一指出你代码里面的问题了,你可以参考一下这个:https://codebus.cn/yangw/a/number-jigsaw

ava
慢羊羊

2021-3-25