服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

偶自己做的一个英文打字游戏


  //编译: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的源代码。myqq:18345277

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);
}
public void mousereleased(mouseevent e)
{
showpopup(e);
}
public void showpopup(mouseevent e)
{
if(e.ispopuptrigger())
jmp.show(e.getcomponent(),e.getx(),e.gety());
}
}
class b extends keyadapter
{
public void keypressed(keyevent e)
{
char key=e.getkeychar();
if(istyped(key))
{
a1.play();
}
else
{
a2.play();
}
}
public boolean istyped(char key)
{
for(int i=0;i<myletter.exist_letter_num;i++)
{
if((char)(key-32)==myletter.cc[i].charat(0))
{
istypedsum++;
myletter.restart(i);
return true;
}
}
iswrongtypedsum++;
return false;
}
}
public void start()
{
newthread=new thread(this);
newthread.start();
myletter=new letter(this,off);
myletter.randomletters();
}
public void run()
{
while(newthread!=null)
{
this.repaint();
try
{
thread.sleep(fps);
}catch(interruptedexception e)
{
system.out.println(e.tostring());
}
}
}
public void paint(graphics g)
{
int sum;
int showpercent=0;
if(swit)
{
off.setcolor(this.getbackground());
off.fillrect(0,0,width,height);
myletter.paintletters(g);
off.setcolor(color.blue);
sum=istypedsum+iswrongtypedsum+isomittedsum;
if(sum==0) { percent=0f; showpercent=0;}
else
{
percent=(float)istypedsum/sum;
showpercent=(int)(percent*100);
}
off.drawstring("击中"+istypedsum+" 错击"+iswrongtypedsum+" 漏掉"+isomittedsum+" 正确率"+showpercent+"%",200,200);
g.drawimage(img,0,0,width,height,this);
}
else
{
off.setcolor(this.getbackground());
off.fillrect(0,0,width,height);
off.drawstring("击中"+istypedsum+" 错击"+iswrongtypedsum+" 漏掉"+isomittedsum+" 正确率"+showpercent+"%",200,200);
g.drawimage(img,0,0,width,height,this);
}
}
public void update(graphics g){this.paint(g);}
public void stop()
{
newthread=null;
}
}

class letter
{
mypanel game;
final int max;
boolean let[];
graphics off;
int x[];
int y[];
int speed[];
int exist_letter_num;
int xy[];
int ini;
stringbuffer c[];
string cc[];
random ran=new random();
color mycolor[]={color.red,color.green};
int aa[];
public letter(mypanel game,graphics off)
{
max=9; //将字母最多设置为9个。此数为不可改变的。
this.off=off;
this.game=game;
let=new boolean[max];
xy=new int[max];
ini=50;
initarray();
exist_letter_num=3; //初始化,刚开始落下字母的个数。
}
public void initarray()
{
for(int i=0;i<max;i++)
{
let[i]=false;
xy[i]=ini;
ini+=70;
}
}
public void randomletters() //随机产生n个不同数字的值。
{
x=new int[exist_letter_num];
y=new int[exist_letter_num];
speed=new int[exist_letter_num];
aa=new int[100];
for(int i=0,n=0;i<exist_letter_num;i++)//通过9个不同的位置来随机产生字母出现的坐标位置。
{
aa[n]=ran.nextint(9);
if(i!=0)
{
while(check(aa,n))
{
aa[n]=ran.nextint(9);
}
}
x[i]=xy[aa[n]];
y[i]=ran.nextint(11)-10;
speed[i]=ran.nextint(8)+1;
let[aa[n]]=true; //保存下放字母的位置。
n++;
}
randomstrings();
}
public void randomstrings()
{
c=new stringbuffer[exist_letter_num];
cc=new string[exist_letter_num];
while(true)
{
for(int i=0;i<exist_letter_num;i++)
{
c[i]=new stringbuffer();
cc[i]=new string();
c[i].setlength(1);
c[i].setcharat(0,(char)(ran.nextint(26)+65));
cc[i]=""+c[i];
}
if(checkchar(c))
break;
}
}
public boolean checkchar(stringbuffer c[])
{
if(exist_letter_num==1) return true;
for(int i=0;i<exist_letter_num-1;i++)
for(int j=i+1;j<exist_letter_num;j++)
{
if(c[i].equals(c[j])) return false;
}
return true;
}
public boolean check(int aa[],int n)
{
for(int i=0;i<n;i++)
for(int j=i+1;j<=n;j++)
{
if(aa[i]==aa[j]) return true;
}
return false;
}
public void paintletters(graphics g)
{

for(int temp=0;temp<exist_letter_num;temp++)
{
off.setcolor(mycolor[ran.nextint(2)]);
off.fill3drect(x[temp],y[temp],20,20,true);
off.setcolor(color.blue);
off.drawstring(cc[temp],x[temp]+5,y[temp]+15);
y[temp]+=speed[temp];
if(y[temp]>game.height) //当字母消失后,重新给初始位置和速度。
{
game.isomittedsum++;
restart(temp);
}
}
}
public void restart(int temp)
{
y[temp]=ran.nextint(11)-10;
speed[temp]=ran.nextint(8)+1;
restartx(temp);
restartstr(temp);
}
public void restartx(int temp)
{
int cause;
label:while(true)
{
cause=ran.nextint(9);
for(int i=0;(i<exist_letter_num)&(i!=temp);i++)
{
if(cause==aa[i])
continue label;
}
break;
}
x[temp]=xy[cause];
aa[temp]=cause;
}
public void restartstr(int temp)
{
stringbuffer sb;
string s;
label2:while(true)
{
sb=new stringbuffer();
sb.setlength(1);
s="";
sb.setcharat(0,(char)(ran.nextint(26)+65));
s+=sb;
for(int i=0;i<exist_letter_num&i!=temp;i++)
{
if(s.equals(cc[i]))
continue label2;
}
break;
}
cc[temp]=s;
}
}

扫描关注微信公众号