怎么按方向键没用?
这是Tank的代码:
import java.awt.*;
import java.awt.event.*;
public class Tank {
int x;int y;
private static final int XSPEED = 5;
private static final int YSPEED = 5;
public boolean bL = false,bR = false,bU = false,bD = false;
enum Direction{L,R,U,D,LU,LD,RD,RU,STOP}
private Direction dir = Direction.STOP;
public Tank(int x, int y) {
this.x = x;
this.y = y;
} public void draw (Graphics g){
Color c = g.getColor();
g.setColor(Color.RED );
g.fillOval( x, y,30,30);
g.setColor(c);
}
public void move(){
switch(dir){
case R:
x += XSPEED;
case L:
x -= XSPEED;
case U:
y += YSPEED;
case D:
y -= YSPEED;
case RU:
x += XSPEED;
y += YSPEED;
case RD:
x += XSPEED;
y -= YSPEED;
case LD:
x -= XSPEED;
y -= YSPEED;
case LU:
x -= XSPEED;
y += YSPEED;
case STOP:
break;
}
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT:
bL = true;
break;
case KeyEvent.VK_RIGHT:
bR= true;
break;
case KeyEvent.VK_DOWN:
bD= true;
break;
case KeyEvent.VK_UP:
bU= true;
break;
}
locatDirection();
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_R:
bR = false;
case KeyEvent.VK_L:
bL = false;
case KeyEvent.VK_D:
bD = false;
case KeyEvent.VK_U:
bU = false;
}
locatDirection();
}
public void locatDirection(){
if(!bL && bR &&! bU && !bD) dir = Direction.R;
if(bL && !bR &&! bU && !bD) dir = Direction.L;
if(!bL && !bR && bU && !bD) dir = Direction.U;
if(!bL && !bR &&! bU && bD) dir = Direction.D;
if(!bL && bR && bU && !bD) dir = Direction.RU;
if(!bL && bR &&! bU && bD) dir = Direction.RD;
if(bL && !bR && bU && !bD) dir = Direction.LU;
if(bL && bR &&! bU && bD) dir = Direction.LD;
}
}
这是Tank01的代码:
import java.awt.*;
import java.awt.event.*;public class TankWor01 extends Frame {
public static final int GAME_WIDE = 800;
public static final int GAME_HIGHT=600;
Tank tk = new Tank(400,300);
Image offScreenImage = null;
public void paint(Graphics g){
tk.draw(g);
//y += 5;
}
public void update(Graphics g){
if(offScreenImage == null){
offScreenImage = this.createImage(GAME_WIDE, GAME_HIGHT);
}
Graphics offScreen = offScreenImage.getGraphics();
Color c = offScreen.getColor();
offScreen.setColor(Color.GREEN);
offScreen.fillRect(0,0,GAME_WIDE,GAME_HIGHT);
offScreen.setColor(c);
paint (offScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
public void TwFrame(){
this.setTitle("TankWar");
this.setBackground(Color.GREEN);
this.setSize(800,600);
this.setLocation(250,100);
this.setVisible(true);
this.setResizable(false);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
new Thread(new Mythread()).start();
this.addKeyListener(new KeyMonitor());
}
public static void main(String[] args) {
TankWor01 tw = new TankWor01();
tw.TwFrame();
}
private class Mythread implements Runnable{
public void run (){
while(true){
repaint();
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
public class KeyMonitor extends KeyAdapter{
public void keyPressed(KeyEvent e){
tk.keyPressed(e);
System.out.println("OK");
}
}}
这是Tank的代码:
import java.awt.*;
import java.awt.event.*;
public class Tank {
int x;int y;
private static final int XSPEED = 5;
private static final int YSPEED = 5;
public boolean bL = false,bR = false,bU = false,bD = false;
enum Direction{L,R,U,D,LU,LD,RD,RU,STOP}
private Direction dir = Direction.STOP;
public Tank(int x, int y) {
this.x = x;
this.y = y;
} public void draw (Graphics g){
Color c = g.getColor();
g.setColor(Color.RED );
g.fillOval( x, y,30,30);
g.setColor(c);
}
public void move(){
switch(dir){
case R:
x += XSPEED;
case L:
x -= XSPEED;
case U:
y += YSPEED;
case D:
y -= YSPEED;
case RU:
x += XSPEED;
y += YSPEED;
case RD:
x += XSPEED;
y -= YSPEED;
case LD:
x -= XSPEED;
y -= YSPEED;
case LU:
x -= XSPEED;
y += YSPEED;
case STOP:
break;
}
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT:
bL = true;
break;
case KeyEvent.VK_RIGHT:
bR= true;
break;
case KeyEvent.VK_DOWN:
bD= true;
break;
case KeyEvent.VK_UP:
bU= true;
break;
}
locatDirection();
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_R:
bR = false;
case KeyEvent.VK_L:
bL = false;
case KeyEvent.VK_D:
bD = false;
case KeyEvent.VK_U:
bU = false;
}
locatDirection();
}
public void locatDirection(){
if(!bL && bR &&! bU && !bD) dir = Direction.R;
if(bL && !bR &&! bU && !bD) dir = Direction.L;
if(!bL && !bR && bU && !bD) dir = Direction.U;
if(!bL && !bR &&! bU && bD) dir = Direction.D;
if(!bL && bR && bU && !bD) dir = Direction.RU;
if(!bL && bR &&! bU && bD) dir = Direction.RD;
if(bL && !bR && bU && !bD) dir = Direction.LU;
if(bL && bR &&! bU && bD) dir = Direction.LD;
}
}
这是Tank01的代码:
import java.awt.*;
import java.awt.event.*;public class TankWor01 extends Frame {
public static final int GAME_WIDE = 800;
public static final int GAME_HIGHT=600;
Tank tk = new Tank(400,300);
Image offScreenImage = null;
public void paint(Graphics g){
tk.draw(g);
//y += 5;
}
public void update(Graphics g){
if(offScreenImage == null){
offScreenImage = this.createImage(GAME_WIDE, GAME_HIGHT);
}
Graphics offScreen = offScreenImage.getGraphics();
Color c = offScreen.getColor();
offScreen.setColor(Color.GREEN);
offScreen.fillRect(0,0,GAME_WIDE,GAME_HIGHT);
offScreen.setColor(c);
paint (offScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
public void TwFrame(){
this.setTitle("TankWar");
this.setBackground(Color.GREEN);
this.setSize(800,600);
this.setLocation(250,100);
this.setVisible(true);
this.setResizable(false);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
new Thread(new Mythread()).start();
this.addKeyListener(new KeyMonitor());
}
public static void main(String[] args) {
TankWor01 tw = new TankWor01();
tw.TwFrame();
}
private class Mythread implements Runnable{
public void run (){
while(true){
repaint();
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
public class KeyMonitor extends KeyAdapter{
public void keyPressed(KeyEvent e){
tk.keyPressed(e);
System.out.println("OK");
}
}}
比如按下向走的话...X+=10,Y不变; 还有你那个BR,BL,每次都得重新设置为false.相当于复位
Tank的类:
import java.awt.*;
import java.awt.event.*;
public class Tank {
int x;int y;
private static final int XSPEED = 5;
private static final int YSPEED = 5;
public boolean bL = false,bR = false,bU = false,bD = false;
enum Direction{L,R,U,D,LU,LD,RD,RU,STOP}
private Direction dir = Direction.STOP;
public Tank(int x, int y) {
this.x = x;
this.y = y;
} public void draw (Graphics g){
Color c = g.getColor();
g.setColor(Color.RED );
g.fillOval( x, y,30,30);
g.setColor(c);
move();
}
public void move(){
switch(dir){
case R:
x += XSPEED;
break;
case L:
x -= XSPEED;
break;
case U:
y += YSPEED;
break;
case D:
y -= YSPEED;
break;
case RU:
x += XSPEED;
y += YSPEED;
break;
case RD:
x += XSPEED;
y -= YSPEED;
break;
case LD:
x -= XSPEED;
y -= YSPEED;
break;
case LU:
x -= XSPEED;
y += YSPEED;
break;
case STOP:
break;
}
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT:
//x -= 5;
bL = true;
break;
case KeyEvent.VK_RIGHT:
bR= true;
break;
case KeyEvent.VK_DOWN:
bD= true;
break;
case KeyEvent.VK_UP:
bU= true;
break;
}
locatDirection();
}
public void locatDirection(){
if(!bL && bR &&! bU && !bD) dir = Direction.R;
if(bL && !bR &&! bU && !bD) dir = Direction.L;
if(!bL && !bR && bU && !bD) dir = Direction.U;
if(!bL && !bR &&! bU && bD) dir = Direction.D;
if(!bL && bR && bU && !bD) dir = Direction.RU;
if(!bL && bR &&! bU && bD) dir = Direction.RD;
if(bL && !bR && bU && !bD) dir = Direction.LU;
if(bL && bR &&! bU && bD) dir = Direction.LD;
if(!bL && !bR &&! bU && !bD) dir = Direction.STOP;
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_RIGHT:
bR = false;
break;
case KeyEvent.VK_LEFT:
bL = false;
break;
case KeyEvent.VK_DOWN:
bD = false;
break;
case KeyEvent.VK_UP:
bU = false;
break;
}
locatDirection();
}
}
Tank01的类:
import java.awt.*;
import java.awt.event.*;public class TankWor01 extends Frame {
public static final int GAME_WIDE = 800;
public static final int GAME_HIGHT=600;
Tank tk = new Tank(400,300);
Image offScreenImage = null;
public void paint(Graphics g){
tk.draw(g);
//y += 5;
}
public void update(Graphics g){
if(offScreenImage == null){
offScreenImage = this.createImage(GAME_WIDE, GAME_HIGHT);
}
Graphics offScreen = offScreenImage.getGraphics();
Color c = offScreen.getColor();
offScreen.setColor(Color.GREEN);
offScreen.fillRect(0,0,GAME_WIDE,GAME_HIGHT);
offScreen.setColor(c);
paint (offScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
public void TwFrame(){
this.setTitle("TankWar");
this.setBackground(Color.GREEN);
this.setSize(800,600);
this.setLocation(250,100);
this.setVisible(true);
this.setResizable(false);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
new Thread(new Mythread()).start();
this.addKeyListener(new KeyMonitor());
}
public static void main(String[] args) {
TankWor01 tw = new TankWor01();
tw.TwFrame();
}
private class Mythread implements Runnable{
public void run (){
while(true){
repaint();
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
public class KeyMonitor extends KeyAdapter{
public void keyPressed(KeyEvent e){
tk.keyPressed(e);
System.out.println("OK");
}
}}
我说过要复位的吧...
加上这句话的效果是:你按下向左它可以向左...但是它会一直向左...直到你按下向右,它就改为向右了...如果你要做到按一下动一下的效果...你应该move()方法最后加上dir = Direction.STOP;