⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eg7.cpp

📁 C++ 关于.NET方便的东西..是我很长时间才搜集道德
💻 CPP
字号:
// eg7.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Point
{
	int m_nX;
	int m_nY;
};

int g_nMap[10][15] = 
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,2,0,0,0,0,1,0,1,0,1,1,1,1},
{1,0,1,0,1,1,0,1,0,0,0,0,0,1,1},
{1,0,1,0,0,1,0,1,1,0,1,1,1,1,1},
{1,0,1,1,0,1,0,0,1,0,1,0,0,0,1},
{1,0,0,1,0,1,1,1,1,0,1,0,1,1,1},
{1,0,0,1,0,0,0,0,0,0,1,0,1,1,1},
{1,0,0,1,1,1,1,1,1,0,1,0,1,1,1},
{1,0,0,0,0,0,0,1,1,0,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};

void MapFresh()
{
	system("cls");
	for(int nA=0; nA<10; nA++)
	{
		for(int nB=0; nB<15; nB++)
		{
			if(g_nMap[nA][nB] == 1)
				printf("#");
			else if(g_nMap[nA][nB] == 2)
				printf("@");
			else 
				printf(" ");
		}
		printf("\n");
	}
}


int main(int argc, char* argv[])
{
	MapFresh();
	Point ptCurrentPos = {1,2};		//当前位置
	Point ptOldPos = ptCurrentPos;	//记录上一次的位置
	Point ptTestPos;				//记录检测位置
	printf("输入 a左 d右  w上 s下 \n");
	char nInput;					//输入
	while(1)
	{

		scanf("%c",&nInput);
		ptTestPos = ptCurrentPos;
		switch(nInput)
		{
		case 'a':
//		case 'A':
			ptTestPos.m_nX--;
			break;
		case 'd':
//		case 'D':
			ptTestPos.m_nX++;
			break;
		case 's':
//		case 'S':
			ptTestPos.m_nY++;
			break;
		case 'w':
//		case 'W':
			ptTestPos.m_nY--;
			break;
		}
		if(g_nMap[ptTestPos.m_nX][ptTestPos.m_nY] == 0)
		{
			//把检测到的位置作为下一次的新位置
			ptCurrentPos = ptTestPos;
			//在老位置变成0
			g_nMap[ptOldPos.m_nX][ptOldPos.m_nY] = 0;
			//新位置变成2,既人
			g_nMap[ptCurrentPos.m_nX][ptCurrentPos.m_nY] = 2;

			//画地图
			MapFresh();
			//把这次的新位置作为下一次的老位置
			ptOldPos = ptCurrentPos;
		}
		else
		{
			printf("次路不通\n");
//			getch();
		}

	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -