C++迷宫游戏代码

  今天分享一个c++迷宫游戏的代码,废话不多说,上代码:

#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h> 
using namespace std;
char maze[8][9]={
	"########",
	"#O #$$$#",
	"#  #$$$#",
	"# X $#$#",
	"#$ ##$ #",
	"#$###$##",
	"#$$   E#",
	"########"
};
int main(){
	char op;
	int x=1,y=1,ex=3,ey=2,score=0;
	srand(time(0));
	for(int i=0;i<8;i++){
		for(int j=0;j<8;j++){
			cout<<maze[i][j];
		}
		cout<<endl;
	}
	cout<<"score:"<<score<<endl;
	bool done=false;
	while(true){
		op=getch();
		if(op=='w'&&maze[x-1][y]!='#'){
			maze[x][y]=' ';
			x--;
		}else if(op=='s'&&maze[x+1][y]!='#'){
			maze[x][y]=' ';
			x++;
		}else if(op=='a'&&maze[x][y-1]!='#'){
			maze[x][y]=' ';
			y--;
		}else if(op=='d'&&maze[x][y+1]!='#'){
			maze[x][y]=' ';
			y++;
		}else{
			continue;
		}if(maze[x][y]=='X') break;
		if(maze[x][y]=='E'){
			maze[x][y]='O';done=true;break;
		}if(maze[x][y]=='$'){
			score++;
		}
		maze[x][y]='O';
		system("cls");
		for(int i=0;i<8;i++){
			for(int j=0;j<8;j++){
				cout<<maze[i][j];
			}
			cout<<endl;
		}	
		cout<<"score:"<<score<<endl;
	}
	system("cls");
	for(int i=0;i<8;i++){
		for(int j=0;j<8;j++){
			cout<<maze[i][j];
		}
		cout<<endl;
	}
	cout<<"___________________"<<endl;
	if(done) cout<<"You win!";
	else cout<<"You failed.";
	cout<<endl;
	cout<<"score:"<<score<<endl;
	return 0;
} 

运行结果:

按wsad键移动,X是怪物,碰到就会失败;$是金币,碰到就会消失,分数加1;E是终点,碰到就会成功。

注:此代码以亲测,可放心食用。

给点赞吧,求求了QwQ。

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值