1. 程式人生 > >推箱子游戲使用C語言實現的簡單例項

推箱子游戲使用C語言實現的簡單例項

/*
1.遊戲實現步驟
1).遊戲一開始,就顯示遊戲地圖。
	while(1)
	{
		2).輸入小人的前進方向。
		3).根據小人的前進方向,來移動小人。 
	}
	2.根據步驟 搭建專案框架


*/

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
//行和列,該遊戲一共有10行、10列。
#define ROW 10
#define COL 11

//初始化一個地圖
char map[ROW][COL] = {
	"##########",//0
	"###     ##",//1
	"###     ##",//2
	"##OX  # ##",//3
	"###  ##   ",//4
	"#####    #",//5
	"##       #",//6
	"#     ####",//7
	"###       ",//8
	"##########" //9
	//O:人  , X:箱子 
};

//列印地圖 
void showMap();

//接收小人的方向
char enterDirection();

//小人向上移動的方法
void moveToUp();

//小人向下移動的方法
void moveToDown();

//小人向右移動的方法
void moveToRight();

//小人向左移動的方法
void moveToLeft();

//當前小人的座標
int currentPersonRow = 3;
int currentPersonCol = 2;

//當前箱子的座標 
int currentBoxRow = 3;
int currentBoxCol = 3;



int main(int argc, char *argv[]) {
	//system("clear");
	printf("點選回車鍵開始遊戲!\n\n");
	//1代表執行   0停止 
	int flag = 1;
	while (flag == 1){
		//顯示地圖 
		showMap();
		//接收小人的方向
		char dir = enterDirection();
		switch (dir){
			//小人向上移動 
		case 'w':
		case 'W':
			moveToUp();
			break;

			//小人向下移動 
		case 's':
		case 'S':
			moveToDown();
			break;
			//小人向右移動 
		case 'd':
		case 'D':
			moveToRight();
			break;
			//小人向左移動 
		case 'a':
		case 'A':
			moveToLeft();
			break;
			//停止執行 
		case 'q':
		case 'Q':
			printf("你的智商真低!T_T\n");
			flag = 0;
			break;
		}
		//showMap();
		if (currentBoxRow == 8 & currentBoxCol == 9){
			printf("SUCCESS,WELL DOWN!!!\n");
			flag = 0;
		}

	}

}
/*
方法的實現
*/


//列印地圖 
void showMap(){
	int i;
	for (i = 0; i < ROW; i++){
		printf("%s\n", map[i]);
	}
	printf("\n\n\n\n\n");
	printf("W:上,S:下, A:左, D:右。Q:退出");
	printf("\n\n\n\n\n");
}
//接收小人的方向
char enterDirection(){
	//清除SCANF中的緩衝區 
	rewind(stdin);
	char dir;
	dir = getchar();
	//scanf("%c",&dir);
	return dir;
}
//小人向上移動的方法
void moveToUp(){
	//小人的下一個座標 
	int nextPersonCol = currentPersonCol;
	int nextPersonRow = currentPersonRow - 1;
	//箱子的下一個座標
	int nextBoxRow = currentBoxRow - 1;
	int nextBoxCol = currentBoxCol;

	//如果小人的下一個座標是路 
	if (map[nextPersonRow][nextPersonCol] == ' '){
		map[nextPersonRow][nextPersonCol] = 'O';
		map[currentPersonRow][currentPersonCol] = ' ';
		currentPersonRow = nextPersonRow;
		currentPersonCol = nextPersonCol;
	}
	//如果小人的下一個座標是牆 
	if (map[nextPersonRow][nextPersonCol] == '#'){
		//什麼也不做 
	}
	//如果小人的下一個座標是箱子 
	if (map[nextPersonRow][nextPersonCol] == 'X'){
		if (map[nextBoxRow][nextBoxCol] == ' '){

			map[nextPersonRow][nextPersonCol] = 'O';
			map[currentPersonRow][currentPersonCol] = ' ';

			map[nextBoxRow][nextBoxCol] = 'X';
			map[currentBoxRow][currentBoxCol] = 'O';


			currentPersonRow = nextPersonRow;
			currentPersonCol = nextPersonCol;
			currentBoxRow = nextBoxRow;
			currentBoxCol = nextBoxCol;
		}
	}
}
//小人向下移動的方法
void moveToDown(){
	//小人的下一個座標 
	int nextPersonCol = currentPersonCol;
	int nextPersonRow = currentPersonRow + 1;
	//箱子的下一個座標
	int nextBoxRow = currentBoxRow + 1;
	int nextBoxCol = currentBoxCol;

	//如果小人的下一個座標是路 
	if (map[nextPersonRow][nextPersonCol] == ' '){
		map[nextPersonRow][nextPersonCol] = 'O';
		map[currentPersonRow][currentPersonCol] = ' ';
		currentPersonRow = nextPersonRow;
		currentPersonCol = nextPersonCol;
	}
	//如果小人的下一個座標是牆 
	if (map[nextPersonRow][nextPersonCol] == '#'){
		//什麼也不做 
	}
	//如果小人的下一個座標是箱子 
	if (map[nextPersonRow][nextPersonCol] == 'X'){
		if (map[nextBoxRow][nextBoxCol] == ' '){

			map[nextPersonRow][nextPersonCol] = 'O';
			map[currentPersonRow][currentPersonCol] = ' ';

			map[nextBoxRow][nextBoxCol] = 'X';
			map[currentBoxRow][currentBoxCol] = 'O';

			currentPersonRow = nextPersonRow;
			currentPersonCol = nextPersonCol;
			currentBoxRow = nextBoxRow;
			currentBoxCol = nextBoxCol;
		}
	}
}
//小人向右移動的方法
void moveToRight(){
	//小人的下一個座標 
	int nextPersonCol = currentPersonCol + 1;
	int nextPersonRow = currentPersonRow;
	//箱子的下一個座標
	int nextBoxRow = currentBoxRow;
	int nextBoxCol = currentBoxCol + 1;

	//如果小人的下一個座標是路 
	if (map[nextPersonRow][nextPersonCol] == ' '){
		map[nextPersonRow][nextPersonCol] = 'O';
		map[currentPersonRow][currentPersonCol] = ' ';
		currentPersonRow = nextPersonRow;
		currentPersonCol = nextPersonCol;
	}
	//如果小人的下一個座標是牆 
	if (map[nextPersonRow][nextPersonCol] == '#'){
		//什麼也不做 
	}
	//如果小人的下一個座標是箱子 
	if (map[nextPersonRow][nextPersonCol] == 'X'){
		if (map[nextBoxRow][nextBoxCol] == ' '){

			map[nextPersonRow][nextPersonCol] = 'O';
			map[currentPersonRow][currentPersonCol] = ' ';

			map[nextBoxRow][nextBoxCol] = 'X';
			map[currentBoxRow][currentBoxCol] = 'O';

			currentPersonRow = nextPersonRow;
			currentPersonCol = nextPersonCol;
			currentBoxRow = nextBoxRow;
			currentBoxCol = nextBoxCol;
		}
	}
}
//小人向左移動的方法
void moveToLeft(){
	//小人的下一個座標 
	int nextPersonCol = currentPersonCol - 1;
	int nextPersonRow = currentPersonRow;
	//箱子的下一個座標
	int nextBoxRow = currentBoxRow;
	int nextBoxCol = currentBoxCol - 1;

	//如果小人的下一個座標是路 
	if (map[nextPersonRow][nextPersonCol] == ' '){
		map[nextPersonRow][nextPersonCol] = 'O';
		map[currentPersonRow][currentPersonCol] = ' ';
		currentPersonRow = nextPersonRow;
		currentPersonCol = nextPersonCol;
	}
	//如果小人的下一個座標是牆 
	if (map[nextPersonRow][nextPersonCol] == '#'){
		//什麼也不做 
	}
	//如果小人的下一個座標是箱子 
	if (map[nextPersonRow][nextPersonCol] == 'X'){
		if (map[nextBoxRow][nextBoxCol] == ' '){
			map[nextPersonRow][nextPersonCol] = 'O';
			map[currentPersonRow][currentPersonCol] = ' ';

			map[nextBoxRow][nextBoxCol] = 'X';
			map[currentBoxRow][currentBoxCol] = 'O';

			currentPersonRow = nextPersonRow;
			currentPersonCol = nextPersonCol;
			currentBoxRow = nextBoxRow;
			currentBoxCol = nextBoxCol;
		}
	}
}