C语言迷宫游戏改进版

模拟迷宫游戏

#include <iostream>
#include <stdlib.h>
#include <conio.h>


#define  ROW 10
#define  CUL 10 


//设置X,Y坐标(全局变量);
int currentX=8,currentY=1;
//移动后的XY坐标(全局变量);
int nextX,nextY;
//看下一步是否能走  int[x][y]==' ' ;
char street = ' ';


char map[ROW][CUL] = {
	{'#','#','#','#','#','#','#','#','#','#'},
	{'#','#',' ',' ',' ',' ','#','#','#','#'},
	{'#',' ',' ','#','#',' ',' ','#','#','#'},
	{'#',' ','#',' ','#','#',' ',' ','#','#'},
	{'#',' ',' ',' ',' ','#','#',' ','#','#'},
	{'#',' ','#','#',' ','#','#',' ','#','#'},
	{'#',' ','#','#',' ',' ','#',' ','#','#'},
	{'#',' ','#','#','#',' ','#',' ',' ',' '},
	{'#','0','#',' ',' ',' ','#','#','#','#'},
	{'#','#','#','#','#','#','#','#','#','#'},
};

using namespace std;



void show();  // 打印地图 
void moveTo(char direction);  //控制小人移动
void judge();  // 判断是非否为边界




int main()
{
	nextX = currentX;
    nextY = currentY;
    //屏幕打印出迷宫;
    show();
    char direction;
    while (1) {
        cout  <<"请移动人物,用键盘W/S/A/D(上下左右)操作" << endl;
        
        direction = getch();
        moveTo(direction);
        judge();
		system("CLS");
        show();
        if (currentX==ROW-1||currentY==CUL-1){
            cout << "通关了,呵呵!" << endl;
            break;
        }
    }
    return 0;
	
	show();
	
}


//打印地图
void show(){
    for(int i = 0;i<ROW;i++){
        for (int j = 0;j<CUL;j++) {
            printf("%c",map[i][j]);
        }
        printf("\n");
    }
}
//移动人物

void judge(){
    if (map[nextX][nextY]==street) {
        char temp = map[currentX][currentY];
        map[currentX][currentY] = map[nextX][nextY];
        map[nextX][nextY] = temp;
        currentX = nextX;
        currentY = nextY;
        
    }else{
        nextX = currentX;
        nextY = currentY;
    }
}
//计算下一个位置
void moveTo(char direction){
    switch (direction) {
        case 'w':{
            nextX = currentX - 1;
            break;
        }
        case 's':{
            nextX = currentX + 1;
            break;
        }
        case 'a':{
            nextY = currentY - 1;
            break;
        }
        case 'd':{
            nextY = currentY + 1;
            break;
        }
        default:
            break;
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值