模拟DOTA小游戏

先做分析,需求分析?不就是简单的一些想法实现。 dota有英雄,有小兵,我的游戏也要有英雄,还有一大堆的比如商人,动物之类的东西。我可不想每一个都写一个类。所以提取共性,创造父类。Unit 代表所有单位,他们有坐标,必须要有显示在窗口的能力。然后英雄有比单位多一些什么,比如技能,属性,可以攻击别人,可以操控移动……而敌人也有一堆属性,但他们都是单位,都有单位必须有的方法。

看下面的代码:(因为懒得写一些特殊的敌人,所以就没有将Unit完全抽象出来,而是直接作为敌人来表示。)

package demo01; import java.awt.event.*; import java.awt.*; import java.util.*; public class Unit implements ShowElem, Runnable {//ShowElem 是一个定义的接口,只有一个方法show用来提醒我必须写show方法。当然也有其它用处 public int health = 100;//一些属性都有初始值这样你就可以为这个类用main方法测试,看看得到的数据是不是符合自己预期。 public int id = 0; public String s = “not define”; int x = 0, y = 0; private int width=30,height=30; private int cd = 1000; private int speedx=10,speedy=10;//速度分为x和y为了计算方便。 public void setxspeed(int i){ this.speedx=i; } public int getxspeed(){ return this.speedx; } public int getyspeed(){ return this.speedy; } public void setyspeed(int i){ this.speedy=i; } private boolean islive = true;//后来添加的,判断单位是否挂了。 public boolean islive(){ return this.islive; } public int getw() { return this.width; } public int geth(){ return this.height; } private Gun gun = new Ak47();//枪类,这个其实就是一个技能接口,枪的涉及难道和白虎的穿云箭不像吗? private DataBank dataBank = DataBank.init(); Thread t = new Thread(this);//线程,除非游戏结束要不然这个单位还是要不停在屏幕上运动的 Random rnd = new Random(); Unit() { t.start(); this.dataBank.add(this); this.id = this.dataBank.uSize() – 1; } Unit(int x, int y) { t.start(); this.x = x; this.y = y; this.dataBank.add(this); this.id = this.dataBank.uSize() – 1; } Unit(String s, int x, int y) { t.start(); this.s = s; this.x = x; this.y = y; this.dataBank.add(this); this.id = this.dataBank.uSize() – 1; } public void setGun(Gun gun) { this.gun = gun; } public Gun getGun() { return this.gun; } public void action() { ActionType.move(this); } public int getx() { return x; } public int gety() { return y; } public void show(Graphics g) {//这里可以换成贴图,但是我懒的找图,就画个圆吧,带个血条。 if(this.islive){ g.setColor(Color.blue); g.drawRect(x, y-5, width, 5); g.fillRect(x, y-5, width*health/100, 5); g.drawOval(x, y, width, height); } } public void run() {//我的run()写的不好,应该将里面的代码集合成一个action函数的,但是这是beta版能用就行了。 while (this.islive) { if(this.health<0){this.islive=false;break;} try { Thread.sleep(100); this.action(); this.cd -= 100; for (int i = 0; i < DataBank.data.uSize(); i++) { if (DataBank.data.uGet(i) != this) { if(this.cd<=0){ this.getGun().shoot(this, DataBank.data.uGet(i)); this.cd=1000; } } } } catch (InterruptedException e) { } } } public static void main(String[] args) {//每一个类里面都写main方法测试,这是一个好习惯,除非你完全确定自己写的类的效果。 Unit a = new Unit(); Unit b = new Unit(“b”, 20, 30); Unit c = new Unit(“c”, 30, 30); c.setGun(new Ak47()); DataBank db = DataBank.init();//DataBank 是什么?这个后面说 System.out.println(db.uSize()); System.out.println(db.uGet(0).x); System.out.println(a.id + ” ,” + b.id + ” ,” + c.id + c.getGun().getName()); try { Thread.sleep(2000); System.out.println(db.uGet(a.id).x + db.uGet(a.id).s); System.out.println(db.uGet(b.id).x + db.uGet(b.id).s); System.out.println(db.uGet(c.id).x + db.uGet(c.id).s); } catch (Exception e) {//里面没有操作,懒得写。运行结果符合自己预期。 } } }

Unit类有了看Hero类

在时间里面我们什么也不能留下,包括痛苦,快乐和生命。

模拟DOTA小游戏

相关文章:

你感兴趣的文章:

标签云: