easyx游戏编程:魔塔(c++语言开发)源码

easyx游戏编程:魔塔(C++游戏开发)

源码介绍

我的编程环境是vs2019
#include "stdafx.h"是自带的,其他版本可能需要添加#include “stdafx.h”
还需要添加众多的图片这里我下载链接放下面了

代码下载链接
提取码:hzzd

除了下面代码外,无其他改动

#include <stdio.h>
#include <graphics.h>
#include <Windows.h>
#include <stdlib.h>
#include <mmsystem.h>
#include <conio.h>
#pragma comment(lib,"winmm.lib")

#define SIZE                60           //一张图片的像素


void Welcome();                      //游戏欢迎界面
void Init();						 //初始化游戏  
void FindRole(int*, int*);			 //寻找人物位置
void Book();                        //查看怪物手册
void Convey(char);                  //楼层传送
char* turn(int);					 //数字转化为字符串
void Game(int, int ,int ,int );				 //人物操作


//角色结构体
typedef struct _ROLEMAN
{
	int HP;          //血量
	int att;         //攻击力
	int def;         //防御力
	int money;       //金钱
	int ykey;        //黄钥匙
	int bkey;        //蓝钥匙
}ROLEMAN;

//怪物结构体
typedef struct _MONSTER
{
	int HP;      //血量
	int att;     //攻击力
	int def;     //防御力
	int money;   //掉落金钱
}MONSTER;

int map[6][13][13]     //13是增加多余的墙,防止游戏过程中出现数组越界问题
{
	//第一层地图	
	{
		{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
		{ 0, 25, 1, 10, 11, 10, 1, 1, 1, 1, 1, 1, 0 },
		{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
		{ 0, 8, 1, 1, 2, 1, 0, 6, 4, 1, 0, 1, 0 },
		{ 0, 1, 13, 1, 0, 1, 0, 7, 8, 1, 0, 1, 0 },
		{ 0, 0, 2, 0, 0, 1, 0, 0, 0, 2, 0, 1, 0 },
		{ 0, 4, 1, 1, 0, 1, 2, 12, 15, 12, 0, 1, 0 },
		{ 0, 1, 14, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0 },
		{ 0, 0, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0 },
		{ 0, 1, 1, 1, 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, 0 },
	{ 0, 2, 2, 12, 1, 0, 1, 0, 4, 4, 0, 1, 0 },
	{ 0, 0, 0, 0, 2, 0, 12, 0, 4, 4, 0, 1, 0 },
	{ 0, 4, 1, 15, 1, 0, 1, 0, 0, 0, 0, 1, 0 },
	{ 0, 4, 1, 1, 12, 0, 1, 10, 1, 1, 1, 1, 0 },
	{ 0, 0, 14, 0, 0, 0, 1, 0, 0, 0, 0, 11, 0 },
	{ 0, 1, 1, 1, 1, 0, 10, 0, 1, 1, 1, 1, 0 },
	{ 0, 9, 4, 8, 1, 0, 1, 0, 10, 0, 0, 0, 0 },
	{ 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 29, 0 },
	{ 0, 26, 19, 1, 1, 1, 1, 0, 1, 2, 1, 28, 0 },
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
},
//boss地图
{
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
	{ 0, 36, 36, 36, 36, 0, 32, 0, 36, 36, 36, 36, 0 },
	{ 0, 36, 36, 36, 36, 0, 1, 0, 36, 36, 36, 36, 0 },
	{ 0, 36, 36, 36, 36, 0, 35, 0, 36, 36, 36, 36, 0 },
	{ 0, 36, 36, 36, 36, 0, 1, 0, 36, 36, 36, 36, 0 },
	{ 0, 36, 36, 36, 36, 0, 35, 0, 36, 36, 36, 36, 0 },
	{ 0, 36, 36, 36, 36, 0, 1, 0, 36, 36, 36, 36, 0 },
	{ 0, 36, 36, 36, 36, 0, 35, 0, 36, 36, 36, 36, 0 },
	{ 0, 36, 36, 36, 36, 0, 1, 0, 36, 36, 36, 36, 0 },
	{ 0, 36, 36, 36, 36, 0, 1, 0, 36, 36, 36, 36, 0 },
	{ 0, 36, 36, 36, 36, 0, 1, 0, 36, 36, 36, 36, 0 },
	{ 0, 36, 36, 36, 36, 0, 16, 0, 36, 36, 36, 36, 0 },
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
}
};

//初始化结构体变量
ROLEMAN roleman = { 1000, 100, 100, 0, 0, 0 };    //角色
MONSTER gslime = { 35, 18, 1, 1 };                //绿史莱姆
MONSTER rslime = { 45, 20, 2, 2 };                //红史莱姆
MONSTER bat = { 35, 38, 3, 3 };                   //蝙蝠
MONSTER skull = { 50, 42, 6, 6 };                 //骷髅人
MONSTER bskull = { 55, 52, 12, 8 };               //骷髅士兵
MONSTER wizard = { 60, 32, 8, 5 };                //法师
MONSTER protection = { 100, 50, 15, 15 };         //守卫
MONSTER sboss = { 110, 55, 15, 0 };               //boss侍卫
MONSTER boss = { 120, 60, 20, 0 };                //boss


//声明图片变量
IMAGE wall, floor, ydoor, bdoor, ykey, bkey, rstone, bstone, rliquid, bliquid;       //墙,地板,黄门,蓝门,黄钥匙,蓝钥匙,红水晶,蓝水晶,红药水,蓝药水
IMAGE _gslime, _rslime, _bat, _skull, _bskull, _wizard;                              //绿史莱姆,红史莱姆,蝙蝠,骷髅人,骷髅士兵,法师
IMAGE roleup, roledown, roleleft, roleright, leader, helper, suffering, store;          //角色朝向,同是塔中人,指引者,帮助者,狱友,商店
IMAGE stairsup, stairsdown, cage, sword, shield, convey;                              //上下楼梯,笼子,剑,盾,传送道具
IMAGE defend, _boss, _sboss, fire;

int level = 0, buycount = 0, money = 20, defenceover = 0, highlevel = 1;           //当前所在层数 - 1,在商店购买的次数,商店购买所需金钱,打败守卫的个数,去过的最高层数
bool book = false, updown = false, final = true, final2 = false;          //是否得到怪物手册,传送道具;是否可以挑战最终boss,查看最终怪物手册
char str[10];
HWND hwnd;

//游戏欢迎界面
void Welcome()
{

	initgraph(830,680);

	//输出魔塔
	settextstyle(150, 150, "楷体");
	settextcolor(RED);
	outtextxy(120, 100, "魔塔");

	//输出按回车键开始游戏
	settextstyle(40, 40, "黑体");
	settextcolor(WHITE);
	outtextxy(90, 450, "按任意键开始游戏");

	//输出作者
	settextstyle(32, 16, "华文琥珀");
	outtextxy(500, 630, "制作人: 奇·大大 ");

	_getch();
	
	cleardevice();
	settextstyle(16, 8, "黑体");
}

//初始化游戏
void Init()
{
	//加载图片
	loadimage(&wall, "魔塔/墙.jpg", SIZE, SIZE);
	loadimage(&floor, "魔塔/地板.jpg", SIZE, SIZE);
	loadimage(&ydoor, "魔塔/黄门.jpg", SIZE, SIZE);
	loadimage(&bdoor, "魔塔/蓝门.jpg", SIZE, SIZE);
	loadimage(&ykey, "魔塔/黄钥匙.jpg", SIZE, SIZE);
	loadimage(&bkey, "魔塔/蓝钥匙.jpg", SIZE, SIZE);
	loadimage(&rstone, "魔塔/红水晶.jpg", SIZE, SIZE);
	loadimage(&bstone, "魔塔/蓝水晶.jpg", SIZE, SIZE);
	loadimage(&rliquid, "魔塔/红药水.jpg", SIZE, SIZE);
	loadimage(&bliquid, "魔塔/蓝药水.jpg", SIZE, SIZE);
	loadimage(&_gslime, "魔塔/绿史莱姆.jpg", SIZE, SIZE);
	loadimage(&_rslime, "魔塔/红史莱姆.jpg", SIZE, SIZE);
	loadimage(&_bat, "魔塔/蝙蝠.jpg", SIZE, SIZE);
	loadimage(&_skull, "魔塔/骷髅人.jpg", SIZE, SIZE);
	loadimage(&_bskull, "魔塔/骷髅士兵.jpg", SIZE, SIZE);
	loadimage(&_wizard, "魔塔/法师.jpg", SIZE, SIZE);
	loadimage(&roleup, "魔塔/角色上.jpg", SIZE, SIZE);
	loadimage(&roledown, "魔塔/角色下.jpg", SIZE, SIZE);
	loadimage(&roleleft, "魔塔/角色左.jpg", SIZE, SIZE);
	loadimage(&roleright, "魔塔/角色右.jpg", SIZE, SIZE);
	loadimage(&leader, "魔塔/指引者.jpg", SIZE, SIZE);
	loadimage(&helper, "魔塔/帮助者.jpg", SIZE, SIZE);
	loadimage(&suffering, "魔塔/狱友.jpg", SIZE, SIZE);
	loadimage(&store, "魔塔/商店.jpg", SIZE, SIZE);
	loadimage(&stairsup, "魔塔/上楼梯.jpg", SIZE, SIZE);
	loadimage(&stairsdown, "魔塔/下楼梯.jpg", SIZE, SIZE);
	loadimage(&cage, "魔塔/笼子.jpg", SIZE, SIZE);
	loadimage(&sword, "魔塔/剑.jpg", SIZE, SIZE);
	loadimage(&shield, "魔塔/盾.jpg", SIZE, SIZE);
	loadimage(&convey, "魔塔/传送道具.jpg", SIZE, SIZE);
	loadimage(&defend, "魔塔/守卫.jpg", SIZE, SIZE);
	loadimage(&_boss, "魔塔/BOSS.jpg", SIZE, SIZE);
	loadimage(&_sboss, "魔塔/boss侍卫.jpg", SIZE, SIZE);
	loadimage(&fire, "魔塔/岩浆.jpg", SIZE, SIZE);

	//背景音乐
	mciSendString("open 魔塔/背景音乐.mp3 alias BGM", 0, 0, 0);
	mciSendString("play BGM repeat", 0, 0, 0);

	//输出信息框及信息
	rectangle(680, 10, 820, 670);
	outtextxy(700, 75, "生命:");
	outtextxy(700, 100, "攻击:");
	outtextxy(700, 125, "防御:");
	outtextxy(700, 150, "金钱:");
	outtextxy(700, 210, "黄钥匙:");
	outtextxy(700, 235, "蓝钥匙:");
	outtextxy(700, 295, "当前层数:");
	outtextxy(690, 360, "当前武器:神圣剑");
	outtextxy(690, 385, "当前防具:神圣盾");
	
}

//画地图以及输出信息
void Draw()
{
	int j, i;

	for (j = 1; j <= 11; j++)
	{
		for (i = 1; i <= 11; i++)
		{
			switch (map[level][j][i])
			{
			case 0: putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &wall); break;
			case 1:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &floor); break;
			case 2:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &ydoor); break;
			case 3:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &bdoor); break;
			case 4:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &ykey); break;
			case 5:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &bkey); break;
			case 6:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &rstone); break;
			case 7:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &bstone); break;
			case 8:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &rliquid); break;
			case 9:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &bliquid); break;
			case 10:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &_gslime); break;
			case 11:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &_rslime); break;
			case 12:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &_bat); break;
			case 13:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &_skull); break;
			case 14:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &_bskull); break;
			case 15:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &_wizard); break;
			case 16:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &roleup); break;
			case 17:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &roledown); break;
			case 18:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &roleleft); break;
			case 19:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &roleright); break;
			case 20:
			case 21:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &leader); break;
			case 22:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &helper); break;
			case 23:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &suffering); break;
			case 24:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &store); break;
			case 25:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &stairsup); break;
			case 26:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &stairsdown); break;
			case 27:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &cage); break;
			case 28:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &sword); break;
			case 29:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &shield); break;
			case 30:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &convey); break;
			case 31:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &defend); break;
			case 32:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &_boss); break;
			case 33:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &_sboss); break;
			case 34:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &floor); break;
			case 35:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &_sboss); break;
			case 36:putimage((i - 1) * SIZE + 10, (j - 1) * SIZE + 10, &fire); break;
			}
		}
	}

	//画游戏框
	rectangle(10, 10, 670, 670);

	//消除之前的信息
	setfillcolor(BLACK);
	solidrectangle(740, 75, 772, 91);		//生命
	solidrectangle(740, 100, 772, 116);		//攻击
	solidrectangle(740, 125, 772, 141);		//防御
	solidrectangle(740, 150, 772, 166);		//金钱
	solidrectangle(756, 210, 772, 226);		//黄钥匙
	solidrectangle(756, 235, 772, 251);		 //蓝钥匙

	//输出信息
	outtextxy(740, 75, turn(roleman.HP));      //生命值
	outtextxy(740, 100, turn(roleman.att));     //攻击
	outtextxy(740, 125, turn(roleman.def));    //防御
	outtextxy(740, 150, turn(roleman.money));  //金钱
	outtextxy(756, 210, turn(roleman.ykey));   //黄钥匙
	outtextxy(756, 235, turn(roleman.bkey));   //蓝钥匙
	outtextxy(772, 295, turn(level + 1));        //当前层数
}

//寻找人物位置
void FindRole(int* prow, int* pcol)
{
	for (*prow = 1; *prow <= 13; (*prow)++)
	{
		for (*pcol = 1; *pcol <= 13; (*pcol)++)
		{
			switch (map[level][*prow][*pcol])
			{
			case 16:
			case 17:
			case 18:
			case 19: return;
			}
		}
	}
}

//数字转化为字符串
char* turn(int num)
{
	sprintf_s(str, "%d", num);
	return str;
}

//查看怪物手册
void Book()
{
	if (book)
	{
		switch (level + 1)
		{
		case 1:
			MessageBox(hwnd, "怪物名字  生命  攻击  防御  金钱\n\
绿史莱姆	35    18    1       1\n\
红史莱姆	45    20    2       2\n\
法师        60    32    8       5\n\
蝙蝠        35    38    3       3\n\
骷髅人		50    42    6       6\n\
骷髅士兵	55    52    12		8", "", MB_OK); break;

		case 2:
			MessageBox(hwnd, "怪物名字  生命  攻击  防御  金钱\n\
守卫        100   50    15     15", "", MB_OK); break;

		case 3:
			MessageBox(hwnd, "怪物名字  生命  攻击  防御  金钱\n\
红史莱姆	45    20    2       2\n\
法师        60    32    8       5\n\
蝙蝠        35    38    3       3\n\
骷髅人		50    42    6       6", "", MB_OK); break;

		case 4:
			MessageBox(hwnd, "怪物名字  生命  攻击  防御  金钱\n\
绿史莱姆	35    18    1       1\n\
红史莱姆	45    20    2       2\n\
法师        60    32    8       5\n\
蝙蝠        35    38    3       3\n\
骷髅人		50    42    6       6\n\
骷髅士兵	55    52    12		8", "", MB_OK); break;

		case 5:
			if (!final2)
				MessageBox(hwnd, "怪物名字  生命  攻击  防御  金钱\n\
绿史莱姆	35    18    1       1\n\
红史莱姆	45    20    2       2\n\
法师        60    32    8       5\n\
蝙蝠        35    38    3       3\n\
骷髅士兵	55    52    12		8", "", MB_OK);
			else
				MessageBox(hwnd, "怪物名字  生命  攻击  防御  金钱\n\
魔王侍卫	110   55    15       0\n\
魔王        120   60    20       0", "", MB_OK);
			break;
		}
	}
}

//楼层传送
void Convey(char con)
{
	int ID;

	if (updown)
	{
		switch (con)
		{
		case 'U':
		case 'u':
			ID = MessageBox(hwnd, "您要传送至上一层吗?", "", MB_YESNO);

			if (ID == IDNO)
			{
				return;
			}

			if (level + 2 <= highlevel)
			{
				level++;
			}

			else
			{
				if (level == 5 )
					MessageBox(hwnd, "您已到达塔的最高层!", "", MB_OK);

				else
					MessageBox(hwnd, "您还没去过上层!", "", MB_OK);
			}
			break;

		case 'N':
		case 'n':
			ID = MessageBox(hwnd, "您要传送至下一层吗?", "", MB_YESNO);

			if (ID == IDNO)
			{
				return;
			}

			if (level > 0)
			{
				level--;
			}

			else
			{
				MessageBox(hwnd, "您已在塔的最底层!", "", MB_OK);
			}
			break;
		}
	}
}

//人物操作
void Game(int row, int col, int x, int y)
{
	int count, loss, ID, HP;      //攻击次数与角色损失血量,弹框返回值,商店购买增加的血量
	int c = 0;						  //人物运动状态
	char con;
	hwnd = GetHWnd();    //弹窗前置
	if (x < 0 && y == 0)
		c = 17;
	if (x > 0 && y == 0)
		c = 16;
	if (y < 0 && x == 0)
		c = 18;
	if (y > 0 && x == 0)
		c = 19;


	switch (map[level][row + x][col + y])
	{
	case 0: //墙
		map[level][row][col] = c;
		break;

	case 1: //空地
		map[level][row + x][col + y] = c;
		map[level][row][col] = 1;
		break;

	case 2: //人的前面是黄门
		if (roleman.ykey > 0)
		{
			roleman.ykey--;
			map[level][row + x][col + y] = c;
			map[level][row][col] = 1;
		}
		else
		{
			MessageBox(hwnd, "您的黄钥匙数不足!", "", MB_OK);
			map[level][row][col] = c;
		}
		break;

	case 3: //人的前面是蓝门
		if (roleman.bkey > 0)
		{
			roleman.bkey--;
			map[level][row + x][col + y] = c;
			map[level][row][col] = 1;
		}
		else
		{
			MessageBox(hwnd, "您的蓝钥匙数不足!", "", MB_OK);
			map[level][row][col] = c;
		}
		break;

	case 4: //人的前面是黄钥匙
		roleman.ykey++;
		map[level][row + x][col + y] = c;
		map[level][row][col] = 1;
		break;

	case 5: //人的前面是蓝钥匙
		roleman.bkey++;
		map[level][row + x][col + y] = c;
		map[level][row][col] = 1;
		break;

	case 6: //人的前面是红水晶
		roleman.att++;
		map[level][row + x][col + y] = c;
		map[level][row][col] = 1;
		break;

	case 7: //人的前面是蓝水晶
		roleman.def++;
		map[level][row + x][col + y] = c;
		map[level][row][col] = 1;
		break;

	case 8: //人的前面是红药水
		roleman.HP += 50;
		map[level][row + x][col + y] = c;
		map[level][row][col] = 1;
		break;
	case 9: //人的前面是蓝药水
		roleman.HP += 200;
		map[level][row + x][col + y] = c;
		map[level][row][col] = 1;
		break;
	case 10: //人的前面是绿史莱姆
		map[level][row][col] = c;
		//角色攻击小于怪物防御
		if (roleman.att <= gslime.def)
		{
			MessageBox(hwnd, "打不过!", "", MB_OK);
		}

		else
		{
			//怪物攻击小于角色防御
			if (gslime.att - roleman.def <= 0)
			{
				loss = 0;
			}
			else
			{
				count = gslime.HP / (roleman.att - gslime.def);
				loss = count * (gslime.att - roleman.def);
			}

			if (loss >= roleman.HP)
			{
				MessageBox(hwnd, "打不过!", "", MB_OK);
			}
			else
			{
				roleman.HP -= loss;
				roleman.money += gslime.money;
				map[level][row + x][col + y] = 1;
			}
		}

		break;

	case 11: //红史莱姆
		map[level][row][col] = c;
		//角色攻击小于怪物防御
		if (roleman.att <= rslime.def)
		{
			MessageBox(hwnd, "打不过!", "", MB_OK);
		}

		else
		{
			//怪物攻击小于角色防御
			if (rslime.att - roleman.def <= 0)
			{
				loss = 0;
			}
			else
			{
				count = rslime.HP / (roleman.att - rslime.def);
				loss = count * (rslime.att - roleman.def);
			}

			if (loss >= roleman.HP)
			{
				MessageBox(hwnd, "打不过!", "", MB_OK);
			}
			else
			{
				roleman.HP -= loss;
				roleman.money += rslime.money;
				map[level][row + x][col + y] = 1;
			}
		}

		break;

	case 12: //人的前面是蝙蝠
		map[level][row][col] = c;
		//角色攻击小于怪物防御
		if (roleman.att <= bat.def)
		{
			MessageBox(hwnd, "打不过!", "", MB_OK);
		}

		else
		{
			//怪物攻击小于角色防御
			if (bat.att - roleman.def <= 0)
			{
				loss = 0;
			}
			else
			{
				count = bat.HP / (roleman.att - bat.def);
				loss = count * (bat.att - roleman.def);
			}

			if (loss >= roleman.HP)
			{
				MessageBox(hwnd, "打不过!", "", MB_OK);
			}
			else
			{
				roleman.HP -= loss;
				roleman.money += bat.money;
				map[level][row + x][col + y] = 1;
			}

		}
		break;

	case 13: //人的前面是骷髅人
		map[level][row][col] = c;
		//角色攻击小于怪物防御
		if (roleman.att <= skull.def)
		{
			MessageBox(hwnd, "打不过!", "", MB_OK);
		}

		else
		{
			//怪物攻击小于角色防御
			if (skull.att - roleman.def <= 0)
			{
				loss = 0;
			}
			else
			{
				count = skull.HP / (roleman.att - skull.def);
				loss = count * (skull.att - roleman.def);
			}

			if (loss >= roleman.HP)
			{
				MessageBox(hwnd, "打不过!", "", MB_OK);
			}
			else
			{
				roleman.HP -= loss;
				roleman.money += skull.money;
				map[level][row + x][col + y] = 1;
			}
		}

		break;

	case 14: //人的前面是骷髅士兵
		map[level][row][col] = c;

		//角色攻击小于怪物防御
		if (roleman.att <= bskull.def)
		{
			MessageBox(hwnd, "打不过!", "", MB_OK);
		}

		else
		{
			//怪物攻击小于角色防御
			if (bskull.att - roleman.def <= 0)
			{
				loss = 0;
			}
			else
			{
				count = bskull.HP / (roleman.att - bskull.def);
				loss = count * (bskull.att - roleman.def);
			}

			if (loss >= roleman.HP)
			{
				MessageBox(hwnd, "打不过!", "", MB_OK);
			}
			else
			{
				roleman.HP -= loss;
				roleman.money += bskull.money;
				map[level][row + x][col + y] = 1;
			}
		}

		break;

	case 15: //人的前面是法师
		map[level][row][col] = c;
		//角色攻击小于怪物防御
		if (roleman.att <= wizard.def)
		{
			MessageBox(hwnd, "打不过!", "提示", MB_OK);
		}

		else
		{
			//怪物攻击小于角色防御
			if (wizard.att - roleman.def <= 0)
			{
				loss = 0;
			}
			else
			{
				count = wizard.HP / (roleman.att - wizard.def);
				loss = count * (wizard.att - roleman.def);
			}

			if (loss >= roleman.HP)
			{
				MessageBox(hwnd, "打不过!", "", MB_OK);
			}
			else
			{
				roleman.HP -= loss;
				roleman.money += wizard.money;
				map[level][row + x][col + y] = 1;
			}
		}

		break;

	case 20: //人的前面是怪物手册
		MessageBox(hwnd, "给你一本怪物手册,上面有你所在楼层所有怪物的属性。\n请按Q键查看。", "", MB_OK);
		outtextxy(690, 490, "Q:查看怪物手册");
		book = true;
		map[level][row + x][col + y] = 1;
		map[level][row][col] = c;
		break;

	case 21: //人的前面是300金币
		MessageBox(hwnd, "谢谢你救了我,你一定要收下这300金币。", "", MB_OK);
		roleman.money += 300;
		map[level][row + x][col + y] = 1;
		map[level][row][col] = c;
		break;

	case 22: //人的前面是帮助者
		ID = MessageBox(hwnd, "谢谢你救了我,我可以提升你10%的攻击和防御。\n你确定要现在提升吗?", "", MB_YESNO);
		if (ID == IDYES)
		{
			roleman.att = (int)(roleman.att * 1.1) + 1;
			roleman.def = (int)(roleman.def * 1.1) + 1;
			map[level][row + x][col + y] = 1;
			map[level][row][col] = c;
		}
		break;

	case 24: //人的前面是商店		
		HP = 100 * (buycount + 1);
		map[level][row][col] = 16;

		//绘制商店
		solidrectangle(190, 250, 390, 395);
		rectangle(190, 250, 390, 395);
		outtextxy(195, 255, "欢迎来到商店!");
		outtextxy(195, 275, "请选择需求!");
		outtextxy(195, 285, "金币消耗:");
		outtextxy(567, 295, turn(money));
		outtextxy(195, 315, "1.攻击+2");
		outtextxy(195, 335, "2.防御+4");
		outtextxy(195, 355, "3.生命+");
		outtextxy(251, 355, turn(HP));
		outtextxy(195, 375, "4.离开商店");

		while (1)
		{
			switch (_getch())
			{
			case '1':
				if (roleman.money >= money)
				{
					roleman.money -= money;
					roleman.att += 2;
					buycount++;
					money += 20 * buycount;
				}
				else
				{
					MessageBox(hwnd, "买不起!", "", MB_OK);
				}
				return;
			case '2':
				if (roleman.money >= money)
				{
					roleman.money -= money;
					roleman.def += 4;
					buycount++;
					money += 20 * buycount;
				}
				else
				{
					MessageBox(hwnd, "买不起!", "", MB_OK);
				}
				return;
			case '3':
				if (roleman.money >= money)
				{
					roleman.money -= money;
					roleman.HP += HP;
					buycount++;
					money += 20 * buycount;
				}
				else
				{
					MessageBox(hwnd, "买不起!", "", MB_OK);
				}
				return;
			case '4':
				return;
			}
		}
		break;

	case 25: //上楼梯
		level++;
		break;

	case 26: //下楼梯
		level--;
		break;

	case 27: //笼子
		map[level][row][col] = c;
		break;

	case 28: //人的前面是剑
		roleman.att += 10;
		map[level][row + x][col + y] = c;
		map[level][row][col] = 1;
		solidrectangle(762, 360, 810, 376);
		outtextxy(762, 360, "铁剑");
		break;

	case 29: //人的前面是盾
		roleman.def += 10;
		map[level][row + x][col + y] = c;
		map[level][row][col] = 1;
		solidrectangle(762, 385, 810, 401);
		outtextxy(762, 385, "铁盾");
		break;

	case 30: //人的前面是传送道具
		map[level][row + x][col + y] = c;
		map[level][row][col] = 1;
		updown = true;
		outtextxy(690, 510, "U:传送至上一层");
		outtextxy(690, 540, "N:传送至下一层");
		break;

	case 31://人的前面是守卫
		map[level][row][col] = c;
		//角色攻击小于怪物防御
		if (roleman.att <= protection.def)
		{
			MessageBox(hwnd, "打不过!", "", MB_OK);
		}

		else
		{
			//怪物攻击小于角色防御
			if (protection.att - roleman.def <= 0)
			{
				loss = 0;
			}
			else
			{
				count = protection.HP / (roleman.att - protection.def);
				loss = count * (protection.att - roleman.def);
			}

			if (loss >= roleman.HP)
			{
				MessageBox(hwnd, "打不过!", "", MB_OK);
			}
			else
			{
				defenceover++;
				roleman.HP -= loss;
				roleman.money += protection.money;
				map[level][row + x][col + y] = 1;
				if (defenceover == 2)
				{
					MessageBox(hwnd, "监狱之门已打开!", "", MB_OK);

					int j, i;
					for (j = 1; j <= 13; j++)
					{
						for (i = 1; i <= 13; i++)
						{
							if (map[level][j][i] == 27)
							{
								map[level][j][i] = 1;
							}
						}
					}
				}
			}
		}

		break;

	case 33: //人的前面是检查者
		ID = MessageBox(hwnd, "你确定要挑战吗?", "", MB_YESNO);

		//改变最终地图
		if (ID == IDYES)
		{
			updown = false;
			level++;
			break;
		}
		else
		{
			MessageBox(hwnd, "再去修炼下来挑战魔王吧!", "", MB_OK);
		}
		break;

	case 32: //人的前面是boss
		ID = MessageBox(hwnd, "遇到魔王,是否攻击?", "", MB_YESNO);
		map[level][row][col] = c;

		if (ID == IDYES)
		{
			//角色攻击小于怪物防御
			if (roleman.att <= boss.def)
			{
				MessageBox(hwnd, "打不过!", "", MB_OK);
			}

			else
			{
				//怪物攻击小于角色防御
				if (boss.att - roleman.def <= 0)
				{
					loss = 0;
				}
				else
				{
					count = boss.HP / (roleman.att - boss.def);
					loss = count * (boss.att - roleman.def);
				}

				if (loss >= roleman.HP)
				{
					MessageBox(hwnd, "打不过!", "", MB_OK);
				}
				else
				{
					roleman.HP -= loss;
					map[level][row + x][col + y] = 1;

					Draw();

					MessageBox(hwnd, "战斗胜利,恭喜通关!", "提示", MB_OK);

					Sleep(2000);

					cleardevice();
					settextstyle(150, 150, "楷体");
					settextcolor(GREEN);
					outtextxy(120, 100, "恭喜");
					outtextxy(120, 300, "通关");
					settextstyle(40, 40, "黑体");
					settextcolor(WHITE);
					outtextxy(90, 550, "按回车键退出游戏");
					settextstyle(32, 16, "华文琥珀");
					outtextxy(500, 630, "制作人: 奇·大大 ");

					while (1)
					{
						con = _getch();

						if (con == 13)
						{
							exit(0);
						}
					}
				}
			}
		}
		break;

	case 35: //人的前面boss侍卫
		map[level][row][col] = c;
		//角色攻击小于怪物防御
		if (roleman.att <= sboss.def)
		{
			MessageBox(hwnd, "打不过!", "", MB_OK);
		}

		else
		{
			//怪物攻击小于角色防御
			if (sboss.att - roleman.def <= 0)
			{
				loss = 0;
			}
			else
			{
				count = sboss.HP / (roleman.att - sboss.def);
				loss = count * (sboss.att - roleman.def);
			}

			if (loss >= roleman.HP)
			{
				MessageBox(hwnd, "打不过!", "", MB_OK);
			}
			else
			{
				roleman.HP -= loss;
				map[level][row + x][col + y] = 1;
				MessageBox(hwnd, "战斗胜利!", "", MB_OK);
			}
		}

		break;

	case 34: //角色第一次遇到BOSS
		map[level][row - 1][col] = 16;
		map[level][row - 3][col] = 32;
		map[level][row - 2][col] = 33;
		map[level][row][col] = 33;
		map[level][row - 1][col - 1] = 33;
		map[level][row - 1][col + 1] = 33;

		Draw();
		Sleep(1000);
		MessageBox(hwnd, "欢迎来到魔塔,你若能打败我所有的手下,我就与你决斗!\n但现在你必须接受我的安排!", "", MB_OK);
		Sleep(1000);

		map[level][row - 1][col] = 1;
		map[level][row - 3][col] = 1;
		map[level][row - 2][col] = 1;
		map[level][row][col] = 1;
		map[level][row - 1][col - 1] = 1;
		map[level][row - 1][col + 1] = 1;
		roleman.HP = 400;
		roleman.att = 20;
		roleman.def = 20;

		level--;
		map[level][8][3] = 16;
		map[level][10][1] = 1;
		map[2][11][2] = 19;

		solidrectangle(10, 10, 670, 670);
		rectangle(10, 10, 670, 670);

		MessageBox(hwnd, "啊!!!", "", MB_OK);
		Sleep(1000);

		Draw();
		solidrectangle(762, 360, 810, 376);
		solidrectangle(762, 385, 810, 401);
		outtextxy(762, 360, "无");
		outtextxy(762, 385, "无");

		MessageBox(hwnd, "你终于清醒了,我刚完成逃跑的暗道,我们一起越狱吧!", "", MB_OK);
		MessageBox(hwnd, "你的剑的盾被警卫拿走了,它们在五楼,我现在有事没法帮你,再见!", "", MB_OK);

		map[level][7][3] = 1;
		map[level][7][2] = 1;
		break;
	}
}

int main() 
{
	int row, col;

	Welcome();		//游戏界面
	Init();         //初始化游戏

	while (1)
	{
		if (highlevel < level + 1)     //更新所到过的最高层数
			highlevel++;

		Draw();       //画地图及输出信息

		FindRole(&row, &col);    //寻找人物位置

		switch (_getch())
		{
		case 'w':Game(row, col, -1, 0); break;
		case 'W':Game(row, col, -1, 0); break;
		case 's':Game(row, col, 1, 0); break;
		case 'S':Game(row, col, 1, 0); break;
		case 'a':Game(row, col, 0, -1); break;
		case 'A':Game(row, col, 0, -1); break;
		case 'd':Game(row, col, 0, 1); break;
		case 'D':Game(row, col, 0, 1); break;
		case 'Q':
		case 'q':Book(); break;
		case 'U':Convey('U'); break;
		case 'u':Convey('u'); break;
		case 'N':Convey('N'); break;
		case 'n':Convey('n'); break;
		}
	}

	return 0;
}
  • 14
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
以下是使用 EasyX 图形库创建迷宫并进行自动寻路的完整 C++ 代码: ```cpp #include <graphics.h> #include <conio.h> #include <stack> using namespace std; const int SIZE = 20; // 迷宫单元格大小 const int ROWS = 20; // 迷宫行数 const int COLS = 20; // 迷宫列数 int maze[ROWS][COLS]; // 迷宫数组 IMAGE img[3]; // 图片数组 stack<POINT> s; // 用于存储路径的栈 // 初始化迷宫数组 void initMaze() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (i == 0 || i == ROWS - 1 || j == 0 || j == COLS - 1) { maze[i][j] = 1; // 边界为墙壁 } else { maze[i][j] = 0; // 其余为路 } } } } // 生成迷宫 void generateMaze() { int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; stack<POINT> stk; POINT p; p.x = 2; p.y = 2; stk.push(p); while (!stk.empty()) { p = stk.top(); stk.pop(); int x = p.x; int y = p.y; if (maze[x][y] == 1) { continue; } maze[x][y] = 1; int order[4] = {0, 1, 2, 3}; for (int i = 0; i < 4; i++) { int j = rand() % 4; int t = order[i]; order[i] = order[j]; order[j] = t; } for (int i = 0; i < 4; i++) { int nx = x + dx[order[i]]; int ny = y + dy[order[i]]; if (maze[nx][ny] == 0) { POINT np = {nx, ny}; stk.push(np); } } } } // 加载图片 void loadImages() { loadimage(&img[0], L"wall.jpg", SIZE, SIZE); loadimage(&img[1], L"road.jpg", SIZE, SIZE); loadimage(&img[2], L"foot.jpg", SIZE, SIZE); } // 绘制迷宫 void drawMaze() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (maze[i][j] == 1) { putimage(j * SIZE, i * SIZE, &img[0]); } else { putimage(j * SIZE, i * SIZE, &img[1]); } } } } // 判断是否为终点 bool isEnd(POINT p) { return p.x == ROWS - 2 && p.y == COLS - 2; } // 判断是否为可行路径 bool isPath(POINT p) { return maze[p.x][p.y] == 0; } // 绘制路径 void drawPath() { stack<POINT> tmp; while (!s.empty()) { tmp.push(s.top()); s.pop(); } while (!tmp.empty()) { POINT p = tmp.top(); putimage(p.y * SIZE, p.x * SIZE, &img[2]); s.push(p); tmp.pop(); } } // 自动寻路 bool findPath(POINT cur) { if (isEnd(cur)) { s.push(cur); return true; } if (isPath(cur)) { s.push(cur); maze[cur.x][cur.y] = 2; POINT next = cur; next.x--; if (findPath(next)) { return true; } next.x += 2; if (findPath(next)) { return true; } next.x--; next.y--; if (findPath(next)) { return true; } next.y += 2; if (findPath(next)) { return true; } s.pop(); maze[cur.x][cur.y] = 0; } return false; } int main() { initMaze(); generateMaze(); loadImages(); initgraph(COLS * SIZE, ROWS * SIZE); drawMaze(); POINT start = {1, 1}; findPath(start); drawPath(); getch(); closegraph(); return 0; } ``` 注:本代码使用了 EasyX 图形库,需要事先安装该库。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值