关于使用JAVA实现坦克大战练习后的感想

  1. 首先,基础!基础!基础!用的全部都是基础的知识,只是连接了起来!
  2. 然后,思想!思想!思想!编程的思想一定要锻炼出来!
  3. 再就是,细节!细节!细节!很多语句及各种类/接口/枚举等使用上的细节务必注意!!!

     只是做了一个Demo,因为生疏了,花了一天时间,想想就很气,尤其气自己平时的小聪明.

The specification of Class/Interface  and method
Class Father:Element Son:SteelWall,WaterWal,Wall,GrassWall,Tank(MyTank,EnemyTank),Blast,Bullet
Interface Config,Moveable,Hitable,Destroyable,Blockable,Attackable
Enum Direction,(Keyboard ->UP,DOWN,RIGHT,LEFT)
Package libs\jar ,
// The method of using important

public Window(String title, int width, int height, int fps);
public final void start();
protected abstract void onCreate();
protected abstract void onKeyEvent(int key);
protected abstract void onDisplayUpdate()
public boolean checkHit(Blockable blockable);
public abstract boolean checkAttack(Hitable hitable);
public abstract boolean isDestroy();
public abstract Blast showAttack();

 

//Window子类

package com.itheima.game;

import com.itheima.game.business.*;
import com.itheima.game.Inter.Config;
import com.itheima.game.Inter.Direction;
import com.itheima.game.domain.*;
import org.itheima.game.DrawUtils;
import org.itheima.game.SoundUtils;
import org.itheima.game.Window;
import org.lwjgl.input.Keyboard;

import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.concurrent.CopyOnWriteArrayList;

public class GameWindow extends Window {

    MyTank mt;
    EnemyTank et1;
    EnemyTank et2;

    CopyOnWriteArrayList cwlist = new CopyOnWriteArrayList<>();

    public GameWindow(String title, int width, int height, int fps) {
        super(title, width, height, fps);
    }

    @Override
    protected void onCreate() {
        for (int i = 1; i < Config.WIDTH / 64; i++) {
            if (i != 12) {
                Wall wall = new Wall(i * 64, 64 * 5);
                cwlist.add(wall);
            }
        }
        for (int i = 1; i < Config.WIDTH / 64; i++) {
            Water water = new Water(i * 64, 64 * 3);
            cwlist.add(water);
        }
        for (int i = 0; i < Config.WIDTH / 64 - 1; i++) {
            if (i != 6) {
                Steel steel = new Steel(i * 64, 64 * 1);
                cwlist.add(steel);
            }
        }
        for (int i = 1; i < Config.WIDTH / 64; i++) {
            Grass grass = new Grass(i * 64, 64 * 7);
            cwlist.add(grass);
        }
        mt = new MyTank(Config.WIDTH / 2 - 32, Config.HEIGHT - 64);
        cwlist.add(mt);
        et1 = new EnemyTank(0, 0);
        et2 = new EnemyTank(Config.WIDTH - 64, 0);
        cwlist.add(et1);
        cwlist.add(et2);


        Collections.sort(cwlist, new Comparator() {
            @Override
            public int compare(Element o1, Element o2) {
                return o1.getOrder() - o2.getOrder();
            }
        });

        try {
            SoundUtils.play("res\\snd\\start.wav");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onMouseEvent(int key, int x, int y) {

    }

    @Override
    protected void onKeyEvent(int key) {
        if (key == Keyboard.KEY_UP) {
            mt.move(Direction.UP);
        } else if (key == Keyboard.KEY_DOWN) {
            mt.move(Direction.DOWN);
        } else if (key == Keyboard.KEY_RIGHT) {
            mt.move(Direction.RIGHT);
        } else if (key == Keyboard.KEY_LEFT) {
            mt.move(Direction.LEFT);
        } else if (key == Keyboard.KEY_SPACE) {
            Bullet bt = mt.shot();
            if (bt != null) {
                cwlist.add(bt);
            }
        }
    }


    @Override
    protected void onDisplayUpdate() {

        if(mt.getBlood() == 0){
            cwlist.clear();
            try {
                DrawUtils.draw("G:\\照片\\timg.gif",Config.WIDTH/4,Config.HEIGHT/4-32);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        for (Element e1 : cwlist) {
            if (e1 instanceof Moveable) {
                for (Element e2 : cwlist) {
                    if (e2 instanceof Blockable) {
                        if (e1 == e2) {
                            continue;
                        }
                        Moveable moveable = (Moveable) e1;
                        Blockable blockable = (Blockable) e2;
                        boolean r = moveable.checkHit(blockable);
                        if (r) {
                            System.out.println(" there is collsion happening");
                            break;
                        }
                    }
                }
            }
        }

        for (Element e : cwlist) {
            if (e instanceof Attackable) {
                for (Element e1 : cwlist) {
                    if (e1 instanceof Hitable) {
                        if (e == e1) {
                            continue;
                        }
                        Attackable attackable = (Attackable) e;
                        Hitable hitable = (Hitable) e1;
                        if (attackable instanceof Bullet) {
                            Bullet bt = (Bullet) attackable;
                            if (bt.tank.getClass() == hitable.getClass()) {
                                continue;
                            }
                            if(hitable instanceof Bullet){
                                Bullet bt1 = (Bullet)hitable;
                                if(bt.tank.getClass() == bt1.tank.getClass()){
                                    continue;
                                }
                            }

                            System.out.println("need to juge class");
                        }
                        boolean res1 = attackable.checkAttack(hitable);
                        if (res1) {
                            System.out.println("bullet needs disppear");
                            cwlist.remove(e);
                            Blast blast = hitable.showAttack();
                            cwlist.add(blast);
                            System.out.println("blast is needed");
                            break;
                        }
                    }
                }
            }
        }

        System.out.println(cwlist.size());

        for (Element e : cwlist) {
            if (e instanceof Destroyable) {
                Destroyable destroyable = (Destroyable) e;
                boolean res = destroyable.isDestroy();
                if (res) {
                    cwlist.remove(e);
                    System.out.println("remove element that instanceof not need");
                }
            }
        }

        for (Element e : cwlist) {
            if (e instanceof Bullet) {
                Bullet bt = (Bullet) e;
                bt.move();
                System.out.println("bullet moves");
            }

            if (e instanceof EnemyTank) {
                EnemyTank et = (EnemyTank) e;
                et.move();
                Bullet bt = et.shot();
                if (bt != null) {
                    cwlist.add(bt);
                }
            }
            e.draw();
        }

    }
}

 

 

//画面中各种墙及坦克,子弹等的父类

package com.itheima.game.domain;

import org.itheima.game.DrawUtils;

import java.io.IOException;

public abstract class Element {

    protected int x;
    protected int y;
    protected int width;
    protected int height;


    public int getOrder() {
        return 0;
    }


    public Element() {
    }


    public Element(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public abstract void draw();
}

 


//我方坦克类

package com.itheima.game.domain;

import com.itheima.game.business.*;
import com.itheima.game.Inter.Config;
import com.itheima.game.Inter.Direction;
import org.itheima.game.CollsionUtils;
import org.itheima.game.DrawUtils;

import java.io.IOException;

public class MyTank extends Tank implements Moveable, Blockable,Destroyable,Hitable {
    private int blood = 12;
    public Direction badDirection;
    public int badspeed;
    public long lastShotTime;
    protected int speed = 32;

    public int getBlood() {
        return blood;
    }

    public MyTank() {
    }

    public String path1 = "res\\img\\tank_u.gif";
    public String path2 = "res\\img\\tank_d.gif";
    public String path3 = "res\\img\\tank_r.gif";
    public String path4 = "res\\img\\tank_l.gif";

    public MyTank(int x, int y) {
        this.x = x;
        this.y = y;

        try {
            int[] arr = DrawUtils.getSize(path2);
            this.width = arr[0];
            this.height = arr[1];
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void draw() {
        String path = "";
        switch (direction) {
            case UP:
                path = path1;
                break;
            case DOWN:
                path = path2;
                break;
            case RIGHT:
                path = path3;
                break;
            case LEFT:
                path = path4;
                break;
        }

        try {
            DrawUtils.draw(path, x, y);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void move(Direction direction) {
        if (direction == badDirection) {
            switch (direction) {
                case UP:
                    y -= badspeed;
                    break;
                case DOWN:
                    y += badspeed;
                    break;
                case RIGHT:
                    x += badspeed;
                    break;
                case LEFT:
                    x -= badspeed;
                    break;
            }
            return;
        }
        //如果坦克当时方向和键盘控制的方向不一致,就将坦克方向按照键盘方向更改,但坐标不更改!!!
        if (this.direction != direction) {
            this.direction = direction;
            return;
        }
        //this.direction = direction;//注意,此处需要把坦克移动的方向记录下来,以便在draw方法中根据坦克移动方向来判断坦克的绘制图片路径
        switch (direction) {
            case UP:
                y -= speed;
                break;
            case DOWN:
                y += speed;
                break;
            case RIGHT:
                x += speed;
                break;
            case LEFT:
                x -= speed;
                break;
        }
        if (x < 0) {
            x = 0;
        }
        if (y < 0) {
            y = 0;
        }
        if (x > Config.WIDTH - 64) {
            x = Config.WIDTH - 64;
        }
        if (y > Config.HEIGHT - 64) {
            y = Config.HEIGHT - 64;
        }
    }


    public Bullet shot() {
        long nowTime = System.currentTimeMillis();
        if (nowTime - lastShotTime < 300) {
            return null;
        } else {
            lastShotTime = nowTime;
            return new Bullet(this);
        }
    }


    @Override
    public boolean checkHit(Blockable blockable) {

        Element e = (Element) blockable;

        int x1 = this.x;
        int y1 = this.y;
        int h1 = this.height;
        int w1 = this.width;

        switch (direction) {
            case UP:
                y1 -= speed;
                break;
            case DOWN:
                y1 += speed;
                break;
            case RIGHT:
                x1 += speed;
                break;
            case LEFT:
                x1 -= speed;
                break;
        }
        int x2 = e.x;
        int y2 = e.y;
        int h2 = e.height;
        int w2 = e.width;

        boolean flag = CollsionUtils.isCollsionWithRect(x1, y1, h1, w1, x2, y2, w2, h2);

        if (flag) {
            switch (direction) {
                case UP:
                    badspeed = this.y - y2 - h2;
                    break;
                case DOWN:
                    badspeed = y2 - this.y - this.height;
                    break;
                case LEFT:
                    badspeed = this.x - x2 - w2;
                    break;
                case RIGHT:
                    badspeed = x2 - this.x - this.width;
                    break;
            }
            this.badDirection = direction;
            System.out.println("不可移动");
        } else {
            badDirection = null;
        }
        return flag;
    }

    @Override
    public boolean isDestroy() {
        return blood > 0 ? false : true;
    }

    @Override
    public Blast showAttack() {
        blood--;
        return new Blast(x, y, width, height, blood);
    }
}

 

//实现爆炸效果的类,因为这个涉及细节比较多,因此也给放上来

package com.itheima.game.domain;

import com.itheima.game.business.Blockable;
import com.itheima.game.business.Destroyable;
import com.itheima.game.business.Hitable;
import org.itheima.game.DrawUtils;
import org.itheima.game.SoundUtils;

import java.io.IOException;

public class Blast extends Element implements Destroyable {


    String[] arrstr = {"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"};
    int index = 0;

    int len = arrstr.length;

    boolean flag;

    public Blast(int x, int y, int w, int h, int boold) {

        try {
            int[] arr = DrawUtils.getSize(arrstr[index]);
            this.width = arr[0];
            this.height = arr[1];
        } catch (IOException e) {
            e.printStackTrace();
        }

        this.x = x - (this.width - w) / 2;
        this.y = y - (this.height - h) / 2;

        if (boold > 4) {
            len = 2;
        } else if (boold > 3) {
            len = 3;
        } else if (boold > 2) {
            len = 5;
        } else if (boold == 1) {
            len = 8;
        }
            try {
                SoundUtils.play("res\\snd\\blast.wav");
            } catch (IOException e) {
                e.printStackTrace();
            }

    }

    public void draw() {
        if (index > 8) {
            index = 0;
            flag = true;
        }
        try {
            DrawUtils.draw(arrstr[len - 1], x, y);
        } catch (IOException e) {
            e.printStackTrace();
        }
        index++;
    }

    @Override
    public boolean isDestroy() {
        return flag;
    }
}

 

你可能感兴趣的:(坦,JAVA,坦克大,Dem)