From 941e07407c507e92ec7bb65749a9b326ae48ec20 Mon Sep 17 00:00:00 2001 From: bingor_yhj Date: Thu, 29 Sep 2022 21:47:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=8C=89=E9=94=AE=E6=9D=A5?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E5=9D=A6=E5=85=8B=EF=BC=8C=E5=B9=B6=E4=B8=94?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=90=8C=E6=97=B6=E6=8C=89=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=96=B9=E5=90=91=E9=94=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/msb/DirEnum.java | 11 +++++++ src/com/msb/TankFrame.java | 63 +++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 35 deletions(-) create mode 100644 src/com/msb/DirEnum.java diff --git a/src/com/msb/DirEnum.java b/src/com/msb/DirEnum.java new file mode 100644 index 0000000..a6131de --- /dev/null +++ b/src/com/msb/DirEnum.java @@ -0,0 +1,11 @@ +package com.msb; + +/** + * @Author bingor + * @Date 2022-09-29 21:17 + * @Description: 方向枚举 + * @Version: 1.0 + */ +public enum DirEnum { + LEFT, RIGHT, UP, DOWN +} diff --git a/src/com/msb/TankFrame.java b/src/com/msb/TankFrame.java index 5ccbb4e..2381bfc 100644 --- a/src/com/msb/TankFrame.java +++ b/src/com/msb/TankFrame.java @@ -16,6 +16,8 @@ public class TankFrame extends Frame { private int x = 200; private int y = 200; + private DirEnum dir = DirEnum.RIGHT; + private static final int SPEED = 10; public TankFrame() { @@ -40,7 +42,14 @@ public class TankFrame extends Frame { public void paint(Graphics g) { super.paint(g); g.fillRect(x, y, 50, 50); - x += 10; + + switch (dir) { + case UP: y -= SPEED; break; + case DOWN: y += SPEED; break; + case LEFT: x -= SPEED; break; + case RIGHT: x += SPEED; break; + default: break; + } } //这里使用内部内的原因是,只有TankFrame用到 @@ -54,51 +63,35 @@ public class TankFrame extends Frame { @Override public void keyPressed(KeyEvent e) { super.keyPressed(e); - /*x += 10; - repaint();*/ switch (e.getKeyCode()) { - case KeyEvent.VK_LEFT : { - bL = true; - break; - } - case KeyEvent.VK_RIGHT : { - bR = true; - break; - } - case KeyEvent.VK_UP : { - bU = true; - break; - } - case KeyEvent.VK_DOWN : { - bD = true; - break; - } + case KeyEvent.VK_LEFT : bL = true; break; + case KeyEvent.VK_RIGHT : bR = true; break; + case KeyEvent.VK_UP : bU = true; break; + case KeyEvent.VK_DOWN : bD = true; break; default: break; } + setDir(); } @Override public void keyReleased(KeyEvent e) { super.keyReleased(e); switch (e.getKeyCode()) { - case KeyEvent.VK_LEFT : { - bL = false; - break; - } - case KeyEvent.VK_RIGHT : { - bR = false; - break; - } - case KeyEvent.VK_UP : { - bU = false; - break; - } - case KeyEvent.VK_DOWN : { - bD = false; - break; - } + case KeyEvent.VK_LEFT : bL = false; break; + case KeyEvent.VK_RIGHT : bR = false; break; + case KeyEvent.VK_UP : bU = false; break; + case KeyEvent.VK_DOWN : bD = false; break; default: break; } +// setDir(); //通过做了实验,现阶段就算不用这个方法,也不影响,但是按键抬起,坦克应该停止,如果还按照原来的方向移动就不对了 + } + + private void setDir() { + //为什么不用if-else 因为有可能是一起按两个方向键,那最后拿到哪个算哪个 + if(bL) dir = DirEnum.LEFT; + if(bR) dir = DirEnum.RIGHT; + if(bU) dir = DirEnum.UP; + if(bD) dir = DirEnum.DOWN; } }