只写了两层,但是基本功能都能实现,部分效果图如下:
随便放一点代码,需要完整代码的可以加下QQ裙【八零六零四/一五九九】领取
#include "header.h"
#include
using namespace std;
#include
// 怪物数组 名字 血量 攻击 防御 加经验 加金币
Monster monsterArray[3] =
{
{ "小小怪", 40, 5, 2, 5, 10 },
{ "小怪", 50, 10, 5, 10, 30 },
{ "大大怪", 200, 50, 35, 30, 60 }
};
// 物品数组
Goods goodsArray[6] =
{
{ "红宝石", 0, 25, 0 },
{ "蓝宝石", 0,0, 25 },
{ "剑", 0, 20, 0},
{ "盾", 0, 0, 20 },
{ "红瓶", 200, 0, 0 },
{ "蓝瓶", 400, 0, 0}
};
// 英雄移动方向
Vec2 hero_up = { -1, 0 };
Vec2 hero_down = { 1, 0 };
Vec2 hero_left = { 0, -1 };
Vec2 hero_right = { 0, 1 };
// 英雄结构体对象
Hero g_hero;
// 当前关卡
short g_nLv = 1;
// 初始化英雄
void initHero()
{
cout << "请输入昵称:" << endl;
cin >> g_hero.name;
g_hero.lv = 1;
g_hero.hp = 100;
g_hero.atk = 10;
g_hero.def = 5;
g_hero.gold = 0;
g_hero.exp = 0;
g_hero.rKey = 0;
g_hero.bKey = 0;
g_hero.yKey = 0;
}
// 渲染英雄信息
void RenderHero()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);//黄
cout << "********************" << endl;
cout << g_hero.name << " 等级:" << g_hero.lv << " 血量:" << g_hero.hp << endl;
cout << "攻击:" << g_hero.atk << " 防御:" << g_hero.def << endl;
cout << "经验:" << g_hero.exp << "金币:" << g_hero.gold << endl;
cout << "********************" << endl;
cout << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
cout << "红钥匙:" << g_hero.rKey << "蓝钥匙:" << g_hero.bKey << "黄钥匙:" << g_hero.yKey << endl;
cout << endl;
cout << "\t第" << g_nLv << "层" << endl;
cout << endl;
}
// 渲染UI
// 渲染操作信息
// 获取指定位置的元素
int getElementOfPos(const Vec2& dir)
{
return g_mapArray[g_nLv - 1][dir.x][dir.y];
}
// 更新地图指定位置的为指定元素
void updateMap(const Vec2& pos, MapElement e)
{
g_mapArray[g_nLv - 1][pos.x][pos.y] = e;
}
Goods* createrGoods(MapElement e)
{
Goods* pGoods = new Goods;
(*pGoods) = goodsArray[e%HELP_A];
return pGoods;
}
// 创建指定类型的怪物
Monster* createMonster(MapElement e)
{
Monster* pMonster = new Monster;
// e%MONSTER_A
(*pMonster) = monsterArray[e%MONSTER_A];
return pMonster;
switch (e)
{
case MONSTER_A:
(*pMonster) = monsterArray[0];
break;
case MONSTER_B:
(*pMonster) = monsterArray[1];
break;
case MONSTER_C:
(*pMonster) = monsterArray[2];
break;
default:
break;
}
}
// 打死怪物增加英雄的对应属性
void add(Monster* pMonster)
{
g_hero.exp += pMonster->addexp;
g_hero.gold += pMonster->addgold;
}
// 战斗(参数:怪物)
bool pk(Monster* pMonster)
{
// 创建对应类型怪物
while (true)
{
// 英雄对怪物造成的伤害
short hurt = g_hero.atk - pMonster->def;
hurt = hurt < 0 ? 0 : hurt;// 避免出现负伤害
pMonster->hp -= hurt;
cout << g_hero.name << "发起普攻对" <<
pMonster->name << "造成了" <<
hurt << "的伤害,血量为" <<
pMonster->hp << endl;
if (pMonster->hp <= 0)
{
// 增加英雄属性
add(pMonster);
// 释放怪物
delete pMonster;
pMonster = nullptr;
return true;
}
Sleep(500);
// 怪物对英雄造成的伤害值
hurt = pMonster->atk - g_hero.def;
hurt = hurt < 0 ? 0 : hurt;// 避免出现负伤害
g_hero.hp -= hurt;
cout << pMonster->name << "对" <<
g_hero.name << "敲了一下,造成了" <<
hurt << "的伤害,血量为" <<
g_hero.hp << endl;
if (g_hero.hp <= 0)
{
// 释放怪物
delete pMonster;
pMonster = nullptr;
return false;
}
Sleep(500);
}
}
//增加装备属性
void AddGoods(Goods *newGoods)
{
g_hero.hp += newGoods->addhp;
g_hero.atk += newGoods->addatk;
g_hero.def += newGoods->adddef;
}
// 移动函数
void operate(const Vec2& dir)
{
// 下一个位置
Vec2 nextpos = { g_hero.pos.x + dir.x, g_hero.pos.y + dir.y };
// 获取下一个位置的元素
int e = getElementOfPos(nextpos);
switch (e)
{
case ROAD:
// 更新地图数据
updateMap(g_hero.pos, ROAD);
updateMap(nextpos, HERO);
break;
case MONSTER_A:
case MONSTER_B:
case MONSTER_C:
{//注意:如果有在case语句中定义变量并初始化需要打大括号
// 战斗函数
Monster* pMonster = createMonster((MapElement)e);
if (g_hero.atk > pMonster->def)
{
if (pk(pMonster))
{
// 更新地图数据
updateMap(g_hero.pos, ROAD);
updateMap(nextpos, HERO);
}
}
else{
cout << "变强一些再来吧!" << endl;
Sleep(500);
}
}
break;
case UP:
g_nLv++;
// 设置英雄坐标
break;
case DOWN:
g_nLv--;
// 设置英雄坐标
break;
case KEY_B:
// 更新地图数据
updateMap(g_hero.pos, ROAD);
updateMap(nextpos, HERO);
g_hero.bKey++;
break;
case KEY_R:
// 更新地图数据
updateMap(g_hero.pos, ROAD);
updateMap(nextpos, HERO);
g_hero.rKey++;
break;
case KEY_Y:
// 更新地图数据
updateMap(g_hero.pos, ROAD);
updateMap(nextpos, HERO);
g_hero.yKey++;
break;
case DOOR_B:
if (g_hero.bKey > 0)
{// 更新地图数据
updateMap(g_hero.pos, ROAD);
updateMap(nextpos, HERO);
g_hero.bKey--;
}
else
cout << "去获得对应的钥匙再来开门吧!" << endl;
Sleep(500);
break;
case DOOR_R:
if (g_hero.rKey > 0)
{// 更新地图数据
updateMap(g_hero.pos, ROAD);
updateMap(nextpos, HERO);
g_hero.rKey--;
}
else
cout << "去获得对应的钥匙再来开门吧!" << endl;
Sleep(500);
break;
case DOOR_Y:
if (g_hero.yKey > 0)
{// 更新地图数据
updateMap(g_hero.pos, ROAD);
updateMap(nextpos, HERO);
g_hero.yKey--;
}
else
cout << "去获得对应的钥匙再来开门吧!" << endl;
Sleep(500);
break;
case HELP_A:
case HELP_B:
case HELP_C:
case HELP_D:
case HELP_E:
case HELP_F:
{
Goods* pGoods = createrGoods((MapElement)e);
AddGoods(pGoods);
// 更新地图数据
updateMap(g_hero.pos, ROAD);
updateMap(nextpos, HERO);
}
break;
default:
break;
}
}
// 获取用户的按键信息
void KeyBoardListener()
{
char ch = _getch();
switch (ch)
{
case 'w':
case 'W':
operate(hero_up);
break;
case 's':
case 'S':
operate(hero_down);
break;
case 'a':
case 'A':
operate(hero_left);
break;
case 'd':
case 'D':
operate(hero_right);
break;
default:
break;
}
}
// 游戏初始化
void GameInit()
{
initHero();
}
// 游戏渲染
void GameRender()
{
system("CLS");
RenderHero();
// 渲染地图
RenderMap();
// ui 英雄数据..
cout << endl;
cout << " W" << endl;
cout << "按下 A S D 进行移动" << endl;
cout << "获取对应颜色的钥匙(";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);//红
cout << "K";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白)
cout << ")打开大门(";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);//红
cout << "‖";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白)
cout << ")" << endl;
cout << "通过宝石(";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);//红
cout << "◆";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
cout << ")增强力量,打败怪兽(";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);//红
cout << "※";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
cout << ")吧!" << endl;
}
// 游戏更新
void GameUpdate()
{
// 监听键盘
KeyBoardListener();
}
// 渲染地图
void RenderMap()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
switch (g_mapArray[g_nLv - 1][i][j])
{
case ROAD:
cout << " ";
break;
case WALL:
cout << "■";
break;
case MONSTER_A:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);//红
cout << "※";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
case MONSTER_B:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);//红
cout << "※";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
case MONSTER_C:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);//红
cout << "※";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
case HERO:
cout << "♀";
// 更新英雄坐标
g_hero.pos.x = i;
g_hero.pos.y = j;
break;
case UP:
cout << "上";
break;
case DOWN:
cout << "下";
break;
case DOOR_R:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);//红
cout << "‖";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
case DOOR_B:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);//浅蓝
cout << "‖";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
case DOOR_Y:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);//黄
cout << "‖";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
case KEY_B:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);//浅蓝
cout << "K ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
case KEY_R:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);//红
cout << "K ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
case KEY_Y:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);//黄
cout << "K ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
case HELP_A:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);//红
cout << "◇";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
case HELP_B:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);//浅蓝
cout << "◇";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
case HELP_C:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);//黄
cout << "◇";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
case HELP_D:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);//黄
cout << "◇";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
case HELP_E:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);//红
cout << "◆";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
case HELP_F :
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);//浅蓝
cout << "◆";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//白
break;
break;
default:
cout << "错";
break;
}
}
cout << endl;
}
}
#include