用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

实现坦克大战

2019-10-21 作者: 我真的很帅举报

[java]代码库

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();
}
});


}




}

[代码运行效果截图]


实现坦克大战

[源代码打包下载]




网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...