【发布时间】:2016-08-03 02:39:38
【问题描述】:
我用 java 制作了一个游戏,但它只运行一轮。我希望它能够根据对话框中按下的数字运行一定次数。我认为答案是在一场比赛结束后运行该方法,尽管我不确定如何执行此操作。
jframe的创建及方法
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;
}
创建网格和鼠标监听器
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()
-
他们点击游戏的部分在鼠标监听器中,我认为不是
-
RockGame() 函数中有什么?
-
在 rockGame() 方法中创建网格并为其提供图像以及 jframe 等。然后在我添加到播放网格的 mouselistener 中,我有一个循环,使在网格上消失。当我单击左上角的那个时,我想为另一轮重置网格。如果这有意义
-
当用户输入回合数时赚大钱?