日期:2014-05-20  浏览次数:20860 次

为什么我的方块不能移动呢
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class BlockDiagram extends Frame{
public static final int GAME_LINE = 25;
public static final int GAME_LIST = 20;
public static final int BLOCK = 20;
Wall w1 = new Wall(20,100,20,400);
Wall w2 = new Wall(40,480,200,20);
Wall w3 = new Wall(240,100,20,400);
Tetris ts = new Tetris(this,100,120);
Image offScreenImage = null;
public static void main(String[] args) {
new BlockDiagram().launchFrame();

}
private void launchFrame() {
this.setLocation(200,300);
this.setSize(GAME_LIST*BLOCK,GAME_LINE*BLOCK);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.setVisible(true);
this.setResizable(false);
new Thread(new PaintThread()).start();
this.addKeyListener(new KeyMonitor());
}
public void paint(Graphics g){
Color c = g.getColor();
g.setColor(Color.white);
g.fillRect(0, 0,GAME_LIST*BLOCK,GAME_LINE*BLOCK);
g.setColor(c);
ts.draw(g);
w1.draw(g);
w2.draw(g);
w3.draw(g);
}
public void update(Graphics g){
if(offScreenImage==null){
offScreenImage = this.createImage(GAME_LIST*BLOCK,GAME_LINE*BLOCK);
}
Graphics goff=offScreenImage.getGraphics();
paint(goff);
g.drawImage(offScreenImage, 0, 0, null);
}
private class KeyMonitor extends KeyAdapter{
public void keyPressed(KeyEvent e){
ts.keyPressed(e);
}
}
class PaintThread implements Runnable{

@Override
public void run() {
while(true){
//repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}

}




import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;


public class Tetris {

    int x,y;
   
BlockDiagram bd;
Direction dir = Direction.D;
Block bk = null;
public final static int XSPEED=5;
public final static int YSPEED=5;
private boolean BL=false,BU=false,BR=false,BD=false;
Tetris(BlockDiagram bd,int x,int y){
this.bd = bd;
this.x = x;
this.y = y;

bk = new Block(x,y,Direction.D);

}

public void draw(Graphics g){
if(bk!=null)
bk.draw(g);
move();
}
/*private void move() {


y = y+YSPEED; 
if(BL&&!BR&&!BD){
x = x-XSPEED;
}else if(!BL&&BR&&!BD){
x = x+XSPEED;
}else if(!BL&&!BR&&!BD){
y = y+YSPEED;
}