本文最后更新于:2024年7月25日 晚上
[toc]
Java小游戏——文字格斗 尝试
GameRole类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| import java.util.Scanner; import java.util.Random;
public class GameRole { private String name; private int health; private int DamageLow; private int DamageHigh; public GameRole() { } public GameRole(String name, int health, int DamageLow, int DamageHigh) { this.name = name; this.health = health; this.DamageLow = DamageLow; this.DamageHigh = DamageHigh; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDamageLow() { return DamageLow; } public void setDamageLow(int damageLow) { DamageLow = damageLow; } public int getDamageHigh() { return DamageHigh; } public void setDamageHigh(int damageHigh) { DamageHigh = damageHigh; } public int getHealth() { return health; } public void setHealth(int health) { this.health = health; } public void damage(GameRole AnotherRole) { Random damageRandom = new Random(); int damage; if(this.getDamageLow() == 1) damage = damageRandom.nextInt(this.getDamageHigh()) + this.getDamageLow(); else damage = damageRandom.nextInt(this.getDamageHigh() - 1) + this.getDamageLow(); int remainHealth = AnotherRole.getHealth() - damage; remainHealth = (remainHealth <= 0) ? 0 : remainHealth; System.out.printf("%s攻击%s,造成%d伤害,%s剩余%d生命值%n", this.getName(), AnotherRole.getName(), damage, AnotherRole.getName(), remainHealth); AnotherRole.setHealth(remainHealth); } }
|
Main类及main方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Player1: Health: 200 & Damage: [1, 20]"); System.out.println("Player2: Health: 100 & Damage: [2, 40]"); System.out.println("Input Player1's Name:"); String Player1Name = sc.next(); System.out.println("Input Player2's Name:"); String Player2Name = sc.next(); GameRole Player1 = new GameRole(Player1Name, 200, 1, 20); GameRole Player2 = new GameRole(Player2Name, 100, 2, 40); while(Player1.getHealth() != 0 && Player2.getHealth() != 0) { Player1.damage(Player2); if(Player2.getHealth() == 0) { System.out.printf("Player1(%s) Win!", Player1.getName()); break; } Player2.damage(Player1); if(Player1.getHealth() == 0) { System.out.printf("Player2(%s) Win!", Player2.getName()); break; } } } }
|
演示
平衡度
理论上倾向于Player2,
Player2打出伤害∈[2, 19]的概率为18/39,打出伤害∈[21, 40]为20/39;
Player1打出伤害∈[1, 9]的概率为9/20,打出伤害∈[11, 20]为9/20。
以上均为理论概率,底层随机数实现未进行更多了解,若有大佬欢迎联系补充。
为什么这样设计
觉得(100, 1, 20)的设计太扁平了,没有美感()。
就是个人不喜欢这种完全平衡的,比较喜欢相对平衡的。
为什么开始学Java了 Java好玩吗
准备免修
不好玩,语法有点麻烦
并且还是习惯同时了解一些底层的东西
例如说.pyc .pyo .class