用java写一个级简版的dota =.= (轻喷)
设计类图:
Unit.class:
package com.dota;
public abstract class Unit {
public String name;
private int HP = 0;
private int ATK = 1;
private int armor = 0;
private boolean isLive = true;
public void getHurt(int harm){
this.HP -= (harm-armor);
if(HP <= 0)
isLive = false;
}
public boolean isLive() {
return isLive;
}
public void setLive(boolean isLive) {
this.isLive = isLive;
}
abstract void attack(Unit target);
abstract void beAttack(Unit attacker);
public int getHP() {
return HP;
}
public void setHP(int hP) {
HP = hP;
}
public int getATK() {
return ATK;
}
public void setATK(int aTK) {
ATK = aTK;
}
public int getArmor() {
return armor;
}
public void setArmor(int armor) {
this.armor = armor;
}
public String getName() {
return name;
}
}
Tower.class:
package com.dota;
public class Tower extends Unit {
private boolean isDefense = false;
Tower(String name, int HP, int ATK, int armor){
this.name = name;
this.setHP(HP);
this.setATK(ATK);
this.setArmor(armor);
}
void attack(Unit target) {
}
public boolean isDefense() {
return isDefense;
}
public void setDefense(boolean isDefense) {
this.isDefense = isDefense;
}
void defense(){
isDefense = true;
System.out.println("开启塔防");
}
@Override
void beAttack(Unit attacker) {
}
}
Creature.class:
package com.dota;
public abstract class Creature extends Unit {
private int MP = 0;
private int speed = 300;
public int getMP() {
return MP;
}
public void setMP(int mP) {
MP = mP;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void move(Point p){
System.out.println("移动到"+p.toString());
}
}
Hero.class
package com.dota;
import java.util.Random;
public class Hero extends Creature {
private Skills[] skills = new Skills[4];
public Skills[] getSkills() {
return skills;
}
public void setSkills(Skills[] skills) {
this.skills = skills;
}
public Hero(String name, int hp, int atk, int armor, int mp, Skills[] skills){
this.name = name;
this.setHP(hp);
this.setATK(atk);
this.setArmor(armor);
this.setMP(mp);
this.skills = skills;
}
void attack(Unit target) {
int temp = new Random().nextInt(100);
if(temp < 30){
System.out.println("⊙_⊙ !!,"+ this.name +"打出了一下 Mis");
}else{
target.beAttack(this);
}
}
@Override
void beAttack(Unit attacker) {
int temp = new Random().nextInt(100);
if(temp < 30){
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
System.out.println("致!命!一!击!"+ attacker.name +"攻击"+ this.name +",造成了"+ this.getATK()*2 +"点>>*爆炸*<<伤害 ");
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
getHurt(this.getATK()*2);
}else{
System.out.println(attacker.name +"正在攻击"+ getName() +",造成了"+ attacker.getATK() +"点伤害");
getHurt(this.getATK());
}
}
}
Point.class
package com.dota;
public class Point {
public int x, y;
public String toString(){
return "坐标("+ x +","+ y +")";
}
public double getDistance(Point p){
double d = Math.pow((x - p.x), 2) + Math.pow((y - p.y), 2);
return Math.sqrt(d);
}
}
Skills.class
package com.dota;
import java.util.Timer;
import java.util.TimerTask;
public class Skills{
public String skillName;
public int costMP = 0, spellTime = 0, harm = 0, CD = 0;
public Skills(String skillName, int costMP, int spellTime, int harm, int cD) {
super();
this.skillName = skillName;
this.costMP = costMP;
this.spellTime = spellTime;
this.harm = harm;
CD = cD;
}
public void use(Hero h){
// new Timer().schedule(new TimerTask() {
// public void run() {
// System.out.println("消耗了"+ costMP +"点魔法值,"+"对"+ h.name +"使用了"+ skillName +",造成了"+ harm +"点伤害");
// }
// }, spellTime * 1000);
System.out.println("消耗了"+ costMP +"点魔法值,"+"对"+ h.name +"使用了"+ skillName +",造成了"+ harm +"点伤害");
h.getHurt(harm);
}
}
写在后面:
当然实现一个dota是不可能的,主要是为了巩固一下java面向对象的知识,一些概念的深化理解,自己动手写确实是最有效的途径