📄 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 + -