Java实现的英文打字游戏
时间:2011-01-19
//编译:javac MyPanel.java
//运行:appletviewer MyPanel.html
//需要大家自写一个MyPanel.html
/*
<html>
<body bgcolor="pink">
<applet code="MyPanel.class" width="750" height="380">
</applet>
</body>
</html>
*/
//下面是MyPanel.java的源代码。www.bianceng.cn
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
public class MyPanel extends Applet implements Runnable
{
public int FPS;
public Thread newthread;
public static boolean swit;
public Image img;
Graphics off;
int width,height;
MouseListener ml=new A();
KeyListener kl=new B();
JPopupMenu jmp;
JMenuItem jmi;
letter myletter;
AudioClip A1,A2,A3;
Random r;
int isTypedSum;
int isOmittedSum;
int isWrongTypedSum;
float percent;
public void init()
{
this.setBackground(Color.pink);
this.setLayout(new FlowLayout());
FPS=100;
isTypedSum=isOmittedSum=isWrongTypedSum=0;
percent=0f;
width=this.getSize().width;
height=this.getHeight();
img=this.createImage(width,height);
off=img.getGraphics();
r=new Random();
addComponents();
A1=this.getAudioClip(this.getDocumentBase(),"Audio/AUDIO1.au");
A2=this.getAudioClip(this.getDocumentBase(),"Audio/AUDIO2.au");
A3=this.getAudioClip(this.getDocumentBase(),"Audio/AUDIO3.au");
}
public void addComponents()
{
this.addKeyListener(kl);
jmp=new JPopupMenu();
jmi=new JMenuItem("开始游戏");
jmi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
isTypedSum=isOmittedSum=isWrongTypedSum=0;
swit=true;
start();
A3.loop();
}
});
jmp.add(jmi);
jmi=new JMenuItem("结束游戏");
jmi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
stop();
swit=false;
A3.stop();
}
});
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);
this.addMouseListener(ml);
}
class A extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
showPopup(e
|