本帖最后由 schubertw 于 2015-11-26 00:29 编辑
刚学了GUI,自己试着做一小游戏,但想做到完美还是碰到各种问题,希望大神帮忙出出主意!
游戏功能:
1.点击"开始打地鼠"进行游戏,隐藏所有按钮然后显示一个按钮
2.点击该按钮,隐藏该按钮,并从9个地鼠按钮中随机显示一个按钮
3.点击"我不玩了"按钮,停止游戏
问题如下
1.点击"开始打地鼠"按钮后怎么让该按钮的监听动作暂时停止,点击"我不玩了"重新让该按钮监听动作
2.怎么让9个地鼠按钮的监听动作在点击"开始打地鼠"按钮之后才开始
3.我自己试着用多线程解决以上问题,无奈多线程还是不太理解,完成不了
4.另外我还想实现计数和计时功能,即打一个地鼠记一次数,游戏结束时显示游戏所用时间
package cn.itcast.demo03_按钮小游戏之打地鼠;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class Demo {
public static void main(String[] args) {
Frame f = new Frame("打地鼠小游戏");
// 设置窗体大小
f.setSize(520, 620);
// 设置窗体初始位置
Toolkit t = Toolkit.getDefaultToolkit();
Dimension d = t.getScreenSize();
int x = (d.width - f.getWidth()) / 2;
int y = (d.height - f.getHeight()) / 2;
f.setLocation(x, y);
// 设置窗体布局为null
f.setLayout(null);
// 给窗口添加关闭事件
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 给窗口添加按钮-->开始和结束
Button b1 = new Button("开始打地鼠");
final Button b2 = new Button("我不玩了");
b1.setBounds(20, 40, 235, 80);
b2.setBounds(265, 40, 235, 80);
f.add(b1);
f.add(b2);
// 创建游戏所用的按钮
Button b3 = new Button("来抓我啊");
b3.setBounds(20, 130, 145, 145);
f.add(b3);
Button b4 = new Button("来抓我啊");
b4.setBounds(185, 130, 145, 145);
f.add(b4);
Button b5 = new Button("来抓我啊");
b5.setBounds(350, 130, 145, 145);
f.add(b5);
Button b6 = new Button("来抓我啊");
b6.setBounds(20, 295, 145, 145);
f.add(b6);
Button b7 = new Button("来抓我啊");
b7.setBounds(185, 295, 145, 145);
f.add(b7);
Button b8 = new Button("来抓我啊");
b8.setBounds(350, 295, 145, 145);
f.add(b8);
Button b9 = new Button("来抓我啊");
b9.setBounds(20, 460, 145, 145);
f.add(b9);
Button b10 = new Button("来抓我啊");
b10.setBounds(185, 460, 145, 145);
f.add(b10);
Button b11 = new Button("来抓我啊");
b11.setBounds(350, 460, 145, 145);
f.add(b11);
final Button[] buttonArray = new Button[9];
buttonArray[0] = b3;
buttonArray[1] = b4;
buttonArray[2] = b5;
buttonArray[3] = b6;
buttonArray[4] = b7;
buttonArray[5] = b8;
buttonArray[6] = b9;
buttonArray[7] = b10;
buttonArray[8] = b11;
// 添加按钮事件
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
for(Button b : buttonArray){
b.setVisible(false);
}
Random rdm = new Random();
int i = rdm.nextInt(buttonArray.length);
buttonArray.setVisible(true);
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
for(Button b : buttonArray){
b.setVisible(true);
}
}
});
show(b3,buttonArray);
show(b4,buttonArray);
show(b5,buttonArray);
show(b6,buttonArray);
show(b7,buttonArray);
show(b8,buttonArray);
show(b9,buttonArray);
show(b10,buttonArray);
show(b11,buttonArray);
f.setVisible(true);
}
public static void show(final Button b ,final Button[] buttonArray){
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
b.setVisible(false);
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
Random rdm = new Random();
int i = rdm.nextInt(buttonArray.length);
buttonArray.setVisible(true);
}
});
}
public static long saveTime(long time){
long temp = time;
return temp;
}
} |
|