拼图过程中,移动的图片与鼠标点击的图片不一致,并且有时候会出现多张黑色的图。
#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;
}