1.4版走迷宫小游戏

这次更新加入了一个熊孩子,他会放置障碍物,小心点哦!

#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;
}

小尾巴:啥也没有

原创文章 75 获赞 126 访问量 1万+

猜你喜欢

转载自blog.csdn.net/liuzich/article/details/103645946
今日推荐