当前位置:   article > 正文

JavaSE学习总结-坦克大战

JavaSE学习总结-坦克大战

马哥的淘宝店:https://shop592330910.taobao.com/

总结

观看马士兵老师讲课的坦克大战 视频的源代码。
学编程要亲自敲写代码,不要照抄代码,要跟着思路总结去写代码,就像你定义的类名,变量名等都可以不一样。

    在这几个方法里面遍历一个集合的时候,遍历的同时还要删除某个元素,这里要特别注意写法,要用迭代器。其他的方法会报错的。

        drawExplode(g);
        drawEnemyTanks(g);
        drawMissiles(g);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

坦克大战游戏的主类

package tank.war.client.com;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class TankClient extends Frame {
   
    private static final String title = "大战";

    private Tank tank = new Tank(50, 50, true, this);

    public ArrayList<Missile> missileList = new ArrayList<Missile>();
    public ArrayList<Explode> explodeList = new ArrayList<Explode>();
    public ArrayList<Tank> enemyTankList = new ArrayList<Tank>();
    public ArrayList<Wall> wallList = new ArrayList<Wall>();

    public Blood blood = new Blood();

    public static final int GAME_WIDTH = 800;
    public static final int GAME_HEIGHT = 600;

    private static final int x = 0;
    private static final int y = 0;

    private static final Color GAME_BACKGROUND = Color.lightGray;

    private Image offScreenImage = null;


    private void launchFrame() {
        createEnemyTanks(50);
        createWalls();

        this.setLocation(x, y);
        this.setSize(GAME_WIDTH, GAME_HEIGHT);
        this.setBackground(GAME_BACKGROUND);
        this.setTitle(title);
        this.setResizable(false);
        this.addWindowListener(new WindowsMonitor());
        this.addKeyListener(new KeyMonitor());
        setVisible(true);
        new Thread(new PaintThread()).start();
    }

    private void createWalls() {
        wallList.add(new Wall(100, 150, 20, 250, this));
        wallList.add(new Wall(300, 400, 300, 20, this));
    }

    private void createEnemyTanks(int count) {
        for (int i = 0; i <= count; i++) {
            Tank t = new Tank(50 + 40 * (i + 1), new Random().nextInt(GAME_HEIGHT), false, this);
            Iterator<Wall> wallIterator = wallList.iterator();
            while (wallIterator.hasNext()) {
                Wall wall = wallIterator.next();
                if (!tank.getRectangle().intersects(wall.getRectangle())) {
                    t.setGood(false);
                    t.setDirection(Tank.Direction.D);
                    this.enemyTankList.add(t);
                }
            }
        }
    }

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(GAME_BACKGROUND);
        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        drawMainTank(g);
        drawEnemyTanks(g);
        drawMissiles(g);
        drawExplode(g);
        drawString(g);
        drawWall(g);
        drawBlood(g);
    }

    private void drawMainTank(Graphics g) {
        tank.draw(g);
    }

    private void drawBlood(Graphics g) {
        if (blood.isLive()) {
            tank.eatBlood(blood);
            blood.draw(g);
        }
    }

    private void drawString(Graphics g) {
        g.drawString("Missile size: " + this.missileList.size(), 10, 50);
        g.drawString("Explode size: " + this.explodeList.size(), 10, 70);
        g.drawString("Tank    size: " + this.enemyTankList.size(), 10, 90);
        g.drawString("life        : " + this.tank.getLife(), 10, 110);
    }

    private void drawWall(Graphics g) {
        Iterator<Wall> wallIterator = wallList.iterator();
        while (wallIterator.hasNext()) {
            Wall wall = wallIterator.next();
            wall.draw(g);
        }
    }


    private void drawExplode(Graphics g) {
        Iterator<Explode> explodeIterator = explodeList.iterator();
        while (explodeIterator.hasNext()) {
            Explode explode = explodeIterator.next();
            if (!explode.isLive()) {
                explodeIterator.remove();
            } else {
                explode.draw(g);
            }
        }
    }

    private void drawMissiles(Graphics g) {
        Iterator<Missile> missileIterator = missileList.iterator();
        while (missileIterator.hasNext()) {
            Missile missile = missileIterator.next();

            if (!missile.isLive()) {
                missileIterator.remove();
            } else {
                missile.hitTanks(enemyTankList);
                missile.hitWalls(wallList);
                missile.hitTank(tank);
                missile.draw(g);
            }
        }
    }

    private void drawEnemyTanks(Graphics g) {
        if (enemyTankList.size() <= 10) {
            createEnemyTanks(20);
        }

        Iterator<Tank> tankIterator = enemyTankList.iterator();
        while (tankIterator.hasNext()) {
            Tank enemyTank = tankIterator.next();
            if (!enemyTank.isLive()) {
                tankIterator.remove();
            } else {
                enemyTank.hitWalls(wallList);
                enemyTank.hitTanks(enemyTankList);
                enemyTank.draw(g);
            }
        }
    }


    private class WindowsMonitor extends WindowAdapter {
   
        @Override
        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/326301?site
推荐阅读
相关标签
  

闽ICP备14008679号