java课设,一个游戏中有多种角色(Character),例如:国王(King)、皇后(Queen)、骑士(Knight)、老怪(Troll)。
角色之间可能要发生战斗(fight),每场战斗都是一个角色与另一角色之间 的 一 对 一 战 斗 。 每 个 角 色 都 有 自 己 的 生 命 值 (hitPoint) 、 魔法值(magicPoint)、攻击力值(damage)和防御力值(defense)。每种角色都有一种武器进行攻击(fight);在程序运行中,可以动态修改角色的武器(setWeaponBehavior)。
每种角色都有一种魔法对自己或者其他角色施(performMagic);可以动态改变拥有的魔法(setMagicBehavior)
角色抽象类
// An highlighted block
/**
* 这是文档注释
* @author 张光远
* @version 创建时间:2020年6月14日 下午2:33:38
*/
package zhangguangyuan5377.characters;
import zhangguangyuan5377.behavior.*;
// TODO: Auto-generated Javadoc
/**
* The Class Characters.
*/
public abstract class Characters {
/** The Id. */
//类的实现
private int Id;
/** The name. */
private String name;
/** The hitpoint. */
private int hitpoint;//生命值
/** The magicpoint. */
private int magicpoint;//魔法值
/** The damage. */
private int damage;//攻击力值
/** The defense. */
private int defense;//防御力值
/** The point. */
private int point[]=new int[4];//记录角色自身各项值,切换武器时用以恢复初始值
/** The weapon. */
protected WeaponBehavior weapon;//武器
/** The magic. */
protected MagicBehavior magic;//魔法
/** The skill. */
protected String skill[]=new String[3];//平a技能,武器技能,魔法技能
/** The feature. */
protected String feature;
/**
* Instantiates a new characters.
*/
public Characters() {
this.Id=0;
this.name="未命名";
hitpoint=800;
magicpoint=200;
damage=80;
defense=15;
point[0]= hitpoint;
point[1]=magicpoint;
point[2]=damage;
point[3]=defense;
}
/**
* Instantiates a new characters.
*
* @param name the name
*/
public Characters(String name) {
this();
this.name=name;
/*hitpoint=800;
magicpoint=200;
damage=80;
defense=15;*/
}
/**
* Fight.
*
* @param c the c
* @param choice the choice
* @return the int
*/
public int fight(Characters c,boolean choice) {//完成本角色攻击角色 c 的操作
int hurt=0;
if(choice) {
hurt=weapon.useWeapon(this.getDamage(),c);
}
else {
hurt=-this.getPoint()[2]+c.getDefense()*2;
c.setHitpoint(hurt);
}
if(c.getHitpoint()<=0) {
return 1;//游戏胜利
}
else if(this.getHitpoint()<=0) {
return 2;//敌方胜利
}
else
return 0;//游戏未结束
}
/**
* Perform magic.
*
* @param c the c
* @param e the e
* @return the int
*/
public int performMagic(Characters c,Characters e) {
//if(getMagicpoint()>=50) {
return magic.useMagic(c,e);
// setMagicpoint(-50);
// }
// else
// System.out.println("magicpoint is not enough");
}
/**
* Sets the weapon behavior.
*
* @param w the new weapon behavior
*/
public void setWeaponBehavior(WeaponBehavior w) {
this.weapon=w;
this.skill[1]=weapon.getSkill();
rePoint();//恢复角色无武器状态值
//int a[]=getPoint();
setDamage((int)(w.getAt()*getDamage()));
setDefense((int)(w.getDe()*getDefense()));
/*int t = 0;
boolean wea[]=new boolean[4];
wea[0]=w instanceof KnifeBehavior;
wea[2]=w instanceof AxeBehavior;
wea[1]=w instanceof BowAndArrowBehavior;
wea[3]=w instanceof SwordBehavior;
for(int i=0;i<4;i++) {
if(wea[i]) {
t=i;
break;
}
}
double da = 0,de = 0;
switch(t) {
case 0:
da=0.1;
de=0.5;
break;
case 1:
da=0.4;
de=-0.1;
break;
case 2:
da=0.3;
de=0;
break;
case 3:
da=0.2;
de=0.25;
break;
default:
System.out.println("error");
}
da*=getDamage();
de*=getDefense();
setDamage((int)da);
setDefense((int)de);*/
}//改变武器
/**
* Sets the magic behavior.
*
* @param m the new magic behavior
*/
public void setMagicBehavior(MagicBehavior m) {
this.magic=m;
/*int t = 0;
boolean wea[]=new boolean[2];
wea[0]=m instanceof HealBehavior;
wea[1]=m instanceof InvisibleBehavior;
for(int i=0;i<2;i++) {
if(wea[i]) {
t=i;
break;
}
}
//double ta = 0;
switch(t) {
case 0:
setHitpoint((int)(0.1*hitpoint));//子类能否看见
break;
case 1:
setDamage((int)(0.1*damage));
break;
default:
System.out.println("error");
}
setMagicpoint(-50);*/
}//改变魔法
/**
* Gets the id.
*
* @return the id
*/
public int getId() {
return Id;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Display.
*/
public abstract void display();
/**
* Gets the hitpoint.
*
* @return the hitpoint
*/
public int getHitpoint() {
return hitpoint;
}
/**
* Gets the magicpoint.
*
* @return the magicpoint
*/
public int getMagicpoint() {
return magicpoint;
}
/**
* Gets the damage.
*
* @return the damage
*/
public int getDamage() {
return damage;
}
/**
* Gets the defense.
*
* @return the defense
*/
public int getDefense() {
return defense;
}
/**
* Gets the skill.
*
* @return the skill
*/
public String[] getSkill(){
return skill;
}
/**
* Gets the point.
*
* @return the point
*/
public int[] getPoint() {
int a[]=new int[4];
System.arraycopy(point, 0, a, 0, 4);
return a;
}
/**
* Gets the feature.
*
* @return the feature
*/
public String getFeature() {
return feature;
}
/**
* Sets the id.
*
* @param id the new id
*/
//能不能用protected
public void setId(int id) {
this.Id=id;
}
/**
* Sets the name.
*
* @param name the new name
*/
public void setName(String name) {
this.name=name;
}
/**
* Sets the hitpoint.
*
* @param value the new hitpoint
*/
public void setHitpoint(int value) {
hitpoint+=value;
}
/**
* Sets the magicpoint.
*
* @param value the new magicpoint
*/
public void setMagicpoint(int value) {
magicpoint+=value;
}
/**
* Sets the damage.
*
* @param value the new damage
*/
public void setDamage(int value) {
damage+=value;
}
/**
* Sets the defense.
*
* @param value the new defense
*/
public void setDefense(int value) {
defense+=value;
}
/**
* Re point.
*/
public void rePoint() {
//this.hitpoint=point[0];
//this.magicpoint=point[1];
this.damage=point[2];
this.defense=point[3];
}
}
具体英雄类
下面展示一些 内联代码片
。
// A code block
var foo = 'bar';
// An highlighted block
/**
*
*/
package zhangguangyuan5377.characters;
// TODO: Auto-generated Javadoc
/**
* The Class King.
*/
public class King extends Characters {
/**
* Instantiates a new king.
*
* @param name the name
*/
public King(String name) {
this();
setName(name);
// TODO Auto-generated constructor stub
//this.hipoint=0;
/*setHitpoint(50);
setMagicpoint(100);
setDamage(0);
setDefense(3);*/
}
/**
* Instantiates a new king.
*/
public King() {
// TODO Auto-generated constructor stub
//setId(1);
setHitpoint(50);
setMagicpoint(100);
setDamage(0);
setDefense(3);
this.skill[0]="王之圣裁";
this.feature="末代皇帝,一声令下,群臣应之";
}
/**
* Display.
*/
@Override
public void display() {
// TODO Auto-generated method stub
System.out.println("This is King showtime!");
}
}
武器接口
// An highlighted block
/**
*
*/
package zhangguangyuan5377.behavior;
import zhangguangyuan5377.characters.Characters;
// TODO: Auto-generated Javadoc
/**
* The Interface WeaponBehavior.
*/
public interface WeaponBehavior {
/**
* Use weapon.
*
* @param damage the damage
* @param enemy the enemy
* @return the int
*/
public int useWeapon(int damage,Characters enemy);
/**
* Gets the at.
*
* @return the at
*/
public double getAt();
/**
* Gets the de.
*
* @return the de
*/
public double getDe();
/**
* Gets the skill.
*
* @return the skill
*/
public String getSkill();
}
武器类
// An highlighted block
/**
*
*/
package zhangguangyuan5377.behavior;
import zhangguangyuan5377.characters.Characters;
// TODO: Auto-generated Javadoc
/**
* The Class SwordBehavior.
*/
public class SwordBehavior implements WeaponBehavior {
/** The id. */
private int id=4;
/** The at. */
private double at=0.2;
/** The de. */
private double de=0.25;
/** The skill. */
private String skill="一剑化三清";
/**
* Use weapon.
*
* @param damage the damage
* @param enemy the enemy
* @return the int
*/
@Override
public int useWeapon(int damage,Characters enemy) {
// TODO Auto-generated method stub
int a=-damage+enemy.getDefense();
enemy.setHitpoint(a);
return a;
}
/**
* Gets the at.
*
* @return the at
*/
@Override
public double getAt() {
// TODO Auto-generated method stub
return at;
}
/**
* Gets the de.
*
* @return the de
*/
@Override
public double getDe() {
// TODO Auto-generated method stub
return de;
}
/**
* Gets the skill.
*
* @return the skill
*/
public String getSkill() {
return skill;
}
/**
* Gets the id.
*
* @return the id
*/
public int getId() {
return id;
}
}