C++实现拼图游戏代码(graphics图形库)
时间:2021-01-13 09:51:03|栏目:C代码|点击: 次
本文实例为大家分享了C++实现拼图游戏的具体代码,供大家参考,具体内容如下
#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; 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++;//如过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() { int x, y; 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; } } } 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 sql[200]; initgraph(850, 300); Initmap(); PaintInit(); while (1) { cleardevice(); BeginBatchDraw(); GameInit(); outtextxy(650, 210, _T("原图")); _stprintf(sql, _T("走了:%d"), num); outtextxy(500, 250, sql); EndBatchDraw(); PlayGame(); } getch(); closegraph(); system("pause"); return }
效果图:
栏 目:C代码
本文地址:http://www.codeinn.net/misctech/44494.html