韩顺平 java 第37讲 动起来的坦克

来源:互联网 发布:软件咨询服务公司 编辑:程序博客网 时间:2024/05/22 17:07

让坦克移动起来

注意,创建敌人坦克的时候,需要注意用什么存储??
数组VS集合?
首先,敌人的坦克会爆炸,用数组的时候不好控制敌人的坦克数量什么的,所以用集合。但是集合中要用ArrayList还是Vector?
我们知道后者是线程安全的,坦克后期肯定是多线程的,所以用Vector。

Draw.java

package com.chen;import java.awt.*;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.Vector;import javax.swing.*;public class Draw extends JFrame{    MyPanel mp;    public static void main(String[] args) {        Draw d = new Draw();    }    public Draw(){        mp = new MyPanel();        this.add(mp);        this.addKeyListener(mp);        this.setSize(800,600);        this.setLocation(200, 100);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        this.setVisible(true);    }}class MyPanel extends JPanel implements KeyListener{    Hero hero;    Vector<EnemyTank> ets = new Vector<EnemyTank>();    int ensize = 3;    public MyPanel(){        hero = new Hero(100, 200);        for(int i = 0;i < 3; ++i){            EnemyTank et = new EnemyTank(50 + i * 50, 0);            et.setColor(1);            et.setDirect(1);            ets.add(et);        }    }    public void paint(Graphics g){        super.paint(g);         g.fillRect(0, 0, 650, 600);        //画坦克        this.drawTank(hero.getX(), hero.getY(), g, hero.direct, 0);        for(int i = 0;i < ets.size(); ++i){            this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).getDirect(), 1);        }    }    public void drawTank(int x,int y,Graphics g,int direct,int type){        //判断是什么类型的坦克        switch(type){        case 0:            g.setColor(Color.cyan);            break;        case 1:            g.setColor(Color.yellow);            break;        }        //判断方向 0:上    1:下   2:左   3:右        //坦克规格:        //两个轮子:30*5        //中间车厢:10*20        //中间圆形:10*10        //炮筒:15        switch(direct){        case 0:            g.fill3DRect(x, y, 5, 30,false);            g.fill3DRect(x + 15, y, 5, 30,false);            g.fill3DRect(x + 5, y + 5, 10, 20,false);            g.fillOval(x + 5, y + 10, 10, 10);            g.drawLine(x + 10, y, x + 10, y + 15);              break;        case 1:            g.fill3DRect(x, y, 5, 30,false);            g.fill3DRect(x + 15, y, 5, 30,false);            g.fill3DRect(x + 5, y + 5, 10, 20,false);            g.fillOval(x + 5, y + 10, 10, 10);            g.drawLine(x + 10, y + 15, x + 10, y + 30);             break;        case 2:            g.fill3DRect(x, y, 30, 5,false);            g.fill3DRect(x , y+ 15, 30, 5,false);            g.fill3DRect(x + 5, y + 5, 20, 10,false);            g.fillOval(x + 10, y + 5, 10, 10);            g.drawLine(x, y + 10, x + 15, y + 10);              break;        case 3:            g.fill3DRect(x, y, 30, 5,false);            g.fill3DRect(x , y+ 15, 30, 5,false);            g.fill3DRect(x + 5, y + 5, 20, 10,false);            g.fillOval(x + 10, y + 5, 10, 10);            g.drawLine(x+ 15, y + 10, x + 30, y + 10);              break;        }    }    //asdw 控制方向    public void keyPressed(KeyEvent e) {        if(e.getKeyCode() == KeyEvent.VK_A){            hero.moveLeft();        }else if(e.getKeyCode() == KeyEvent.VK_S){            hero.moveDown();        }else if(e.getKeyCode() == KeyEvent.VK_D){            hero.moveRight();        }else if(e.getKeyCode() == KeyEvent.VK_W){            hero.moveUp();        }        this.repaint();    }    public void keyTyped(KeyEvent e) {    }    public void keyReleased(KeyEvent e) {    }}

Members.java

package com.chen;class Tank{    int x = 0,y = 0;//坦克的坐标    int direct = 0;//0:上    1:下   2:左   3:右    int speed = 5;    int color;    public int getSpeed() {        return speed;    }    public void setSpeed(int speed) {        this.speed = speed;    }    public int getColor() {        return color;    }    public void setColor(int color) {        this.color = color;    }    //0:上    1:下   2:左   3:右    public void moveUp(){        this.setY(this.getY() - speed);        this.setDirect(0);    }    public void moveDown(){        this.setY(this.getY() + speed);        this.setDirect(1);    }    public void moveLeft(){        this.setX(this.getX() - speed);        this.setDirect(2);    }    public void moveRight(){        this.setX(this.getX() + speed);        this.setDirect(3);    }    public int getDirect() {        return direct;    }    public void setDirect(int direct) {        this.direct = direct;    }    public int getX() {        return x;    }    public void setX(int x) {        this.x = x;    }    public int getY() {        return y;    }    public void setY(int y) {        this.y = y;    }    public Tank(int x,int y){        this.x = x;        this.y = y;    }}class Hero extends Tank{    public Hero(int x, int y) {        super(x, y);        }}class EnemyTank extends Tank{    public EnemyTank(int x, int y) {        super(x, y);        // TODO Auto-generated constructor stub    }}

这里写图片描述

0 0