内测小游戏
2024-09-08 21:27:53
发布于:北京
小游戏试测版,拿的时候说一声,谢谢(不好的话轻点喷谢谢)
各位有什么建议都可以提
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h> // 导入 Windows API
// 定义颜色常量
const WORD COLOR_GREEN = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
const WORD COLOR_YELLOW = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
const WORD COLOR_DEFAULT = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
// 设置控制台字体颜色
void setColor(WORD color) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
}
// 基础角色类
class Character {
protected:
std::string name;
int health;
int maxHealth;
int attackPower;
public:
Character(std::string name, int maxHealth, int attackPower) : name(name), maxHealth(maxHealth), health(maxHealth), attackPower(attackPower) {}
// 攻击方法
void attack(Character& target) {
int damage = this->attackPower;
if (rand() % 100 < 20) { // 20% 的概率触发暴击
damage *= 2; // 暴击造成双倍伤害
setColor(COLOR_YELLOW); // 设置字体颜色为黄色
std::cout << this->name << " 触发了暴击!造成了 " << damage << " 点伤害。\n";
setColor(COLOR_DEFAULT); // 重置字体颜色为默认
} else {
std::cout << this->name << " 攻击了 " << target.name << ",造成了 " << damage << " 点伤害。\n";
}
target.health -= damage;
if (!target.isAlive()) {
std::cout << target.name << " 被击败了!\n";
}
}
bool isAlive() const {
return health > 0;
}
std::string getName() const {
return name;
}
int getHealth() const {
return health;
}
void setHealth(int newHealth) {
health = std::max(0, newHealth); // 确保健康值不低于0
}
int getMaxHealth() const {
return maxHealth;
}
int getAttackPower() const {
return attackPower;
}
void setAttackPower(int newAttackPower) {
attackPower = newAttackPower;
}
void defend(int reduction) {
setAttackPower(getAttackPower() * reduction / 100);
}
void restoreAttackPower() {
setAttackPower(attackPower);
}
void heal() {
int healAmount = rand() % (getMaxHealth() - getHealth()) + 1; // 随机回血量
setHealth(getHealth() + healAmount);
setColor(COLOR_GREEN); // 设置字体颜色为绿色
std::cout << this->name << " 回复了 " << healAmount << " 点生命值,现在生命值为 " << getHealth() << "。\n";
setColor(COLOR_DEFAULT); // 重置字体颜色为默认
}
void printHealthBar() const {
int barLength = 20;
int currentHealthRatio = static_cast<int>((static_cast<double>(health) / maxHealth) * barLength);
std::cout << "[" << std::string(currentHealthRatio, '#') << std::string(barLength - currentHealthRatio, '-') << "] ";
std::cout << health << "/" << maxHealth << std::endl;
}
};
// 玩家类
class Player : public Character {
public:
Player(std::string name, int maxHealth, int attackPower) : Character(name, maxHealth, attackPower) {}
void chooseAction(Character& opponent) {
std::cout << "请选择行动:1-普通攻击,2-重击,3-连招,4-防守\n";
int choice;
std::cin >> choice;
switch (choice) {
case 1:
attack(opponent);
break;
case 2:
heavyAttack(opponent);
break;
case 3:
randomCombo(opponent);
break;
case 4:
defend(50); // 防守减少一半攻击力
std::cout << "你选择了防守。\n";
break;
default:
std::cout << "无效的选择,请重新输入。\n";
chooseAction(opponent); // 递归调用,直到输入正确为止
break;
}
}
void heavyAttack(Character& opponent) {
int heavyDamage = getAttackPower() * 2;
if (rand() % 100 < 20) { // 20% 的概率触发暴击
heavyDamage *= 2; // 暴击造成双倍伤害
setColor(COLOR_YELLOW); // 设置字体颜色为黄色
std::cout << this->getName() << " 的重击触发了暴击!造成了 " << heavyDamage << " 点伤害。\n";
setColor(COLOR_DEFAULT); // 重置字体颜色为默认
} else {
std::cout << this->getName() << " 使用了重击,造成了 " << heavyDamage << " 点伤害。\n";
}
opponent.setHealth(opponent.getHealth() - heavyDamage);
if (!opponent.isAlive()) {
std::cout << opponent.getName() << " 被击败了!\n";
}
}
void randomCombo(Character& opponent) {
int comboType = rand() % 4; // 随机选择一种连招模式
switch (comboType) {
case 0:
quickStrikeCombo(opponent);
break;
case 1:
powerStrikeCombo(opponent);
break;
case 2:
criticalStrikeCombo(opponent);
break;
case 3:
multiStrikeCombo(opponent);
break;
}
}
void quickStrikeCombo(Character& opponent) {
int numAttacks = 3;
int damagePerHit = getAttackPower();
for (int i = 0; i < numAttacks; ++i) {
setColor(COLOR_YELLOW); // 设置字体颜色为黄色
int damage = damagePerHit;
if (rand() % 100 < 20) { // 20% 的概率触发暴击
damage *= 2; // 暴击造成双倍伤害
std::cout << this->getName() << " 的快速打击触发了暴击!造成了 " << damage << " 点伤害。\n";
} else {
std::cout << this->getName() << " 进行快速打击,造成了 " << damage << " 点伤害。\n";
}
setColor(COLOR_DEFAULT); // 重置字体颜色为默认
opponent.setHealth(opponent.getHealth() - damage);
}
std::cout << "快速打击结束。\n";
if (!opponent.isAlive()) {
std::cout << opponent.getName() << " 被击败了!\n";
}
}
void powerStrikeCombo(Character& opponent) {
int numAttacks = 2;
int damagePerHit = getAttackPower() * 2;
for (int i = 0; i < numAttacks; ++i) {
setColor(COLOR_YELLOW); // 设置字体颜色为黄色
int damage = damagePerHit;
if (rand() % 100 < 20) { // 20% 的概率触发暴击
damage *= 2; // 暴击造成双倍伤害
std::cout << this->getName() << " 的强力打击触发了暴击!造成了 " << damage << " 点伤害。\n";
} else {
std::cout << this->getName() << " 进行强力打击,造成了 " << damage << " 点伤害。\n";
}
setColor(COLOR_DEFAULT); // 重置字体颜色为默认
opponent.setHealth(opponent.getHealth() - damage);
}
std::cout << "强力打击结束。\n";
if (!opponent.isAlive()) {
std::cout << opponent.getName() << " 被击败了!\n";
}
}
void criticalStrikeCombo(Character& opponent) {
int numAttacks = 1;
int damagePerHit = getAttackPower() * 3;
for (int i = 0; i < numAttacks; ++i) {
setColor(COLOR_YELLOW); // 设置字体颜色为黄色
int damage = damagePerHit;
if (rand() % 100 < 20) { // 20% 的概率触发暴击
damage *= 2; // 暴击造成双倍伤害
std::cout << this->getName() << " 的致命打击触发了暴击!造成了 " << damage << " 点伤害。\n";
} else {
std::cout << this->getName() << " 进行致命打击,造成了 " << damage << " 点伤害。\n";
}
setColor(COLOR_DEFAULT); // 重置字体颜色为默认
opponent.setHealth(opponent.getHealth() - damage);
}
std::cout << "致命打击结束。\n";
if (!opponent.isAlive()) {
std::cout << opponent.getName() << " 被击败了!\n";
}
}
void multiStrikeCombo(Character& opponent) {
int numAttacks = 4;
int damagePerHit = getAttackPower() / 2;
for (int i = 0; i < numAttacks; ++i) {
setColor(COLOR_YELLOW); // 设置字体颜色为黄色
int damage = damagePerHit;
if (rand() % 100 < 20) { // 20% 的概率触发暴击
damage *= 2; // 暴击造成双倍伤害
std::cout << this->getName() << " 的多重打击触发了暴击!造成了 " << damage << " 点伤害。\n";
} else {
std::cout << this->getName() << " 进行多重打击,造成了 " << damage << " 点伤害。\n";
}
setColor(COLOR_DEFAULT); // 重置字体颜色为默认
opponent.setHealth(opponent.getHealth() - damage);
}
std::cout << "多重打击结束。\n";
if (!opponent.isAlive()) {
std::cout << opponent.getName() << " 被击败了!\n";
}
}
};
// 怪物类
class Monster : public Character {
public:
Monster(std::string name, int maxHealth, int attackPower) : Character(name, maxHealth, attackPower) {}
};
void drawBattleScene(Player& player, Monster& monster) {
system("cls"); // 清屏(Windows)
// system("clear"); // 清屏(Linux/Mac)
std::cout << "勇者" << std::endl;
std::cout << " ^_^ " << std::endl;
player.printHealthBar();
std::cout << "-------------------" << std::endl;
std::cout << "恶魔" << std::endl;
std::cout << " >_< " << std::endl;
monster.printHealthBar();
}
int main() {
srand(time(0)); // 用于随机数种子
Player player("勇者", 30, 10); // 将勇者的攻击伤害改为10
Monster monster("恶魔", 30, 10);
while (player.isAlive() && monster.isAlive()) {
drawBattleScene(player, monster);
// 如果生命值低于最大生命值的20%,则有一定几率回血
if (player.getHealth() < player.getMaxHealth() * 0.2) {
if (rand() % 2 == 0) { // 50% 的几率回血
player.heal();
}
}
if (monster.getHealth() < monster.getMaxHealth() * 0.2) {
if (rand() % 2 == 0) { // 50% 的几率回血
monster.heal();
}
}
player.chooseAction(monster);
if (monster.isAlive()) {
monster.attack(player);
player.restoreAttackPower(); // 恢复原攻击力
}
}
std::cout << "-------------------\n";
if (player.isAlive()) {
std::cout << "恭喜你,赢得了胜利!\n";
} else {
std::cout << "很遗憾,你被击败了。\n";
}
std::cout << "\n本游戏由 [Cr] 开发。\n";
return 0;
}
全部评论 4
加个等待啊,要不然一闪而过就没了
昨天 来自 四川
0转人工(
2024-09-15 来自 广东
0不是一个回合秒了?
2024-09-15 来自 广东
0在
2024-09-15 来自 北京
0
加油
2024-09-15 来自 浙江
0你做的?这么厉害
2024-09-15 来自 北京
0
有帮助,赞一个