📄
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0065)http://www.liva.com.cn/web/articles/2004/9/17/1095376011671.shtml -->
<HTML><HEAD><TITLE>一个英文打字游戏</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312"><LINK
href="一个英文打字游戏.files/cool0.css" type=text/css rel=stylesheet>
<SCRIPT language=JavaScript>
function fontZoom(size)
{
document.getElementById('fontzoom').style.fontSize=size+'px'
}
</SCRIPT>
<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY>
<TABLE class=tabcss width="98%" align=center>
<TBODY>
<TR>
<TD class=bgimg align=middle colSpan=7 height=25><STRONG><FONT
size=4>一个英文打字游戏</FONT></STRONG></TD></TR>
<TR>
<TD height=25>
<DIV align=center>作者:未知 发布时间:2004-09-17 07:06 来源:未知</DIV></TD></TR>
<TR>
<TD align=middle colSpan=2 height=25>
<DIV align=right>【<A href="javascript:fontZoom(16)">大</A> <FONT
color=#ffffff><A href="javascript:fontZoom(14)">中</A> <A
href="javascript:fontZoom(12)">小</A></FONT>】【<A
href="javascript:print()">打 印</A>】【<A href="javascript:window.close()">关
闭</A>】</DIV></TD></TR>
<TR>
<TD id=fontzoom align=middle colSpan=2 height=6>
<DIV align=left>
<P style="LINE-HEIGHT: 200%">//编译:javac MyPanel.java <BR>//运行:appletviewer
MyPanel.html <BR>//需要大家自写一个MyPanel.html <BR>/* <BR><html>
<BR><body bgcolor="pink"> <BR><applet code="MyPanel.class"
width="750" height="380"> <BR></applet> <BR></body>
<BR></html> <BR>*/ <BR>//下面是MyPanel.java的源代码。MyQQ:18345277
<BR><BR>import java.awt.*; <BR>import java.awt.event.*; <BR>import
java.util.*; <BR>import java.applet.*; <BR>import javax.swing.*;
<BR><BR>public class MyPanel extends Applet implements Runnable <BR>{
<BR>public int FPS; <BR>public Thread newthread; <BR>public static boolean
swit; <BR>public Image img; <BR>Graphics off; <BR>int width,height;
<BR>MouseListener ml=new A(); <BR>KeyListener kl=new B(); <BR>JPopupMenu
jmp; <BR>JMenuItem jmi; <BR>letter myletter; <BR>AudioClip A1,A2,A3;
<BR>Random r; <BR>int isTypedSum; <BR>int isOmittedSum; <BR>int
isWrongTypedSum; <BR>float percent; <BR>public void init() <BR>{
<BR>this.setBackground(Color.pink); <BR>this.setLayout(new FlowLayout());
<BR>FPS=100; <BR>isTypedSum=isOmittedSum=isWrongTypedSum=0;
<BR>percent=0f; <BR>width=this.getSize().width;
<BR>height=this.getHeight(); <BR>img=this.createImage(width,height);
<BR>off=img.getGraphics(); <BR>r=new Random(); <BR>addComponents();
<BR>A1=this.getAudioClip(this.getDocumentBase(),"Audio/AUDIO1.au");
<BR>A2=this.getAudioClip(this.getDocumentBase(),"Audio/AUDIO2.au");
<BR>A3=this.getAudioClip(this.getDocumentBase(),"Audio/AUDIO3.au"); <BR>}
<BR>public void addComponents() <BR>{ <BR>this.addKeyListener(kl);
<BR>jmp=new JPopupMenu(); <BR>jmi=new JMenuItem("开始游戏");
<BR>jmi.addActionListener(new ActionListener() <BR>{ <BR>public void
actionPerformed(ActionEvent e) <BR>{
<BR>isTypedSum=isOmittedSum=isWrongTypedSum=0; <BR>swit=true; <BR>start();
<BR>A3.loop(); <BR>} <BR>}); <BR>jmp.add(jmi); <BR>jmi=new
JMenuItem("结束游戏"); <BR>jmi.addActionListener(new ActionListener() <BR>{
<BR>public void actionPerformed(ActionEvent e) <BR>{ <BR>stop();
<BR>swit=false; <BR>A3.stop(); <BR>} <BR>}); <BR>jmp.add(jmi);
<BR>jmp.addSeparator(); <BR>jmi=new JMenuItem("增加字母数字");
<BR>jmi.addActionListener(new ActionListener() <BR>{ <BR>public void
actionPerformed(ActionEvent e) <BR>{ <BR>if(myletter.exist_letter_num==9);
<BR>else <BR>myletter.exist_letter_num++; <BR>myletter.randomLetters();
<BR>} <BR>}); <BR>jmp.add(jmi); <BR>jmi=new JMenuItem("加快下落速度");
<BR>jmi.addActionListener(new ActionListener() <BR>{ <BR>public void
actionPerformed(ActionEvent e) <BR>{ <BR>for(int
i=0;i<myletter.exist_letter_num;i++) <BR>myletter.speed[i]++; <BR>}
<BR>}); <BR>jmp.add(jmi); <BR>jmp.addSeparator(); <BR>jmi=new
JMenuItem("减少字母数字"); <BR>jmi.addActionListener(new ActionListener() <BR>{
<BR>public void actionPerformed(ActionEvent e) <BR>{
<BR>if(myletter.exist_letter_num==1); <BR>else
<BR>myletter.exist_letter_num--; <BR>myletter.randomLetters(); <BR>}
<BR>}); <BR>jmp.add(jmi); <BR>jmi=new JMenuItem("减缓下落速度");
<BR>jmi.addActionListener(new ActionListener() <BR>{ <BR>public void
actionPerformed(ActionEvent e) <BR>{ <BR>for(int
i=0;i<myletter.exist_letter_num;i++) <BR>{
<BR>if(myletter.speed[i]>1) <BR>myletter.speed[i]--; <BR>} <BR>}
<BR>}); <BR>jmp.add(jmi); <BR>this.addMouseListener(ml); <BR>} <BR>class A
extends MouseAdapter <BR>{ <BR>public void mousePressed(MouseEvent e)
<BR>{ <BR>showPopup(e); <BR>} <BR>public void mouseReleased(MouseEvent e)
<BR>{ <BR>showPopup(e); <BR>} <BR>public void showPopup(MouseEvent e)
<BR>{ <BR>if(e.isPopupTrigger())
<BR>jmp.show(e.getComponent(),e.getX(),e.getY()); <BR>} <BR>} <BR>class B
extends KeyAdapter <BR>{ <BR>public void keyPressed(KeyEvent e) <BR>{
<BR>char key=e.getKeyChar(); <BR>if(isTyped(key)) <BR>{ <BR>A1.play();
<BR>} <BR>else <BR>{ <BR>A2.play(); <BR>} <BR>} <BR>public boolean
isTyped(char key) <BR>{ <BR>for(int
i=0;i<myletter.exist_letter_num;i++) <BR>{
<BR>if((char)(key-32)==myletter.cc[i].charAt(0)) <BR>{ <BR>isTypedSum++;
<BR>myletter.reStart(i); <BR>return true; <BR>} <BR>}
<BR>isWrongTypedSum++; <BR>return false; <BR>} <BR>} <BR>public void
start() <BR>{ <BR>newthread=new Thread(this); <BR>newthread.start();
<BR>myletter=new letter(this,off); <BR>myletter.randomLetters(); <BR>}
<BR>public void run() <BR>{ <BR>while(newthread!=null) <BR>{
<BR>this.repaint(); <BR>try <BR>{ <BR>Thread.sleep(FPS);
<BR>}catch(InterruptedException e) <BR>{
<BR>System.out.println(e.toString()); <BR>} <BR>} <BR>} <BR>public void
paint(Graphics g) <BR>{ <BR>int sum; <BR>int showPercent=0; <BR>if(swit)
<BR>{ <BR>off.setColor(this.getBackground());
<BR>off.fillRect(0,0,width,height); <BR>myletter.paintLetters(g);
<BR>off.setColor(Color.blue);
<BR>sum=isTypedSum+isWrongTypedSum+isOmittedSum; <BR>if(sum==0) {
percent=0f; showPercent=0;} <BR>else <BR>{
<BR>percent=(float)isTypedSum/sum; <BR>showPercent=(int)(percent*100);
<BR>} <BR>off.drawString("击中"+isTypedSum+" 错击"+isWrongTypedSum+"
漏掉"+isOmittedSum+" 正确率"+showPercent+"%",200,200);
<BR>g.drawImage(img,0,0,width,height,this); <BR>} <BR>else <BR>{
<BR>off.setColor(this.getBackground());
<BR>off.fillRect(0,0,width,height); <BR>off.drawString("击中"+isTypedSum+"
错击"+isWrongTypedSum+" 漏掉"+isOmittedSum+" 正确率"+showPercent+"%",200,200);
<BR>g.drawImage(img,0,0,width,height,this); <BR>} <BR>} <BR>public void
update(Graphics g){this.paint(g);} <BR>public void stop() <BR>{
<BR>newthread=null; <BR>} <BR>} <BR><BR>class letter <BR>{ <BR>MyPanel
game; <BR>final int Max; <BR>boolean let[]; <BR>Graphics off; <BR>int X[];
<BR>int Y[]; <BR>int speed[]; <BR>int exist_letter_num; <BR>int XY[];
<BR>int ini; <BR>StringBuffer c[]; <BR>String cc[]; <BR>Random ran=new
Random(); <BR>Color mycolor[]={Color.red,Color.green}; <BR>int aa[];
<BR>public letter(MyPanel game,Graphics off) <BR>{ <BR>Max=9;
//将字母最多设置为9个。此数为不可改变的。 <BR>this.off=off; <BR>this.game=game; <BR>let=new
boolean[Max]; <BR>XY=new int[Max]; <BR>ini=50; <BR>initArray();
<BR>exist_letter_num=3; //初始化,刚开始落下字母的个数。 <BR>} <BR>public void
initArray() <BR>{ <BR>for(int i=0;i<Max;i++) <BR>{ <BR>let[i]=false;
<BR>XY[i]=ini; <BR>ini+=70; <BR>} <BR>} <BR>public void randomLetters()
//随机产生n个不同数字的值。 <BR>{ <BR>X=new int[exist_letter_num]; <BR>Y=new
int[exist_letter_num]; <BR>speed=new int[exist_letter_num]; <BR>aa=new
int[100]; <BR>for(int
i=0,n=0;i<exist_letter_num;i++)//通过9个不同的位置来随机产生字母出现的坐标位置。 <BR>{
<BR>aa[n]=ran.nextInt(9); <BR>if(i!=0) <BR>{ <BR>while(check(aa,n)) <BR>{
<BR>aa[n]=ran.nextInt(9); <BR>} <BR>} <BR>X[i]=XY[aa[n]];
<BR>Y[i]=ran.nextInt(11)-10; <BR>speed[i]=ran.nextInt(8)+1;
<BR>let[aa[n]]=true; //保存下放字母的位置。 <BR>n++; <BR>} <BR>randomStrings();
<BR>} <BR>public void randomStrings() <BR>{ <BR>c=new
StringBuffer[exist_letter_num]; <BR>cc=new String[exist_letter_num];
<BR>while(true) <BR>{ <BR>for(int i=0;i<exist_letter_num;i++) <BR>{
<BR>c[i]=new StringBuffer(); <BR>cc[i]=new String();
<BR>c[i].setLength(1); <BR>c[i].setCharAt(0,(char)(ran.nextInt(26)+65));
<BR>cc[i]=""+c[i]; <BR>} <BR>if(checkChar(c)) <BR>break; <BR>} <BR>}
<BR>public boolean checkChar(StringBuffer c[]) <BR>{
<BR>if(exist_letter_num==1) return true; <BR>for(int
i=0;i<exist_letter_num-1;i++) <BR>for(int
j=i+1;j<exist_letter_num;j++) <BR>{ <BR>if(c[i].equals(c[j])) return
false; <BR>} <BR>return true; <BR>} <BR>public boolean check(int aa[],int
n) <BR>{ <BR>for(int i=0;i<n;i++) <BR>for(int j=i+1;j<=n;j++) <BR>{
<BR>if(aa[i]==aa[j]) return true; <BR>} <BR>return false; <BR>} <BR>public
void paintLetters(Graphics g) <BR>{ <BR><BR>for(int
temp=0;temp<exist_letter_num;temp++) <BR>{
<BR>off.setColor(mycolor[ran.nextInt(2)]);
<BR>off.fill3DRect(X[temp],Y[temp],20,20,true);
<BR>off.setColor(Color.blue);
<BR>off.drawString(cc[temp],X[temp]+5,Y[temp]+15);
<BR>Y[temp]+=speed[temp]; <BR>if(Y[temp]>game.height)
//当字母消失后,重新给初始位置和速度。 <BR>{ <BR>game.isOmittedSum++; <BR>reStart(temp);
<BR>} <BR>} <BR>} <BR>public void reStart(int temp) <BR>{
<BR>Y[temp]=ran.nextInt(11)-10; <BR>speed[temp]=ran.nextInt(8)+1;
<BR>reStartX(temp); <BR>reStartStr(temp); <BR>} <BR>public void
reStartX(int temp) <BR>{ <BR>int cause; <BR>Label:while(true) <BR>{
<BR>cause=ran.nextInt(9); <BR>for(int
i=0;(i<exist_letter_num)&(i!=temp);i++) <BR>{ <BR>if(cause==aa[i])
<BR>continue Label; <BR>} <BR>break; <BR>} <BR>X[temp]=XY[cause];
<BR>aa[temp]=cause; <BR>} <BR>public void reStartStr(int temp) <BR>{
<BR>StringBuffer sb; <BR>String s; <BR>Label2:while(true) <BR>{ <BR>sb=new
StringBuffer(); <BR>sb.setLength(1); <BR>s="";
<BR>sb.setCharAt(0,(char)(ran.nextInt(26)+65)); <BR>s+=sb; <BR>for(int
i=0;i<exist_letter_num&i!=temp;i++) <BR>{ <BR>if(s.equals(cc[i]))
<BR>continue Label2; <BR>} <BR>break; <BR>} <BR>cc[temp]=s; <BR>} <BR>}
<BR><BR><BR></P></DIV></TD></TR>
<TR>
<TD align=middle colSpan=2 height=25>
<DIV align=right>【<A href="javascript:fontZoom(16)">大</A> <A
href="javascript:fontZoom(14)">中</A> <A
href="javascript:fontZoom(12)">小</A>】【<A href="javascript:print()">打
印</A>】【<A href="javascript:window.close()">关
闭</A>】</DIV></TD></TR></TBODY></TABLE>
<P align=center>联系人:李艳生 柳晓伟 联系方式:李艳生:QQ[43871852] Email[wlxlys@163.com]
柳晓伟:QQ[9736770]刘恩义:QQ[176754284]</P>
<P align=center>Powered By : <A
href="http://www.phy.hbnu.edu.cn:8080/dsp">双狮新闻发布系统 Version 1.0</A> </P>
<P align=center>CopyRight© 2004 双狮资源网 版权所有</P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -