改进后的Java英文字母打字游戏
时间:2011-01-19
//以下代码在JDK1.4下通过
//编译:javac MyPanel.java
//运行:java MyPanel
//Made By Qiukai
//注意:启动后,在窗口中点鼠标反键选择开始游戏进行
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class MyPanel extends JFrame
{
public int FPS;
public Thread newthread;
public static boolean swit;
MouseListener ml=new C();
KeyListener kl=new D();
JPopupMenu jmp;
JMenuItem jmi;
letter myletter;
Random r;
int isTypedSum;
int isOmittedSum;
int isWrongTypedSum;
int width,height;
float percent;
Toolkit KT;
public static void main(String args[])
{
new MyPanel();
}
public MyPanel()
{
KT=this.getToolkit();
width=KT.getScreenSize().width;
height=KT.getScreenSize().height;
this.setSize(new Dimension(width,height));
this.setContentPane(new A());
this.show();
FPS=100;
isTypedSum=isOmittedSum=isWrongTypedSum=0;
percent=0f;
r=new Random();
}
class A extends JPanel implements Runnable
{
public A()
{
this.setBackground(Color.pink);
addComponents();
sta();
}
public void sta()
{
newthread=new Thread(this);
newthread.start();
myletter=new letter(MyPanel.this);
myletter.randomLetters();
}
public void run()
{
while(newthread!=null)
{
this.repaint();
try
{
Thread.sleep(FPS);
}catch(InterruptedException e)
{
System.out.println(e.toString());
}
}
}
public void addComponents()
{
MyPanel.this.addKeyListener(kl);
jmp=new JPopupMenu();
jmi=new JMenuItem("开始游戏");
jmi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
isTypedSum=isOmittedSum=isWrongTypedSum=0;
swit=true;
sta();
}
});
jmp.add(jmi);
jmi=new JMenuItem("结束游戏");
jmi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
stop();
swit=false;
}
});
jmp.add(jmi);
jmp.addSeparator();
jmi=new JMenuItem("增加字母数字");
jmi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(myletter.exist_letter_num==9);
else
myletter.exist_letter_num++;
myletter.randomLetters();
}
});
jmp.add(jmi);
jmi=new JMenuItem("加快下落速度");
jmi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<myletter.exist_letter_num;i++)
myletter.speed[i]++;
}
});
jmp.add(jmi);
jmp.addSeparator();
jmi=new JMenuItem("减少字母数字");
jmi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(myletter.exist_letter_num==1);
else
myletter.exist_letter_num--;
myletter.randomLetters();
}
});
jmp.add(jmi);
jmi=new JMenuItem("减缓下落速度");
jmi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<myletter.exist_letter_num;i++)
{
if(myletter.speed[i]>1)
myletter.speed[i]--;
}
}
});
jmp.add(jmi);
MyPanel.this.addMouseListener(ml);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int sum;
int showPercent=0;
if(swit)
{
myletter.paintLetters(g);
sum=isT
|