话不多说,上代码:
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <windows.h>
#include <conio.h>
#include <algorithm>
#include <iomanip>
#include <sstream>
using namespace std;
enum Color {
BLACK = 0,
BLUE = 1,
GREEN = 2,
CYAN = 3,
RED = 4,
MAGENTA = 5,
YELLOW = 6,
WHITE = 7,
GRAY = 8,
LIGHT_BLUE = 9,
LIGHT_GREEN = 10,
LIGHT_CYAN = 11,
LIGHT_RED = 12,
LIGHT_MAGENTA = 13,
LIGHT_YELLOW = 14,
BRIGHT_WHITE = 15
};
void SetColor(int foreground, int background = BLACK) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, foreground | (background << 4));
}
void SetCursorPosition(int x, int y) {
COORD coord = { static_cast<SHORT>(x), static_cast<SHORT>(y) };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
enum class Rarity {
WHITE,
GREEN,
BLUE,
PURPLE,
GOLD,
RED
};
enum class EquipmentType {
HELMET,
ARMOR,
BOOTS,
WEAPON,
SHIELD
};
class Equipment {
public:
string name;
EquipmentType type;
Rarity rarity;
int hpBonus;
int minAttackBonus;
int maxAttackBonus;
Equipment(string n, EquipmentType t, Rarity r, int hp, int minAtk, int maxAtk)
: name(n), type(t), rarity(r), hpBonus(hp), minAttackBonus(minAtk), maxAttackBonus(maxAtk) {
}
void display() const {
int color;
switch (rarity) {
case Rarity::WHITE: color = WHITE; break;
case Rarity::GREEN: color = GREEN; break;
case Rarity::BLUE: color = BLUE; break;
case Rarity::PURPLE: color = MAGENTA; break;
case Rarity::GOLD: color = YELLOW; break;
case Rarity::RED: color = RED; break;
default: color = WHITE;
}
SetColor(color);
cout << name;
SetColor(WHITE);
}
};
class Monster {
public:
string name;
int x, y;
int maxHp;
int currentHp;
int maxAttack;
bool isBoss;
bool alive;
Monster(string n, int posX, int posY, int hp, int atk, bool boss = false)
: name(n), x(posX), y(posY), maxHp(hp), currentHp(hp), maxAttack(atk), isBoss(boss), alive(true) {
}
void takeDamage(int damage) {
currentHp -= damage;
if (currentHp <= 0) {
currentHp = 0;
alive = false;
}
}
int attack() const {
return rand() % maxAttack + 1;
}
void display() const {
if (isBoss) {
SetColor(RED);
cout << "龙";
}
else {
SetColor(GREEN);
cout << "$";
}
SetColor(WHITE);
}
};
class Player {
public:
string name;
int x, y;
int level;
int maxHp;
int currentHp;
int maxAttack;
int minAttack;
vector<Equipment> inventory;
Equipment* helmet;
Equipment* armor;
Equipment* boots;
Equipment* weapon;
Equipment* shield;
string currentMap;
Player(string n)
: name(n), x(1), y(1), level(1), maxHp(4000), currentHp(4000),
maxAttack(500), minAttack(150), helmet(nullptr), armor(nullptr),
boots(nullptr), weapon(nullptr), shield(nullptr), currentMap("皇城") {
}
~Player() {
if (helmet) delete helmet;
if (armor) delete armor;
if (boots) delete boots;
if (weapon) delete weapon;
if (shield) delete shield;
}
void respawn() {
x = 1;
y = 1;
currentHp = maxHp;
currentMap = "皇城";
}
void levelUp() {
level++;
maxHp += 250;
currentHp = maxHp;
maxAttack += 50;
minAttack += 25;
}
void equip(Equipment* item) {
switch (item->type) {
case EquipmentType::HELMET:
if (helmet) {
maxHp -= helmet->hpBonus;
inventory.push_back(*helmet);
delete helmet;
}
helmet = item;
break;
case EquipmentType::ARMOR:
if (armor) {
maxHp -= armor->hpBonus;
inventory.push_back(*armor);
delete armor;
}
armor = item;
break;
case EquipmentType::BOOTS:
if (boots) {
maxHp -= boots->hpBonus;
inventory.push_back(*boots);
delete boots;
}
boots = item;
break;
case EquipmentType::WEAPON:
if (weapon) {
maxAttack -= weapon->maxAttackBonus;
minAttack -= weapon->minAttackBonus;
inventory.push_back(*weapon);
delete weapon;
}
weapon = item;
break;
case EquipmentType::SHIELD:
if (shield) {
maxHp -= shield->hpBonus;
inventory.push_back(*shield);
delete shield;
}
shield = item;
break;
}
maxHp += item->hpBonus;
maxAttack += item->maxAttackBonus;
minAttack += item->minAttackBonus;
}
int attack() const {
return rand() % (maxAttack - minAttack + 1) + minAttack;
}
void takeDamage(int damage) {
currentHp -= damage;
if (currentHp < 0) currentHp = 0;
}
void heal(int amount) {
currentHp += amount;
if (currentHp > maxHp) currentHp = maxHp;
}
void displayStats(int x, int y) const {
SetCursorPosition(x, y++);
cout << "玩家: " << name;
SetCursorPosition(x, y++);
cout << "等级: " << level;
SetCursorPosition(x, y++);
cout << "生命: " << currentHp << "/" << maxHp;
SetCursorPosition(x, y++);
cout << "攻击: " << minAttack << "-" << maxAttack;
SetCursorPosition(x, y++);
cout << "头盔: ";
if (helmet) helmet->display();
else cout << "无";
SetCursorPosition(x, y++);
cout << "护甲: ";
if (armor) armor->display();
else cout << "无";
SetCursorPosition(x, y++);
cout << "鞋子: ";
if (boots) boots->display();
else cout << "无";
SetCursorPosition(x, y++);
cout << "武器: ";
if (weapon) weapon->display();
else cout << "无";
SetCursorPosition(x, y++);
cout << "盾牌: ";
if (shield) shield->display();
else cout << "无";
SetCursorPosition(x, y++);
cout << "背包:";
}
};
class Game {
private:
Player* player;
vector<Monster> monsters;
bool gameRunning;
bool inInventory;
bool inCharacterSelection;
int selectedInventoryItem;
vector<string> characterNames;
int selectedCharacter;
bool bossSpawned;
int screenWidth;
int screenHeight;
int normalMonstersDefeated;
int bossesDefeated;
int monstersDefeatedCount;
void SetFullScreen() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT rect = { 0, 0, 119, 49 };
SetConsoleWindowInfo(hConsole, TRUE, &rect);
COORD size = { 120, 500 };
SetConsoleScreenBufferSize(hConsole, size);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
screenWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
screenHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
}
Equipment* generateRandomEquipment(Rarity rarity) {
string name;
EquipmentType type = static_cast<EquipmentType>(rand() % 5);
int hpBonus = 0, minAtkBonus = 0, maxAtkBonus = 0;
switch (rarity) {
case Rarity::WHITE:
switch (type) {
case EquipmentType::HELMET: name = "草帽"; hpBonus = 50; break;
case EquipmentType::ARMOR: name = "布衣"; hpBonus = 100; break;
case EquipmentType::BOOTS: name = "草鞋"; hpBonus = 15; break;
case EquipmentType::WEAPON: name = "匕首"; maxAtkBonus = 25; minAtkBonus = 5; break;
case EquipmentType::SHIELD: name = "木盾"; hpBonus = 15; break;
} break;
case Rarity::GREEN:
switch (type) {
case EquipmentType::HELMET: name = "铜质头盔"; hpBonus = 75; break;
case EquipmentType::ARMOR: name = "铜质盔甲"; hpBonus = 125; break;
case EquipmentType::BOOTS: name = "铜质护靴"; hpBonus = 45; break;
case EquipmentType::WEAPON: name = "铜剑"; maxAtkBonus = 40; minAtkBonus = 10; break;
case EquipmentType::SHIELD: name = "铜质盾牌"; hpBonus = 35; break;
} break;
case Rarity::BLUE:
switch (type) {
case EquipmentType::HELMET: name = "乌金护额"; hpBonus = 100; break;
case EquipmentType::ARMOR: name = "乌金护甲"; hpBonus = 140; break;
case EquipmentType::BOOTS: name = "乌金战靴"; hpBonus = 70; break;
case EquipmentType::WEAPON: name = "乌金剑"; maxAtkBonus = 65; minAtkBonus = 20; break;
case EquipmentType::SHIELD: name = "乌金盾"; hpBonus = 60; break;
} break;
case Rarity::PURPLE:
switch (type) {
case EquipmentType::HELMET: name = "精钢头盔"; hpBonus = 125; break;
case EquipmentType::ARMOR: name = "精钢护甲"; hpBonus = 165; break;
case EquipmentType::BOOTS: name = "精钢战靴"; hpBonus = 100; break;
case EquipmentType::WEAPON: name = "精钢之剑"; maxAtkBonus = 100; minAtkBonus = 50; break;
case EquipmentType::SHIELD: name = "护身之盾"; hpBonus = 100; break;
} break;
case Rarity::GOLD:
switch (type) {
case EquipmentType::HELMET: name = "魔神战盔"; hpBonus = 180; break;
case EquipmentType::ARMOR: name = "魔神铠甲"; hpBonus = 250; break;
case EquipmentType::BOOTS: name = "魔神战靴"; hpBonus = 180; break;
case EquipmentType::WEAPON: name = "赤血魔神剑"; maxAtkBonus = 150; minAtkBonus = 85; break;
case EquipmentType::SHIELD: name = "护身的魔神盾"; hpBonus = 150; break;
} break;
case Rarity::RED:
switch (type) {
case EquipmentType::HELMET: name = "龙魂头盔"; hpBonus = 225; break;
case EquipmentType::ARMOR: name = "龙魂战铠"; hpBonus = 275; break;
case EquipmentType::BOOTS: name = "龙魂战靴"; hpBonus = 225; break;
case EquipmentType::WEAPON: name = "龙魂双刃剑"; maxAtkBonus = 200; minAtkBonus = 100; break;
case EquipmentType::SHIELD: name = "神圣的龙魂盾"; hpBonus = 225; break;
} break;
}
return new Equipment(name, type, rarity, hpBonus, minAtkBonus, maxAtkBonus);
}
bool isValidPosition(int x, int y) {
if (x <= 0 || x >= 99 || y <= 0 || y >= 99) return false;
if (player->currentMap == "皇城") {
vector<pair<int, int>> houses = {
{2,2}, {2,4}, {2,5}, {2,7}, {2,8},
{4,2}, {4,4}, {4,5}, {4,7}, {4,8},
{5,2}, {5,4}, {5,5}, {5,7}, {5,8},
{7,2}, {7,4}, {7,5}, {7,7}, {7,8}
};
for (auto& house : houses) {
if (x == house.first && y == house.second) return false;
}
}
for (auto& monster : monsters) {
if (monster.alive && monster.x == x && monster.y == y) return false;
}
if (player->x == x && player->y == y) return false;
return true;
}
void generateMonsters() {
monsters.clear();
bossSpawned = false;
monstersDefeatedCount = 0;
if (player->currentMap == "炼狱") {
int monstersGenerated = 0;
while (monstersGenerated < 30) {
int x = rand() % 96 + 2;
int y = rand() % 96 + 2;
if (isValidPosition(x, y)) {
monsters.emplace_back("地狱使者", x, y, 4000, 500);
monstersGenerated++;
}
}
}
}
void generateBoss() {
if (player->currentMap == "炼狱" && !bossSpawned) {
int x, y;
do {
x = rand() % 96 + 2;
y = rand() % 96 + 2;
} while (!isValidPosition(x, y));
monsters.emplace_back("魔龙领主", x, y, 12000, 800, true);
bossSpawned = true;
SetCursorPosition(0, 107);
cout << "警告!魔龙领主降临了!";
}
}
void drawGame() {
system("cls");
for (int y = 0; y < 100; y++) {
for (int x = 0; x < 100; x++) {
if (x == 0 || x == 99 || y == 0 || y == 99) {
SetCursorPosition(x, y);
cout << "#";
}
}
}
if (player->currentMap == "皇城") {
vector<pair<int, int>> houses = {
{2,2}, {2,4}, {2,5}, {2,7}, {2,8},
{4,2}, {4,4}, {4,5}, {4,7}, {4,8},
{5,2}, {5,4}, {5,5}, {5,7}, {5,8},
{7,2}, {7,4}, {7,5}, {7,7}, {7,8}
};
for (auto& house : houses) {
SetCursorPosition(house.first, house.second);
cout << "^";
}
}
for (auto& monster : monsters) {
if (monster.alive) {
SetCursorPosition(monster.x, monster.y);
monster.display();
}
}
SetCursorPosition(player->x, player->y);
SetColor(BLUE);
cout << "乂";
SetColor(WHITE);
int statsX = 101;
int statsY = 0;
player->displayStats(statsX, statsY);
drawInventory(statsX, statsY + 11);
Monster* attackedMonster = getAttackedMonster();
if (attackedMonster) {
SetCursorPosition(statsX, statsY + 11 + 2 + (int)player->inventory.size());
if (attackedMonster->isBoss) {
SetColor(RED);
cout << attackedMonster->name;
SetColor(WHITE);
cout << " HP: " << attackedMonster->currentHp;
}
else {
cout << attackedMonster->name << " HP: " << attackedMonster->currentHp;
}
}
SetCursorPosition(0, 100);
}
void drawInventory(int x, int y) {
SetCursorPosition(x, y++);
cout << "物品列表:";
for (int i = 0; i < player->inventory.size(); i++) {
SetCursorPosition(x, y++);
if (inInventory && i == selectedInventoryItem) {
cout << "> ";
}
else {
cout << " ";
}
player->inventory[i].display();
}
if (player->inventory.empty()) {
SetCursorPosition(x, y++);
cout << " (空)";
}
}
void drawCharacterSelection() {
system("cls");
cout << "选择角色:" << endl << endl;
for (int i = 0; i < characterNames.size(); i++) {
if (i == selectedCharacter) {
cout << "<" << characterNames[i] << ">" << endl;
}
else {
cout << " " << characterNames[i] << " " << endl;
}
}
cout << endl << "使用W/S选择,回车确认" << endl;
cout << "按N创建新角色";
}
void drawStartScreen() {
system("cls");
SetCursorPosition(50, 10);
cout << " /|";
SetCursorPosition(50, 11);
cout << " / |";
SetCursorPosition(50, 12);
cout << " / |";
SetCursorPosition(50, 13);
cout << " / |";
SetCursorPosition(50, 14);
cout << "/____|";
SetCursorPosition(50, 15);
cout << " ||";
SetCursorPosition(50, 16);
cout << " ||";
SetCursorPosition(50, 17);
cout << " ||";
SetCursorPosition(50, 18);
cout << " ||";
SetCursorPosition(45, 20);
cout << "传说:神明之剑0.1.9";
SetCursorPosition(45, 22);
cout << "按回车开始游戏";
}
void drawLoadingScreen() {
system("cls");
for (int i = 1; i <= 100; i++) {
SetCursorPosition(50, 15);
cout << i << "/100";
Sleep(50);
}
}
void createNewCharacter() {
system("cls");
cout << "输入角色名称: ";
string name;
getline(cin, name);
if (!name.empty()) {
characterNames.push_back(name);
saveCharacters();
}
}
void saveCharacters() {
ofstream outFile("characters.txt");
if (outFile) {
for (const auto& name : characterNames) {
outFile << name << endl;
}
}
}
void loadCharacters() {
ifstream inFile("characters.txt");
characterNames.clear();
string name;
while (getline(inFile, name)) {
if (!name.empty()) {
characterNames.push_back(name);
}
}
}
void saveGame() {
ofstream outFile("save_" + player->name + ".txt");
if (outFile) {
outFile << player->name << endl;
outFile << player->level << endl;
outFile << player->maxHp << endl;
outFile << player->currentHp << endl;
outFile << player->maxAttack << endl;
outFile << player->minAttack << endl;
outFile << player->currentMap << endl;
if (player->helmet) outFile << "HELMET " << player->helmet->name << endl;
if (player->armor) outFile << "ARMOR " << player->armor->name << endl;
if (player->boots) outFile << "BOOTS " << player->boots->name << endl;
if (player->weapon) outFile << "WEAPON " << player->weapon->name << endl;
if (player->shield) outFile << "SHIELD " << player->shield->name << endl;
outFile << "INVENTORY " << player->inventory.size() << endl;
for (const auto& item : player->inventory) {
outFile << item.name << endl;
}
}
}
bool loadGame(const string& name) {
ifstream inFile("save_" + name + ".txt");
if (!inFile) return false;
player = new Player(name);
string line;
getline(inFile, line);
inFile >> player->level;
inFile >> player->maxHp;
inFile >> player->currentHp;
inFile >> player->maxAttack;
inFile >> player->minAttack;
inFile.ignore();
getline(inFile, player->currentMap);
while (getline(inFile, line)) {
if (line.find("INVENTORY") != string::npos) break;
istringstream iss(line);
string type, itemName;
iss >> type >> itemName;
Equipment* eq = nullptr;
if (type == "HELMET") {
if (itemName == "草帽") eq = new Equipment("草帽", EquipmentType::HELMET, Rarity::WHITE, 50, 0, 0);
else if (itemName == "铜质头盔") eq = new Equipment("铜质头盔", EquipmentType::HELMET, Rarity::GREEN, 75, 0, 0);
else if (itemName == "乌金护额") eq = new Equipment("乌金护额", EquipmentType::HELMET, Rarity::BLUE, 100, 0, 0);
else if (itemName == "精钢头盔") eq = new Equipment("精钢头盔", EquipmentType::HELMET, Rarity::PURPLE, 125, 0, 0);
else if (itemName == "魔神战盔") eq = new Equipment("魔神战盔", EquipmentType::HELMET, Rarity::GOLD, 180, 0, 0);
else if (itemName == "龙魂头盔") eq = new Equipment("龙魂头盔", EquipmentType::HELMET, Rarity::RED, 225, 0, 0);
if (eq) player->helmet = eq;
}
else if (type == "ARMOR") {
if (itemName == "布衣") eq = new Equipment("布衣", EquipmentType::ARMOR, Rarity::WHITE, 100, 0, 0);
else if (itemName == "铜质盔甲") eq = new Equipment("铜质盔甲", EquipmentType::ARMOR, Rarity::GREEN, 125, 0, 0);
else if (itemName == "乌金护甲") eq = new Equipment("乌金护甲", EquipmentType::ARMOR, Rarity::BLUE, 140, 0, 0);
else if (itemName == "精钢护甲") eq = new Equipment("精钢护甲", EquipmentType::ARMOR, Rarity::PURPLE, 165, 0, 0);
else if (itemName == "魔神铠甲") eq = new Equipment("魔神铠甲", EquipmentType::ARMOR, Rarity::GOLD, 250, 0, 0);
else if (itemName == "龙魂战铠") eq = new Equipment("龙魂战铠", EquipmentType::ARMOR, Rarity::RED, 275, 0, 0);
if (eq) player->armor = eq;
}
else if (type == "BOOTS") {
if (itemName == "草鞋") eq = new Equipment("草鞋", EquipmentType::BOOTS, Rarity::WHITE, 15, 0, 0);
else if (itemName == "铜质护靴") eq = new Equipment("铜质护靴", EquipmentType::BOOTS, Rarity::GREEN, 45, 0, 0);
else if (itemName == "乌金战靴") eq = new Equipment("乌金战靴", EquipmentType::BOOTS, Rarity::BLUE, 70, 0, 0);
else if (itemName == "精钢战靴") eq = new Equipment("精钢战靴", EquipmentType::BOOTS, Rarity::PURPLE, 100, 0, 0);
else if (itemName == "魔神战靴") eq = new Equipment("魔神战靴", EquipmentType::BOOTS, Rarity::GOLD, 180, 0, 0);
else if (itemName == "龙魂战靴") eq = new Equipment("龙魂战靴", EquipmentType::BOOTS, Rarity::RED, 225, 0, 0);
if (eq) player->boots = eq;
}
else if (type == "WEAPON") {
if (itemName == "匕首") eq = new Equipment("匕首", EquipmentType::WEAPON, Rarity::WHITE, 0, 5, 25);
else if (itemName == "铜剑") eq = new Equipment("铜剑", EquipmentType::WEAPON, Rarity::GREEN, 0, 10, 40);
else if (itemName == "乌金剑") eq = new Equipment("乌金剑", EquipmentType::WEAPON, Rarity::BLUE, 0, 20, 65);
else if (itemName == "精钢之剑") eq = new Equipment("精钢之剑", EquipmentType::WEAPON, Rarity::PURPLE, 0, 50, 100);
else if (itemName == "赤血魔神剑") eq = new Equipment("赤血魔神剑", EquipmentType::WEAPON, Rarity::GOLD, 0, 85, 150);
else if (itemName == "龙魂双刃剑") eq = new Equipment("龙魂双刃剑", EquipmentType::WEAPON, Rarity::RED, 0, 100, 200);
if (eq) player->weapon = eq;
}
else if (type == "SHIELD") {
if (itemName == "木盾") eq = new Equipment("木盾", EquipmentType::SHIELD, Rarity::WHITE, 15, 0, 0);
else if (itemName == "铜质盾牌") eq = new Equipment("铜质盾牌", EquipmentType::SHIELD, Rarity::GREEN, 35, 0, 0);
else if (itemName == "乌金盾") eq = new Equipment("乌金盾", EquipmentType::SHIELD, Rarity::BLUE, 60, 0, 0);
else if (itemName == "护身之盾") eq = new Equipment("护身之盾", EquipmentType::SHIELD, Rarity::PURPLE, 100, 0, 0);
else if (itemName == "护身的魔神盾") eq = new Equipment("护身的魔神盾", EquipmentType::SHIELD, Rarity::GOLD, 150, 0, 0);
else if (itemName == "神圣的龙魂盾") eq = new Equipment("神圣的龙魂盾", EquipmentType::SHIELD, Rarity::RED, 225, 0, 0);
if (eq) player->shield = eq;
}
}
if (line.find("INVENTORY") != string::npos) {
int count;
istringstream iss(line);
string dummy;
iss >> dummy >> count;
for (int i = 0; i < count; i++) {
getline(inFile, line);
if (line == "草帽") player->inventory.push_back(Equipment("草帽", EquipmentType::HELMET, Rarity::WHITE, 50, 0, 0));
else if (line == "布衣") player->inventory.push_back(Equipment("布衣", EquipmentType::ARMOR, Rarity::WHITE, 100, 0, 0));
else if (line == "草鞋") player->inventory.push_back(Equipment("草鞋", EquipmentType::BOOTS, Rarity::WHITE, 15, 0, 0));
else if (line == "匕首") player->inventory.push_back(Equipment("匕首", EquipmentType::WEAPON, Rarity::WHITE, 0, 5, 25));
else if (line == "木盾") player->inventory.push_back(Equipment("木盾", EquipmentType::SHIELD, Rarity::WHITE, 15, 0, 0));
else if (line == "铜质头盔") player->inventory.push_back(Equipment("铜质头盔", EquipmentType::HELMET, Rarity::GREEN, 75, 0, 0));
else if (line == "铜质盔甲") player->inventory.push_back(Equipment("铜质盔甲", EquipmentType::ARMOR, Rarity::GREEN, 125, 0, 0));
else if (line == "铜质护靴") player->inventory.push_back(Equipment("铜质护靴", EquipmentType::BOOTS, Rarity::GREEN, 45, 0, 0));
else if (line == "铜剑") player->inventory.push_back(Equipment("铜剑", EquipmentType::WEAPON, Rarity::GREEN, 0, 10, 40));
else if (line == "铜质盾牌") player->inventory.push_back(Equipment("铜质盾牌", EquipmentType::SHIELD, Rarity::GREEN, 35, 0, 0));
else if (line == "乌金护额") player->inventory.push_back(Equipment("乌金护额", EquipmentType::HELMET, Rarity::BLUE, 100, 0, 0));
else if (line == "乌金护甲") player->inventory.push_back(Equipment("乌金护甲", EquipmentType::ARMOR, Rarity::BLUE, 140, 0, 0));
else if (line == "乌金战靴") player->inventory.push_back(Equipment("乌金战靴", EquipmentType::BOOTS, Rarity::BLUE, 70, 0, 0));
else if (line == "乌金剑") player->inventory.push_back(Equipment("乌金剑", EquipmentType::WEAPON, Rarity::BLUE, 0, 20, 65));
else if (line == "乌金盾") player->inventory.push_back(Equipment("乌金盾", EquipmentType::SHIELD, Rarity::BLUE, 60, 0, 0));
else if (line == "精钢头盔") player->inventory.push_back(Equipment("精钢头盔", EquipmentType::HELMET, Rarity::PURPLE, 125, 0, 0));
else if (line == "精钢护甲") player->inventory.push_back(Equipment("精钢护甲", EquipmentType::ARMOR, Rarity::PURPLE, 165, 0, 0));
else if (line == "精钢战靴") player->inventory.push_back(Equipment("精钢战靴", EquipmentType::BOOTS, Rarity::PURPLE, 100, 0, 0));
else if (line == "精钢之剑") player->inventory.push_back(Equipment("精钢之剑", EquipmentType::WEAPON, Rarity::PURPLE, 0, 50, 100));
else if (line == "护身之盾") player->inventory.push_back(Equipment("护身之盾", EquipmentType::SHIELD, Rarity::PURPLE, 100, 0, 0));
else if (line == "魔神战盔") player->inventory.push_back(Equipment("魔神战盔", EquipmentType::HELMET, Rarity::GOLD, 180, 0, 0));
else if (line == "魔神铠甲") player->inventory.push_back(Equipment("魔神铠甲", EquipmentType::ARMOR, Rarity::GOLD, 250, 0, 0));
else if (line == "魔神战靴") player->inventory.push_back(Equipment("魔神战靴", EquipmentType::BOOTS, Rarity::GOLD, 180, 0, 0));
else if (line == "赤血魔神剑") player->inventory.push_back(Equipment("赤血魔神剑", EquipmentType::WEAPON, Rarity::GOLD, 0, 85, 150));
else if (line == "护身的魔神盾") player->inventory.push_back(Equipment("护身的魔神盾", EquipmentType::SHIELD, Rarity::GOLD, 150, 0, 0));
else if (line == "龙魂头盔") player->inventory.push_back(Equipment("龙魂头盔", EquipmentType::HELMET, Rarity::RED, 225, 0, 0));
else if (line == "龙魂战铠") player->inventory.push_back(Equipment("龙魂战铠", EquipmentType::ARMOR, Rarity::RED, 275, 0, 0));
else if (line == "龙魂战靴") player->inventory.push_back(Equipment("龙魂战靴", EquipmentType::BOOTS, Rarity::RED, 225, 0, 0));
else if (line == "龙魂双刃剑") player->inventory.push_back(Equipment("龙魂双刃剑", EquipmentType::WEAPON, Rarity::RED, 0, 100, 200));
else if (line == "神圣的龙魂盾") player->inventory.push_back(Equipment("神圣的龙魂盾", EquipmentType::SHIELD, Rarity::RED, 225, 0, 0));
}
}
if (player->helmet) player->maxHp += player->helmet->hpBonus;
if (player->armor) player->maxHp += player->armor->hpBonus;
if (player->boots) player->maxHp += player->boots->hpBonus;
if (player->shield) player->maxHp += player->shield->hpBonus;
if (player->weapon) {
player->maxAttack += player->weapon->maxAttackBonus;
player->minAttack += player->weapon->minAttackBonus;
}
return true;
}
Monster* getAttackedMonster() {
for (auto& monster : monsters) {
if (monster.alive && abs(monster.x - player->x) <= 2 && abs(monster.y - player->y) <= 2) {
return &monster;
}
}
return nullptr;
}
void handleAttack() {
Monster* monster = getAttackedMonster();
if (monster) {
int damage = player->attack();
monster->takeDamage(damage);
int healAmount = rand() % 401 + 100;
player->heal(healAmount);
SetCursorPosition(0, 101);
cout << "你对" << monster->name << "造成了" << damage << "点伤害!";
SetCursorPosition(0, 102);
cout << "你恢复了" << healAmount << "点生命值!";
if (!monster->alive) {
SetCursorPosition(0, 103);
if (monster->isBoss) {
cout << "你击败了强大的魔龙领主!";
}
else {
cout << "你击败了" << monster->name << "!";
monstersDefeatedCount++;
SetCursorPosition(0, 104);
cout << "已击败怪物: " << monstersDefeatedCount << "/10";
if (monstersDefeatedCount >= 10) {
generateBoss();
monstersDefeatedCount = 0;
}
}
if (rand() % 100 < 60) {
Rarity rarity;
int roll = rand() % 100;
if (roll < 40) rarity = Rarity::WHITE;
else if (roll < 80) rarity = Rarity::GREEN;
else if (roll < 95) rarity = Rarity::BLUE;
else rarity = Rarity::PURPLE;
Equipment* eq = generateRandomEquipment(rarity);
player->inventory.push_back(*eq);
delete eq;
SetCursorPosition(0, 105);
cout << "获得了" << player->inventory.back().name << "!";
}
}
else {
int monsterDamage = monster->attack();
player->takeDamage(monsterDamage);
SetCursorPosition(0, 105);
cout << monster->name << "对你造成了" << monsterDamage << "点伤害!";
if (player->currentHp <= 0) {
SetCursorPosition(0, 106);
cout << "你已死亡!将在皇城复活...";
Sleep(2000);
player->respawn();
generateMonsters();
}
}
}
}
void checkMapTransition() {
if (player->currentMap == "皇城" && player->x >= 98) {
player->currentMap = "炼狱";
player->x = 1;
generateMonsters();
SetCursorPosition(0, 106);
cout << "你已进入炼狱地图!";
Sleep(2000);
}
}
void checkLevelUp() {
int totalNormal = 0, totalBosses = 0;
for (auto& monster : monsters) {
if (!monster.alive) {
if (monster.isBoss) totalBosses++;
else totalNormal++;
}
}
if (totalNormal > normalMonstersDefeated || totalBosses > bossesDefeated) {
int levelsGained = 0;
int newNormalKills = totalNormal - normalMonstersDefeated;
levelsGained += newNormalKills / 2;
int newBossKills = totalBosses - bossesDefeated;
levelsGained += newBossKills;
if (newNormalKills > 0 && newBossKills > 0) {
levelsGained += 1;
}
for (int i = 0; i < levelsGained; i++) {
player->levelUp();
}
if (levelsGained > 0) {
SetCursorPosition(0, 107);
cout << "恭喜升级!当前等级:" << player->level;
}
normalMonstersDefeated = totalNormal;
bossesDefeated = totalBosses;
}
}
public:
Game() : player(nullptr), gameRunning(false), inInventory(false),
inCharacterSelection(false), selectedInventoryItem(0),
selectedCharacter(0), bossSpawned(false), screenWidth(120), screenHeight(50),
normalMonstersDefeated(0), bossesDefeated(0), monstersDefeatedCount(0) {
srand(static_cast<unsigned int>(time(nullptr)));
}
~Game() {
if (player) delete player;
}
void run() {
gameRunning = true;
SetFullScreen();
drawStartScreen();
while (gameRunning && _getch() != 13);
drawLoadingScreen();
loadCharacters();
inCharacterSelection = true;
while (gameRunning && inCharacterSelection) {
drawCharacterSelection();
int key = _getch();
switch (key) {
case 'w':
case 'W':
selectedCharacter = (selectedCharacter - 1 + characterNames.size()) % characterNames.size();
break;
case 's':
case 'S':
selectedCharacter = (selectedCharacter + 1) % characterNames.size();
break;
case 'n':
case 'N':
createNewCharacter();
break;
case 13:
if (!characterNames.empty()) {
if (loadGame(characterNames[selectedCharacter])) {
inCharacterSelection = false;
}
else {
player = new Player(characterNames[selectedCharacter]);
inCharacterSelection = false;
}
}
break;
case 27:
gameRunning = false;
break;
}
}
generateMonsters();
while (gameRunning) {
drawGame();
if (inInventory) {
int key = _getch();
switch (key) {
case 'w':
case 'W':
selectedInventoryItem = max(0, selectedInventoryItem - 1);
break;
case 's':
case 'S':
selectedInventoryItem = min((int)player->inventory.size() - 1, selectedInventoryItem + 1);
break;
case 13:
if (!player->inventory.empty()) {
Equipment item = player->inventory[selectedInventoryItem];
player->inventory.erase(player->inventory.begin() + selectedInventoryItem);
player->equip(new Equipment(item));
selectedInventoryItem = min(selectedInventoryItem, (int)player->inventory.size() - 1);
}
break;
case 'b':
case 'B':
case 27:
inInventory = false;
break;
}
}
else {
int key = _getch();
switch (key) {
case 'w':
case 'W':
if (player->y > 1) {
player->y--;
drawGame();
}
break;
case 'a':
case 'A':
if (player->x > 1) {
player->x--;
drawGame();
}
break;
case 's':
case 'S':
if (player->y < 98) {
player->y++;
drawGame();
}
break;
case 'd':
case 'D':
if (player->x < 98) {
player->x++;
drawGame();
checkMapTransition();
}
break;
case 'b':
case 'B':
if (!player->inventory.empty()) {
inInventory = true;
selectedInventoryItem = 0;
drawGame();
}
break;
case 27:
gameRunning = false;
break;
case 0xE0:
key = _getch();
switch (key) {
case 72:
handleAttack();
drawGame();
break;
case 75:
handleAttack();
drawGame();
break;
case 77:
handleAttack();
drawGame();
break;
case 80:
handleAttack();
drawGame();
break;
}
break;
}
}
checkLevelUp();
saveGame();
}
}
};
int main() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(hConsole, &cursorInfo);
cursorInfo.bVisible = false;
SetConsoleCursorInfo(hConsole, &cursorInfo);
Game game;
game.run();
return 0;
}