打地鼠游戏代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
package org.hzy.app.timeMod;
import java.awt.Color;
public class TimeChallenge
private BgPanel pnlMain; //画板,用来装载所有控件
private SoundPlayer bgSound; //音乐对象1
private SoundPlayer hitSound; //音乐对象2,用来在程序中替换音乐对象1 private AnimalLabel[] lblRats; //自定义标签,用来显示地鼠或者兔子
private JLabel lblScore; //显示分数的标签
private int score; //统计玩家得分
private int re_time; //剩余时间
private JLabel lblRemainT; //显示剩余时间
//计时模式下的游戏总时间
private long totalTime;
//计时模式下的游戏开始时间
private long beginTime;
//显示的剩余时间
private int showTime=60;
//倒计时显示
private JLabel lblTime;
private Player[] topTen; //玩家数组
private JFrame parentFrame;
public Object time_bar;
public TimeChallenge (JFrame parentFrame) {
this.parentFrame = parentFrame;
initSound(); //初始化音乐
initControls(); //初始化控件,即窗口上的所有东西
initTop10(); //初始化分数文件,采用对象流读写}
private void closeMainFrame(){ //关闭当前窗口的方法
int rst = JOptionPane.showConfirmDialog(TimeChallenge.this,
"你确定要返回吗?\n关卡进度将不会被保存!",
"确认", JOptionPane.OK_CANCEL_OPTION);
if(rst == JOptionPane.OK_OPTION){ //如果确定关闭当前窗口
TimeChallenge.this.dispose();
bgSound.stop(); //播放音乐停止
}
}
private void initSound() { //初始化音频
bgSound = new SoundPlayer("sound/_bg.wav");
bgSound.loop(); //循环音乐,直到关闭窗口
}
private void initControls() { //初始化当前窗口上的各个控件
lblScore = new JLabel("得分:" + score);
lblScore.setFont(new Font("华文琥珀", Font.BOLD, 20));//字体设置为华文琥珀,字体样式设置为粗体
lblScore.setSize(150, 50);
lblScore.setLocation(90, 397);
lblScore.setBackground(Color.CYAN);
lblScore.setForeground(new Color(191, 18, 205));
lblRemainT = new JLabel("剩余时间:" + re_time);
lblRemainT.setFont(new Font("华文琥珀", Font.BOLD, 20));
lblRemainT.setSize(150, 50);
lblRemainT.setLocation(345, 397);
lblRemainT.setBackground(Color.BLACK);
lblRemainT.setForeground(new Color(217, 44, 6));
pnlMain = new BgPanel("img/bg.jpg");
pnlMain.setLayout(null);
pnlMain.setBounds(0, 0, 640, 480);
// pnlMain.add(lblRat);
lblRats = new AnimalLabel[9];
for (int i = 0; i < lblRats.length; i++) {
lblRats[i] = new AnimalLabel();
lblRats[i].setSize(90, 90);
lblRats[i].setVisible(false);
lblRats[i].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
AnimalLabel lbl = (AnimalLabel) e.getSource();
if (lbl.isVisible()) {
switch (lbl.getScore()) {
case 10:
hitSound = new SoundPlayer("sound/_hit.wav");
hitSound.play();
break;