一个小打字游戏java源码

package test.example;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioSystem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Vector;

public class TypeGameFrame extends JFrame implements Runnable {

 /**
  *
  */
 private static final long serialVersionUID = 112233445566L;

 JPanel contentPane;

 JPanel jMainPanel = new JPanel();

 // 开始、结束 按钮
 JButton jBtnStart = new JButton();
 JButton jBtnStop = new JButton();

 JSlider jSlider1 = new JSlider();

 JLabel jLblRate = new JLabel();
    JLabel jLblTypedResult = new JLabel();

 int count = 1, rapidity = 80;  // count 当前进行的个数, rapidity 游标的位置

 // 打字后的成功个数和失败个数
 int nTypedCorrectNum = 0;
 int nTypedErrorNum = 0;

 int rush[] = { 10, 20, 30 }; // 游戏每关的个数 可以自由添加.列 { 10 ,20 ,30 ,40,50}

 // 记录关数
 int rush_count = 0;

 // 随机出现的数字列表,可以自由添加
 char chrList[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
   'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
   'Z', '0','1', '2', '3', '4', '5', '6', '7', '8', '9' };
                   

 Vector number = new Vector();

 String paiduan = "true";

 AudioClip Musci_anjian, Music_shibai, Music_chenggong;

 public TypeGameFrame() {
  try {
   setDefaultCloseOperation(EXIT_ON_CLOSE);

   // -----------------声音文件---------------------
   Musci_anjian = Applet.newAudioClip(new File("sounds//anjian.wav").toURL());
   Music_shibai = Applet.newAudioClip(new File("sounds//shibai.wav").toURL());
   Music_chenggong = Applet.newAudioClip(new File("sounds//chenggong.wav").toURL());

   // ---------------------------------------
   jbInit();
  } catch (Exception exception) {
   exception.printStackTrace();
  }
 }

 /**
  * Component initialization.
  *
  * @throws java.lang.Exception
  */
 private void jbInit() throws Exception {
  contentPane = (JPanel) getContentPane();
  contentPane.setLayout(null);
  setSize(new Dimension(588, 530));
  setTitle("打字练习");
  ImageIcon icon = new ImageIcon("resource/images/poinfoedit.gif");
  setIconImage(icon.getImage());
  
  jMainPanel.setBorder(BorderFactory.createEtchedBorder());
  jMainPanel.setBounds(new Rectangle(4, 4, 573, 419));
  jMainPanel.setLayout(null);
    
  // 开始
  jBtnStart.setBounds(new Rectangle(277, 442, 89, 31));
  jBtnStart.setText("开始");
  jBtnStart.addActionListener(new TypeGameFrame_jBtnStart_actionAdapter(this));
  jBtnStart.addKeyListener(new MyListener());
  // 结束
  jBtnStop.setBounds(new Rectangle(408, 442, 89, 31));
  jBtnStop.setText("结束");
  jBtnStop.addActionListener(new TypeGameFrame_jBtnStop_actionAdapter(this));
  
  // 速度
  jSlider1.setBounds(new Rectangle(83, 448, 164, 21));
  jSlider1.setMaximum(100);
  jSlider1.setMinimum(1);
  jSlider1.setValue(50);
  jSlider1.addKeyListener(new MyListener());
  jSlider1.addChangeListener(new ChangeListener() {
   public void stateChanged(ChangeEvent e) {
    rapidity = jSlider1.getValue();
    byte nTmpRapidity = (byte) (101 - rapidity);
    jLblRate.setText("速度:" + nTmpRapidity);
   }
  });
  jLblRate.setText("速度");
  jLblRate.setBounds(new Rectangle(25, 451, 49, 18));
  
  // 打字结果
  jLblTypedResult.setText(" ");
  jLblTypedResult.setBounds(new Rectangle(414, 473, 171, 21));
  contentPane.add(jMainPanel);
  contentPane.add(jBtnStop);
  contentPane.add(jBtnStart);
  contentPane.add(jSlider1);
  contentPane.add(jLblRate);
  contentPane.add(jLblTypedResult);
  this.addKeyListener(new MyListener());
 }

 public void run() {
  number.clear();
  nTypedCorrectNum = 0;
  nTypedErrorNum = 0;
  paiduan = "true";
  while (count <= rush[rush_count]) {
   try {
    Thread t = new Thread(new Tthread());
    t.start();
    count += 1;
    Thread.sleep(1000 + (int) (Math.random() * 2000)); // 生产下组停顿时间
    // 最快1快.最慢2秒
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }

  while (true) { // 等待最后一个字符消失
   if (number.size() == 0) {
    break;
   }
  }

  if (nTypedCorrectNum == 0) { // 为了以后相除..如果全部正确或者错误就会出现错误. 所以..
   nTypedCorrectNum = 1;
  }
  if (nTypedErrorNum == 0) {
   nTypedErrorNum = 1;
  }

  if (paiduan.equals("true")) { // 判断是否是自然结束
   if (nTypedCorrectNum / nTypedErrorNum >= 2) {
    JOptionPane.showMessageDialog(null, "恭喜你过关了");
    rush_count += 1; // 自动加1关
    if (rush_count < rush.length) {
     if (rapidity > 10) { // 当速度大于10的时候在-5提加速度.怕速度太快
      rapidity -= 5;   // 速度自动减10毫秒
      jSlider1.setValue(rapidity); // 选择位置
     }
     Thread t = new Thread(this);
     t.start();
    } else {
     JOptionPane.showMessageDialog(null, "恭喜...您通关了!");
     rush_count = 0;
     count = 0;
    }
   } else {
    JOptionPane.showMessageDialog(null, "请您再接再励");
    rush_count = 0;
    count = 0;
   }
  } else {
   rush_count = 0;
   count = 0;
  }
 }

 public void jBtnStart_actionPerformed(ActionEvent e) {
  Thread t = new Thread(this);
  t.start();
 }

 public void jBtnStop_actionPerformed(ActionEvent e) {
  count = rush[rush_count] + 1;
  paiduan = "flase";
 }

 /**
  *
  * */
 class Tthread implements Runnable {
  public void run() {
   boolean fo = true;
   int Y = 0, X = 0;
   JLabel show = new JLabel();
   show.setFont(new java.awt.Font("宋体", Font.PLAIN, 43));
   jMainPanel.add(show);
   X = 10 + (int) (Math.random() * 400);
   String parameter = chrList[(int) (Math.random() * chrList.length)] + "";
   Bean bean = new Bean();
   bean.setParameter(parameter);
   bean.setShow(show);
   number.add(bean);
   show.setText(parameter);
   while (fo) {
    // ---------------------数字下移--------------------
    show.setBounds(new Rectangle(X, Y += 2, 33, 33));
    try {
     Thread.sleep(rapidity);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    if (Y >= 419) {
     fo = false;
     for (int i = number.size() - 1; i >= 0; i--) {
      Bean bn = ((Bean) number.get(i));
      if (parameter.equalsIgnoreCase(bn.getParameter())) {
       nTypedErrorNum += 1;
       jLblTypedResult.setText("正确:" + nTypedCorrectNum + "个,错误:" + nTypedErrorNum
         + "个");
       number.removeElementAt(i);
       Music_shibai.play();
       break;
      }
     }
    }
   }

  }
 }

 class MyListener extends KeyAdapter {
  public void keyPressed(KeyEvent e) {
   String uu = e.getKeyChar() + "";
   for (int i = 0; i < number.size(); i++) {
    Bean bean = ((Bean) number.get(i));
    if (uu.equalsIgnoreCase(bean.getParameter())) {
     nTypedCorrectNum += 1;
     number.removeElementAt(i);
     bean.getShow().setVisible(false);
     jLblTypedResult.setText("正确:" + nTypedCorrectNum + "个,错误:" + nTypedErrorNum + "个");
     Music_chenggong.play();
     break;
    }
   }

   Musci_anjian.play();
  }
 }

 public static void main(String[] args) {
  try {
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  } catch (Exception exception) {
   exception.printStackTrace();
  }
  
  // 创建窗口
  TypeGameFrame gameFrame = new TypeGameFrame();
  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  Dimension frameSize = gameFrame.getSize();
  if (frameSize.height > screenSize.height) {
   frameSize.height = screenSize.height;
  }
  if (frameSize.width > screenSize.width) {
   frameSize.width = screenSize.width;
  }
  gameFrame.setLocation((screenSize.width - frameSize.width) / 2,
    (screenSize.height - frameSize.height) / 2);
  gameFrame.setVisible(true);
 }
}

/**
 *  停止按钮适配器
 * */
class TypeGameFrame_jBtnStop_actionAdapter implements ActionListener {
 private TypeGameFrame adaptee;

 TypeGameFrame_jBtnStop_actionAdapter(TypeGameFrame adaptee) {
  this.adaptee = adaptee;
 }

 public void actionPerformed(ActionEvent e) {
  adaptee.jBtnStop_actionPerformed(e);
 }
}

/**
 *  开始按钮适配器
 * */
class TypeGameFrame_jBtnStart_actionAdapter implements ActionListener {
 private TypeGameFrame adaptee;

 TypeGameFrame_jBtnStart_actionAdapter(TypeGameFrame adaptee) {
  this.adaptee = adaptee;
 }

 public void actionPerformed(ActionEvent e) {
  adaptee.jBtnStart_actionPerformed(e);
 }
}

class Bean {
 String parameter = null;

 JLabel show = null;

 public JLabel getShow() {
  return show;
 }

 public void setShow(JLabel show) {
  this.show = show;
 }

 public String getParameter() {
  return parameter;
 }

 public void setParameter(String parameter) {
  this.parameter = parameter;
 }
}

 

一个小打字游戏java源码_第1张图片

你可能感兴趣的:(Java实用功能源码集锦)