2017-02-23 89 views
-3

我需要帮助了解如何重新启动游戏。我正在使用Java中的Killer Game Programming一书中的教程,并且我想重新开始游戏而不必退出并再次运行游戏。我正在努力理解我必须重新启动新代码的哪部分代码。我试图找出一种让我保持游戏规则的方式,但重置游戏。现在我只知道如何重置游戏,因为我还没有统计数据。我的代码中涉及重新启动游戏的部分?

我有一个keylistener,我想按“N”重新开始游戏。

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 
+2

您的代码太不完整,无法给出比根据[MVC]的代码更好的建议(https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller),开始新游戏应该很简单。 – Paul

+0

哦,不,我希望我已经简化了足够!以及 – tore

+0

@paul也,谢谢你设置我的方向MVC :) – tore

回答

1

您应该在关键监听移到基类(WormChase)开始游戏,那么你应该创建一个初始化所有东西的方法,就像WormChase的构造函数一样,因为这正是启动游戏的原因。所以你需要做的就是抓住构造函数中的所有东西,将它移到像“startGame”这样的方法,并在构造函数中调用它。我现在在想,你可以把关键的监听器放在窗口类中,但是你需要注意基类,你已经在重新启动它的时候结束了游戏。这意味着,当您按下N键时,关键听众需要杀死运行游戏的窗口,并注意您开始新游戏的主线程。这意味着你会再次调用构造函数。

如果你想重新开始游戏,即使没有杀死以main方法开始的窗口,你需要考虑游戏开始时设置的所有东西以及存在的值。然后,您只需创建一个方法,将所有内容设置为“开始”值,然后您就可以运行游戏。

这基本上意味着重新每一个对象,你已经和在WormPanel类中使用,像弗雷德=新WormChase等等

我真的嵌套函数不是一个球迷,所以我会确定尽量避免它即使Java不支持它(newGame() - > run())

+0

嘿谢谢你。我需要尝试重新阅读您的评论几次以了解所有内容。感谢您花时间回答这个问题:) – tore