c++迷宫游戏

#include <conio.h>
#include <windows.h>
#include <bits/stdc++.h>
using namespace std;
void SetPos(COORD a)
{
	HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(out, a);
}
void SetPos(int i, int j)
{
	COORD pos= {i, j};
	SetPos(pos);
}
void color(int a) {
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
int main() {
	int m,w=2;
	char a[308][308];
	int x=1,y=1,xx=20,yy=20;
	for(int i=1; i<=30; i++) {
		for(int j=1; j<=30; j++) {
			if(i==1&&j==1||i==30&&j==30||i==20&&j==2) {
				if(i==1&&j==1)
					m=3;
				if(i==30&&j==30)
					m=2;
				if(i==20&&j==20)
					m=4;
			} else {
				m=rand()%w;
			}
			if(m==1) {
				a[i][j]='*';
			}
			if(m==0) {
				a[i][j]='#';
			}
			if(m==2) {
				a[i][j]='@';
			}
			if(m==3) {
				a[i][j]='O';
			}
			if(m==4) {
				a[i][j]='X';
			}
		}
	}
	while(1) {
		SetPos(0,0);
		int x_=x,y_=y,xx_=xx,yy_=yy;
		char e='.';
		for(int i=0; i<=31; i++) {
			for(int j=0; j<=31; j++) {
				if(i==0||i==31||j==0||j==31)
				{
					color(14);cout<<"★";
				}
				else
				{
					switch(a[i][j])
					{
						case 'O':color(15*16+0);printf("♀");break;
						case '*':color(0);printf("  ");break;
						case '#':color(10);printf("■");break;
						case '@':color(15);printf("¤");break;
						case 'X':color(15*16+0);printf("♂");break;
					}
				}
				
			}
			cout<<endl;
		}
		cout<<"W:向上\nX:向下\nA:向左\nD:向右\nQ:左上\nZ:左下\nE:右上\nC:右下\n请选择\n";
		int zzz=rand()%4;
		switch(zzz)
		{
			case 0:if(xx+1>0&&xx+1<31)xx+=1;break;
			case 1:if(xx-1>0&&xx-1<31)xx-=1;break;
			case 2:if(yy+1>0&&yy+1<31)yy+=1;break;
			case 3:if(yy-1>0&&yy-1<31)yy-=1;break;
		}
		while(e!='w'&&e!='x'&&e!='a'&&e!='d'&&e!='q'&&e!='z'&&e!='e'&&e!='c') {
			e=getch();
			if(e=='w') {
				x--;
			}
			if(e=='x') {
				x++;
			}
			if(e=='a') {
				y--;
			}
			if(e=='d') {
				y++;
			}
			if(e=='q') {
				x--;
				y--;
			}
			if(e=='z') {
				x++;
				y--;
			}
			if(e=='e') {
				x--;
				y++;
			}
			if(e=='c') {
				x++;
				y++;
			}
			if(e!='w'&&e!='x'&&e!='a'&&e!='d'&&e!='q'&&e!='z'&&e!='e'&&e!='c') {
				cout<<"无,请重新选择\n";
			}
		}
		if(x<1||y<1||x>30||y>30||xx==x&&yy==y) {
			system("cls");
			color(8);
			MessageBox(0,"游戏失败","游戏结束",MB_OK);
			return 0;
		}
		if(a[x][y]=='#') {
			system("cls");
			color(8);
			MessageBox(0,"游戏失败","游戏结束",MB_OK);
			return 0;
		}
		if(a[x][y]=='@') {
			system("cls");
			color(8);
			MessageBox(0,"游戏成功","游戏结束",MB_OK);
			return 0;
		}
		a[x_][y_]='*';
		a[x][y]='O';
		a[xx_][yy_]='#';
		a[xx][yy]='X';
	}

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是代码 #include #include #include #include #define Height 25 //迷宫的高度,必须为奇数 #define Width 25 //迷宫的宽度,必须为奇数 #define Wall 1 #define Road 0 #define Start 2 #define End 3 #define Esc 5 #define Up 1 #define Down 2 #define Left 3 #define Right 4 int map[Height+2][Width+2]; void gotoxy(int x,int y) //移动坐标 { COORD coord; coord.X=x; coord.Y=y; SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord ); } void hidden()//隐藏光标 { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cci; GetConsoleCursorInfo(hOut,&cci;); cci.bVisible=0;//赋1为显示,赋0为隐藏 SetConsoleCursorInfo(hOut,&cci;); } void create(int x,int y) //随机生成迷宫 { int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向 int i,j,t; //将方向打乱 for(i=0;i<4;i++) { j=rand()%4; t=c[i][0];c[i][0]=c[j][0];c[j][0]=t; t=c[i][1];c[i][1]=c[j][1];c[j][1]=t; } map[x][y]=Road; for(i=0;i<4;i++) if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall) { map[x+c[i][0]][y+c[i][1]]=Road; create(x+2*c[i][0],y+2*c[i][1]); } } int get_key() //接收按键 { char c; while(c=getch()) { if(c==27) return Esc; //Esc if(c!=-32)continue; c=getch(); if(c==72) return Up; //上 if(c==80) return Down; //下 if(c==75) return Left; //左 if(c==77) return Right; //右 } return 0; } void paint(int x,int y) //画迷宫 { gotoxy(2*y-2,x-1); switch(map[x][y]) { case Start: printf("入");break; //画入口 case End: printf("出");break; //画出口 case Wall: printf("▇");break; //画墙 case Road: printf(" ");break; //画路 } } void game() { int x=2,y=1; //玩家当前位置,刚开始在入口处 int c; //用来接收按键 while(1) { gotoxy(2*y-2,x-1); printf("●"); //画出玩家当前位置 if(map[x][y]==End) //判断是否到达出口 { gotoxy(30,24); printf("到达终点,按任意键结束"); getch(); break; } c=get_key(); if(c==Esc) { gotoxy(0,24); break; } switch(c) { case Up: //向上走 if(map[x-1][y]!=Wall) { paint(x,y); x--; } break; case Down: //向下走 if(map[x+1][y]!=Wall) { paint(x,y); x++; } break; case Left: //向左走 if(map[x][y-1]!=Wall) { paint(x,y); y--; } break; case Right: //向右走 if(map[x][y+1]!=Wall) { paint(x,y); y++; } break; } } } int main() { system("title yourname"); int i,j; srand((unsigned)time(NULL)); //初始化随即种子 hidden(); //隐藏光标 for(i=0;i<=Height+1;i++) for(j=0;j<=Width+1;j++) if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫 map[i][j]=Road; else map[i][j]=Wall; create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数 for(i=0;i<=Height+1;i++) //边界处理 { map[i][0]=Wall; map[i][Width+1]=Wall; } for(j=0;j<=Width+1;j++) //边界处理 { map[0][j]=Wall; map[Height+1][j]=Wall; } map[2][1]=Start; //给定入口 map[Height-1][Width]=End; //给定出口 for(i=1;i<=Height;i++) for(j=1;j<=Width;j++) //画出迷宫 paint(i,j); game(); //开始游戏 getch(); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值