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

让坦克移动起来

注意,创建敌人坦克的时候,需要注意用什么存储??
数组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
    }

}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值