青少年编程知识记录 codecoming

C++实现走迷宫小游戏

1.简单版本

简单版本是利用字符数组实现的,利用字符数组设定入口和出口,用户每次按键就刷新一下当前字符数组。

参考代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<conio.h>
int main() {
   char a[50][50] = { "######",
                       "#O #  ",
                       "# ## #",
                       "#  # #",
                       "##   #",
                       "######"};
   int  i, x, y, p, q;
   x = 1; y = 1; p = 1; q = 5; //出生点和出口点
   char ch;
   for (i = 0; i <=5; i++)   //循环6次
       puts(a[i]);   //输出得到6行
   while (x!=p||y!=q)   //除了出口位置以外都可以移动
   {
       ch = _getch();     //赋值字符
       if (ch == 's')   //方向“下”键,,移动方向已二维坐标系为准
       {  
           if (a[x + 1][y] != '#')
           {//当碰到墙壁则不移动,不然就变成穿墙外挂了
               a[x][y] = ' '; //无墙时则移动
               x++;
               a[x][y] = 'O';
           }
       }
       if (ch=='w')
       {
           if (a[x - 1][y] != '#')
           {
               a[x][y] = ' ';
               x--;
               a[x][y] = 'O';
           }
       }
       if (ch=='a')
       {
           if(a[x][y-1]!='#')
           {
               a[x][y] = ' ';
               y--;
               a[x][y] = 'O';
           }
       }
       if (ch=='d')
       {
           if(a[x][y + 1] != '#')
           {
               a[x][y] = ' ';
               y++;
               a[x][y] = 'O';
           }
       }
       system("cls");   //清屏 ,,因为每次移动要清楚之前的位置,,可以理解为 刷新
       for (i = 0; i <= 5; i++)   //给每行加6个字符
           puts(a[i]);   //得到一行6个字符串
   }
   system("cls");
   printf("恭喜你通关了\n");
       Sleep(5000);
   return 0;
}
2.复杂版本

复杂版本,完成度80% ,没有检测边界碰撞问题。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<conio.h>
#include<windows.h>
int x=0,y=1,m=8,n=8; //x,y表示入口,m,n表示出口
void gotoxy(int x, int y) {
   COORD pos = {x,y};
   HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
   SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}
//地图函数
char map[50][50]{
   "* ********", //0
   "* ********", //1
   "* ********", //2
   "* ********", //3
   "* ********", //4
   "* ********", //5
   "* ********", //6
   "* ********", //7
   "*         ", //8
   "**********", //9
  //0123456789
};
//展示地图函数
void showMap(){  
   for(int i=0;i<10;i++)
       puts(map[i]);
}
//判断结束游戏函数
int  check(int p,int q)
{
   if(p==m&&q==n) //如果是出口
   {
       system("color A9");
       system("cls");
       puts("恭喜过关");
       return 0 ;
   }else if(map[q][p]=='*')
   {
       gotoxy(23,23);
       printf("xing");
       return 1;
   }
   else   //剩余是通道
   {
       puts("@");
       gotoxy(20,20);
       printf("%d,%d,%d,%d",p,m,q,n);
       gotoxy(25,25);
       printf("%c",map[p][q]);
       return 2;
       
   }
}
using namespace std;
int main()
{
   
   system("cls");
   showMap();
   gotoxy(y,x);
   puts("@");
 
   char ch;  
   while (1){
       if (_kbhit()){//如果有按键按下,则_kbhit()函数返回真
           ch = _getch();//使用_getch()函数获取按下的键值
           if (ch == 'w'){  
                system("cls");
                showMap();
                gotoxy(y,x-1);
                    x-=1;
                check(y,x);     //检查边界以及墙
                   
               
           };
           if (ch == 's'){
               system("cls");
                showMap();
                gotoxy(y,x+1);
                  x+=1;
                 check(y,x);
                 
           };
           if (ch == 'a'){
               system("cls");
                showMap();
                gotoxy(y-1,x);
                 y-=1;
                 check(y,x);
                 
           };
           if (ch == 'd'){  
                system("cls");
                showMap();
                gotoxy(y+1,x);
                 y+=1;
                check(y,x);
                 
           };
       }
   }
   
   
   
}



(adsbygoogle = window.adsbygoogle || []).push({});

作者:亿万年的星光 分类:趣味小程序 浏览: