我用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);
}
}
}
}
}
}
爲(其中你想3,5或10)的函數=圓形或者是所有的地方的代碼? TL; DR我可以稱之爲startRound() –
他們點擊遊戲的部分是在mouselistener中,我不認爲它是 – michael
RockGame()函數中有什麼?在rockGame()方法中, –