Logo
Section 1 / 2

设计 Player 类

学习 C++ 中类和对象的工作原理,并了解它们如何帮助你建模现实世界中的实体——例如战斗模拟器中的游戏角色。

导入

设计 Player 类

学习 C++ 中类和对象的工作原理,并了解它们如何帮助你建模现实世界中的实体——例如战斗模拟器中的游戏角色。

...

代码示例

1string name1 = "Knight Alex"; 2int health1 = 100; 3int attack1 = 15; 4 5string name2 = "Orc Blaze"; 6int health2 = 100; 7int attack2 = 12;
1#include <iostream> 2using namespace std; 3 4class Example { 5public: 6 int number; 7}; 8 9int main() { 10 Example obj; // 从 Example 类创建对象 11 obj.number = 42; // 设置其数据 12 cout << "Number: " << obj.number << endl; 13 return 0; 14}
1#include <iostream> 2using namespace std; 3 4class Greeter { 5public: 6 string name; 7 8 void sayHello() { 9 cout << "Hello, " << name << "!" << endl; 10 } 11}; 12 13int main() { 14 Greeter g; 15 g.name = "Alex"; 16 g.sayHello(); 17 return 0; 18}
1#include <iostream> 2using namespace std; 3 4class Box { 5public: 6 int width; 7 8 // 构造函数 9 Box(int w) { 10 width = w; 11 } 12}; 13 14int main() { 15 Box b(10); // 构造函数自动运行 16 cout << "Box width: " << b.width << endl; 17 return 0; 18}
1#include <iostream> 2#include <string> 3using namespace std; 4 5// 定义 Player 蓝图 6class Player { 7public: 8 string name; 9 int health; 10 int attackPower; 11 12 // 构造函数用于设置 Player 13 Player(string n, int h, int a) { 14 name = n; 15 health = h; 16 attackPower = a; 17 } 18 19 // 显示玩家属性 20 void display() { 21 cout << "Name: " << name 22 << " | Health: " << health 23 << " | Attack: " << attackPower << endl; 24 } 25}; 26 27int main() { 28 // 创建 Player 对象 29 Player player1("Knight Alex", 100, 15); 30 Player player2("Orc Blaze", 100, 12); 31 32 cout << "=== 角色概览 ===" << endl; 33 player1.display(); 34 player2.display(); 35 36 return 0; 37}

代码示例

1=== 战斗开始 === 2Knight Alex | Health: 100 | Attack: 15 3Orc Blaze | Health: 100 | Attack: 12 4 5Knight Alex 攻击 Orc Blaze! 6Orc Blaze 的生命值现在:85 7 8Orc Blaze 攻击 Knight Alex! 9Knight Alex 的生命值现在:88
PageFlux AI 助手

👋 你好!我是 PageFlux AI 助手,专门帮助您理解课程内容。有什么问题尽管问我吧!

💡 选中文本后点击"AI 询问",或 Ctrl+Enter 发送消息
1// 在此处添加你的代码
1#include <iostream> 2#include <string> 3using namespace std; 4 5class Player { 6public: 7 string name; 8 int health; 9 int attackPower; 10 11 Player(string n, int h, int a) { 12 name = n; 13 health = h; 14 attackPower = a; 15 } 16 17 void display() { 18 cout << name << " | Health: " << health 19 << " | Attack: " << attackPower << endl; 20 } 21 22 // 攻击另一个玩家 23 void attack(Player target) { 24 cout << name << " 攻击 " << target.name << "!" << endl; 25 target.health -= attackPower; 26 if (target.health < 0) 27 target.health = 0; 28 cout << target.name << "'s health is now: " << target.health << endl; 29 } 30}; 31 32int main() { 33 Player player1("Knight Alex", 100, 15); 34 Player player2("Orc Blaze", 100, 12); 35 36 cout << "=== 战斗开始 ===" << endl; 37 player1.display(); 38 player2.display(); 39 cout << endl; 40 41 player1.attack(player2); 42 cout << endl; 43 player2.attack(player1); 44 45 return 0; 46}
1#include <iostream> 2#include <string> 3using namespace std; 4 5class Player { 6public: 7 string name; 8 int health; 9 int attackPower; 10 11 Player(string n, int h, int a) { 12 name = n; 13 health = h; 14 attackPower = a; 15 } 16 17 void display() { 18 cout << name << " | Health: " << health 19 << " | Attack: " << attackPower << endl; 20 } 21 22 // 攻击另一个玩家 23 void attack(Player target) { 24 cout << name << " 攻击 " << target.name << "!" << endl; 25 target.health -= attackPower; 26 if (target.health < 0) 27 target.health = 0; 28 cout << target.name << "'s health is now: " << target.health << endl; 29 } 30}; 31 32int main() { 33 Player player1("Knight Alex", 100, 15); 34 Player player2("Orc Blaze", 100, 12); 35 36 cout << "=== 战斗开始 ===" << endl; 37 player1.display(); 38 player2.display(); 39 cout << endl; 40 41 player1.attack(player2); 42 cout << endl; 43 player2.attack(player1); 44 45 return 0; 46}

代码示例

1damage = attackPower - (defense / 2)

代码示例

1Knight Alex 攻击 Orc Blaze!

代码示例

1Damage dealt: 10

代码示例

1Orc Blaze's health is now: 90

代码示例

1Orc Blaze has been defeated!

代码示例

1Knight Alex 攻击 Orc Blaze! 2Damage dealt: 10 3Orc Blaze's health is now: 90
1// 在此处添加你的代码
1#include <iostream> 2#include <string> 3using namespace std; 4 5class Player { 6public: 7 string name; 8 int health; 9 int attackPower; 10 int defense; 11 12 // 带防御的构造函数 13 Player(string n, int h, int a, int d) { 14 name = n; 15 health = h; 16 attackPower = a; 17 defense = d; 18 } 19 20 // 显示玩家属性 21 void display() { 22 cout << name 23 << " | Health: " << health 24 << " | Attack: " << attackPower 25 << " | Defense: " << defense << endl; 26 } 27 28 // 带防御计算的攻击另一个玩家 29 void attack(Player& target) { 30 cout << name << " 攻击 " << target.name << "!" << endl; 31 32 // 伤害减少目标防御的一半 33 int damage = attackPower - (target.defense / 2); 34 35 // 手动防止负伤害(而不是使用 max()) 36 if (damage < 0) 37 damage = 0; 38 39 // 应用伤害并确保生命值不低于零 40 target.health -= damage; 41 if (target.health < 0) 42 target.health = 0; 43 44 cout << "Damage dealt: " << damage << endl; 45 cout << target.name << "'s health is now: " << target.health << endl; 46 47 // 如果目标生命值归零,打印击败消息 48 if (target.health == 0) 49 cout << target.name << " 已被击败!" << endl; 50 } 51}; 52 53int main() { 54 Player knight("Knight Alex", 100, 15, 6); 55 Player orc("Orc Blaze", 100, 12, 4); 56 57 cout << "=== 角色概览 ===" << endl; 58 knight.display(); 59 orc.display(); 60 cout << endl; 61 62 // 模拟一轮攻击 63 knight.attack(orc); 64 cout << endl; 65 orc.attack(knight); 66 67 return 0; 68}
1#include <iostream> 2#include <string> 3using namespace std; 4 5class Player { 6public: 7 string name; 8 int health; 9 int attackPower; 10 int defense; 11 12 // 带防御的构造函数 13 Player(string n, int h, int a, int d) { 14 name = n; 15 health = h; 16 attackPower = a; 17 defense = d; 18 } 19 20 // 显示玩家属性 21 void display() { 22 cout << name 23 << " | Health: " << health 24 << " | Attack: " << attackPower 25 << " | Defense: " << defense << endl; 26 } 27 28 // 带防御计算的攻击另一个玩家 29 void attack(Player& target) { 30 cout << name << " 攻击 " << target.name << "!" << endl; 31 32 // 伤害减少目标防御的一半 33 int damage = attackPower - (target.defense / 2); 34 35 // 手动防止负伤害(而不是使用 max()) 36 if (damage < 0) 37 damage = 0; 38 39 // 应用伤害并确保生命值不低于零 40 target.health -= damage; 41 if (target.health < 0) 42 target.health = 0; 43 44 cout << "Damage dealt: " << damage << endl; 45 cout << target.name << "'s health is now: " << target.health << endl; 46 47 // 如果目标生命值归零,打印击败消息 48 if (target.health == 0) 49 cout << target.name << " 已被击败!" << endl; 50 } 51}; 52 53int main() { 54 Player knight("Knight Alex", 100, 15, 6); 55 Player orc("Orc Blaze", 100, 12, 4); 56 57 cout << "=== 角色概览 ===" << endl; 58 knight.display(); 59 orc.display(); 60 cout << endl; 61 62 // 模拟一轮攻击 63 knight.attack(orc); 64 cout << endl; 65 orc.attack(knight); 66 67 return 0; 68}

数据结构与指针

对象之间的交互