c++ 简易可视化推箱子

第一次接触这种,找了个视频看(抄)了看(抄)

找不到啥好看的图标

c++ 简易可视化推箱子_第1张图片c++ 简易可视化推箱子_第2张图片

#include 
#include 
#include 
using namespace std;
IMAGE img[6];
//0 空地,1 墙,3 目的地,4 球,5 人,7 完成
int imgIndex[6] = { 0, 1, 3, 4, 5, 7 };
int mp[7][8] = {
	1, 1, 1, 1, 1, 1, 1, 1,
	1, 3, 0, 0, 0, 4, 3, 1,
	1, 0, 1, 4, 0, 1, 0, 1,
	1, 0, 4, 0, 5, 0, 0, 1,
	1, 0, 1, 1, 0, 1, 0, 1,
	1, 3, 0, 0, 4, 0, 3, 1,
	1, 1, 1, 1, 1, 1, 1, 1,
};
void loadResource() {
	for (int i = 0; i < 6; i++) {
		char filename[20] = "";
		sprintf_s(filename, "%d.ico", imgIndex[i]);
		loadimage(img + i, filename);
	}
}
void drawMap() {
	int x, y;
	for (int i = 0; i < 7; i++) {
		for (int j = 0; j < 8; j++) {
			x = 64 * j;
			y = 64 * i;
			switch (mp[i][j]) {
			case 0:
				putimage(x, y, img + 0);
				break;
			case 1:
				putimage(x, y, img + 1);
				break;
			case 3:
				putimage(x, y, img + 2);
				break;
			case 4:
				putimage(x, y, img + 3);
				break;
			case 5:
			case 8:
				putimage(x, y, img + 4);
				break;
			case 7:
				putimage(x, y, img + 5);
				break;
			}
		}
	}
}
void keyDown() {
	int i, j;
	for (i = 0; i < 7; i++) {
		for (j = 0; j < 8; j++) {
			if (mp[i][j] == 5 || mp[i][j] == 8)
				break;
		}
		if (mp[i][j] == 5 || mp[i][j] == 8)
			break;
	}
	char userKey = _getch();
	switch (userKey) {
	//小键盘 72 80 75 77
	case 'w':
	case 'W':
	case 72:
		if (mp[i - 1][j] == 0 || mp[i - 1][j] == 3) {
			mp[i][j] -= 5;
			mp[i - 1][j] += 5;
		}
		else if (mp[i - 1][j] == 4 || mp[i - 1][j] == 7) {
			if (mp[i - 2][j] == 0 || mp[i - 2][j] == 3) {
				mp[i][j] -= 5;
				mp[i - 1][j] += 1;
				mp[i - 2][j] += 4;
			}
		}
		break;
	case 's':
	case 'S':
	case 80:
		if (mp[i + 1][j] == 0 || mp[i + 1][j] == 3) {
			mp[i][j] -= 5;
			mp[i + 1][j] += 5;
		}
		else if (mp[i + 1][j] == 4 || mp[i + 1][j] == 7) {
			if (mp[i + 2][j] == 0 || mp[i + 2][j] == 3) {
				mp[i][j] -= 5;
				mp[i + 1][j] += 1;
				mp[i + 2][j] += 4;
			}
		}
		break;
	case 'a':
	case 'A':
	case 75:
		if (mp[i][j - 1] == 0 || mp[i][j - 1] == 3) {
			mp[i][j] -= 5;
			mp[i][j - 1] += 5;
		}
		else if (mp[i][j - 1] == 4 || mp[i][j - 1] == 7) {
			if (mp[i][j - 2] == 0 || mp[i][j - 2] == 3) {
				mp[i][j] -= 5;
				mp[i][j - 1] += 1;
				mp[i][j - 2] += 4;
			}
		}
		break;
	case 'd':
	case 'D':
	case 77:
		if (mp[i][j + 1] == 0 || mp[i][j + 1] == 3) {
			mp[i][j] -= 5;
			mp[i][j + 1] += 5;
		}
		else if (mp[i][j + 1] == 4 || mp[i][j + 1] == 7) {
			if (mp[i][j + 2] == 0 || mp[i][j + 2] == 3) {
				mp[i][j] -= 5;
				mp[i][j + 1] += 1;
				mp[i][j + 2] += 4;
			}
		}
		break;
	}
}
int gameOver() {
	int cnt = 1;
	for (int i = 0; i < 7; i++)
		for (int j = 0; j < 8; j++) {
			cout << mp[i][j] << " ";
			if (mp[i][j] == 4) {
				cnt = 0;
				return cnt;
			}
		}
	return cnt;
}
void gameOverLayout() {
	IMAGE wxy;
	loadimage(&wxy, "wxy.jpg", 324, 405);
	initgraph(wxy.getwidth(), wxy.getheight());
	putimage(0, 0, &wxy);
	getchar();
}
int main() {
	initgraph(64*8, 64*7);
	while (1) {
		loadResource();
		drawMap();
		if (gameOver()) {
			break;
		}
		keyDown();
	}
	gameOverLayout();
	closegraph();
	return 0;
}

你可能感兴趣的:(code)