锛坖ava椤圭洰锛夊潶鍏嬪ぇ鎴?2.0

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了锛坖ava椤圭洰锛夊潶鍏嬪ぇ鎴?2.0相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/on()' title='on()'>on()   --   trace   serve   als   rgs   utils   mouse   .so   

杩欎釜鐗堟湰锛屽彧鑳界畻鏄竴涓洀褰紝鎶婃渶鍩烘湰鐨勪笢瑗跨粰瀹屾垚浜嗭紝涓嶈繃锛屽悗闈㈠彲娣诲姞鐨勪篃涓嶅銆傛湁涓€鐐癸紝杩樻槸鎯冲幓瀹炵幇锛岄偅灏辨槸鏁屾柟鍧﹀厠鑷繁瀵绘壘瀵规墜锛佷篃灏辨槸娓告垙AI.

emmm, 浠€涔堟椂鍊欏彲浠ヤ簡瑙d竴涓嬭繖涓狝I銆傞『渚垮瀛ython銆?/p>

 

杩欎釜甯栧瓙鍙槸涓轰簡璐翠笂浠g爜锛屽鍚庣画鐨勪唬鐮佷綔涓轰紭鍖栧拰娣诲姞娓告垙AI鍋氬噯澶囩殑銆?/p>

 

 


1. 鍚勭被鎺ュ彛

package Event;
/*
 *鍏锋湁鏀诲嚮鍔涚殑鎺ュ彛
 */
public interface Attackable {
    /*
     * 鏍¢獙鍏锋湁鏀诲嚮鍔涚殑瀹炵墿, 鍜岋紝 鍏锋湁琚敾鍑荤殑瀹炵墿,鏄惁鑳芥挒鍦ㄤ竴璧?     *
     */
    boolean checkAttack(Hitable hit);
}

 

package Event;
/*
 * 鍏锋湁闃绘尅鍔熻兘鐨勫疄鐗? */
public interface Blockable {

}
package Event;
/*
 * 鑳借閿€姣佺殑绫? */
import domain.Blast;
/*
 * 閿€姣佹帴鍙? */
public interface Destoryable {
    /*
     * 鍒ゆ柇鏄惁闇€瑕侀攢姣?     */
    boolean isDestoryable();
    
    /*
     * 閿€姣佹椂锛岀粯鍒剁垎鐐哥墿
     */
    Blast showDestory();
}

 

package Event;

import domain.Blast;
/*
 * 鑳借鏀诲嚮鐨勭被
 */
public interface Hitable {
    Blast showAttack();
}
package Event;
/*
 * 鍏锋湁绉诲姩鍔熻兘鐨勪簨鐗? * 
 */
public interface Moveable {
    boolean checkHit(Blockable blockable);
}

2.鍚勭被娓告垙鍏冪礌鐨勭埗绫?/p>

package domain;

/*
 * 鐖剁被锛? * 鏄爾澧欙紝鍧﹀厠锛屾按澧欑瓑鎵€鏈夊厓绱犵殑鎶借薄鍑烘潵鐨勬娊璞$被
 */
public abstract class Element {
    /*
     * 灞炴€?     * 鍧愭爣
     */
    protected int x;
    protected int y;
    //瀹介珮
    protected int width;
    protected int hight;
    
    //鏋勯€犲嚱鏁?    //绌哄弬鏋勯€犲嚱鏁?/span>
    public Element() {super();}        //鍙互涓嶅啓super()锛宩ava浼氳嚜宸辫皟鐢?涔熷氨鏄皟鐢ㄦ娊璞$被鐨勭埗绫?/span>
    
    public Element(int x, int y) {
        this.x=x;        this.y=y;
    }
    
    /*
     * 缁樺埗鍏冪礌
     */
    public abstract void draw();
    
    /*
     * 璁剧疆鍏冪礌鐨勬覆鏌撶骇鍒紝鏁板瓧瓒婇珮锛屾覆鏌撶骇鍒秺楂?     */
    public int getOrder() {return 0;}
}

鍧﹀厠鐨勭埗绫?/p>

package domain;

import org.itheima.game.utils.CollisionUtils;

import Event.Blockable;
import Event.Destoryable;
import Event.Hitable;
import Event.Moveable;
/*
 * 鎵€鏈夊潶鍏嬬殑鐖剁被 * 鍧﹀厠鍏锋湁绉诲姩锛岄樆鎸★紝琚敾鍑伙紝琚攢姣佺殑鍔熻兘
 * 
 */
public abstract class Tank extends Element implements Moveable, Blockable, Hitable,Destoryable{
    /*
     * 灞炴€?     */
    //鏀诲嚮鍔?/span>
    protected int power=1;
    //琛€閲?/span>
    protected int blood=5;
    
    //鍧﹀厠鐨勭Щ鍔ㄦ柟鍚?/span>
    protected Direction direction=Direction.UP;
    
    //姣忔绉诲姩鐨勯€熷害
    protected int speed=3;
    
    //璁板綍鏈€鍚庝竴棰楀瓙寮圭殑鍙戝皠鏃堕棿
    protected long lastTime;
    
    //璁板綍鍧﹀厠涓嶈兘绉诲姩鐨勬柟鍚?/span>
    protected Direction badDirection;
    
    //璁板綍鍧﹀厠纰版挒鐨勬渶灏忕Щ鍔ㄨ窛绂?/span>
    protected int badSpeed;
    
    
    /*
     * 鏋勯€犲嚱鏁?     */
    public Tank() {super();}
    public Tank(int x, int y) {super(x, y);}
    
    /*
     * 缁樺埗鍧﹀厠
     */
    public abstract void draw();
    
    /*
     * 鍙戝皠瀛愬脊
     */
    public Bullet shot() {
        //鎬濊矾锛氬姝屾渶鍚庝竴棰楀瓙寮? - 褰撳墠鍙戝皠鐨勬椂闂? > 650ms 灏卞彂灏勫瓙寮?        //鑾峰彇褰撳墠鏃堕棿
        long newTime=System.currentTimeMillis();
        if(newTime-lastTime<650) {
            return null;    //涓嶅彂灏勫瓙寮?/span>
        }else {
            lastTime=newTime;
            return new Bullet(this);        //璋佽皟鐢ㄤ簡灏辨槸璋佺殑瀛愬脊
        }
    }
    
    /*
     * 鑾峰彇鍧﹀厠鐨勭Щ鍔ㄦ柟鍚?     */
    public Direction getDirection() {return direction;}
    
    /*
     * 鏍¢獙锛氱Щ鍔ㄧ墿(鍧﹀厠)鏄惁涓庨殰纰嶇墿鏄惁鎾炰笂
     */
    public boolean checkHit(Blockable block){
        Element e=(Element)block;
        int x1=e.x;
        int y1=e.y;
        int w1=e.width;
        int h1=e.hight;
        
        //棰勫垽鍧﹀厠鐨勫潗鏍?/span>
        int x2=x;
        int y2=y;
        switch (direction) {
        case UP:
            y2-=speed;
            break;
        case DOWN:
            y2+=speed;
            break;
        case LEFT:
            x2-=speed;
            break;
        case RIGHT:
            x2+=speed;
            break;
        default:
            break;
        }
        boolean flag=CollisionUtils.isCollsionWithRect(x1, y1, w1, h1, x2, y2, width, hight);
        if(flag) {
            //璇存槑棰勫垽鍙互鎾炰笂,涓嶄粎瑕佽褰曚笉鑳界Щ鍔ㄧ殑鏂瑰悜锛岃繕瑕佽褰曟渶灏忛棿闅?/span>
            badDirection=direction;
            switch (direction) {
            case UP:
                badSpeed=y-y1-h1;
                break;
            case DOWN:
                badSpeed = y1 - y - hight;
                break;
            case LEFT:
                badSpeed = x - x1 - w1;
                break;
            case RIGHT:
                badSpeed = x1 - w1 - width;
                break;
            default:
                break;
            }
        }else {
            badDirection=null;        //娌℃湁鎾炰笂鐨勮瘽锛屽氨娌℃湁鍧忕殑鏂瑰悜
        }
        return flag;
    }
    
    /*
     * 閿€姣佺殑鏂规硶
     */
    public boolean isDestory() {return blood<=0;}
    
    /*
     * 琚攢姣佺墿锛屽湪琚攢姣佹椂鐨勮〃鐜般€?     */
    public Blast showDestory() {return new Blast(this);}
    
    
}

鍚勭娓告垙鍏冪礌

鎶€鏈垎浜浘鐗? id=
package domain;

import java.io.IOException;

import org.itheima.game.utils.DrawUtils;
import org.itheima.game.utils.SoundUtils;

import Event.Blockable;
import Event.Destoryable;
import Event.Hitable;

/*
 * 
 * 鐖嗙偢鐗╃被
 * 
 */
public class Blast extends Element implements Destoryable{
    /*
     * 灞炴€?     */
    //1.鍥剧墖
    private String[]arr= {"res/img/blast_1.gif", "res/img/blast_2.gif", "res/img/blast_3.gif", "res/img/blast_4.gif",
            "res/img/blast_5.gif", "res/img/blast_6.gif", "res/img/blast_7.gif", "res/img/blast_8.gif"};
    //2.瀹氫箟鍙橀噺锛岃褰曡缁樺埗鐨勫浘鐗囩储寮?/span>
    private int index;
    
    //3.瀹氫箟鍙橀噺锛岃褰曟槸鍚﹂渶瑕侀攢姣佽鐖嗙偢鐗?    //true锛氶攢姣侊紝 false 锛氫笉閿€姣?/span>
    private boolean isDestory;

    /*
     * 鏋勯€犳柟娉?     * 鐖嗙偢鐗╃殑鍧愭爣渚濊禆澧欑殑鍧愭爣
     */
    public Blast(Hitable hit) {
        this(hit, false);
        try {
            SoundUtils.play("res/snd/blast.wav");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /*
     * flag :true琛ㄧず鎸ㄦ墦锛岀粯鍒?~4鐨勫浘鐗?     *         false 璇存槑鏄攢姣侊紝缁樺埗1~8鐨勫浘鐗?     */
    public Blast(Hitable hit, boolean flag) {
        Element element=(Element)hit;
        //璁$畻鐖嗙偢鐗╃殑浣嶇疆
        //鑾峰彇澧欑殑鍧愭爣鍜屽楂?/span>
        int x1=element.x;
        int y1=element.y;
        int w1=element.width;
        int h1=element.hight;
        //鑾峰彇鐖嗙偢鐗╃殑瀹藉害
        try {
            int size[]=DrawUtils.getSize("res/img/blast_1.gif");
            width=size[0];    hight=size[1];
            
            //璁$畻鐖嗙偢鐗╃殑鍧愭爣
            x=x1+(w1-width)/2;
            y=y1+(h1-hight)/2;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        if(flag) {
            arr=new String[] {"res/img/blast_1.gif", "res/img/blast_2.gif", "res/img/blast_3.gif", "res/img/blast_4.gif"};
            try {
                SoundUtils.play("res/snd/hit.wav");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    @Override
    public void draw() {
        // TODO Auto-generated method stub
        //瀹氫箟鍙橀噺锛岃褰曡缁樺埗鐨勫浘鐗囩殑璺緞
        String res=arr[index++];
        if(index>=arr.length) {
            index=0;
            //璇存槑鐖嗙偢鐗╁凡缁忕粯鍒惰繃涓€娆′簡鍙互閿€姣?/span>
            isDestory=true;
        }
        try {
            DrawUtils.draw(res, x, y);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    /*
     * 鑾峰彇鐖嗙偢鐗╃殑鐘舵€?     */
    public boolean isDestoryable() {
        // TODO Auto-generated method stub
        return isDestory;
    }
    /*
     * 閿€姣佹椂锛岀粯鍒堕攢姣佺殑鐖嗙偢鐗?     */
    public Blast showDestory() {return null;}
    
    
}
View Code
鎶€鏈垎浜浘鐗? id=
package domain;

import java.io.IOException;

import org.itheima.game.utils.CollisionUtils;
import org.itheima.game.utils.DrawUtils;
import org.itheima.game.utils.SoundUtils;

import Event.Attackable;
import Event.Destoryable;
import Event.Hitable;
import Grame.config;

/*
 * 瀛愬脊绫?鍏锋湁鏀诲嚮鑳藉姏锛岃閿€姣佺殑鑳藉姏
 */
public class Bullet extends Element implements Attackable, Destoryable {

    //璁板綍瀛愬脊绉诲姩鐨勬柟鍚?/span>
    private Direction direction;
    //璁板綍瀛愬脊鐨勯€熷害
    private int speed=7;
    //瀹氫箟鍙橀噺锛岃褰曞瓙寮规墍灞炵殑鍧﹀厠
    private Tank tank;
    
    
    /*
     * 鏋勯€犲嚱鏁?non-Javadoc)
     * @see Event.Destoryable#isDestoryable()
     */
    //瀛愬脊鐨勫潗鏍囦緷璧栧潶鍏?/span>
    public Bullet(Tank tank) {
        // TODO Auto-generated constructor stub
        super();
        //鐢ㄥ彉閲弔ank璁板綍锛屽瓙寮规墍灞炵殑鍧﹀厠
        this.tank=tank;
        
        //鑾峰彇鍧﹀厠鐨勫潗鏍囷紝瀹介珮锛屾柟鍚?/span>
        int tankX=tank.x;
        int tankY=tank.y;
        int tankWidth=tank.width;
        int tankHight=tank.hight;
        direction=tank.getDirection();
        
        //鑾峰彇瀛愬脊鐨勫楂?/span>
        try {
            int size[]=DrawUtils.getSize("res/img/bullet_u.gif");
            width=size[0];        hight=size[1];
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        //璁$畻瀛愬脊鐨勫潗鏍?/span>
        switch (direction) {
        case UP:
            x = tankX + (tankWidth - width)/2;
            y = tankY - hight / 2;
            break;
        case DOWN:
            x = tankX + (tankWidth - width)/2;
            y = tankY + tankHight - hight /2;
            break;
        case LEFT:
            x=tankX-width;
            y=tankY+(tankHight-hight)/2;
            break;
        case RIGHT:
            x = tankX + tankWidth - width / 2;
            y = tankY + (tankHight - hight)/2;
            break;
        default:
            break;
        }
        
        try {
            SoundUtils.play("res/snd/fire.wav");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    /* 鍑鸿竟鐣?     * 閿€姣佸瓙寮? true锛氶攢姣侊紝false锛氫笉閿€姣?non-Javadoc)
     * @see Event.Destoryable#isDestoryable()
     */
    @Override
    public boolean isDestoryable() {
        // TODO Auto-generated method stub
        if(x<0||x>config.WIDTH||y<0||y>config.HIGHT) return true;
        return false;
    }

    /*
     * 瀛愬脊閿€姣佹椂鐨勫弽鏄?non-Javadoc)
     * @see Event.Destoryable#showDestory()
     */
    @Override
    public Blast showDestory() {
        // TODO Auto-generated method stub
        return null;
    }
    /*鏍¢獙鏄惁涓庨樆纰嶇墿纰颁笂
     * (non-Javadoc)
     * @see Event.Attackable#checkAttack(Event.Hitable)
     */
    @Override
    public boolean checkAttack(Hitable hit) {
        // TODO Auto-generated method stub
        Element e=(Element)hit;
        int x1=e.x;
        int y1=e.y;
        int w1=e.width;
        int h1=e.hight;
        //绗竴涓煩褰細闅滅鐗╋紝绗簩涓挒鍑荤墿:瀛愬脊
        return CollisionUtils.isCollsionWithRect(x1, y1, w1, h1, x, y, width, hight);
    }

    
    /*
     * 缁樺埗瀛愬脊(non-Javadoc)
     * @see domain.Element#draw()
     */
    @Override
    public void draw() {
        // TODO Auto-generated method stub
        String res="";
        //鍒ゆ柇鍧﹀厠鏂瑰悜
        switch (direction) {
        case UP:
            res = "res/img/bullet_u.gif";
            y-=speed;
            break;
        case DOWN:
            res = "res/img/bullet_d.gif";
            y += speed;
            break;
        case LEFT:
            res = "res/img/bullet_l.gif";
            x -= speed;
            break;
        case RIGHT:
            res = "res/img/bullet_r.gif";
            x += speed;
            break;
        default:
            break;
        }
        
        try {
            DrawUtils.draw(res, x, y);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /*
     * 鑾峰彇瀛愬脊鎵€灞炲潶鍏?     */
    public Tank getTank() {
        return tank;
    }
}
View Code
鎶€鏈垎浜浘鐗? id=
package domain;
/*
 * 鏋氫妇鍥涗釜鏂瑰悜
 */
public enum Direction {
    UP,DOWN,LEFT,RIGHT;
}
View Code

鎶€鏈垎浜浘鐗? id=
package domain;

import java.io.IOException;
import java.util.Random;

import org.itheima.game.utils.DrawUtils;

import Grame.config;

public class EnemyTank extends Tank{

    /*
     * 灞炴€?non-Javadoc)
     * @see Event.Hitable#showAttack()
     */
    public EnemyTank(int x, int y) {
        super(x, y);
        try {
            int size[]=DrawUtils.getSize("res/img/enemy_u.gif");
            width=size[0];    hight=size[1];
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public EnemyTank() {super();}
    
    
    @Override
    public Blast showAttack() {
        // TODO Auto-generated method stub
        //鎵h
        blood-=new Mytank().power;
        return new Blast(this, true);
    }

    @Override
    public boolean isDestoryable() {
        // TODO Auto-generated method stub
        return blood<=0;
    }

    /*
     * 鍧﹀厠鐨勭粯鍒?non-Javadoc)
     * @see domain.Tank#draw()
     */
    @Override
    public void draw() {
        // TODO Auto-generated method stub
        String res="";
        //鍒ゆ柇鍧﹀厠鐨勬柟鍚?/span>
        switch (direction) {
        case UP:
            res = "res/img/enemy_u.gif";
            break;
        case DOWN:
            res = "res/img/enemy_d.gif";
            break;
        case LEFT:
            res = "res/img/enemy_l.gif";
            break;
        case RIGHT:
            res = "res/img/enemy_r.gif";
            break;
        default:
            break;
        }
        
        try {
            DrawUtils.draw(res, x, y);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    /*
     * 闅忔満鑾峰彇鏁屾柟鍧﹀厠鐨勬柟鍚?     */
    public Direction getRandDirection() {
        int num=new Random().nextInt(4);
        switch (num) {
        case 0:
            return Direction.UP;
        case 1:
            return Direction.DOWN;
        case 2:
            return Direction.LEFT;
        case 3:
            return Direction.RIGHT;
        default:
            break;
        }
        return Direction.UP;
    }
    
    /*
     * 绉诲姩
     */
    public void move() {
        //濡傛灉浼犺繃鏉ョ殑鏂瑰悜锛屼笌鍧﹀厠涓嶈兘绉诲姩鐨勬柟鍚戠浉鍚岋紝鍒欑Щ鍔ㄦ渶灏忚窛绂?/span>
        if(direction==badDirection) {
            switch (direction) {
            case UP:
                y-=badSpeed;
                break;
            case DOWN:
                y+=badSpeed;
                break;
            case LEFT:
                x-=badSpeed;
                break;
            case RIGHT:
                x+=badSpeed;
                break;
            default:
                break;
            }
            //涓嶈兘绉诲姩灏遍殢鏈烘崲涓€涓嬫柟鍚?/span>
            direction=getRandDirection();
            return ;
        }
        
        switch (direction) {
        case UP:
            y-=speed;
            break;
        case DOWN:
            y+=speed;
            break;
        case LEFT:
            x-=speed;
            break;
        case RIGHT:
            x+=speed;
            break;
        default:
            break;
        }
        
        //杈圭晫澶勭悊
        if(x<0) {
            x=0;
            direction=getRandDirection();
        }
        if(x>config.WIDTH-64) {
            x=config.WIDTH-64;
            direction=getRandDirection();
        }
        if(y<0) {
            y=0;
            direction=getRandDirection();
        }
        if(y>config.HIGHT-64) {
            y=config.HIGHT-64;
            direction=getRandDirection();
        }
    }
}
View Code
 
鎶€鏈垎浜浘鐗? id=
package domain;

import java.io.IOException;

import org.itheima.game.utils.DrawUtils;
/*
 * 鑽夊潽
 */
public class Grass extends Element{

    public Grass(int x, int y) {
        // TODO Auto-generated constructor stub
        super(x, y);
        //鑾峰彇瀹介珮
        try {
            int size[]=DrawUtils.getSize("res/img/grass.gif");
            width=size[0];        hight=size[1];
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    @Override
    public void draw() {
        // TODO Auto-generated method stub
        try {
            DrawUtils.draw("res/img/grass.gif", x, y);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /*
     * 淇敼鑽夊潽鐨勬覆鏌撶骇鍒?     */
    public int getOrder() {return 1;}
}
View Code
鎶€鏈垎浜浘鐗? id=
package domain;

import java.io.IOException;

import javax.swing.plaf.DimensionUIResource;

import org.itheima.game.utils.DrawUtils;

import Grame.config;

public class Mytank extends Tank{
    public Mytank(int x, int y) {
        // TODO Auto-generated constructor stub
        super(x, y);
        //鍒濆鍖栭€熷害
        speed=30;    power=2;    blood=10;
        
        //鑾峰彇鍧﹀厠鐨勫楂?/span>
        try {
            int size[] = DrawUtils.getSize("res/img/tank_u.gif");
            width=size[0];
            hight=size[1];
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public Mytank() {
        // TODO Auto-generated constructor stub
        super();
        power=2;
    }
    
    
    /*
     * 鎸ㄦ墦鏂规硶(non-Javadoc)
     * @see Event.Hitable#showAttack()
     */
    @Override
    public Blast showAttack() {
        // TODO Auto-generated method stub
        //鎵h
        blood-=new EnemyTank().power;
        return new Blast(this, true);
    }

    @Override
    public boolean isDestoryable() {
        // TODO Auto-generated method stub
        return blood<=0;
    }

    @Override
    public void draw() {
        // TODO Auto-generated method stub
        String res="";
        switch (direction) {
        case UP:
            res = "res/img/tank_u.gif";
            break;
        case DOWN:
            res = "res/img/tank_d.gif";
            break;
        case LEFT:
            res = "res/img/tank_l.gif";
            break;
        case RIGHT:
            res = "res/img/tank_r.gif";
            break;
        default:
            break;
        }
        try {
            DrawUtils.draw(res, x, y);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /*
     * 鍧﹀厠鐨勭Щ鍔?     */
    public void move(Direction direction) {
        //濡傛灉浼犺繃鏉ョ殑鏂瑰悜鍜屽潶鍏嬩笉鑳藉姩鐨勬柟鍚戜竴鑷达紝鍒欑Щ鍔ㄦ渶灏?/span>
        if(badDirection==direction) {
            //绉诲姩鏈€灏忛棿璺?/span>
            switch (direction) {
            case UP:
                y -= badSpeed;
                break;
            case DOWN:
                y += badSpeed;
                break;
            case LEFT:
                x -= badSpeed;
                break;
            case RIGHT:
                x += badSpeed;
                break;
            default:
                break;
            }
            return ;
        }
        
        //鍒锋柊鏂瑰悜,鐢ㄦ埛鎸変笅鐨勬柟鍚戜紶缁欏潶鍏嬶細鍧﹀厠鐨勬柟鍚?/span>
        if(this.direction!=direction)
        {
            this.direction=direction;
            return ;
        }
        
        switch (direction) {
        case UP:
            y -= speed;
            break;
        case DOWN:
            y += speed;
            break;
        case LEFT:
            x -= speed;
            break;
        case RIGHT:
            x += speed;
            break;
        default:
            break;
        }
        
        /*
         * 杈圭晫澶勭悊
         */
        if(x<0) {x=0;}
        if(x>config.WIDTH-64) {x=config.WIDTH-64;}
        if(y<0) {y=0;}
        if(y>config.HIGHT-64) {y=config.HIGHT-64;}
    }
    
}
View Code

package domain;

import java.io.IOException;

import org.itheima.game.utils.DrawUtils;

import Event.Blockable;
import Event.Destoryable;
import Event.Hitable;

public class Steel extends Element implements Blockable, Hitable, Destoryable{
    /*
     * 灞炴€э細
     */
    //1.琛€閲?/span>
    private int blood=5;
    
    public Steel(int x, int y) {
        super(x, y);
        
        //鑾峰彇閾佸鐨勫楂?/span>
        try {
            int size[]=DrawUtils.getSize("res/img/steel.gif");
            width=size[0];
            hight=size[1];
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    

    

    @Override
    public void draw() {
        // TODO Auto-generated method stub
        try {
            DrawUtils.draw("res/img/steel.gif", x, y);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /*
     * 鎸ㄦ墦鍚庣殑鍙嶆槧(non-Javadoc)
     * @see Event.Hitable#showAttack()
     */
    @Override
    public Blast showAttack() {
        // TODO Auto-generated method stub
        blood--;
        return new Blast(this,true);
    }
    
    /*
     * 閿€姣佹椂鍝嶅簲鐨勫姩浣?non-Javadoc)
     * @see Event.Destoryable#showDestory()
     */
    @Override
    public Blast showDestory() {
        // TODO Auto-generated method stub
        return new Blast(this);
    }
    
    
    /*
     * 閿€姣侀搧澧?non-Javadoc)
     * @see Event.Destoryable#isDestoryable()
     */
    @Override
    public boolean isDestoryable() {
        // TODO Auto-generated method stub
        return blood<=0;
    }
}
package domain;

import java.io.IOException;

import org.itheima.game.utils.DrawUtils;

import Event.Blockable;
import Event.Destoryable;
import Event.Hitable;

public class Wall extends Element implements Hitable, Blockable, Destoryable{

    //琛€閲?/span>
    private int blood=3;
    
    /*
     * 鍚弬鏁扮殑鏋勯€犲嚱鏁?     */
    public Wall(int x, int y) {
        // TODO Auto-generated constructor stub
        super(x, y);
        
        //鑾峰彇瀹藉害锛岄暱搴?/span>
        try {
            int size[]=DrawUtils.getSize("res/img/wall.gif");
            width=size[0];
            hight=size[1];
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    /*
     *缁樺埗
     * @see domain.Element#draw()
     */
    @Override
    public void draw() {
        try {
            DrawUtils.draw("res/img/wall.gif", x, y);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /*
     * 鎸ㄦ墦
     * Blast鐖嗙偢绫?     */
    public Blast showAttack() {
        blood--;        //鎸ㄦ墦涓€娆¤閲忓噺灏?/span>
        return new Blast(this, true);
    }
    
    /*
     * 閿€姣佸垽鏂?琛€閲忓皬浜庣瓑浜?鏃讹紝灏遍攢姣?     */
    public boolean isDestoryable() {return blood<=0;}
    
    /*
     * 閿€姣佸搷搴斿姩浣?     */
    public Blast showDestory() {return new Blast(this);}

}
package domain;

import java.io.IOException;

import org.itheima.game.utils.DrawUtils;

import Event.Blockable;

public class Water extends Element implements Blockable{

    public Water(int x, int y) {
        super(x, y);
        try {
            int size[]=DrawUtils.getSize("res/img/water.gif");
            width=size[0];    hight=size[1];
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    @Override
    public void draw() {
        try {
            DrawUtils.draw("res/img/water.gif", x, y);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
package Grame;

public class APP {
        /*
         * 娓告垙鐨勪富鍏ュ彛锛屾墍鏈変唬鐮佺殑涓诲叆鍙?         */
    public static void main(String[] args) {
        //缁樺埗绐楀彛
        MyWindows gr=new MyWindows(config.TITLE, config.WIDTH, config.HIGHT, config.PFS);
        
        //寮€濮嬫父鎴?/span>
        gr.start();
        
    }
    
}

package Grame;

public interface config {
    String TITLE="鍧﹀厠澶ф垬";
    int WIDTH=64*15;
    int HIGHT=64*10;
    int PFS=50;
}
package Grame;

import java.awt.HeadlessException;
import java.awt.Window;
import java.io.IOException;
import java.nio.file.Watchable;
import java.util.Comparator;
import java.util.concurrent.CopyOnWriteArrayList;

import org.itheima.game.utils.DrawUtils;
import org.itheima.game.utils.SoundUtils;
import org.lwjgl.input.Keyboard;
import org.omg.PortableServer.ID_ASSIGNMENT_POLICY_ID;

import Event.Attackable;
import Event.Blockable;
import Event.Destoryable;
import Event.Hitable;
import Event.Moveable;
import domain.Blast;
import domain.Bullet;
import domain.Direction;
import domain.Element;
import domain.EnemyTank;
import domain.Grass;
import domain.Mytank;
import domain.Steel;
import domain.Tank;
import domain.Wall;
import domain.Water;


/*
 * 娓告垙绐楀彛绫? * 
 */
public class MyWindows extends org.itheima.game.Window{

    //瀹氫箟锛氶泦鍚堬紝瀛樺偍鍏冪礌
            //CopyOnWeiteArrayList 鍙互瑙e喅骞跺彂淇敼寮傚父鐨勯泦鍚?/span>
            CopyOnWriteArrayList<Element>list=new CopyOnWriteArrayList<>();
            //鍒涘缓鍧﹀厠瀵硅薄
            Mytank mt;
            //鍒涘缓鏁屾柟鍧﹀厠
            EnemyTank et1;
            EnemyTank et2;
    
    public MyWindows(String title, int width, int height, int fps) {super(title, width, height, fps);}

    /*
     * 缁樺埗绐楀彛鏃惰繍琛?     */
    @Override
    protected void onCreate() {
        //鎾斁寮€濮嬮煶涔?/span>
        try {
            SoundUtils.play("res/snd/start.wav");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        //鍒涘缓鐮栧
        for(int i=0;i<config.WIDTH/64-1;++i) {
            Wall wall=new Wall(i*64, 64);
            addElement(wall);
        }
        //鍒涘缓姘村
        for(int i=1;i<config.WIDTH/64;++i) {
            Water water=new Water(i*64, 64*3);
            addElement(water);
        }
        //缁樺埗閾佸
        for(int i=0;i<config.WIDTH/64-1;++i) {
            Steel steel=new Steel(i*64, 64*5);
            addElement(steel);
        }
        //鍒涢€犺崏鍧?/span>
        for(int i=1;i<config.WIDTH/64;++i) {
            Grass grass=new Grass(i*64, 64*7);
            addElement(grass);
        }
        
        
        //鍒涢€犲繁鏂瑰潶鍏?/span>
        mt=new Mytank(config.WIDTH/2-32, config.HIGHT-64);
        addElement(mt);
        //鍒涘缓鏁屾柟鍧﹀厠
        et1=new EnemyTank(0, 0);
        et2=new EnemyTank(config.WIDTH-64, 0);
        addElement(et1);
        addElement(et2);
    }

    //榧犳爣浜嬩欢
    @Override
    protected void onMouseEvent(int key, int x, int y) {
        // TODO Auto-generated method stub
        
    }

    //閿洏浜嬩欢
    @Override
    protected void onKeyEvent(int key) {
        // TODO Auto-generated method stub
        switch (key) {
        case Keyboard.KEY_UP:
            mt.move(Direction.UP);
            break;
        case Keyboard.KEY_DOWN:
            mt.move(Direction.DOWN);
            break;
        case Keyboard.KEY_LEFT:
            mt.move(Direction.LEFT);
            break;
        case Keyboard.KEY_RIGHT:
            mt.move(Direction.RIGHT);
            break;
        case Keyboard.KEY_SPACE:    //鍥炶溅閿?/span>
            Bullet shot=mt.shot();        //鍙戝皠瀛愬脊
            //瀵瑰瓙寮硅繘琛岄潪绌烘牎楠?/span>
            if(shot!=null) {
                addElement(shot);
            }
            break;
        default:
            break;
        }
    }

    //浜嬪疄鍒锋柊
    @Override
    protected void onDisplayUpdate() {
        // TODO Auto-generated method stub
        //濡傛灉宸辨柟鍧﹀厠宸茬粡閿€姣侊紝鎴栬€呮晫鏂瑰潶鍏嬮兘閿€姣侊紝鍒欐父鎴忕粨鏉?/span>
        if(mt.isDestory()||(et1.isDestory()&&et2.isDestory())) {
            list.clear();
            //缁樺埗缁撴潫鍥剧墖
            try {
                DrawUtils.draw("res/img/gameover.gif", (config.WIDTH-96)/2, (config.HIGHT-96)/2);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            //鍋滄鎵€鏈夐煶涔?/span>
            SoundUtils.stop("res/snd/fire.wav");
            
            return;
        }
        
        //缁樺埗鍏冪礌
        for(Element e:list) {
            e.draw();
        }
        
        //璋冪敤鏁屾柟鍧﹀厠鐨勯殢鏈虹Щ鍔ㄧ殑鍔熻兘
        for(Element e:list) {
            if(e instanceof EnemyTank) {
                //濡傛灉鏄晫鏂瑰潶鍏嬶紝灏遍殢鏈虹Щ鍔?/span>
                ((EnemyTank)e).move();
                
                //濡傛灉鏄晫鏂瑰潶鍏嬶紝灏辫皟鐢ㄥ叾鍙戝皠瀛愬脊鐨勮兘鍔?/span>
                Bullet shot=((EnemyTank)e).shot();
                if(shot!=null) {
                    addElement(shot);
                }
            }
        }
        
        //閿€姣佸嚭鐣屽瓙寮?/span>
        for(Element e:list) {
            //濡傛灉鏄瓙寮?/span>
            if(e instanceof Bullet) {
                //鍒ゆ柇瀛愬脊鏄惁鍑虹晫
                boolean flag=((Bullet)e).isDestoryable();
                if(flag) {
                    list.remove(e);
                }
            }
        }
        
        //閿€姣佹墍鏈夐渶瑕侀攢姣佺殑瀹炵墿
        for(Element e:list) {
            //鍒ゆ柇浜嬬墿鏄惁闇€瑕侀攢姣?/span>
            if( e instanceof Destoryable) {
            boolean blast=((Destoryable)e).isDestoryable();
            if(blast) {
                //璇存槑浜嬬墿闇€瑕侀攢姣?                //1.缁樺埗閿€姣佹椂鐨勭垎鐐哥墿
                Blast blast2=((Destoryable)e).showDestory();
                if(blast2!=null) {
                    addElement(blast2);
                }
                
                //娑堥櫎璇ヤ簨鐗?/span>
                list.remove(e);
                }
            }
        }
        
        /*
         * 鏍¢獙锛氳繍鍔ㄧ墿浣?涓?闅滅鐗?鐨勭鎾?         */
        for(Element e1:list) {
            for(Element e2:list) {
                if(e1!=e2&&e1 instanceof Moveable&&e2 instanceof Blockable) {
                    //璇存槑 e1杩愬姩鐗? 涓?e2 闅滅鐗?
                    boolean flag=((Moveable)e1).checkHit((Blockable)e2);
                    if(flag) {        //璇存槑纰版挒浜?/span>
                        break;
                    }
                }
            }
        }
        
        /*
         * 鏀诲嚮鑰?涓?琚敾鍑昏€?鏄惁纰版挒
         */
        for(Element e1: list) {
            for(Element e2: list) {
                if(e1 instanceof Attackable&&e2 instanceof Hitable) {
                    //璇存槑  e1 鏀诲嚮鑰?e2 琚敾鍑昏€?/span>
                    boolean flag=((Attackable)e1).checkAttack((Hitable)e2);
                    if(flag) {
                        //鏍¢獙锛氭槸宸辨柟鍧﹀厠鍙戣繃鏉ョ殑瀛愬脊锛屾槸鍒欎笉鑳芥敾鍑伙紝鍚﹀垯鑳戒骇鐢熸敾鍑?/span>
                        if(((Bullet)e1).getTank().getClass()==e2.getClass()) {
                            continue;
                        }
                        
                        //鏍¢獙锛氳兘涓嶈兘鏀诲嚮鏁屽啗
                        //瀛愬脊閿€姣?/span>
                        list.remove(e1);
                        //琚敾鍑昏€呭彈鍒版敾鍑伙紝缁樺埗鍑虹垎鐐哥墿
                        Blast blast=((Hitable)e2).showAttack();
                        addElement(blast);
                    }
                }
            }
        }
        
    }

    
    
    /*
     * 娣诲姞鍏冪礌
     */
    public void addElement(Element e) {
        list.add(e);
        
        //瀵归泦鍚堟寜鐓ф覆鏌撶骇鍒繘琛屾帓搴?/span>
        list.sort(new Comparator<Element>() {

            @Override
            public int compare(Element o1, Element o2) {
                // TODO Auto-generated method stub
                return o1.getOrder()-o2.getOrder();
            }
        });
    }

    
}

 



 


以上是关于锛坖ava椤圭洰锛夊潶鍏嬪ぇ鎴?2.0的主要内容,如果未能解决你的问题,请参考以下文章

甯︿綘浜嗚В椤圭洰绠$悊 | 椤圭洰绠$悊鍩虹鐭ヨ瘑

TiKV 鎴愪负 CNCF 鎵樼椤圭洰鈥斺€旀灦鏋勫師鐞嗕簡瑙d竴涓嬶紵| Meetup 棰勫憡

娴呰皥 ES 妯″潡鍜?Webpack Tree-shaking

瀹夋帓锛岃胺绮掑晢鍩巎ava鍒嗗竷寮忓紑鍙戝熀纭€绡囬珮绾х瘒涓庨珮鍙敤闆嗙兢鏋舵瀯绡?020

Centos7 + nginx 鎵樼 Django 椤圭洰