c++小游戏 走迷宫

c++小游戏 走迷宫

最近做了一个走迷宫,自我感觉不错。目前制作了10张地图,游玩前需先仔细阅读帮助,不然会无从下手哦!!!查看方法:运行程序,然后按h键。代码如下:

#include<iostream>
#include<windows.h>
#include"GotoXY.h"
#include <conio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
void help();
void migong();
void m();
void help()
{
 char ch2;
 system("cls");
 cout<<"操作说明:"<<endl;
 cout<<endl;
 cout<<"移动说明:"<<endl;
 cout<<"w-向上 s-向下 a-向左 d-向右"<<endl;
 cout<<endl;
 cout<<"标识说明:"<<endl;
 cout<<"&-炸药 @-玩家 #-墙 $-钥匙 ^-地刺 %-传送门 0-可以被炸药炸开的墙"<<endl;
 cout<<endl; 
 cout<<"操作说明:"<<endl;
 cout<<"x-爆破(需要炸药) 移动至物品上方-捡起物品 拥有钥匙后接近传送门附近-传送(按任意键继续)"<<endl; 
 cout<<endl;
 cout<<"注意事项:"<<endl;
 cout<<"踩到地刺会s哦,请小心"<<endl;
 cout<<"按任意键返回"<<endl; 
 ch2=_getch();
 m(); 
}
void migong()
{
 system("cls");
 char ch;
 srand(time(NULL));
 int k; 
 int z=0;
 int key=0;
 int zx[100];
 int zy[100];
 int gq=0; 
 GotoXY(60,5);
 cout<<"TNT x 0"; 
 while (1)
 {
 int map[10][6][10]={ 
 {
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,3,0,4,0,1,1,0,1},
  {1,0,0,0,0,1,1,1,2,1},
  {1,0,0,0,0,1,1,1,0,1},
  {1,0,0,0,0,0,0,0,0,1},
  {1,1,1,1,1,1,1,1,1,1}
 },
 {
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,0,0,0,1,0,2,1},
  {1,0,1,0,1,1,1,0,0,1},
  {1,0,1,0,0,0,0,0,0,1},
  {1,3,1,0,0,0,0,4,0,1},
  {1,1,1,1,1,1,1,1,1,1}
 },
 {
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,0,0,0,0,2,0,1},
  {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7,5,1},
  {1,5,7,7,0,0,7,7,5,1},
  {1,7,3,0,0,0,0,4,7,1},
  {1,1,1,1,1,1,1,1,1,1}
 }
}; 
 k=rand()%10;
 while (1)
 {
  int e=1;
  int keyx,keyy;
  int x;
  int y;
  int dx;
  int dy;
  GotoXY(0,0);
  for (int i=0;i<6;i++)
  {
   for (int j=0;j<10;j++)
   {
    if (map[k][i][j]==1)
    {
     cout<<"#";
    }
    if(map[k][i][j]==0)
    {
     cout<<" ";
    }
    if(map[k][i][j]==2)
    {
     cout<<"$";
     keyx=i;
     keyy=j;
    }
    if (map[k][i][j]==4)
    {
     cout<<"%";
     dx=i;
     dy=j;
    }
    if (map[k][i][j]==3)
    {
     cout<<"@";
     x=i;
     y=j;
    }
    if (map[k][i][j]==5)
    {
     cout<<"&";
     zx[e]=i;
     zy[e]=j;
     e++;
    }
    if (map[k][i][j]==6)
    {
     cout<<"O";
    }
    if (map[k][i][j]==7)
    {
     cout<<"^";
    }
   }
   cout<<endl;
  }
  if(x==keyx&&y==keyy)
  {
   key=1;
   GotoXY(60,4);
   cout<<"YOU GET THE KEY!!!                ";
  }
  else if (x==dx-1&&y==dy||x==dx+1&&y==dy||x==dx&&y==dy+1||x==dx&&y==dy-1)
  {
   if (key==1)
   {
    GotoXY(60,4);
    cout<<"YOU WIN!!! <<<按任意键继续>>>                               ";
    ch=_getch();
    key=0;
    break;
   }
   else
   {
    GotoXY(60,4);
    cout<<"NO KEY!!!                                      ";
   }
  }
  for (int i=1;i<=5;i++) 
  {
  if (x==zx[i]&&y==zy[i])
  {
   z+=1;
   zx[i]=100;
   zy[i]=100;
   GotoXY(60,4);
   cout<<"YOU GET A BAG OF TNT!!!                          ";
   GotoXY(66,5);
   cout<<z;
  }
  }
  GotoXY(60,3);
  cout<<"第"<<gq<<"层" ;
  GotoXY(0,6); 
  ch=_getch();
  switch (ch)
  {
   case 'w':
    if (map[k][x-1][y]==1||map[k][x-1][y]==4||map[k][x-1][y]==6)
    {
     break;
    }
    else if (map[k][x-1][y]==7)
    {
     GotoXY(y,x-1);
     cout<<"@";
     GotoXY(y,x);
     cout<<" ";
     GotoXY(60,4);
     cout<<"YOU LOSE!!!                                          ";
     ch=_getch();
     system("cls");
     m();
    }
    else 
    {
     map[k][x-1][y]=3;
     map[k][x][y]=0;
    }
    break;
   case 's':
    if (map[k][x+1][y]==1||map[k][x+1][y]==4||map[k][x+1][y]==6)
    {
     break;
    }
    else if (map[k][x+1][y]==7)
    {
     GotoXY(y,x+1);
     cout<<"@";
     GotoXY(y,x);
     cout<<" ";
     GotoXY(60,4);
     cout<<"YOU LOSE!!!                                          ";
     ch=_getch();
     system("cls");
     m();
    }
    else
    {
     map[k][x][y]=0;
     map[k][x+1][y]=3;
    }
    break;
   case 'a':
    if (map[k][x][y-1]==1||map[k][x][y-1]==4||map[k][x][y-1]==6)
    {
     break;
    }
    else if (map[k][x][y-1]==7)
    {
     GotoXY(y-1,x);
     cout<<"@";
     GotoXY(y,x);
     cout<<" ";
     GotoXY(60,4);
     cout<<"YOU LOSE!!!                                          ";
     ch=_getch();
     system("cls");
     m();
    }
    else 
    {
     map[k][x][y]=0;
     map[k][x][y-1]=3;
    }
    break;
   case 'd':
    if (map[k][x][y+1]==1||map[k][x][y+1]==4||map[k][x][y+1]==6)
    {
     break;
    }
    else if (map[k][x][y+1]==7)
    {
     GotoXY(y+1,x);
     cout<<"@";
     GotoXY(y,x);
     cout<<" ";
     GotoXY(60,4);
     cout<<"YOU LOSE!!!                                          ";
     ch=_getch();
     system("cls");
     m();
    }
    else 
    {
     map[k][x][y]=0;
     map[k][x][y+1]=3;
    }
    break; 
   case 'x':
    if (z>0)
    {
     z--;
     GotoXY(66,5);
     cout<<z<<" ";
     for (int m=x-1;m<=x+1;m++)
     {
      for (int d=y-1;d<=y+1;d++)
      {
       if (map[k][m][d]==6)
       {
        map[k][m][d]=0;
       }
      }
     }
    }
    else
    {
     GotoXY(60,4);
     cout<<"YOU DON'T HAVE ANY TNT!!!                          ";
    }
    break;
  }
 }
 GotoXY(0,7);
 gq++;
 key=0;
 }
}
void m()
{
 system("cls");
 char ch1;
 system("title 迷宫-作者S_JH330206"); 
 cout<<"                          迷宫游戏"<<endl; 
 cout<<"----------------------------------------------------------------"<<endl;
 cout<<endl;
 cout<<"                          [h]帮助"<<endl;
 cout<<"                          [m]游戏"<<endl;
 cout<<endl;
 cout<<"----------------------------------------------------------------"<<endl;
 ch1=_getch();
 if (ch1=='m')
 {
  migong();
 }
 if (ch1=='h')
 {
  help();
 }
 else
 {
  m();
 } 
}
int main()
{
 m();
 return 0;
}

用到一个自定义头文件,请和上面的文件保存在同一文件夹。代码如下:

/*
  使用前务必阅读以下几点:
    1、c++光标坐标从左上角输出位置(0,0)开始,先列后行,如(10,5)为左数第十列,
 从上往下数第五行;
 2、使用方法:在任意函数(包括自定义函数)中皆可使用,格式为GotoXY(x,y);
 3、要加上#include"GotoXY.h"(注意!!!是引号!!!不是<>!!!说的
 就是->#include"GotoXY.h");
 4、 该文件后缀名为.h,擅自修改后果自负!!! 
 5、本文件作者S_JH330206,转载请声明!!! 
*/ 
#include<iostream>
#include<windows.h>
void GotoXY(int x, int y)  // 移动 
{
 HANDLE hout;  //屏幕尺寸 变量 
 COORD coord={x,y};  //光标坐标 变量  光标x标  光标y标 
 hout=GetStdHandle(STD_OUTPUT_HANDLE);  //获得屏幕尺寸 
 SetConsoleCursorPosition(hout,coord);  //移动光标 
}

看完了,点个赞再走呗❀o( ̄▽ ̄)ブ❀

猜你喜欢

转载自blog.csdn.net/S_JH330206/article/details/107918929