gpt4 book ai didi

java - 我的计时器的逻辑错误在哪里,该计时器将镜头动画移动到帧的顶部?

转载 作者:行者123 更新时间:2023-12-01 13:06:20 24 4
gpt4 key购买 nike

我正在制作一个简单的太空入侵者类型的游戏,当我尝试使用计时器来制作向上发射的射击动画时,我犯了一个错误。我是否应该重新编写代码,以便将镜头添加到单独的面板中,然后将该面板添加到框架中的主面板中?或者我可以保持原样,只清理代码并稍微修改一下逻辑吗?

这是我完成的代码:

   public static void main(String[] args) throws IOException {
GameTest t = new GameTest();
}

public static class GameTest extends JFrame {

private static final int WINDOW_WIDTH = 800;
private static final int WINDOW_HEIGHT = 500;
public static GamePanel gamePanel;

public GameTest() throws IOException {
super("Deep Fried Freedom");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setLayout(new BorderLayout());
gamePanel = new GamePanel();
add(gamePanel);
center(this);
setVisible(true);
this.addKeyListener(new aKeyListener());
this.setFocusable(true);

}

public void center(JFrame frame) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Point center = ge.getCenterPoint();

int w = frame.getWidth();
int h = frame.getHeight();

int x = center.x - w / 2, y = center.y - h / 2;
frame.setBounds(x, y, w, h);
frame.validate();
}//end of center method

public class aKeyListener implements KeyListener {

@Override
public void keyTyped(KeyEvent e) {
}//end empty keyTyped method

@Override
public void keyPressed(KeyEvent e) {
if (Launcher.lxCoord == 0 || Launcher.lxCoord == 735) { //ensure the launcher can't leave the frame
Launcher.lRun *= -1;
} else {
switch (e.getKeyCode()) {
case KeyEvent.VK_A : Launcher.lRun = -5; break;
case KeyEvent.VK_D : Launcher.lRun = 5; break;
case KeyEvent.VK_ENTER :
gamePanel.numShots++; gamePanel.createShots();
break;
default: Launcher.lRun = 0;
}
}
gamePanel.move(gamePanel);
}//end keyPressed method

@Override
public void keyReleased(KeyEvent e) {
}//end empty keyReleased method

}//end aKeyListener class

}//end GameTest class

}// end main class


public class GamePanel extends JPanel {

Launcher launcher1;
Background bground1;
public static ArrayList<Shot> shots;
public int numShots;
public static int counter;


public GamePanel() throws IOException {
super();
this.shots = new ArrayList<>();
this.numShots = 0;
launcher1 = new Launcher();
bground1 = new Background();
}//end constructor

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bground1.background, 0, 0, getWidth(), getHeight(), null);
g.drawImage(launcher1.baldEagleImage, launcher1.getLxCoord(), launcher1.lyCoord, null);//paint the launcher
while (counter == 1) {
for (int i = 0; i < numShots; i++) {
g.drawImage(shots.get(i).mcDShotImage, shots.get(i).staticXLauncherCoord, shots.get(i).getSyCoord(), null);
}
}
}//end paintComponent method

public void move(GamePanel gamePanel) {
launcher1.moveX();
if (numShots > 0) {
moveShot();
}
repaint();
}//end move method

public void moveShot() {
for (int i = 0; i < numShots; i++) {//loop to move all the shots
if (shots.get(i).getSyCoord() > 10) {
counter = 1;
shots.get(i).moveY();
repaint();
} else {
counter = 0;
numShots--;
shots.remove(i);
repaint();
}
}
}//end shot method

public void createShots() {
try {
for (int j = 0; j < numShots; j++) {
shots.add(new Shot());
}
} catch (IOException | IndexOutOfBoundsException e) {
System.out.println("caught an exception" + e);
}
}

}//end GamePanel class


public class Shot {

public int syCoord;
public int sRise = 5;
public BufferedImage mcDShotImage;
GamePanel gPanel;
public static int staticXLauncherCoord;
private Timer timer;

public Shot() throws IOException {
timer = new Timer(20, new TimerListener());
staticXLauncherCoord = Launcher.getLxCoord() + 10;
syCoord = 381;
mcDShotImage = ImageIO.read(new File("mcdonaldsarchesshot.jpg"));
}//end constructor

public void moveY() {
syCoord -= sRise;
setSyCoord(syCoord);
}//end moveY method

public void setSyCoord(int syCoord) {
this.syCoord = syCoord;
}

public int getSyCoord() {
return this.syCoord;
}

public class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < GamePanel.shots.size(); i++) {
moveY();
}
}//end actionPerformed method

}//end TimerListener class

}//end Shot class


public class Launcher {

public static int lxCoord; //the launcher's x coordinate
public static final int lyCoord = 415;
public static int lRun = 0; //the launcher's x change
public static BufferedImage baldEagleImage;

//Constructor
public Launcher() throws IOException {
lxCoord = 350;
baldEagleImage = ImageIO.read(new File("baldeagleimage.jpg"));
}

/**
* The movement of the launcher in the x direction
*/
public void moveX() {
lxCoord += lRun;
setLxCoord(lxCoord);
}//end moveX method

public void setLxCoord(int lxCoord) {
this.lxCoord = lxCoord;
}

public static int getLxCoord() {
return lxCoord;
}

}//end Launcher class

最佳答案

问题#1

paintComponent 中的

while (counter == 1) { 将阻止事件调度线程处理可能添加到事件队列中的任何新事件。基本上,这意味着 counter 变量根本不可能再次设置为 0...

问题#2

Shot 中的Timer 从未启动。

可能的解决方案

更改代码以便...

  • 您有一个中央计时器,其任务是更新游戏模型并触发重绘
  • 您的paintComponent只是在调用时绘制当前帧/状态,然后立即存在

关于java - 我的计时器的逻辑错误在哪里,该计时器将镜头动画移动到帧的顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23234143/

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