C++实现简单推箱子

#include<iostream>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 25
#define COL 80
int Net[ROW][COL] = { 0 };
int posCharX = 0;
int posCharY = 0;
int posBoxX = 0;
int posBoxY = 0;
int count = 0;
using namespace std;
void makeMap();
void printMap();
void creatCharAndBox();
void moveChar();
void pushBox(char key);
int isItAble(char key);
void IfSuccess(char key);
int main()
{
	ios::sync_with_stdio(false);
	srand((unsigned)time(0));
	makeMap();
	creatCharAndBox();
	printMap();
	moveChar();
}

void makeMap() {
	int pos1 = rand() % 25 + 1;
	int pos2 = rand() % 25 + 1;
	for (int i = 0; i<25; i++) {
		if (i != pos1)
			Net[i][34] = 1;
		else Net[i][34] = 0;
		if (i != pos2)
			Net[i][44] = 1;
		else Net[i][44] = 0;
	}
	Net[11][79] = 1;
	Net[11][78] = 1;
	Net[12][78] = 8;
	Net[12][79] = 8;
	Net[13][78] = 8;
	Net[13][79] = 8;
	Net[14][79] = 1;
	Net[14][78] = 1;

}

void printMap() {
	for (int i = 0; i < ROW; i++)
		for (int j = 0; j < COL; j++)
			if (Net[i][j] == 0)
				cout << " ";
			else if (Net[i][j] == 1)
				cout << '\5';
			else if (Net[i][j] == 3)
				cout << '\4';
			else if (Net[i][j] == 2)
				cout << '\2';
			else if (Net[i][j] == 8)
				cout << '\3';

}

void moveChar() {
	char key;
	while (1) {
		key = _getch();
		switch (key)
		{
		case 72: if(isItAble(key)==0)
				 break;
				 pushBox(key);
				 Net[posCharY][posCharX] = 0; //shang
				 posCharY -= 1;
				 Net[posCharY][posCharX] = 2;
				 break;
		case 77: if(isItAble(key)==0)
				 break;
				 pushBox(key);
				 IfSuccess(key);
				 Net[posCharY][posCharX] = 0; //you
				 posCharX += 1;
				 Net[posCharY][posCharX] = 2;
				 break;
		case 80: if(isItAble(key)==0)
				 break;
				 pushBox(key);
			     Net[posCharY][posCharX] = 0; //xia
				 posCharY += 1;
				 Net[posCharY][posCharX] = 2;
				 break;
		case 75: if(isItAble(key)==0)
				 break;
				 pushBox(key);
				 Net[posCharY][posCharX] = 0; //zuo
				 posCharX -= 1;
				 Net[posCharY][posCharX] = 2;
				 break;
		}
		system("cls");
		printMap();
		if(count >=2){
			system("cls");
			cout << "You Win!";
			break;
		}

	}

}

void creatCharAndBox() {
	posCharX = rand() % 34;
	posCharY = rand() % 25;
	posBoxX  = rand() % 34;
	posBoxY  = rand() % 25;
	Net[posCharY][posCharX] = 2;
	Net[posBoxY][posBoxX] = 3;
	if (posCharX == posBoxX && posCharY == posBoxY) {
		cout << "Erorr!";
	}
	// 人用数字2表示,箱子用数字3表示。
}

int isItAble(char key)
{
	int x=1;
	/* None Box */
	if(Net[posCharY][posCharX+1] == 1 && key==77)
		return x=0;
	if(Net[posCharY][posCharX-1] == 1 && key==75)
		return x=0;
	if(Net[posCharY+1][posCharX] == 1 && key==80)
		return x=0;
	if(Net[posCharY-1][posCharX] == 1 && key==72)
		return x=0;
	/* With Box*/
	if(Net[posBoxY][posBoxX+1] == 1 && key==77&& Net[posCharY][posCharX+1] == Net[posBoxY][posBoxX])
		return x=0;
	if(Net[posBoxY][posBoxX-1] == 1 && key==75&& Net[posCharY][posCharX-1] == Net[posBoxY][posBoxX] )
		return x=0;
	if(Net[posBoxY+1][posBoxX] == 1 && key==80&& Net[posCharY+1][posCharX]== Net[posBoxY][posBoxX] )
		return x=0;
	if(Net[posBoxY-1][posBoxX] == 1 && key==72&& Net[posCharY-1][posCharX]== Net[posBoxY][posBoxX])
		return x=0;
	return x;
}

void pushBox(char key){
	if(Net[posCharY][posCharX+1]== Net[posBoxY][posBoxX] && key==77){
		Net[posBoxY][posBoxX] = 0;
		posBoxX+=1;
		Net[posBoxY][posBoxX] = 3;
	}
		if(Net[posCharY][posCharX-1] == Net[posBoxY][posBoxX] && key ==75){
		Net[posBoxY][posBoxX] = 0;
		posBoxX-=1;
		Net[posBoxY][posBoxX] = 3;
	}
		if(Net[posCharY+1][posCharX]== Net[posBoxY][posBoxX] && key == 80){
		Net[posBoxY][posBoxX] = 0;
		posBoxY+=1;
		Net[posBoxY][posBoxX] = 3;
	}
		if(Net[posCharY-1][posCharX]== Net[posBoxY][posBoxX] && key== 72){
		Net[posBoxY][posBoxX] = 0;
		posBoxY-=1;
		Net[posBoxY][posBoxX] = 3;
	}
}

void IfSuccess(char key)
{
	 if(Net[posBoxY][posBoxX+1] == 8 && key ==77)
		 count++;
}

这个推箱子是基于一个二位数组的,通过改变二位数组的一些数字来表示整个地图,然而美中不足的是这样的方法效率很差,游戏在人移动的时候会稍微闪屏,这个不知道有没有大神可以教我如何避免闪屏。

5 条评论

  1. 可可豆

    厉害啦~~~

  2. themebetter

    大学时光没有浪费呀!

  3. FROYO

    在大学里,没事写些自己感兴趣的东西还是很有成就感的

  4. 凯哥自媒体

    厉害了我的弟

  5. 商学院

    厉害厉害,学了这么久的C++,可是我都不怎么会写