/**
* 程序名:打字游戏
* 指导教师:苑鹏掣
* 源代码编写:李硕
*/
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.util.Random;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Timer;
public class Dazi extends JFrame
{
private Pan p = new Pan();
private JMenuBar jmb = new JMenuBar();
private JMenu jm1 = new JMenu("游戏");
private JMenu jm2 = new JMenu("帮助");
public static JMenuItem jmi11 = new JMenuItem("开始");
public static JMenuItem jmi15 = new JMenuItem("关闭");
public static JRadioButtonMenuItem jmi12=new JRadioButtonMenuItem("资料",true);
public static JRadioButtonMenuItem jmi13=new JRadioButtonMenuItem("恶梦");
public static JRadioButtonMenuItem jmi14=new JRadioButtonMenuItem("地狱");
public static ButtonGroup bg=new ButtonGroup();
public static JMenuItem jmi21 = new JMenuItem("帮助");
public static JMenuItem jmi22 = new JMenuItem("关于");
public Dazi()
{
super("打字游戏 ==> 十二星座 (Mr.L 作品)");
bg.add(jmi12);
bg.add(jmi13);
bg.add(jmi14);
jm1.add(jmi11);
jm1.addSeparator();
jm1.add(jmi12);
jm1.add(jmi13);
jm1.add(jmi14);
jm1.addSeparator();
jm1.add(jmi15);
jm2.add(jmi21);
jm2.addSeparator();
jm2.add(jmi22);
jmb.add(jm1);
jmb.add(jm2);
jmi11.addActionListener(p);
jmi12.addActionListener(p);
jmi13.addActionListener(p);
jmi14.addActionListener(p);
jmi15.addActionListener(p);
jmi21.addActionListener(p);
jmi22.addActionListener(p);
this.addKeyListener(p);
this.getContentPane().setLayout( new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(p, BorderLayout.CENTER);
this.getContentPane().add(jmb, BorderLayout.SOUTH);
this.setLocation(100,50);
this.setSize(800,666);
this.setVisible(true);
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
new Dazi();
}
}
class Pan extends JPanel implements KeyListener, ActionListener
{
public final int TIME_BG = 10;
public final int GAME_OVER = 50;
public final int VERY_GOOD = 600;
protected int exac = 0; //正确输入字母的数量
protected int exacerr = 0;//漏掉字母的数量
protected Timer timer = new Timer(1000, this);
protected int level = 8; //速度&&线程数(字母数量)
protected Random r = new Random();
protected int timerTemp = 0;
protected int timeexac = 0;
protected ImageIcon [] ii = new ImageIcon[26]; //26个英文字母
protected ImageIcon [] iiBG = new ImageIcon[12]; //背景
private ThreadDazi [] th ; //线程
public Pan()
{
for (int i = 0; i < iiBG.length; i++)
{
iiBG[i] = new ImageIcon((i+1) + ".jpg");
}
for (int i = 0; i < ii.length; i++)
{
ii[i] = new ImageIcon((char)('a'+i)+".jpg");
}
}
public void paintComponent(Graphics g) //画面板
{
super.paintComponent(g);
g.drawImage( iiBG[(timerTemp/TIME_BG)%12].getImage(),
0, 0, this.getWidth(), this.getHeight(), this );
if (this.exac >= VERY_GOOD)
{
g.setColor(Color.MAGENTA);
g.setFont(new Font("Fixedsys",Font.BOLD,100));
g.drawString("VERY GOOD",this.getWidth()/2-300,300);
g.setFont(new Font("楷体_GB2312",Font.BOLD,30));
g.setColor( Color.RED );
g.drawString("打字速度:" + timeexac + "个/分",
this.getWidth()/2-150,this.getHeight()-100);
return;
}
if (this.exacerr >= this.GAME_OVER)
{
g.setColor(Color.MAGENTA);
g.setFont(new Font("Fixedsys",Font.BOLD,100));
g.drawString("GAME OVER",this.getWidth()/2-300,300);
g.setFont(new Font("楷体_GB2312",Font.BOLD,30));
g.setColor( Color.RED );
g.drawString("还差" + (VERY_GOOD-exac) + "个就过关喽",
this.getWidth()/2-150,this.getHeight()-100);
return;
}
if (th != null)
{
for(int i = 0; i < th.length; i++)
{
g.drawImage(ii[th[i].getLetterID()].getImage(),
th[i].getX(), th[i].getY(),
ii[th[i].getLetterID()].getImageObserver());
}
}
g.setFont(new Font(null,Font.BOLD,20));
g.setColor( Color.yellow );
g.drawString("漏掉" + exacerr + "个",20,this.getHeight()-20);
g.drawString("正确" + exac + "个",
this.getWidth()-110,this.getHeight()-20);
g.drawString("时间" + timerTemp + "秒",
this.getWidth()/2-50,this.getHeight()-20);
g.setFont(new Font(null,Font.BOLD,30));
g.setColor( Color.GREEN );
g.drawString("您的打字速度:" + timeexac + "个/分",
this.getWidth()/2-180,50);
}
public void keyTyped(KeyEvent e)
{
int temp = -1;
for(int i = 0; i < th.length; i++)
{
if(th[i].getLetterID() == e.getKeyChar()-'a')
{
if (temp == -1)
{
temp = i;
}
else
{
if (th[i].getY() > th[temp].getY())
{
temp = i;
}
}
}
}
if (temp == -1)
{
exacerr++;
}
else
{
exac++;
th[temp].isOK();
}
}
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void actionPerformed(ActionEvent e)
{
if( e.getSource() == timer )
{
timeexac = Math.round((float)exac / (++timerTemp) * 60) ;
}
if( e.getSource() == Dazi.jmi11 ) //开始
{
exac = 0;
exacerr = 0;
timerTemp = 0;
timeexac = 0;
if (th != null)
{
for(int i = 0; i < th.length; i++)
{
while (th[i].isAlive())
{
th[i].stopThread();
try
{
Thread.currentThread().sleep(10);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}
if (timer.isCoalesce())
{
timer.start();
}
th = new ThreadDazi[level];
for(int i = 0; i < this.level; i++)
{
th[i] = new ThreadDazi(this, r.nextInt(ii.length),
r.nextInt(this.getWidth()-70)+10);
th[i].start();
}
}
if( e.getSource() == Dazi.jmi15) //结束
{
System.exit(0);
}
if( e.getSource() == Dazi.jmi12) //速度 初级
{
level = 8;
}
if( e.getSource() == Dazi.jmi13) //速度 中级
{
level = 12;
}
if( e.getSource() == Dazi.jmi14) //速度 高级
{
level = 16;
}
if( e.getSource() == Dazi.jmi21)
{
JOptionPane.showMessageDialog(this,
"本游戏可设置初级、中级、高级三个难度,\r\n"+
"难度增加,速度和字母数量也相应增加。\r\n"+
"正确输入"+VERY_GOOD+"个字母则胜利,\r\n"+
"如果漏掉"+GAME_OVER+"个字母则失败。",
"帮助", JOptionPane.QUESTION_MESSAGE);
}
if( e.getSource() == Dazi.jmi22)
{
JOptionPane.showMessageDialog(this,
"程序名:打字游戏\r\n"+
"指导教师:苑鹏掣\r\n"+
"源代码编写:李硕\r\n",
"关于", JOptionPane.WARNING_MESSAGE);
}
}
}
class ThreadDazi extends Thread //线程
{
private int id;
private Pan p;
private int x;
private int y;
private int speed;
private boolean bAlive = true;
private boolean isNotOK = true;
public ThreadDazi(Pan p,int id, int x)
{
this.p = p;
this.id = id;
this.x = x;
this.y = 0;
this.speed = p.r.nextInt(p.level)+1;
}
public void isOK()
{
this.isNotOK = false;
}
public void setX(int x)
{
this.x = x;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void setLetterID(int id)
{
this.id = id;
}
public int getLetterID()
{
return i