问题标题: 金光工作室:推箱子游戏(出自酷町堂)

0
0
已解决
王文博
王文博
缔造者之神
缔造者之神

这个游戏是出自酷町堂的教学当中的,我将根据老师提供的代码来进一步改造。

上代码:

#include <iostream>
#include <cstdio> 
#include <conio.h>//函数_getch()所需头文件 
#include <windows.h>
#include <iomanip>//输入输出流控制符setw()所在头文件,设置字段宽度为n位
using namespace std;
#define R 12
#define C 12
BOOL flag = true;    			/*定义布尔值的标记,方便在游戏时直接退出*/ 
int pass=1;//初始关卡值 
int map[R][C] = { 0 };
int map1[R][C] = {
	{ 0,0,1,1,1,0,0,0 },
	{ 0,0,1,3,1,0,0,0 },
	{ 0,0,1,0,1,1,1,1 },
	{ 1,1,1,0,0,4,3,1 },
	{ 1,3,4,4,0,1,1,1 },
	{ 1,1,1,5,4,1,0,0 },
	{ 0,0,0,1,3,1,0,0 },
	{ 0,0,0,1,1,1,0,0 }
};
int map2[R][C]={
	{1,1,1,1,1,0,0,0,0,0},
	{1,5,0,0,1,0,0,0,0,0},
	{1,0,4,4,1,0,1,1,1,0},
	{1,0,4,0,1,0,1,3,1,0},
	{1,1,1,0,1,1,1,3,1,0},
	{0,1,1,0,0,0,0,3,1,0},
	{0,1,0,0,0,1,0,0,1,0},
	{0,1,0,0,0,1,1,1,1,0},
	{0,1,1,1,1,1,0,0,0,0}
};
int map3[R][C]={
	{ 1,1,1,1,1,1,1,1,1,1,1,1 },
	{ 1,0,0,0,0,0,1,0,0,0,3,1 },
	{ 1,4,0,0,0,0,1,0,0,0,0,1 },
	{ 1,0,1,1,1,0,1,0,0,0,0,1 },
	{ 1,0,0,5,1,0,1,0,0,0,0,1 },
	{ 1,0,1,1,1,0,1,0,0,4,0,1 },
	{ 1,0,0,0,0,0,1,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,4,0,0,1,0,0,0,0,1 },
	{ 1,3,0,0,0,0,1,0,0,0,0,1 },
	{ 1,0,0,0,0,0,1,0,0,0,3,1 },
	{ 1,1,1,1,1,1,1,1,1,1,1,1 }
};
/*二维数组地图
1.表示墙体
3.表示目的地
4.表示箱子
5.表示人
0.表示空地
*/
int map4[R][C]={
    { 1,1,1,1,1,1,1,1,1,1,1,1 },
    { 1,1,0,4,0,0,0,0,0,0,3,1 },
    { 1,1,0,1,1,1,1,1,1,4,0,1 },
    { 1,1,5,0,0,0,0,0,0,0,0,1 },
    { 1,1,1,1,1,1,1,1,0,0,0,1 },
    { 1,1,1,1,1,1,1,1,0,0,0,1 },
    { 1,1,1,1,1,1,1,1,0,0,0,1 },
    { 1,0,3,0,0,0,0,0,4,0,0,1 },
    { 1,0,0,0,0,0,0,0,0,3,0,1 },
    { 1,1,0,1,0,1,0,1,0,1,0,1 },
    { 1,0,1,0,1,0,1,0,1,0,1,1 },
    { 1,1,1,1,1,1,1,1,1,1,1,1 }
};
int map5[R][C]={
	{ 1,1,1,1,1,1,1,1,0,0 },
	{ 1,1,0,0,0,0,3,1,1,1 },
	{ 1,1,0,4,0,0,0,0,3,1 },
	{ 1,0,0,0,5,4,0,0,0,1 },
	{ 1,0,1,4,0,0,0,0,1,1 },
	{ 1,0,0,0,0,0,0,0,0,1 },
	{ 1,1,0,1,1,0,0,0,3,1 },
	{ 1,1,0,0,0,1,0,0,0,1 },
	{ 1,1,1,1,1,1,1,1,1,1 },
	{ 1,1,1,1,1,1,1,1,1,1 }
};
void Game_Menu()
{
 	system("cls");
 	cout << "/************************************\\\n";
 	cout << "*                                    *\n";
 	cout << "*            经 典 小 游 戏          *\n";
 	cout << "*               推 箱 子             *\n";
 	cout << "*         1.按 S 或 s 键 开 始       *\n";
 	cout << "*         2.按 Q 或 q 键 退 出       *\n";
 	cout << "*                                    *\n";
 	cout << "\\***********************************/\n";
 	_getch();
}
void Game_description()
{
 	cout << "/************************************\\\n";
 	cout << "*                                    *\n";
 	cout << "*              操 作 提 示           *\n";
 	cout << "*        操作上移:  W   w   ↑      *\n";
 	cout << "*        操作下移:  S   s   ↓      *\n";
 	cout << "*        操作左移:  A   a   ←      *\n";
 	cout << "*        操作右移:  D   d   →      *\n";
 	cout << "*                                    *\n";
 	cout << "*        退    出:  Q   q           *\n";
 	cout << "*                                    *\n";
 	cout << "*                                    *\n";
 	cout << "\\***********************************/\n";
}
void color(int m) {			
 	HANDLE consolehend;
 	consolehend = GetStdHandle(STD_OUTPUT_HANDLE);
 	SetConsoleTextAttribute(consolehend, m);
 	return ; 
}
int DrawMap(){  
 	cout<<"关卡:"<<pass<<endl; 
 	for (int i = 0; i < R; i++){
  		for (int j = 0; j < C; j++){
   			switch (map[i][j]){
   				case 0:
    				color(0xF);
    				cout << "  ";  
    				break;
   				case 1:
    				color(8);
    				cout << "■";
    				break;
   				case 3:
    				color(0xE);
    				cout << "☆";
    				break;
   				case 4:
    				color(4);
    				cout << "□";
    				break;
   				case 5:
    				color(3);
    				cout << "♀"; 
    				break;
   				case 7:     
    				color(6);   
    				cout << "★";
    				break;
   				case 8:     
    				color(3);
    				cout << "♀";
    				break;
   				default:
    			break;
   			}
  		}
  	cout <<endl;
 	}
 	return 0;
}
void Move(){		
 	int r,c; 
 	for(int i=0;i<R;i++){
  		for(int j=0;j<C;j++){
   			if(map[i][j]==5||map[i][j]==8){
    			r=i;
    			c=j;
   			}
  		}
 	}
 	cout<<"您当前的坐标为:("<<r<<","<<c<<")"<<endl;
 	int ch;
 	ch=_getch();
 	switch(ch){
  		case 'W':
  		case 'w':
  		case 72:
   		if(map[r-1][c]==0||map[r-1][c]==3){
    		map[r-1][c]+=5;
    		map[r][c]-=5;
   		} 
   		else if(map[r-1][c]==4||map[r-1][c]==7){
    		if(map[r-2][c]==0||map[r-2][c]==3){
     			map[r-2][c]+=4;
     			map[r-1][c]+=1;
     			map[r][c]-=5;
    		}
   		}
   		break;
  		case 'S':
  		case 's':
  		case 80:
   		if(map[r+1][c]==0||map[r+1][c]==3){
    		map[r+1][c]+=5;
    		map[r][c]-=5;
   		}
   		else if(map[r+1][c]==4||map[r+1][c]==7){
    		if(map[r+2][c]==0||map[r+2][c]==3){
     			map[r+2][c]+=4;
     			map[r+1][c]+=1;
     			map[r][c]-=5;
    		}
   		}
   		break;
  		case 'A':
  		case 'a':
  		case 75:
   		if(map[r][c-1]==0||map[r][c-1]==3){
    		map[r][c-1]+=5;
    		map[r][c]-=5;
   		}
   		else if(map[r][c-1]==4||map[r][c-1]==7){
    		if(map[r][c-2]==0||map[r][c-2]==3){
     			map[r][c-2]+=4;
     			map[r][c-1]+=1;
     			map[r][c]-=5;
    		}
   		}
   		break;
  		case 'D':
  		case 'd':
  		case 77:
   		if(map[r][c+1]==0||map[r][c+1]==3){
    		map[r][c+1]+=5;
    		map[r][c]-=5;
   		}
   		else if(map[r][c+1]==4||map[r][c+1]==7){
    		if(map[r][c+2]==0||map[r][c+2]==3){
     			map[r][c+2]+=4;
     			map[r][c+1]+=1;
     			map[r][c]-=5;
    		}
   		}
   		break; 
  		case 'Q':
  		case 'q':
   		flag=false;
  		default:
   		break;
 	}
 	return ;
}
int finish()
{			
 	for (int i=0;i<R;i++)
	{
  		for (int j=0;j<C;j++)
		{
   			if (map[i][j] == 4)
			{ 
    			return 0;
   			} 
  		}
 	}
 	return 1;
} 
void setmap(int n){
 	if (n == 1) {
  		memcpy(map, map1, sizeof(map1));
 	}
 	if (n == 2) {
  		memcpy(map, map2, sizeof(map2));
 	}
 	if (n == 3) {
  		memcpy(map, map3, sizeof(map3));
 	}
 	if( n == 4) {
 		memcpy(map, map4, sizeof(map4));
	}
	if(n==5)
	{
		memcpy(map, map4, sizeof(map5));
	}
 	return ;
}
int main()
{
 	char c;
 	Game_Menu();
 	c = getch();
 	setmap(pass);
 	switch(c){
  		case 'Q':
  		case 'q':
   		return 0;
  		case 'S':
  		case 's':
   		while (flag){  
    		system("cls");
    		Game_description();
    		DrawMap();
    		Move();
    		if(finish()){
     			system("cls");
     			DrawMap();
     			printf("游戏胜利!\n");
     			system("pause"); 
     			pass++;
     			setmap(pass);
    		}
   		}
   		break;
 	}
 	return 0;
}

缺点:

1.晃动太大。我试玩的时候,脑袋嗡嗡的。建议不怕晃动的人来玩。

2.延迟太高。当你快速移动时,会有1步-2步的延迟。

3.容错率低。只要有一次走错,就必须重新来。

希望其他同学能帮助我一起修复这个代码!

注:与地图和游戏相关的图片无法上传(因为太大了,请大家谅解,等好了再传)。

王文博在2021-06-17 12:32:20追加了内容

现在发一下图片:

在Excel表格中,黑色是墙,黄色是箱子,绿色是目的地,红色是人物出生地。

希望大家可以帮助改正那3个缺点!


0
已采纳
王雨涵
王雨涵
中级守护
中级守护

赢了之后一直卡在最高的关卡,没有“break”。

0
0
0
0
0
0
0
我要回答