(蔡坤 VS 吴凡)java实现回合制小游戏开发
首先定义属性(get set 有参,无参)
代码如下
java
package com.jie;
import java.util.Random;
class Hero{
private String name;
private String [] skill;
private int[] skillHurts;
private double hp;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String [] getSkill(){
return skill;
}
public void setSkill(String [] skill){
this.skill=skill;
}
public int [] getSKillHurts(){
return skillHurts;
}
public void setSkillHurts(int[] skillHurts){
this.skillHurts=skillHurts;
}
public double getHp(){
return hp;
}
public void setHp(double hp){
this.hp=hp;
}
public Hero(){
super();
}
public Hero(String name,String [] skill,int[] skillHurts,double hp){
super();
this.name=name;
this.skill=skill;
this.skillHurts=skillHurts;
this.hp=hp;
}
public void attack(Hero otherHero){
Random r=new Random();
//技能
int skillNum= r.nextInt(skill.length);
String skill=this.skill[skillNum];
double hurts=otherHero.skillHurts[skillNum];
otherHero.hp=otherHero.hp-hurts;
System.out.println(this.name+"使用了"+skill+"技能,对"+otherHero.getName()+"造成了"+hurts+"点伤害,"+otherHero.getName()
+"现在还剩"+otherHero.hp+"血量");
}
}
##定义技能和其他
package com.jie;
import java.util.Random;
public class HeroDemo {
public static void main(String[] args) {
int[] skillHurts = {50, 70, 80, 100};
String[] skillcxk = {"唱", "跳", "打篮球", "律师函警告"};
Hero cxk = new Hero("蔡坤", skillcxk, skillHurts, 700);
String[] skillwyf = {"rap", "修车", "街舞", "戴帽杀"};
Hero wyf = new Hero("吴凡", skillwyf, skillHurts, 700);
Random r = new Random();
int i = r.nextInt(2);
System.out.println("VS开始,首先摇色子随机判断谁先出手");
while (true) {
if (i == 0) {
System.out.println("蔡坤先开始进攻吴凡");
if (cxk.getHp() <= 0) {
System.out.println(cxk.getName() + "血量低于或等于0,无法继续攻击," + wyf.getName() + "获胜");
break;
}
cxk.attack(wyf);
if (wyf.getHp() <= 0) {
System.out.println(wyf.getName() + "血量低于或等于0,无法继续攻击," + cxk.getName() + "获胜");
}
wyf.attack(cxk);
} else if (i == 1) {
if (wyf.getHp() <= 0) {
System.out.println(wyf.getName() + "血量低于或等于0,无法继续攻击," + cxk.getName() + "获胜");
break;
}
wyf.attack(cxk);
if (cxk.getHp() <= 0) {
System.out.println(cxk.getName() + "血量低于或等于0,无法继续攻击," + wyf.getName() + "获胜");
break;
}
cxk.attack(wyf);
}
}
}
}
测试结果