java游戏动作流畅

java游戏动作流畅,java,game-physics,keylistener,Java,Game Physics,Keylistener,我知道,要实现java图像的流畅运动,我必须设置布尔值,然后从该状态触发动作。 我试着在跑步循环中设置它,但是精灵没有移动。我试过对它进行质疑,它深入到每个方法中,所以我不知道我做错了什么 public void run(){ while (running){ go(); repaint(); System.out.println("The game runs"); try { Thread.sleep(1000/60); } ca

我知道,要实现java图像的流畅运动,我必须设置
布尔值,然后从该状态触发动作。
我试着在跑步循环中设置它,但是精灵没有移动。我试过对它进行质疑,它深入到每个方法中,所以我不知道我做错了什么

public void run(){
    while (running){
    go();
    repaint();
    System.out.println("The game runs");
    try {
        Thread.sleep(1000/60);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }
}


//PAINT GRAPHICS
public void paintComponent (Graphics g){
    super.paintComponent(g);
    g.drawImage(bg, 0, 0, this);
    g.drawImage(sprite, cordX, cordY, this);


}

//LOAD IMAGES
public void load (){
            try {
        String bgpath = "res/bg.png";
        bg = ImageIO.read(new File (sfondopath));
        String spritepath = "res/sprite.png";
        sprite = ImageIO.read(new File (spritespath));
    } catch (IOException e) {

        e.printStackTrace();
    }

}


//MOVEMENT
public void go(){
    cordX += vX;
    cordX += vY;

}

public void gameupdate(){
    vX=0;
    vY=0;
    if (down) vY = speed;
    if (up) vY = -speed;
    if (left) vX = -speed;
    if (right) vX = speed;
}



public void keyPressed(KeyEvent ke) {
     switch (ke.getKeyCode()) {
     //if the right arrow in keyboard is pressed...
     case KeyEvent.VK_RIGHT: {
         down = true;
     }
     break;
     //if the left arrow in keyboard is pressed...
     case KeyEvent.VK_LEFT: {
         up = true;
     }
     break;
     //if the down arrow in keyboard is pressed...
     case KeyEvent.VK_DOWN: {
         right = true;
     }
     break;
     //if the up arrow in keyboard is pressed...
     case KeyEvent.VK_UP: {
         left = true;
     }
     break;
 }
 gameupdate();
}




public void keyReleased(KeyEvent ke) {

     switch (ke.getKeyCode()) {
     //if the right arrow in keyboard is pressed...
     case KeyEvent.VK_RIGHT: {
         down = false;
     }
     break;
     //if the left arrow in keyboard is pressed...
     case KeyEvent.VK_LEFT: {
         up = false;
     }
     break;
     //if the down arrow in keyboard is pressed...
     case KeyEvent.VK_DOWN: {
         right = false;
     }
     break;
     //if the up arrow in keyboard is pressed...
     case KeyEvent.VK_UP: {
         left = false;
     }
     break;
 }
 gameupdate();

}

您正在使用while循环阻塞事件调度线程。这使得swing没有机会实际绘制任何东西。用秋千代替

对你来说,大概是:

ActionListener gameLoop = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
        go();
        repaint();
        System.out.println("The game runs");
   }
};

Timer timer = new Timer(1000/60, gameLoop);

public void run() {
    timer.start();
}

您可以调用
timer.stop()
在通常情况下将
运行设置为未设置的位置

看起来break脱离了case块