• 怎么用Java的代码制作简单的王者荣耀


    怎么用Java的代码制作简单的王者荣耀

    用Java代码实现简单的王者荣耀游戏:
    张飞的生命值:100 攻击力:10 防御力:12 命中率:0.85 )(后羿的生命值:80 攻击力:15 防御力:12 命中率:0.7)(妲己的生命值:50 攻击力:20 防御力:5 命中率:0.75)(元芳的生命值:70 攻击力:10 防御力:8 命中率:0.7)(露露的生命值:70 攻击力:12 防御力:10 命中率:0.8)(大鱼的生命值:100 攻击力:5 防御力:20命中率:0.75) //英雄名字,攻击力,防御力,命中率,用数组来添加,并一 一对应,并添加为英雄类.
    并用game类制作游戏过程

    1. import java.util.Random;
    2. class Hero {
    3. private String name;
    4. private int health;
    5. private int attack;
    6. private int defense;
    7. private double hitRate;
    8. public Hero(String name, int health, int attack, int defense, double hitRate) {
    9. this.name = name;
    10. this.health = health;
    11. this.attack = attack;
    12. this.defense = defense;
    13. this.hitRate = hitRate;
    14. }
    15. public String getName() {
    16. return name;
    17. }
    18. public int getHealth() {
    19. return health;
    20. }
    21. public int getAttack() {
    22. return attack;
    23. }
    24. public int getDefense() {
    25. return defense;
    26. }
    27. public double getHitRate() {
    28. return hitRate;
    29. }
    30. public void attack(Hero target) {
    31. Random random = new Random();
    32. if (random.nextDouble() < hitRate) {
    33. int damage = (int) (attack * (1 - target.getDefense() / 100.0));
    34. target.takeDamage(damage);
    35. System.out.println(name + "攻击了" + target.getName() + ",造成了" + damage + "点伤害!");
    36. } else {
    37. System.out.println(name + "攻击了" + target.getName() + ",但未命中!");
    38. }
    39. }
    40. public void takeDamage(int damage) {
    41. health -= damage;
    42. if (health < 0) {
    43. health = 0;
    44. }
    45. }
    46. public boolean isAlive() {
    47. return health > 0;
    48. }
    49. }
    50. public class Game {
    51. public static void main(String[] args) {
    52. Hero zhangFei = new Hero("张飞", 100, 10, 12, 0.85);
    53. Hero houYi = new Hero("后羿", 80, 15, 12, 0.7);
    54. Hero daJi = new Hero("妲己", 50, 20, 5, 0.75);
    55. Hero yuanFang = new Hero("元芳", 70, 10, 8, 0.7);
    56. Hero luLu = new Hero("露露", 70, 12, 10, 0.8);
    57. Hero daYu = new Hero("大鱼", 100, 5, 20, 0.75);
    58. Hero[] heroes = { zhangFei, houYi, daJi, yuanFang, luLu, daYu };
    59. while (true) {
    60. int attackerIndex = new Random().nextInt(heroes.length);
    61. int targetIndex = new Random().nextInt(heroes.length);
    62. Hero attacker = heroes[attackerIndex];
    63. Hero target = heroes[targetIndex];
    64. if (attacker.isAlive() && target.isAlive() && attacker != target) {
    65. attacker.attack(target);
    66. if (!target.isAlive()) {
    67. System.out.println(target.getName() + "被击败了!");
    68. }
    69. }
    70. boolean allDead = true;
    71. for (Hero hero : heroes) {
    72. if (hero.isAlive()) {
    73. allDead = false;
    74. break;
    75. }
    76. }
    77. if (allDead) {
    78. System.out.println("所有英雄都被击败了!");
    79. break;
    80. }
    81. }
    82. }
    83. }

    在这个示例中,我们首先创建了一个 Hero 类,包含了英雄的各项属性和方法。然后,在 Game 类的 main 方法中,我们创建了各个英雄对象,并将它们存储在一个数组中。然后,通过循环不断进行攻击,直到所有英雄都被击败。 每次攻击时,随机选择一个攻击者和一个目标英雄。攻击者使用自己的攻击力和命中率对目标英雄造成伤害,如果未命中则不造成伤害。如果目标英雄的生命值降为0,则被击败。 最后,检查是否所有英雄都被击败,如果是,则游戏结束。

  • 相关阅读:
    Cn2线路异常采用Nginx反代灾备解决方案
    一文带您快速了解工业交换机
    linux篇【10】:进程信号
    5A同步整流芯片 20V转12V2A/5V4.5A大电流 24W大功率同步整流芯片 大电流降压IC FS2462
    【附源码】计算机毕业设计JAVA药品管理系统演示录像 2021
    1024程序员狂欢节特辑 | 聊一聊Halcon中的3D手眼标定
    mysql数据文件
    还在用typeof、instanceof?是时候给你的类型检查升个级了
    【FPGA+PWM】基于FPGA的三相PWM整流器移相触发电路的设计与实现
    SQL性能分析与优化
  • 原文地址:https://blog.csdn.net/airen3339/article/details/134439059