当你按向上箭头键时,我试图让球上下跳动。我可以让球向上移动,但它不会停止向上移动。我在更新方法中编写了球运动的代码。我试图让球停在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) {
}
}
答案 0 :(得分:0)
而不是:
if(key.up){
yy = -5;
if(y == 400){
yy = 0;}
}
尝试:
if(key.up){
yy = -5;
if(y >= 400){
yy = 0;}
}
如果你的球以每次更新的5px移动,那么实际上它可能会超过400px 等于400px,这就是你检查它是否大于400px的原因。
答案 1 :(得分:0)
您的问题出在key.update()方法中。 key.up只会是而你持有向上键,而不是只按一次向上键。如果,现在,您只关心按下一个按钮,请将其更改为:
public void update() {
if (keys[KeyEvent.VK_UP]) {
up = true;
}
}
一旦你按下,它只会更新一次,而keys.up将保持正确。
此外,y正在减少,而不是增加(你从中减去5),所以你想改变
== 400
至== 400
,或者更好,<= 400
,以防您不使用整数增加(按照其他答案)。
作为一般调试的想法:在if语句中,我打印出y的值,并注意到它只打印出3或4个值(对应于我把它保持多久),这意味着if语句没有被检查,或者没有返回true(这就是这种情况)。