坦克大战0.8

在这里插入图片描述

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.lang.invoke.SwitchPoint;

/**
 * @author 许泽鑫
 * @data 2019/11/10 - 11:08
 */
public class Tank   {
    private static final int TANK_SPEED = 5;
    boolean bu = false,bd = false,bl = false,br = false;
    enum Direction {u,d,l,r,lu,ld,ru,rd,stop};
    private Direction dir = Direction.stop;

    int x;
    int y;

    public void move(){
        switch (dir){
            case u: y -= TANK_SPEED;
                break;
            case d: y += TANK_SPEED;
                break;
            case l: x -= TANK_SPEED;
                break;
            case r: x += TANK_SPEED;
                break;
            case lu: x -= TANK_SPEED;y -= TANK_SPEED;
                break;
            case ru: y -= TANK_SPEED; x += TANK_SPEED;
                break;
            case ld: y += TANK_SPEED; x -= TANK_SPEED;
                break;
            case rd: x += TANK_SPEED; y += TANK_SPEED;
                break;
            case stop:
                break;

        }
    }

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


    }


    public void draw(Graphics g) {


        Color c = g.getColor();
        g.setColor(Color.green);

        g.setColor(Color.RED);
        g.fillOval(x, y, 30, 30);
        g.setColor(c);
        move();

    }
    public void tankRun(KeyEvent e){
        int key = e.getKeyCode();
        switch (key){
            case KeyEvent.VK_RIGHT: br = true;
                break;
            case KeyEvent.VK_LEFT: bl = true;
                break;
            case KeyEvent.VK_UP: bu = true;
                break;
            case KeyEvent.VK_DOWN: bd = true;
                break;
        }

    locateDirection();


    }
    public void locateDirection(){
        if(!br && !bl && !bd && bu){
            dir = Direction.u;

        }
        else if(br && !bl && !bd && !bu){
            dir = Direction.r;

        }
        else if(!br && bl && !bd && !bu){
            dir = Direction.l;

        }
        else if(!br && !bl && bd && !bu){
            dir = Direction.d;

        }
        else if(br && !bl && bd && !bu){
            dir = Direction.rd;

        }
        else if(!br && bl && !bd && !bu){
            dir = Direction.ld;

        }
        else if(br && !bl && !bd && bu){
            dir = Direction.ru;

        }
        else if(!br && bl && !bd && bu){
            dir = Direction.lu;

        }
        else  if(!br && !bl && !bd && !bu){
            dir = Direction.stop;
        }
    }
}

出现问题: bl br bu bd 赋值为true后没有还原;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值