gpt4 book ai didi

java - 测试游戏的简单功能,无法让物体跳跃

转载 作者:太空宇宙 更新时间:2023-11-04 14:37:26 25 4
gpt4 key购买 nike

当您按下向上箭头键时,我试图让球上下弹跳。我可以让球向上移动,但它不会停止向上移动。我已经在更新方法中编写了球运动的代码。我试图让球停在 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) {

}

}

最佳答案

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

public void update() {
if (keys[KeyEvent.VK_UP]) {
up = true;
}
}

一旦你按下向上键,它只会更新一次,并且keys.up将保持true。

此外,y 正在减少,而不是增加(您要从中减去 5),因此您想要更改 == 400== 400 ,或者更好,<= 400 ,以防您不增加整数(按照其他答案)。

作为一般的调试思路:在那个 if 语句中,我打印出了 y 的值,并注意到它只打印了 3 或 4 个值(对应于我按住它的时间),这意味着 if 语句没有被检查,或者没有返回 true(这里就是这种情况)。

关于java - 测试游戏的简单功能,无法让物体跳跃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25389373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com