C++ 代码 (action_game.cpp) #include iostream #include string #include ctime // 脚色类 class Character { public: std::string name; int health; int attack; Character(std::string name, int health, int attack) : name(name), health(health), attack(attack) {}...
C++ 代码 (action_game.cpp)
#include <iostream>
#include <string>
#include <ctime>
// 脚色类
class Character {
public:
std::string name;
int health;
int attack;
Character(std::string name, int health, int attack)
: name(name), health(health), attack(attack) {}
void attackCharacter(Character& other) {
muye148.com.cn,mondtek.com,miguli.com,micooljeans.com,merrillcapital.cn,mengdingshantea.com,luyashigangmuju.com,lontop.net,lnrongchi.com,ledgx.com.cn,landai88.com,kybpc.com,kowloonlk.com,kminn.com,kangnifen168.cn,ka7afz.com,jyxbyq.com,jxymxjm.com,jthg.cn,jldsc.cn,jinangongjiao.com,jgoka.com,jccoal.com.cn,ibenwei.com,hwxqd.com,huanenghg.com,hoyil.com,hnygw.cn,hnrftyy.com,hfholding.com,heyuanhotel.com.cn,hengmei123.com,hbwpn.com,hbrcl.com,hbclzqf.com,gzqyl.com,gzfkyy.com,gzcxedu.com,gyqhkj.com,ftdss.com,fszxyx.com,fdzzy.com,fapaomo.com,ezx.cc,ebay1688.cn,dyhn.net,dfmssc.com,dfbpm.com,desunb.com,daodn.cn,dadidownload.com,csxfsg.com,cn-rsq.com,cnfangdao.com,cjdlm.com,circe168.com,cdjsw.cn,bz300.cn,bysaimeier.com,bjzjry.com,baistc.com,baichao0411.com,aipshd.com.cn,88fafa.com.cn,88284025.com,808sky.com,7qicai.com,52baping.com,521cdd.com,51papj.cn,23888881.com,1aku.com,123sml.com,100well.net,0571gg.cn,0472c.com,0411gg.cn,028srjsj.com,028rshx.com,025scjc.com,022tjhc.com,shefule.com,ctclc.cn,sdrl.com.cn,syrhsb.com,x2565.cn,ywbwhq.com,100fzrc.com,weichanglvcai.com,boxue123.com,wdtz188.com,hch114.com,xsloo.com,sunny1818.com,t1g.cn,xaceeexpo.com,jtdlzz.com,gzdtwl.com,aier100.com,0834ls.com
std::cout << "The " << this->name << " attacks the " << other.name << " for " << this->attack << " damage." << std::endl;
other.health -= this->attack;
if (other.health <= 0) {
std::cout << other.name << " has been defeated!" << std::endl;
other.health = 0;
}
}
bool isAlive() const {
return health > 0;
}
};
// 主函数
int main() {
std::srand(static_cast<unsigned int>(std::time(nullptr)));
Character player("Player", 30, std::rand() % 6 + 1);
Character enemy("Enemy", 20, std::rand() % 4 + 1);
std::cout << "Action Game!" << std::endl;
std::cout << "You are the " << player.name << " with " << player.health << " health." << std::endl;
std::cout << "Your enemy is the " << enemy.name << " with " << enemy.health << " health." << std::endl;
while (player.isAlive() && enemy.isAlive()) {
std::cout << "Type 'a' to attack: ";
char action;
std::cin >> action;
if (action == 'a') {
player.attackCharacter(enemy);
}
}
if (player.isAlive()) {
std::cout << "Congratulations! You have won the game!" << std::endl;
} else {
std::cout << "You have been defeated. Game Over." << std::endl;
}
return 0;
}
编译和开动游戏
将上头的代码保存为 action_game.cpp 文献。
使用C++编译器编译代码:
g++ -o action_game action_game.cpp
开动编译后的法子:
./action_game
游戏玩法
玩家通过输入 'a' 来漏洞敌东说念主。
每次漏洞后,敌东说念主的生命值会减少。
游戏终了条目是玩家或敌东说念主的生命值为0或以下。
这个游戏特殊基础,仅用于演示C++驱散台欺诈法子的基本交互。在实质的游戏建树中,你会使用图形界面和更复杂的逻辑来创建更具互动性和文娱性的游戏。要是你对建树更高等的游戏感赞佩,可能需要学习更多对于C++游戏建树的学问和手段。
stdcoutattacknameaction发布于:中国香港声明:该文不雅点仅代表作家本东说念主,搜狐号系信息发布平台,搜狐仅提供信息存储空间功绩。