Java 测试一个游戏的简单功能,不能让一个对象跳转

Java 测试一个游戏的简单功能,不能让一个对象跳转,java,game-physics,Java,Game Physics,当你按下向上箭头键时,我试图让球上下弹跳。我可以让球向上移动,但它不会停止向上移动。我已经在更新方法中编写了球运动的代码。我试图让球停在y坐标400处,但它只是把球传了上去 import javax.swing.*; import java.awt.*; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; import java.awt.image.DataBufferInt; public cl

当你按下向上箭头键时,我试图让球上下弹跳。我可以让球向上移动,但它不会停止向上移动。我已经在更新方法中编写了球运动的代码。我试图让球停在y坐标400处,但它只是把球传了上去

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

public class Game extends Canvas implements Runnable {

int x,y;
public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 3;
private boolean running = false;

public Thread thread;
public JFrame frame;
public Keyboard key;

private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();

public Game() {
    Dimension size = new Dimension(width * scale, height * scale);
    setPreferredSize(size);

    frame = new JFrame();
    key = new Keyboard();
    addKeyListener(key);
}

public synchronized void start() {
    running = true;
    thread = new Thread(this, "Display");
    thread.start();
}

public synchronized void stop(){
    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

@Override
public void run() {

    long lastTime = System.nanoTime();
    final double nanoSeconds = 1000000000.0 / 60.0;
    double delta = 0;
    int frames = 0;
    int gameUpdates = 0;
    long timer = System.currentTimeMillis();
    requestFocus();

    while(running){

        long now = System.nanoTime();
        delta += (now - lastTime) / nanoSeconds;
        lastTime = now;
        //this ensures that delta only updates the screen 60 times each second
        while (delta >= 1){
            update();
            gameUpdates++;
            delta--;
        }
        render();
        frames++;

        //this if statement happens once a second
        if (System.currentTimeMillis() - timer > 1000){
            timer += 1000;
            frame.setTitle("Bouncy Ball!  ~ ~ ~  Updates per second: " + gameUpdates + ". Frames per second: " + frames + ".");
            gameUpdates = 0;
            frames = 0;
        }
    }
    stop();
}

int yy;

public void update() {
    key.update();

    y = y + yy;

   if(key.up){
       yy = -5;
       if(y == 400){
           yy = 0;}

   }
}

public void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null){
        createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();

    g.drawImage(image,0,0,getWidth(),getHeight(),null);

    g.setColor(Color.DARK_GRAY);
    g.fillRect(0,0,getWidth(),getHeight());

    g.setColor(Color.MAGENTA);
    g.fillOval(300,y+435,50,50);


    g.dispose();
    bs.show();
}

public static void main(String[] args) {

    Game game = new Game();
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setResizable(false);
    game.frame.setVisible(true);
    game.frame.add(game);
    game.frame.pack();
    game.frame.setTitle("Bouncy Ball");

    game.start();
}
}
这是我的keylisener课程

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Keyboard implements KeyListener{

private boolean[] keys = new boolean[120];
public boolean up, down, left, right;

public void update(){

    up = keys[KeyEvent.VK_UP];
    down = keys[KeyEvent.VK_DOWN];
    left = keys[KeyEvent.VK_LEFT];
    right = keys[KeyEvent.VK_RIGHT];

}

@Override
public void keyPressed(KeyEvent e) {
   keys[e.getKeyCode()] = true;
}

@Override
public void keyReleased(KeyEvent e) {
    keys[e.getKeyCode()] = false;
}


@Override
public void keyTyped(KeyEvent e) {

}

}
而不是:

   if(key.up){
   yy = -5;
   if(y == 400){
       yy = 0;}
}

尝试:

}

如果您的球在每次更新时以5px的速度移动,那么它可能会通过400px,而不会实际移动
等于400px,这就是为什么要检查它是否大于400px。

您的问题在key.update()方法中。key.up仅在按住向上键时才为真,而不仅仅是按一次向上键。如果您现在只关心单个按钮的按下,请将其更改为:

public void update() {
    if (keys[KeyEvent.VK_UP]) {
        up = true;
    }
}
这将只更新一次,一旦您按下up键,并且keys.up将保持为true

而且,y是递减的,而不是递增的(从中减去5),所以您想要改变
==400
==400
,或者更好,
如果你想让球像你说的那样反弹,你需要在计算物体位置时加入重力。我将实现重力,但首先我需要担心更基本的事情,比如让球停下来。我甚至不能那样做!我试过你的密码,但球还是直上@krzysztof,很有趣。我想你是用雪碧做球的吧?可能您的精灵在编辑时没有使用相同的y。
public void update() {
    if (keys[KeyEvent.VK_UP]) {
        up = true;
    }
}