01Java基础制作坦克大战游戏

作者: singworld 分类: Java 发布时间: 2019-07-10 22:08

先上第一天运行效果图

QQ20190710-190048-HD.gif
第一天知识让方块动起来,后面会逐步完善

实现步骤

建立新项目 Tank2019V2 .

创建一个窗口

显示这个窗口

画出一个方块

谁初始化了Graphics g这个参数

移动这个方块

自动化,repaint – paint

手动控制:响应键盘事件

Frame.addKeyListener() -> Observer

为什么使用内部类?

不需要让别的类访问键盘监听类
高内聚 低耦合

为什么不使用方法的内部类(局部内部类)

可以用,看起来不方便
内部类可以非常方便访问包装类的局部变量

为什么用KeyAdapter而不是直接实现KeyListener

最基础的面向对象的设计思想

抽象出名词:类, 属性

抽象出动词:方法

为什么Enum 比 int类型好?

编译期间就能知道赋值是不是有问题

STOP作为单独的方向不合适

怎么做?(作业)
项目来源,马士兵坦克大战 手打实现

代码实现

public enum Dir {
    L,U,R,D,STOP
}

import java.awt.*;
import java.awt.event.KeyEvent;

public class Tank {
    private int x;
    private int y;
    private Dir dir;
    private boolean bL,bU,bR,bD;
    public static final int SPEED = 5;

    public Tank(int x, int y, Dir dir) {
        this.x = x;
        this.y = y;
        this.dir = dir;
    }

    public void paint(Graphics g) {
        g.fillRect(x,y,50,50);
        move();
    }

    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();
        switch (key){
            case KeyEvent.VK_LEFT:
                bL = true;
                break;
            case KeyEvent.VK_UP:
                bU = true;
                break;
            case KeyEvent.VK_RIGHT:
                bR = true;
                break;
            case KeyEvent.VK_DOWN:
                bD = true;
                break;
        }

        setMainDir();

    }

    private void move() {
        switch (dir){
            case L:
                x -= SPEED;
                break;
            case U:
                y -= SPEED;
                break;
            case R:
                x += SPEED;
                break;
            case D:
                y += SPEED;
                break;
        }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key){
            case KeyEvent.VK_LEFT:
                bL = false;
                break;
            case KeyEvent.VK_UP:
                bU = false;
                break;
            case KeyEvent.VK_RIGHT:
                bR = false;
                break;
            case KeyEvent.VK_DOWN:
                bD = false;
                break;
        }

        setMainDir();

    }

    private void setMainDir() {
        if(!bL && !bU && !bR && !bD)
            dir = Dir.STOP;
        if(bL && !bU && !bR && !bD)
            dir = Dir.L;
        if(!bL && bU && !bR && !bD)
            dir = Dir.U;
        if(!bL && !bU && bR && !bD)
            dir = Dir.R;
        if(!bL && !bU && !bR && bD)
            dir = Dir.D;

    }
}

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class TankFrame extends Frame {
    private Tank myTank;
    private Tank enemy;

    public TankFrame(){
        this.setTitle("tank war");
        this.setLocation(400,100);
        this.setSize(800,600);
        this.addKeyListener(new TankKeyListener());

        myTank = new Tank(100,100, Dir.R);
        enemy = new Tank(200,200, Dir.D);
    }

    @Override
    public void paint(Graphics g) {
        myTank.paint(g);
        enemy.paint(g);
    }

    private class TankKeyListener extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {

            myTank.keyPressed(e);

        }

        @Override
        public void keyReleased(KeyEvent e) {
            myTank.keyReleased(e);

        }
    }
}

public class Main {

    public static void main(String[] args) {
        TankFrame tf = new TankFrame();
        tf.setVisible(true);

        for (;;){
            try {
                Thread.sleep(25);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            tf.repaint();
        }

    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *