2016-08-03 61 views
-1

我用java做了一个游戏,但它只运行了一轮。我希望它能够根据在一个拨号盒中按下的数字运行一定的时间。我认为答案是在一场比赛结束后运行该方法,但我不确定如何执行此操作。如何让方法运行一定的时间

创建JFrame的和metthod

public Rockgame() { 

    // JFrame 
    p = new ImagePanel(Toolkit.getDefaultToolkit().getImage("space.jpg")); 
    f = new JFrame("SpaceMiners"); 

    f.setSize(700, 500); 
    f.setResizable(false); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

其确定的倍量运行的按钮:

Object[] options = { "Three", "Five", "Ten" }; 
    no_of_games = (int) JOptionPane.showOptionDialog(f, 
      "Would you like to play best of:", "Rounds" + "", 
      JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, 
      null, options, options[2]); 

if (no_of_games == 0) { 
     no_of_games = no_of_games + 3; 
    } 
    if (no_of_games == 1) { 
     no_of_games = no_of_games + 4; 
    } 
    if (no_of_games == 2) { 
     no_of_games = no_of_games + 8; 
    } 

创建网格和的MouseListener

p.setLayout(new GridLayout(10, 10)); 
    f.getContentPane().add(p, BorderLayout.CENTER); 

    for (int x = 0; x < 10; x++) { 

     for (int y = 0; y < 10; y++) { 

      playingGrid[x][y] = new JLabel(new ImageIcon("rock.png")); 
      p.add(playingGrid[x][y]); 

      playingGrid[x][y].addMouseListener(new Rockbreaker()); 

     } 
    } 

} 

public class Rockbreaker implements MouseListener { 
    // manbitesdog6 
    public void mouseClicked(MouseEvent e) { 
     // sets all columns greater than one clicked to invisible 

      if (e.getSource() == playingGrid[0][0]){ 
       for(int u =0;u<10;u++){ 
       for(int y =0;y<10;y++){ 
       Rockgame(); 

       } 
      } 
      } 

      for (int x = 0; x < 10; x++) { 
       for (int y = 0; y < 10; y++) { 
        if (playingGrid[x][y] == e.getSource()) { 
         for (int k = 0; k < 10; k++) { 
          for (int i = 0; i < 10; i++) { 
           if ((i >= x) && (k >= y)) { 
            playingGrid[i][k].setVisible(false); 
           } 
          } 
         } 

        } 
       } 
      } 
+0

为(其中你想3,5或10)的函数=圆形或者是所有的地方的代码? TL; DR我可以称之为startRound() –

+0

他们点击游戏的部分是在mouselistener中,我不认为它是 – michael

+0

RockGame()函数中有什么?在rockGame()方法中, –

回答

0

尝试使用for() -循环。

for(int i = 0; i < no_of_games; i++) { 
    /* 
     * Start game 
     * Reset grid/panel 
     * ... 
     */ 
} 

而且,看看这个:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

+0

我试过但它只是说for循环什么都不做。 – michael

+0

是的,因为在我的示例代码中循环中只有一条注释。 你当然应该自己实现你的代码,我只是给你一个for()循环的代码,它目前什么都不做。 我不会为你实施 - 现在轮到你了...... –