
package cn.itcast.game; |
import java.awt.List; |
import java.io.IOException; |
import java.util.ArrayList; |
import java.util.Comparator; |
import java.util.concurrent.CopyOnWriteArrayList; |
import javax.security.auth.x500.X500Principal; |
import javax.swing.text.BadLocationException; |
import javax.swing.text.StyledEditorKit.ForegroundAction; |
import org.itcast.game.Window; |
import org.itcast.game.utils.DrawUtils; |
import org.itcast.game.utils.SoundUtils; |
import org.lwjgl.Sys; |
import org.lwjgl.input.Keyboard; |
import cn.itcast.game.business.Blockable; |
import cn.itcast.game.business.Destroyable; |
import cn.itcast.game.business.Hitable; |
import cn.itcast.game.business.Moveable; |
import cn.itcast.game.domain.Blast; |
import cn.itcast.game.domain.Bullet; |
import cn.itcast.game.domain.Direction; |
import cn.itcast.game.domain.Element; |
import cn.itcast.game.domain.EnemyTank; |
import cn.itcast.game.domain.Grass; |
import cn.itcast.game.domain.MyTank; |
import cn.itcast.game.domain.Steel; |
import cn.itcast.game.domain.Wall; |
import cn.itcast.game.domain.Water; |
public class GameWindow extends Window { |
public GameWindow(String title, int width, int height, int fps) { |
super(title, width, height, fps); |
// TODO Auto-generated constructor stub |
} |
CopyOnWriteArrayList<Element> list=new CopyOnWriteArrayList<Element>(); |
MyTank myTank; |
EnemyTank emTank1; |
EnemyTank emTank2; |
/** |
* 创建时调用(只执行一次) |
*/ |
@Override |
protected void onCreate() { |
for(int i=0;i<Config.WIDTH/64-1;i++){ |
Wall wall=new Wall(64*i, 64); |
addElement(wall); |
} |
for(int i=1;i<Config.WIDTH/64-1;i++){ |
Water water=new Water(64*i+64, 64*3); |
addElement(water); |
} |
for(int i=0;i<Config.WIDTH/64-1;i++){ |
Steel steel=new Steel(64*i, 64*5); |
addElement(steel); |
} |
for(int i=0;i<Config.WIDTH/64-1;i++){ |
Grass grass=new Grass(64*i+64, 64*7); |
addElement(grass); |
} |
myTank=new MyTank( Config.WIDTH/2-32, Config.HEIGHT-64); |
addElement(myTank); |
emTank1=new EnemyTank(0, 0); |
addElement(emTank1); |
emTank2=new EnemyTank(Config.WIDTH-64, 0); |
addElement(emTank2); |
// try { |
// SoundUtils.play("res//snd//start.wav"); |
// } catch (IOException e1) { |
// // TODO Auto-generated catch block |
// e1.printStackTrace(); |
// } |
|
} |
/** |
* 鼠标点击事件 |
*/ |
@Override |
protected void onMouseEvent(int key, int x, int y) { |
} |
/** |
* 键盘点击事件 |
*/ |
@Override |
protected void onKeyEvent(int key) { |
// TODO Auto-generated method stub |
switch (key) { |
case Keyboard.KEY_UP: |
myTank.move(Direction.UP); |
break; |
case Keyboard.KEY_LEFT: |
myTank.move(Direction.LEFT); |
break; |
case Keyboard.KEY_DOWN: |
myTank.move(Direction.DOWN); |
break; |
case Keyboard.KEY_RIGHT: |
myTank.move(Direction.RIGHT); |
break; |
case Keyboard.KEY_SPACE: |
Bullet bullet=myTank.shot(); |
if(bullet!=null) |
addElement(bullet); |
break; |
default: |
break; |
} |
} |
/** |
* 实时刷新 |
*/ |
@Override |
protected void onDisplayUpdate() { |
if(myTank.isDestroy()||(emTank1.isDestroy()&&emTank2.isDestroy())) |
{ |
list.clear(); |
try { |
DrawUtils.draw("res//img//gameover.gif", (Config.WIDTH-96)/2,(Config.HEIGHT-96)/2); |
} catch (IOException e2) { |
// TODO Auto-generated catch block |
e2.printStackTrace(); |
} |
SoundUtils.stop("res//snd//fire.wav"); |
} |
for(Element li:list){ |
li.draw(); |
} |
for(Element ee :list){ |
if(ee instanceof Destroyable){ |
Destroyable destroyable=(Destroyable)ee; |
if (destroyable.isDestroy()) { |
Blast blast= destroyable.showDestroy(); |
if(blast!=null){ |
addElement(blast); |
} |
list.remove(ee); |
} |
} |
} |
for (Element e1 : list) { |
for (Element e2 : list) { |
if ( e1!= e2&&e1 instanceof Moveable && e2 instanceof Blockable) { |
boolean flag = ((Moveable)e1).checkHit((Blockable)e2); |
if (flag) { //flag = true, |
break; |
} |
} |
} |
} |
for (Element e1 : list) { |
for (Element e2 : list) { |
if (e1 != e2 && e1 instanceof Bullet && e2 instanceof Hitable) { |
boolean flag = ((Bullet)e1).checkHit((Hitable)e2); |
if (flag) { //flag = true, |
if (((Bullet)e1).getTank() == e2) { |
continue; |
} |
if (((Bullet)e1).getTank().getClass() == e2.getClass()) { |
continue; |
} |
list.remove(e1); |
Blast blast = ((Hitable)e2).showAttack(); |
addElement(blast); |
} |
} |
} |
} |
for(Element e :list){ |
if(e instanceof Blast){ |
boolean flag=((Blast)e).isDestroy(); |
if(flag){ |
list.remove(e); |
} |
} |
} |
for(Element ee :list){ |
if(ee instanceof Blast){ |
Blast bullet=(Blast)ee; |
if (bullet.isDestroy()) { |
list.remove(ee); |
} |
} |
} |
for(Element e :list){ |
if(e instanceof EnemyTank){ |
((EnemyTank)e).move(); |
Bullet shot = ((EnemyTank)e).shot(); |
if (shot != null) { |
addElement(shot); |
} |
} |
} |
} |
public void addElement(Element e) { |
list.add(e); |
list.sort(new Comparator<Element>() { |
public int compare(Element e1, Element e2) { |
return e1.getOrder() - e2.getOrder(); |
} |
}); |
} |
} |




中级程序员
by: 秋北先生 发表于:2019-10-23 19:09:53 顶(0) | 踩(0) 回复
这是分三个类来实现的么?
回复评论