高级守护
大佬们请提点建议呗~~~
(最好用通俗易懂的语言,本人不太优秀,听不懂高级的)
o(╥﹏╥)o
求求各位了!!!
#include<iostream>
#include<cstdio>
#include<conio.h>
#include<windows.h>
#include<iomanip>
#define R 10
#define C 10
using namespace std;
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 map5[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 map6[R][C]={
{0,0,0,1,1,1,1,1,1,1},
{0,0,1,1,0,0,1,0,5,1},
{0,0,1,0,0,0,1,0,0,1},
{0,0,1,4,0,4,0,4,0,1},
{0,0,1,0,4,1,1,0,0,1},
{1,1,1,0,4,0,1,0,1,1},
{1,3,3,3,3,3,0,0,1,0},
{1,1,1,1,1,1,1,1,1,0},
};
int map4[R][C]={
{0,0,1,1,1,1,1,1,0,0},
{0,0,1,3,1,1,5,1,0,0},
{0,0,1,0,0,4,0,1,0,0},
{0,0,1,0,0,1,1,1,1,0},
{0,1,1,4,1,3,0,0,1,0},
{0,1,0,0,0,1,4,0,1,0},
{0,1,0,3,0,0,0,1,1,0},
{0,1,1,1,1,1,1,1,0,0},
};
int map2[R][C]={
{0,0,0,0,0,1,1,1},
{0,0,1,1,1,1,5,1},
{0,0,1,0,0,1,0,1},
{0,0,1,0,0,4,0,1},
{0,0,1,0,1,1,1,0},
{0,0,1,0,1,0,0,0},
{0,0,1,3,1,0,0,0},
{0,0,1,1,1,0,0,0},
};
int map3[R][C]={
{0,1,1,1,1,0,0,0,0,0},
{0,1,5,0,1,1,1,1,1,0},
{0,1,1,0,4,0,0,3,1,0},
{0,0,1,0,1,1,1,1,1,0},
{0,0,1,4,0,0,1,1,0,0},
{0,1,0,0,1,1,0,0,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,1,1,1,1,1,3,1,0},
};
void Game_Menu();
void Game_description();
int DrawMap();
void Move();
void cls();
int finish();
void setmap(int n);
void color(int m);
void full_screen();
bool flag=true;
int pass=1;
int main(){
full_screen();
char c,d;
Game_Menu();
Game_description();
c=getch();
setmap(pass);
switch(c){
case 'Q':
case 'q':
return 0;
case 'S':
case 's':
system("cls");
while (flag){ /*游戏主体*/
cls();
DrawMap();
Move();//调用该函数时会有等待键盘输入的中断
if(finish()){
cls();
DrawMap();
Move();
printf("游戏胜利!\n");
system("cls");
system("pause");
pass++;
setmap(pass);
}
}
break;
}
return 0;
}
void Game_Menu(){ //游戏菜单,初始化模块,显示游戏开始菜单
system("cls");
cout<<"———————————————————\n";
cout<<"| |\n";
cout<<"| GAME |\n";
cout<<"| 推 箱 子 |\n";
cout<<"| 按 S 或 Enter+s 键 开 始 |\n";
cout<<"| |\n";
Sleep(500);
}
void Game_description(){/*初始化模块,显示游戏操作说明*/
cout<<"| |\n";
cout<<"| 操 作 提 示 |\n";
cout<<"| 操作上移: w |\n";
cout<<"| 操作下移: s |\n";
cout<<"| 操作左移: a |\n";
cout<<"| 操作右移: d |\n";
cout<<"| 重来当前关卡: u |\n";
cout<<"| 退 出: q |\n";
cout<<"| |\n";
cout<<"| 美化:疯子建设 |\n";
cout<<"———————————————————\n";
_getch();
}
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(4);
cout << "▓";//墙体
break;
case 3:
color(0xE);
cout << "◇";//目的地
break;
case 4:
color(6);
cout << " @";//箱子
break;
case 5:
color(3);
cout << "^"; //人
break;
case 7: //4+3 箱子到达目的地
color(8);
cout << "◆";
break;
case 8: //5+3 人与目的地重合
color(3);
cout << "^^^^";
break;
default:
break;
}
}
cout <<endl;
}
return 0;
}
void Move(){ /*移动模块,操作人物和箱子的移动*/ //核心代码
int r,c; //保存人物位置
for(int i=0;i<R;i++){//i为行值
for(int j=0;j<C;j++){//j为列值
if(map[i][j]==5||map[i][j]==8){
r=i;
c=j;
}
}
}
cout<<"您当前的坐标为:("<<r<<","<<c<<")"<<endl;
int ch;//保存键盘输入的ASCII值
ch=_getch();//接受一个任意键的输入,不用按回车就返回。该函数的返回值是所输入字符的ASCII码,且该函数的输入不会自动显示在屏幕上
switch(ch){
case 'w':
case 72:
if(map[r-1][c]==0||map[r-1][c]==3){//人物上方是空地(0)或者目的地 (3),对应情况5和情况6
map[r-1][c]+=5;//人物坐标上一个坐标值为5(人物)
map[r][c]-=5;//人物原坐标值为0(空地)
}
else if(map[r-1][c]==4||map[r-1][c]==7){//人物上方是箱子,箱子在空格上或者箱子在目的地处,对应情况1,2,3,4
if(map[r-2][c]==0||map[r-2][c]==3){//箱子上方是空格或者前面是目的地,细化为情况1,2或情况3,4
map[r-2][c]+=4;//箱子向上移动,上一个0(空地)就变为 4(箱子)
map[r-1][c]+=1;//4(原箱子)+1变为 5(人物)
map[r][c]-=5;//5(原人物)-5变为0(空地)
}
}
break;
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;//箱子向下移动,下一个0(空地)就变为 4(箱子)
map[r+1][c]+=1;//4(原箱子)+1变为 5(人物)
map[r][c]-=5;//5(原人物)-5变为0(空地)
}
}
break;
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;//箱子向下移动,下一个0(空地)就变为 4(箱子)
map[r][c-1]+=1;//4(原箱子)+1变为 5(人物)
map[r][c]-=5;//5(原人物)-5变为0(空地)
}
}
break;
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;//箱子向下移动,下一个0(空地)就变为 4(箱子)
map[r][c+1]+=1;//4(原箱子)+1变为 5(人物)
map[r][c]-=5;//5(原人物)-5变为0(空地)
}
}
break;
case 'q':
flag=false;
system("cls");
break;
case 'u':
setmap(pass);
break;
default:
break;
}
}
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;/*当整个二维数组没有4(箱子) ,表明箱子都到位,返回1 */
}
void setmap(int n){
if(n==1) {
memcpy(map,map1,sizeof(map1));/* memcpy 将数组map1复制给map */
}
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,map5,sizeof(map5));
}
if(n==6) {
memcpy(map,map6,sizeof(map6));
}
}
void color(int m) { /*设置文本显示时的颜色*/
HANDLE consolehend;
consolehend=GetStdHandle(STD_OUTPUT_HANDLE);//获取控制台缓冲区句柄
SetConsoleTextAttribute(consolehend, m);//参数一为控制台缓冲区句柄,参数二为要设置的颜色
}
void cls(){
COORD coor={0,0};
for(int i=0;i<25;i++){
coor.X=i;
coor.Y=0;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coor);
cout<<" ";
}
}
void full_screen(){
HWND hwnd=GetForegroundWindow();
int cx=GetSystemMetrics(SM_CXSCREEN);
int cy=GetSystemMetrics(SM_CYSCREEN);
LONG l_WinStyle=GetWindowLong(hwnd,GWL_STYLE);
SetWindowLong(hwnd,GWL_STYLE,(l_WinStyle|WS_POPUP|WS_MAXIMIZE)&~WS_CAPTION&~WS_THICKFRAME&~WS_BORDER);
SetWindowPos(hwnd,HWND_TOP,0,0,cx,cy,0);
}
肖俊宁在2021-01-02 15:51:56追加了内容
一定一定!!!
o(╥﹏╥)o