IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> C++迷宫小游戏 -> 正文阅读

[游戏开发]C++迷宫小游戏

放假期间试图制作了迷宫小游戏,用的软件是vc6,有很多不足之处,请多多指教。

代码:

#include <iostream>
#include <cstdio>
#include <conio.h>
#include <windows.h>
#define _UP 0
#define _DOWN 1
#define _LEFT 2
#define _RIGHT 3

using namespace std;

//利用结构体标出坐标
struct point {
    int x, y;
};

enum SYMBOL//枚举函数,方便一一列出
{
	Road='0',
	Wall,
	Player,
	Start,
	End,
	Boom
};

const point PlayerStart = {10, 2};//玩家开始位置
const point PlayerEnd = {2, 20};//玩家结束位置

const int MapLenX = 11, MapLenY = 20;//画面大小

char Map[MapLenX][MapLenY];

const int MoveX[4] = {-1, 1, 0, 0}, MoveY[4] = {0, 0, -1, 1};      //UP, DOWN, LEFT, RIGHT方向

//关闭游戏
void Quit(){
	exit(0);
}

//对玩家的控制
class PlayerActor {
public:
    point  m_Location;
    bool m_IfWin;
    PlayerActor();
    ~PlayerActor();
    void PlayerMove(int _Direc);
    void Refresh(void);
    void CheckIfWin(void);
};

PlayerActor::PlayerActor() {
    m_IfWin = false;
    this->m_Location.x = PlayerStart.x;
    this->m_Location.y = PlayerStart.y;
	Map[this->m_Location.x][this->m_Location.y-1] = '3';//入口位置
    Map[this->m_Location.x][this->m_Location.y] = '2';//玩家位置
    Map[PlayerEnd.x][PlayerEnd.y] = '4';//出口位置
    PlayerActor::Refresh();
    return;
}

PlayerActor::~PlayerActor() {

}
 
void PlayerActor::PlayerMove(int _Direct) {
    if ( Map[this->m_Location.x+MoveX[_Direct]][this->m_Location.y+MoveY[_Direct]] == '0' 
		||Map[this->m_Location.x+MoveX[_Direct]][this->m_Location.y+MoveY[_Direct]] == '5' 
    || Map[this->m_Location.x+MoveX[_Direct]][this->m_Location.y+MoveY[_Direct]] == '4'   ) {        
		// 判断玩家的移动
			if( Map[this->m_Location.x+MoveX[_Direct]][this->m_Location.y+MoveY[_Direct]] == '5')
		{
			MessageBox(NULL, "You Lose!", "失败!", MB_OK);
           Quit(); 
		}
        Map[this->m_Location.x][this->m_Location.y] = '0';
        this->m_Location.x += MoveX[_Direct];
        this->m_Location.y += MoveY[_Direct];
        Map[this->m_Location.x][this->m_Location.y] = '2';
        PlayerActor::Refresh();
        PlayerActor::CheckIfWin();
    }

    return;
}

void PlayerActor::Refresh(void) {
    system("cls");      //刷新画面
	cout<<"#:陷阱"<<endl;
	cout<<"w:上  s:下  a:左  d:右"<<endl;
    for (int i=1; i<=MapLenX; i++) {
        for (int j=1; j<=MapLenY; j++) {
            if (Map[i][j] == '0')
                printf("  ");
            else if (Map[i][j] == '1')
                printf("▇ ");
            else if (Map[i][j] == '2')
                printf("●");
			else if (Map[i][j] == '3')
                printf("入");
            else if (Map[i][j] == '4')
                printf("出 ");
			else if (Map[i][j] == '5')
                printf("##");
        }
        printf("\n");

    }
    return;
}

//判断胜利
void PlayerActor::CheckIfWin(void) {
    if (this->m_Location.x == PlayerEnd.x && this->m_Location.y == PlayerEnd.y)
        m_IfWin = true;
    return;
}

//玩家控制
void PlayerControl(PlayerActor* Player, int _KEY) {
    switch (_KEY) {
        case 'w' : Player->PlayerMove(0);   
            break;
        case 's' : Player->PlayerMove(1); 
            break;
        case 'a' : Player->PlayerMove(2);  
            break;
        case 'd' : Player->PlayerMove(3);   
            break;
        default:
            break;
    }
    return;
}

	void menu(){
    //游戏界面
	
	printf("*************************************\n");
	printf("*           迷 宫 小 游 戏          *\n");
	printf("*                                   *\n");
	printf("*           请 选 择 关 卡          *\n");
	printf("*                 *1                *\n");
	printf("*                 *2                *\n");
	printf("*                 *3                *\n");
	printf("*                 *4                *\n");
	printf("*                 *5                *\n");
	printf("*                 *6退出            *\n");
	printf("*************************************\n");
	
	
	}

int main() {
    menu();
		int level;

		while(1){
	printf("你选择的关卡是:");
		scanf("%d",&level);
		fflush(stdin);
		printf("\n");
		//导入地图
		if (level==1){
    freopen("map5.txt", "r", stdin);
    for (int i=1; i<=MapLenX; i++) {
        for (int j=1; j<=MapLenY; j++) {
            std::cin >> Map[i][j];
        }
    }
	break;
		}else if(level==2){
		freopen("map4.txt", "r", stdin);
    for (int i=1; i<=MapLenX; i++) {
        for (int j=1; j<=MapLenY; j++) {
            std::cin >> Map[i][j];
        }
    }
	break;
		}else if(level==3){
		freopen("map3.txt", "r", stdin);
    for (int i=1; i<=MapLenX; i++) {
        for (int j=1; j<=MapLenY; j++) {
            std::cin >> Map[i][j];
        }
    }
	break;
		}else if(level==4){
		freopen("map2.txt", "r", stdin);
    for (int i=1; i<=MapLenX; i++) {
        for (int j=1; j<=MapLenY; j++) {
            std::cin >> Map[i][j];
        }
    }
	break;
		}else if(level==5){
		freopen("map1.txt", "r", stdin);
    for (int i=1; i<=MapLenX; i++) {
        for (int j=1; j<=MapLenY; j++) {
            std::cin >> Map[i][j];
        }
    }
	break;
		}else if(level==6){
		Quit();
		}
	}
    //创建PLAYERACTOR
    PlayerActor* Player = new PlayerActor;
    while (!Player->m_IfWin) {
        PlayerControl(Player, _getch());
    }
    system("cls");

	MessageBox(NULL, "You Win!", "恭喜!", MB_OK);
	delete Player;
    return;
	
}

map1.txt

11111111111111111111
10000000000011010101
10555555555001050101
10000000550011010101
10151010010110010001
11100011010000110011
10001101010100100101
10505001010111101101
15501011010000100101
10005000010510000001
11111111111111111111

map2.txt

11111111111111111111
10000000000011010101
10151111111001010101
10000000110011010101
10101010010110010001
11100011010000110011
10001101010100100101
10101001010111101101
11101011010000100101
10000000015010000001
11111111111111111111

map3.txt

11111111111111111111
11111111110111111101
11111110000000005001
11111110111011101011
11000000111011100011
11010110111011101111
11150110111011101111
10000110000000000001
11101111101111111101
10000000000000000001
11111111111111111111

map4.txt

11111111111111111111
11150011110000000001
11111000110111101111
11111110110011101011
11000000111010000001
11010110111011101111
11510110111011101111
10000110000011111111
11111111111011110111
10000000000011110111
11111111111111111111

map5.txt

11111111111111111111
11111111111111000001
11111155111111011111
11111555111111011111
11111155111111011111
11111155111111011111
11111155111111011111
11115555551111011111
11111111111111011111
10000000000000011111
11111111111111111111

运行结果:

首页:

?第一关:

?第二关:

?第三关:

?第四关:

第五关:

?

?

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-08-11 12:46:26  更:2021-08-11 12:46:42 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/9 10:03:54-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码