用java实现简单的文字pk的小游戏,用java实现人物PK的小游戏

用java实现简单的文字pk的小游戏,用java实现人物PK的小游戏

首页休闲益智文字大战更新时间:2024-06-05

Hero.java

package game; import java.util.Random; public class Hero { String name; //名字 String [ ] skills;//技能 double [ ] skillHurts;//技能伤害 double hp=700;//初始血量 public void attack(Hero otherHero) { //生成一个随机数 Random rd=new Random(); //生成一个随机数,用来用列表选择一个技能 int index=rd.nextInt(skills.length); //用随机数,选取一个技能来 String skill=skills[index]; //用随机数,选出英雄对应的技能伤害 double skillHurt=skillHurts[index]; //对其他英雄造成伤害 otherHero.hp-=skillHurt; System.out.println(name "使用了" skill "技能,对" otherHero.name "造成了" skillHurt "点伤害" otherHero.name "还剩" otherHero.hp "点血"); } }

Test.java

package game; import java.util.Random; public class Test { public static void main(String[] args) { Random rd = new Random(); int index = rd.nextInt(2); Hero ngls= new Hero(); Hero ln = new Hero(); ngls.name = "尼古拉斯·赵四"; String[] skills = { "踢腿", "拳击", "背击", "话痨" }; ngls.skills = skills; double[] skillHurts = { 50, 70, 80, 150 }; ngls.skillHurts = skillHurts; ln.name = "刘能"; String[] skills1 = { "左勾拳", "结巴", "右勾拳", "诡计" }; ln.skills = skills1; double[] skillHurts1 = { 50, 70, 80, 100 }; ln.skillHurts = skillHurts1; while (true) { if (index == 1) { ngls.attack(ln); if (ln.hp <= 0) { System.out.println(ngls.name "血量低于或等于0,无法攻击" ln.name "获胜"); break; } ln.attack(ngls); if (ngls.hp <= 0) { System.out.println(ln.name "血量低于或等于0,无法攻击" ngls.name "获胜"); break; } } else { ngls.attack(ln); if (ln.hp <= 0) { System.out.println(ngls.name "血量低于或等于0,无法攻击" ln.name "获胜"); break; } } } } }

查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved