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关卡进度将不会被保存!\ \"确认\ 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(\"华文琥珀\字体设置为华文琥珀,字体样式设置为粗体 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(\"华文琥珀\ 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; default: hitSound = new SoundPlayer(\"sound/_hiterror.wav\"); hitSound.play(); break; } score += lbl.getScore(); lblScore.setText(\"得分:\" + score); lbl.setVisible(false); } } }); } lblRats[0].setLocation(115, 110);//九个位置。地鼠或者兔子将随机的出现在这里 lblRats[1].setLocation(255, 110); lblRats[2].setLocation(415, 110); lblRats[3].setLocation(85, 200); lblRats[4].setLocation(258, 200); lblRats[5].setLocation(418, 200); lblRats[6].setLocation(80, 300); lblRats[7].setLocation(258, 300); lblRats[8].setLocation(433, 300); this.setCursor(Toolkit.getDefaultToolkit().createCustomCursor( Toolkit.getDefaultToolkit().getImage(\"img/hammer.png\"), new Point(3, 3), \"myhammer\"));// 把鼠标改成锤子 for (AnimalLabel lbl : lblRats) { pnlMain.add(lbl); //显示各个地鼠或者兔子的标签 } pnlMain.add(lblScore); //显示分数 pnlMain.add(lblRemainT);//显示剩余时间 getContentPane().add(pnlMain); // getContentPane().add(pnlMain); // getContentPane().add(pnlMain); //等于this.add(pnMain); this.setResizable(false); this.setTitle(\"时间挑战——打地鼠\"); this.setSize(640, 480); getContentPane().setLayout(null); this.setResizable(false); //禁止拉伸窗口 this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { closeMainFrame(); } }); ShowAnimal sa = new ShowAnimal(); // Thread t1 = new Thread(sa); //开一个线程用来显示兔纸或者地鼠 // Thread t2 = new Thread(sa); // Thread t3 = new Thread(sa); t1.start(); // t2.start(); // t3.start(); UpdateTime ut=new UpdateTime(); Thread t3=new Thread(ut); t3.start(); } private class UpdateTime implements Runnable{ //倒计时 @Override public void run() { for (int i = 1; i <= 60; i++) { int x = i; re_time=60-x; lblRemainT .setText(\"剩余时间:\" + re_time); if(re_time==0){ JOptionPane.showMessageDialog(TimeChallenge.this, \"Game Over, 你的得分:\" + score); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } private class ShowAnimal implements Runnable { //用线程来显示小动物 @Override public void run() { while (re_time<60&&re_time>0) { //9个数字代表9个动物(兔子或者地鼠),每一次只得到1个 int current = new Random().nextInt(lblRats.length); lblRats[current].setVisible(true); //把随机得到的这个动物设为出现 // public void time(float time ,float num){ // if(time>0&&time<20){ // num=1; // Thread.sleep(1000); // }else if(time>20&&time<30){ // num=1; // Thread.sleep(500); // }else if(time>30&&time<40){ // num=2; // Thread.sleep(500); // }else{ // num=3; // Thread.sleep(300); // } // } try { Thread.sleep(1000); //每次1秒的间隔 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } lblRats[current].setVisible(false); } } } private void initTop10() { try { FileInputStream fis = new FileInputStream(\"scoretime.dat\"); ObjectInputStream ois = new ObjectInputStream(fis); topTen = (Player[]) ois.readObject(); ois.close(); } catch (IOException | ClassNotFoundException e) { topTen = new Player[10]; for (int i = 0; i < topTen.length; i++) { topTen[i] = new Player(); topTen[i].setName(\"No Name\"); topTen[i].setScore(0); } Arrays.sort(topTen, new MyCompare()); } StringBuffer sb = new StringBuffer(); for (int i = topTen.length - 1; i >= 0; i--) { sb.append(topTen[i].getName()); sb.append(\" \"); sb.append(topTen[i].getScore()); sb.append(\"\\n\"); } JOptionPane .showMessageDialog(TimeChallenge.this, sb.toString()); } } 因篇幅问题不能全部显示,请点此查看更多更全内容