?? game.java
字號:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import sun.audio.*;//音效
import java.io.*;//文件輸入輸出流
import javax.swing.event.*; //JSlider所需
import java.util.*;
class Game extends JFrame implements Runnable,ActionListener,MouseListener
{
private javax.swing.Timer timer1,timer2;//地鼠變幻時間,游戲時間
private int score=0,time=20;//積分
private JLabel label,label2,label3,blank;//時間,積分標簽,速度,空白
private JLabel clickMe[]=new JLabel [12];//12個地鼠
private JButton select1,select2; //難度選擇
private Thread thread; //線程倒計時
private MenuBar mb;//菜單欄
private MenuItem m10;//重新開始游戲
private MenuItem m11;// 開始游戲
private MenuItem m13;//退出
private MenuItem m20;//排行榜
private MenuItem m21;//關于
private MenuItem m31;//20
private MenuItem m32;//40
private MenuItem m33;//60
private MenuItem m34;//自定義時間
private Menu m1;//選項
private Menu m2;//幫助
private Menu m3;//游戲時間
private JPanel pane; //面板
private JSlider speed;//速度滾動條
private int ispeed=800;//默認速度
private String line=""; //積分榜一行
private String sum="";//積分榜所有的記錄
private int highest=0;
public Game() //初始化
{
super("打地鼠 1.5");
blank=new JLabel(" ");
add(blank);
String imgePath = "background.jpg"; //背景圖片
final Image img = Toolkit.getDefaultToolkit().createImage(imgePath);
pane = new JPanel()
{
protected void paintChildren(Graphics g)
{
g.drawImage(img,0,0,this);
super.paintChildren(g);
}
};
pane.setSize(380,500);//布局大小
getContentPane().add(pane);
pane.setLayout(new GridLayout(4,3,40,40)); //pane窗體布局
setLayout(new FlowLayout());//窗體布局
timer2=new javax.swing.Timer(20000,this); //設置游戲時間
//////////////////菜單1////////////////////////////////
m1=new Menu("游戲選項");
m11=new MenuItem("開始游戲");
m10=new MenuItem("重新游戲");
m13=new MenuItem("退出游戲");
m1.add(m11);
m1.add(m10);
m1.add(m13);
//////////////////菜單2////////////////////////////////
m3=new Menu("設置時間");
m31=new MenuItem("20秒");
m32=new MenuItem("40秒");
m33=new MenuItem("60秒");
m34=new MenuItem("自定義");
m3.add(m31);
m3.add(m32);
m3.add(m33);
m3.add(m34);
//////////////////菜單3////////////////////////////////
m2=new Menu("幫助");
m20=new MenuItem("排行榜");
m21=new MenuItem("關于");
m2.add(m20);
m2.add(m21);
//////////////////添加主菜單////////////////////////////////
mb=new MenuBar();
mb.add(m1);
mb.add(m3);
mb.add(m2);
//////////////////設置菜單動作////////////////////////////////
m10.addActionListener(this);
m11.addActionListener(this);
m13.addActionListener(this);
m20.addActionListener(this);
m21.addActionListener(this);
m31.addActionListener(this);
m32.addActionListener(this);
m33.addActionListener(this);
m34.addActionListener(this);
pane.addMouseListener(this); //窗體加入鼠標事件
m10.setEnabled(false);//一開始重新開始菜單不可見
select1=new JButton("開始游戲");
select1.setForeground(Color.black);
select1.setBackground(Color.pink);//設置按鈕顏色
select1.setFont(new Font("黑體",Font.BOLD,16));
select1.addActionListener(this);
select2=new JButton("重新游戲");
select2.setForeground(Color.black);
select2.setBackground(Color.pink);//設置按鈕顏色
select2.setFont(new Font("黑體",Font.BOLD,16));
select2.addActionListener(this);
select2.setVisible(false);
label=new JLabel(" 得分:0 ");
label.setForeground(Color.green);
label.setFont(new Font("幼圓",Font.BOLD,12));
label2=new JLabel(" 時間:20 ");
label2.setForeground(Color.red);
label2.setFont(new Font("幼圓",Font.BOLD,12));
label3=new JLabel(" 游戲速度:200");
label3.setForeground(Color.blue);
label3.setFont(new Font("幼圓",Font.BOLD,12));
add(label2);
add(label);
add(label3);
ImageIcon image=new ImageIcon("mouse1.gif"); //地鼠圖片
//////////////////////添加地鼠按鈕///////////////////////////
for(int i=0;i<12;i++)
{
clickMe[i]=new JLabel(image);
clickMe[i].setVisible(false);
clickMe[i].addMouseListener(this); //按鍵鼠標事件
pane.add(clickMe[i]);
}
try //設置鼠標指針1
{
String ico="cursor1.gif";//鼠標圖片的位置
this.setCursor(MyCursor.getSystemCustomCursor(ico));
}
catch(Exception ae){ }
/////////////設置背景音樂//////////////////////
try
{
FileInputStream music=new FileInputStream("music.mid" ); //背景音樂的位置
AudioStream as=new AudioStream(music);
AudioPlayer.player.start(as);
}
catch (Exception p) {}
speed=new JSlider();
speed.setMaximum(999);//反應速度最大為999
speed.setMinimum(1);//反應速度最小為1
speed.setValue(200);//默認值800
/////////////設置速度滾動條////////////
speed.addChangeListener(
new ChangeListener()
{
public void stateChanged( ChangeEvent changeEvent )
{
ispeed=-(speed.getValue()-1000);
label3.setText(" 游戲速度:"+speed.getValue());
}
}
);
add(speed);//速度滾動條
add( select1 ); //開始按鈕
add( select2 ); //重新開始按鈕
setSize(330,560); //設置窗口大小
setLocation(400,100);//設置初始化窗口位置
setResizable(false);//無最大化按鈕
setVisible(true); //窗口可見
setMenuBar(mb);//設置菜單
////////////////讀出積分榜//////////////////
try
{
FileReader reader = new FileReader("data.dat");
Scanner in = new Scanner(reader);
line= in.nextLine();
while(in.hasNextLine())
{
line= in.nextLine();
sum=sum+line+"\n";
}
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
//////////////事件觸發/////////////////////////////////////
public void actionPerformed(ActionEvent b)
{
int i,j,k;
if(b.getSource() == select1||b.getSource() == m11 ) //開始
{
try //讀出最高記錄
{
FileReader reader = new FileReader("data.dat");
Scanner in = new Scanner(reader);
highest=in.nextInt();
System.out.println(highest);
in.close();
}
catch(IOException e){}
try
{
sum="";//積分榜清空
FileReader reader = new FileReader("data.dat");
Scanner in = new Scanner(reader);
line= in.nextLine();
while(in.hasNextLine())//積分榜重置
{
line= in.nextLine();
sum=sum+line+"\n";
}
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
select1.setVisible(false);//開始按鈕不可見
select2.setVisible(true);//重新開始可見
m11.setEnabled(false);//開始菜單不可見
//隱藏時間選擇菜單
m31.setEnabled(false);
m32.setEnabled(false);
m33.setEnabled(false);
m34.setEnabled(false);
m10.setEnabled(true);//重新開始菜單可見
thread = new Thread(this);
thread.start();
timer2.start(); //開始計時
timer1 = new javax.swing.Timer(ispeed,this);
timer1.start();
score=0;
label.setText(" 得分:"+score+" ");
try
{
FileInputStream music=new FileInputStream("button.wav" );
AudioStream as=new AudioStream(music);
AudioPlayer.player.start(as);
}
catch (Exception p) {}
}
if(b.getSource()==timer1) //隨機地鼠
{
j=(int)(Math.random()*12);
clickMe[j].setVisible(true);
for(k=0;k<3;k++)
{
i=(int)(Math.random()*12);
clickMe[i].setVisible(false);
}
}
if(b.getSource()==timer2)//游戲時間到了
{
timer1.stop();
timer2.stop();//游戲時間結束
for(int n=0;n<12;n++)//恢復原始狀態
{
clickMe[n].setVisible(false);
}
try
{
FileInputStream music=new FileInputStream("end.wav" );//游戲時間結束音效
AudioStream as=new AudioStream(music);
AudioPlayer.player.start(as);
}
catch (Exception p) {}
JOptionPane.showMessageDialog( null, "時間到! 您的得分為:"+score);
//////////////////////////破記錄/////////////////////////////
if(score>=highest)
{
highest=score;
try
{
String name=null;
name = JOptionPane.showInputDialog( "恭喜您打破記錄,請輸入您的大名:" );
name=highest+"\n"+name+" "+highest+"\n";
File f = new File("data.dat");
RandomAccessFile raf = new RandomAccessFile(f, "rw");
name=name+sum;
raf.write(name.getBytes());
raf.close();
}
catch(Exception ae){}
}
score=0;
label.setText(" 得分:"+score+" ");//積分清0
select1.setVisible(true);//開始按鈕可見
select2.setVisible(false);//重新開始按鈕不可見
m11.setEnabled(true);// 開始菜單可見
m10.setEnabled(false);//重新開始菜單不可見
//游戲結束,重新顯示時間選擇菜單
m31.setEnabled(true);
m32.setEnabled(true);
m33.setEnabled(true);
m34.setEnabled(true);
}
if(b.getSource()==m10||b.getSource()==select2) //重新開始菜單
{
select1.setVisible(true);//開始按鈕可見
select2.setVisible(false);//重新開始按鈕不可見
m11.setEnabled(true);//開始菜單可見
m10.setEnabled(false);//重新開始菜單不可見
score=0;
timer1.stop();
timer2.stop();
thread.stop();
speed.setValue(200);//默認值800
timer2=new javax.swing.Timer(20000,this); //設置20秒游戲時間
time=20;
label2.setText(" 時間:20 ");
for(int q=0;q<12;q++)
{
clickMe[q].setVisible(false);
}
//重新顯示時間選擇菜單
m31.setEnabled(true);
m32.setEnabled(true);
m33.setEnabled(true);
m34.setEnabled(true);
try
{
FileInputStream music=new FileInputStream("button.wav" );
AudioStream as=new AudioStream(music);
AudioPlayer.player.start(as);
}
catch (Exception p) {}
}
if(b.getSource()==m13) //退出菜單
{
System.exit(0);
}
if(b.getSource()==m20) //排行榜菜單
{
try
{
FileReader reader = new FileReader("data.dat");
Scanner in = new Scanner(reader);
String line1="";
String sum1="";
line1= in.nextLine();
while(in.hasNextLine())
{
line1= in.nextLine();
sum1=sum1+line1+"\n";
}JOptionPane.showMessageDialog( null,sum1);
in.close();
}
catch(IOException e){}
}
if(b.getSource()==m21) //關于菜單
{
JOptionPane.showMessageDialog( null, " 打地鼠1.5\n 作者:劉銳浩\n 時間:2008.05.25~06.21");
}
if(b.getSource()==m31) //20秒
{
timer2=new javax.swing.Timer(20000,this); //設置20秒游戲時間
time=20;
label2.setText(" 時間:20 ");
}
if(b.getSource()==m32) //40秒
{
timer2=new javax.swing.Timer(40000,this); //設置40秒游戲時間
time=40;
label2.setText(" 時間:40 ");
}
if(b.getSource()==m33) //60秒
{
timer2=new javax.swing.Timer(60000,this); //設置60秒游戲時間
time=60;
label2.setText(" 時間:60 ");
}
if(b.getSource()==m34) //設置自定義游戲時間
{
try
{
int count=20000;//默認20秒
String number;
number = JOptionPane.showInputDialog( "請輸入游戲時間:單位(秒)" );
time=Integer.parseInt(number);
label2.setText(" 時間:"+number+" ");
count = Integer.parseInt(number)*1000;
timer2=new javax.swing.Timer(count,this);
}
catch(Exception ae){ }
}
}
///////////////鼠標點擊事件//////////////////////
public void mousePressed(MouseEvent e)//按下
{
//設置鼠標指針2
try
{
String ico="cursor2.gif";//鼠標圖片的位置
this.setCursor(MyCursor.getSystemCustomCursor(ico));
}
catch(Exception ae){ }
if(e.getSource()==pane){}
else
{
try
{
FileInputStream music=new FileInputStream("beat.wav" );//打鼠音效
AudioStream as=new AudioStream(music);
AudioPlayer.player.start(as);
}
catch (Exception p) {}
for (int i=0;i<12;i++)
{
if(e.getSource()==clickMe[i])
{
score+=10;
label.setText(" 得分:"+score+" "); //積分板
clickMe[i].setVisible(false);
}
}
}
}
public void mouseReleased(MouseEvent e)//松開
{
//設置鼠標指針1
try
{
String ico="cursor1.gif";//鼠標圖片的位置
this.setCursor(MyCursor.getSystemCustomCursor(ico));
}
catch(Exception ae){ }
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
////////////////線程倒計時///////////////////
public void run()
{
long i;
try
{
for(i=time ;i>=0 ;i-- )
{
label2.setText(" 時間:"+i+" ");
thread.sleep(1000);
}
}
catch(Exception e){}
}
}
class MyCursor extends Cursor //自定義鼠標指針
{
public MyCursor(int n)
{
super(n);
}
static public Cursor getSystemCustomCursor(final String name) throws AWTException,HeadlessException
{
Toolkit toolkit=Toolkit.getDefaultToolkit();
Image image=toolkit.getImage(name);
return toolkit.createCustomCursor(image,new Point(0,0),"");//0,0為鼠標熱點在左上角
}
}
public class mouse
{
public static void main(String[] args) //主函數
{
new Game(); //游戲開始
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -