?? play.java
字號:
package type;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.net.URL;
public class Play extends JFrame{
Container c=null;
GamePanel[] games;
JLabel[] labels;
JButton jbtn_start;
JButton jbtn_set;
JButton jbtn_pause;
JButton jbtn_exit;
private long startTime;//開始游戲時間
private long currentTime;//當前時間
private URL this_url=null;//音頻文件的地址
private static AudioClip errSound=null;//按鍵錯誤提示聲
private static AudioClip backSound=null;
private static AudioClip startMusic;//游戲開始音
private static AudioClip addLevel;//玩家升級音
private static AudioClip subLevel;//玩家降級音
int curLevel;//玩家當前的級別
int rate; //分數
int time; //時間
int mark; //記分
int level; //級別
int errCount;//錯誤
int baseMark=20;//系統默認值分數大于此值時則增加速度
private boolean IsStarted=false;//是否開始游戲
int showCount; //顯示字符數量
int attackCount;//擊中數量
Thread thread_start=null;//開始控制線程
Thread thread_computer=null;//計算線程
Thread thread_gameover=null;
public Play(String title,int showCount){
super(title);
this.showCount=showCount;
this.setSize(70*showCount,600);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=this.getSize();
if(frameSize.height>screenSize.height){
frameSize.height=screenSize.height;
}
if(frameSize.width>screenSize.width){
frameSize.width=screenSize.width;
}
this.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
this.setBackground(Color.BLACK);
this.setResizable(false);
this.Init();
this.setVisible(true);
try{
Class this_class=Class.forName("type.Play");
this_url=this_class.getResource("../audio/errsound.au");
errSound=Applet.newAudioClip(this_url);
this_url=this_class.getResource("../audio/backsound.wav");
backSound=Applet.newAudioClip(this_url);
this_url=this_class.getResource("../audio/start.wav");
startMusic=Applet.newAudioClip(this_url);
this_url=this_class.getResource("../audio/addlevel.wav");
addLevel=Applet.newAudioClip(this_url);
this_url=this_class.getResource("../audio/sublevel.wav");
subLevel=Applet.newAudioClip(this_url);
}catch(Exception e){
e.printStackTrace();
}
this.startMusic.play();
}
private class StartThread extends Thread{
public void run(){
if(!IsStarted){
Play.this.IsStarted=true;
started();
}
}
}
private class ComputerThread extends Thread{
public void run(){
while(true){//計算打字速度,時間,分數
try{
this.sleep(1000);
}catch(Exception e){
}
Play.this.computer();
}
}
}
public void threadStart(){
Play.startMusic.stop();
thread_start=new StartThread();
//System.out.println("StartThread-------------------------");
this.backSound.loop();//播放背景音樂
thread_start.start();
}
public synchronized void resume(){
for(int i=0;i<showCount;i++){
if(games[i]!=null){
games[i].resume();
}
}
}
public synchronized void stop(){
this.IsStarted=!this.IsStarted;
if(thread_start!=null){
this.backSound.stop();
thread_start.stop();
thread_computer.stop();
thread_start=null;
thread_computer=null;
}
for(int i=0;i<showCount;i++){
if(games[i]!=null){
games[i].Down(false);
System.out.println("線程關閉:"+i);
games[i].stop();
}
}
GamePanel.reSetStaticCount();
}
public synchronized void suspend(){
for(int i=0;i<showCount;i++){
if(games[i]!=null){
games[i].suspend();
}
}
}
private void Init(){
//初始化
GamePanel.setMusic();
games=new GamePanel[showCount];
for(int i=0;i<showCount;i++){
games[i]=new GamePanel("線程:"+i);
}
c=this.getContentPane();
c.setLayout(new BorderLayout());
int i=0;
labels=new JLabel[10];
labels[i++]=new JLabel("速度:");
labels[i++]=new JLabel("0/0 ");
labels[i++]=new JLabel("錯誤:");
labels[i++]=new JLabel("0 ");
labels[i++]=new JLabel("時間:");
labels[i++]=new JLabel("0 ");
labels[i++]=new JLabel("分數:");
labels[i++]=new JLabel("0 ");
labels[i++]=new JLabel("級別:");
labels[i++]=new JLabel("0 ");
JPanel south_Panel=new JPanel();
JPanel center_Panel=new JPanel();
for(int j=0;j<labels.length;j++){
south_Panel.add(labels[j]);
}
i=0;
jbtn_start=new JButton("開始");
jbtn_set=new JButton("設置");
jbtn_pause=new JButton("暫停");
jbtn_exit=new JButton("退出");
south_Panel.add(jbtn_start);
south_Panel.add(jbtn_set);
south_Panel.add(jbtn_pause);
south_Panel.add(jbtn_exit);
c.add(south_Panel,BorderLayout.SOUTH);
center_Panel.setLayout(new GridLayout(1,showCount));
for(i=0;i<showCount;i++){
center_Panel.add(games[i]);
}
c.add(center_Panel,BorderLayout.CENTER);
OnClickAdapter onClickListener=new OnClickAdapter();
jbtn_start.addActionListener(onClickListener);
jbtn_set.addActionListener(onClickListener);
jbtn_pause.addActionListener(onClickListener);
jbtn_exit.addActionListener(onClickListener);
this.addKeyListener(new KeyCheckAdaoter());
//this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
//Overridden so we can exit when window is closed
// protected void processWindowEvent(WindowEvent e) {
// super.processWindowEvent(e);
// if (e.getID() == WindowEvent.WINDOW_CLOSING) {
// System.exit(0);
// }
//}
public void started(){
//System.out.println("started-------------------------");
this.curLevel=1;
thread_gameover=null;
startTime=System.currentTimeMillis();//讀取游戲開始時間
thread_computer=new ComputerThread();
thread_computer.start();
int j=0;
int[] arrposflag=new int[showCount];
for(int i=0;i<showCount;i++){
arrposflag[i]=0;//開始所有線程
games[i].start();
//System.out.println("showCount-------------------------"+String.valueOf(i));
}
GamePanel temp=null;
for(int i=0;i<showCount;i++){
while(true){
j=GamePanel.random.nextInt(showCount);
if(arrposflag[j]==0){
arrposflag[j]=1;
if(temp!=null){
try{
Thread.sleep(1500);
}catch(Exception e){
}
}
//System.out.println("games[j]-------------------------"+games[j]);
temp=games[j];
games[j].Down(true);
break;
}
}
}
}
private class GameOverThread extends Thread{
public void run(){
Play.this.backSound.stop();
GamePanel.gameOver.play();
Play.this.stop();
jbtn_start.setText("開始");
JDialog dialog=new JDialog();
dialog.setSize(200,100);
dialog.setLocation((Play.this.getWidth()-dialog.getWidth())/2,
(Play.this.getHeight()-dialog.getHeight())/2);
dialog.setVisible(true);
}
}
private void computer(){
//計算速度
if(GamePanel.GameOver){
if(thread_gameover==null){
thread_gameover=new GameOverThread();
thread_gameover.start();
}
return;
}
int this_rate=0;
int this_hits=0;
for(int i=0;i<showCount;i++){
this_rate+=games[i].getShowAllCount();
this_hits+=games[i].getHitsCount();
}
this.labels[1].setText(Integer.toString(this_hits)+"/"+Integer.toString(this_rate));
//計算時間
this.currentTime=System.currentTimeMillis();
int thisTime=(int)((this.currentTime-this.startTime)/1000);
this.labels[5].setText(Integer.toString(thisTime)+"秒");
//計算錯誤
this.labels[3].setText(Integer.toString(this.errCount));
//計算分數
this.mark=this_hits*60/thisTime;
this.labels[7].setText(Integer.toString(this.mark));
//計算級別
//System.out.println("計算級別");
this.level=(this.mark-this.baseMark)/10;//10個字符為一級
if(this.level<=0||this_hits<this.baseMark){
this.level=1;
}
if(this.level>this.curLevel){
Play.addLevel.play();
}else if(this.level<this.curLevel){
Play.subLevel.play();
}
this.curLevel=this.level;
GamePanel.addSpeed(this.level);
this.labels[9].setText(Integer.toString(this.level));
}
private void reComputer(){
this.labels[1].setText("0/0 ");
this.labels[3].setText("0 ");
this.labels[5].setText("0 ");
this.labels[7].setText("0 ");
this.labels[9].setText("0 ");
}
private class KeyCheckAdaoter extends KeyAdapter{
public void keyTyped(KeyEvent e){
char keychar;
keychar=e.getKeyChar();
for(int i=0;i<showCount;i++){
if (keychar==games[i].getChar()&&games[i].hasNomarl()){
games[i].imageflag=true;
return;
}
}
Play.this.errSound.play();
Play.this.errCount++;//錯誤的按鍵
}
}
private class OnClickAdapter implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==jbtn_start){
if(jbtn_start.getText().equals("開始")){
jbtn_start.setText("停止");
Play.this.threadStart();
}else{
jbtn_start.setText("開始");
Play.this.stop();
}
Play.this.requestFocus(true);
}else if(e.getSource()==jbtn_set){
Play.this.requestFocus(true);
}else if(e.getSource()==jbtn_pause){
if(jbtn_pause.getText().equals("暫停")){
jbtn_pause.setText("恢復");
Play.this.suspend();
}else{
jbtn_pause.setText("暫停");
Play.this.resume();
}
Play.this.requestFocus(true);
}else if(e.getSource()==jbtn_exit){
Play.this.requestFocus(true);
System.exit(0);
}
}
}
public static void main(String [] args){
Play this_play=new Play("我的打字游戲--作者:楊柯--版本號:V1.0",12);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -