Java 我的代码中的哪一部分涉及重新启动游戏?
我需要帮助了解如何重新启动游戏。我正在学习《Java杀手游戏编程》一书中的教程,我想在不退出并再次运行游戏的情况下重新启动游戏。我正在努力理解我必须重新初始化代码的哪一部分才能开始一个新游戏。我正试图找出一种方法,让我保持我的游戏统计,但重置游戏。现在我是唯一一个知道如何重置游戏的古玩,因为我还没有统计数据 我有一个按键监听器,我想按“N”重新启动游戏Java 我的代码中的哪一部分涉及重新启动游戏?,java,Java,我需要帮助了解如何重新启动游戏。我正在学习《Java杀手游戏编程》一书中的教程,我想在不退出并再次运行游戏的情况下重新启动游戏。我正在努力理解我必须重新初始化代码的哪一部分才能开始一个新游戏。我正试图找出一种方法,让我保持我的游戏统计,但重置游戏。现在我是唯一一个知道如何重置游戏的古玩,因为我还没有统计数据 我有一个按键监听器,我想按“N”重新启动游戏 if(e.getKeyCode() == KeyEvent.VK_N){ newGame(); 我的问题是ne
if(e.getKeyCode() == KeyEvent.VK_N){
newGame();
我的问题是newGame()
这段代码不会运行,因为我试图删除所有我认为与我的问题无关的内容
希望我没有删除太多:S
主类
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class WormChase extends JFrame implements WindowListener
{
private WormPanel wp; // where the worm is drawn
public WormChase(long period)
{ super("The Worm Chase");
makeGUI(period);
pack();
setResizable(false);
setVisible(true);
} // end of WormChase() constructor
// ----------------------------------------------------
public static void main(String args[])
{
int fps = DEFAULT_FPS;
if (args.length != 0)
fps = Integer.parseInt(args[0]);
long period = (long) 1000.0/fps;
System.out.println("fps: " + fps + "; period: " + period + " ms");
new WormChase(period*1000000L); // ms --> nanosecs
}
} // end of WormChase class
二等
public class WormPanel extends JPanel implements Runnable
{
private static final int PWIDTH = 500; // size of panel
private static final int PHEIGHT = 400;
private Thread animator; // the thread that performs the animation
private WormChase wcTop;
private Worm fred; // the worm
private Obstacles obs; // the obstacles
public WormPanel(WormChase wc, long period)
{
wcTop = wc;
this.period = period;
// create game components
addKeyListener( new KeyListener() {
if(e.getKeyCode() == KeyEvent.VK_N){
newGame();
}
});
}
public void addNotify()
// wait for the JPanel to be added to the JFrame before starting
{ super.addNotify(); // creates the peer
startGame(); // start the thread
}
private void startGame()
// initialise and start the thread
{
if (animator == null || !running) {
animator = new Thread(this);
animator.start();
}
} // end of startGame()
private void newGame()
// initialise and start the thread
{
public void run()
/* The frames of the animation are drawn inside the while loop. */
{
running = true;
while(running) {
gameUpdate();
gameRender();
paintScreen();
}
}
} // end of WormPanel class
您应该将密钥侦听器移动到启动游戏的基类(WormChase),然后您应该创建一个初始化所有内容的方法,就像WormChase构造函数一样,因为这就是启动游戏的原因。所以你需要做的就是,抓住构造器中的所有东西,把它移动到像“startGame”这样的方法中,然后在构造器中调用它。我现在在想,您可以将键侦听器保留在window类中,但是您需要注意基类,您已经结束了游戏,并重新启动了它。这意味着,当您按下N键时,按键侦听器需要关闭运行游戏的窗口,并注意主线程,即您正在启动一个新游戏。这意味着您将再次调用构造函数
如果你想在不关闭main方法中启动的窗口的情况下重新启动游戏,你需要考虑游戏启动时设置的所有内容以及有哪些值。然后创建一个方法,将所有内容设置为“开始”值,然后运行游戏
if(e.getKeyCode() == KeyEvent.VK_N){
newGame();
这基本上意味着重新创建您在WormPanel类中拥有和使用的每个对象,比如fred=new WormChase等等
我真的不喜欢嵌套函数,所以即使Java不支持它,我也会尽量避免它(newGame()->run())您的代码太不完整,无法提供比代码更好的建议,而且启动一个新游戏应该很简单。哦,不,我希望我已经足够简化了它!好well@paul另外,谢谢你为我指明了MVC的方向:)嘿,谢谢你。我需要反复阅读你的评论几次才能完全理解。感谢您花时间回答此问题:)