C语言C++开发魔塔游戏(两层)

只写了两层,但是基本功能都能实现,部分效果图如下:

 

 

 

 

 

随便放一点代码,需要完整代码的可以加下QQ裙【八零六零四/一五九九】领取 

#include "header.h"
#include <iostream>
using namespace std;
#include <conio.h>


// 怪物数组 名字 血量 攻击 防御 加经验 加金币
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 <windows.h>

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以为您提供一个简单的魔塔游戏C语言代码示例,但是需要注意的是,这只是一个基础版本,可能不具备完整的游戏体验。 ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define MAP_SIZE 15 #define LEVEL_NUM 10 int map[MAP_SIZE][MAP_SIZE]; int level = 1; int hp = 100; int attack = 10; void init_map() { int i, j; for (i = 0; i < MAP_SIZE; i++) { for (j = 0; j < MAP_SIZE; j++) { if (i == 0 || i == MAP_SIZE - 1 || j == 0 || j == MAP_SIZE - 1) { map[i][j] = 1; } else { map[i][j] = 0; } } } } void print_map() { int i, j; for (i = 0; i < MAP_SIZE; i++) { for (j = 0; j < MAP_SIZE; j++) { if (map[i][j] == 0) { printf(" "); } else if (map[i][j] == 1) { printf("#"); } else if (map[i][j] == 2) { printf("S"); } else if (map[i][j] == 3) { printf("M"); } else if (map[i][j] == 4) { printf("T"); } else if (map[i][j] == 5) { printf("E"); } else if (map[i][j] == 6) { printf("B"); } } printf("\n"); } printf("Level: %d, HP: %d, Attack: %d\n", level, hp, attack); } int get_random(int min, int max) { srand(time(NULL)); return rand() % (max - min + 1) + min; } void generate_monster() { int i, j; do { i = get_random(1, MAP_SIZE - 2); j = get_random(1, MAP_SIZE - 2); } while (map[i][j] != 0); map[i][j] = 3; } void generate_treasure() { int i, j; do { i = get_random(1, MAP_SIZE - 2); j = get_random(1, MAP_SIZE - 2); } while (map[i][j] != 0); map[i][j] = 4; } void generate_exit() { int i, j; do { i = get_random(1, MAP_SIZE - 2); j = get_random(1, MAP_SIZE - 2); } while (map[i][j] != 0); map[i][j] = 5; } void generate_boss() { int i, j; do { i = get_random(1, MAP_SIZE - 2); j = get_random(1, MAP_SIZE - 2); } while (map[i][j] != 0); map[i][j] = 6; } void move(int x, int y) { if (map[x][y] == 1) { printf("You hit a wall!\n"); } else if (map[x][y] == 2) { printf("You find a sword, attack + 5!\n"); attack += 5; map[x][y] = 0; } else if (map[x][y] == 3) { int monster_hp = get_random(50, 100); printf("You encounter a monster, HP: %d!\n", monster_hp); while (hp > 0 && monster_hp > 0) { printf("Your HP: %d, Monster HP: %d\n", hp, monster_hp); monster_hp -= attack; if (monster_hp <= 0) { printf("You kill the monster!\n"); map[x][y] = 0; break; } hp -= get_random(10, 20); if (hp <= 0) { printf("You are dead!\n"); exit(0); } } } else if (map[x][y] == 4) { printf("You find a treasure, HP + 20!\n"); hp += 20; map[x][y] = 0; } else if (map[x][y] == 5) { printf("You find the exit, level up!\n"); level++; hp = 100; attack += 5; if (level == LEVEL_NUM) { printf("Congratulations, you win!\n"); exit(0); } init_map(); generate_monster(); generate_treasure(); generate_exit(); generate_boss(); } else if (map[x][y] == 6) { int boss_hp = 200; printf("You encounter the boss, HP: %d!\n", boss_hp); while (hp > 0 && boss_hp > 0) { printf("Your HP: %d, Boss HP: %d\n", hp, boss_hp); boss_hp -= attack; if (boss_hp <= 0) { printf("You kill the boss, level up!\n"); level++; hp = 100; attack += 5; if (level == LEVEL_NUM) { printf("Congratulations, you win!\n"); exit(0); } init_map(); generate_monster(); generate_treasure(); generate_exit(); generate_boss(); break; } hp -= get_random(20, 30); if (hp <= 0) { printf("You are dead!\n"); exit(0); } } } else { map[x][y] = 2; } } int main() { init_map(); generate_monster(); generate_treasure(); generate_exit(); generate_boss(); while (1) { print_map(); printf("Please input the direction you want to go (w/a/s/d): "); char direction; scanf("%c", &direction); getchar(); int x = 0, y = 0; switch (direction) { case 'w': x = -1, y = 0; break; case 'a': x = 0, y = -1; break; case 's': x = 1, y = 0; break; case 'd': x = 0, y = 1; break; default: printf("Invalid input!\n"); continue; } move(x + 1, y + 1); } return 0; } ``` 以上代码实现了一个简单的魔塔游戏,包括地图初始化、怪物、宝藏、出口和Boss的生成,以及人物的移动和战斗等基本功能。您可以根据自己的需求进行修改和完善。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值