网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  改进后的英文字母打字游戏     
  文章作者:未知  文章来源:水木森林  
  查看:114次  录入:管理员--2007-11-17  
 
  //以下代码在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=istypedsum+iswrongtypedsum+isomittedsum;
if(sum==0) { percent=0f; showpercent=0;}
else
{
percent=(float)istypedsum/sum;
showpercent=(int)(percent*100);
}
g.drawstring("击中"+istypedsum+" 错击"+iswrongtypedsum+" 漏掉"+isomittedsum+" 正确率"+showpercent+"%",200,200);
}
else
{
g.drawstring("击中"+istypedsum+" 错击"+iswrongtypedsum+" 漏掉"+isomittedsum+" 正确率"+showpercent+"%",200,200);
}
}
}

class c 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 d extends keyadapter
{
public void keypressed(keyevent e)
{
char key=e.getkeychar();
if(istyped(key))
{

}
else
{

}
}
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 stop()
{
newthread=null;
}
}

class letter
{
mypanel game;
final int max;
boolean let[];
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)
{
max=9; //将字母最多设置为9个。此数为不可改变的。
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++)
{
g.setcolor(mycolor[ran.nextint(2)]);
g.fill3drect(x[temp],y[temp],20,20,true);
g.setcolor(color.blue);
g.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;
}
}
 
 
上一篇: 分页javabean    下一篇: 改善编码风格(超级简单实用)
  相关文档
在java程序中实现对数字的格式化 11-17
web应用程序的测试与优化概述 11-16
tomcat下配置mysql数据库连接池 11-16
java多线程的优先级 11-17
关于spring中的aop的解释 11-17
使用jdk1.5封箱及拆箱功能时注意的问题 12-24
jsp学习系列之tomcat安装 11-17
netbeans或吞并creator 同室操戈为哪般? 11-17
中国年历算法和程式 11-17
从数据库中读出图片并显示的示例代码 11-17
java反射技术(二) 11-17
starsuite6.0 & sdk安装及配置指南-- linux篇 (以redhat8.0/ba.. 11-17
中间件--apache和tomcat的集群配置 01-30
java与正则表达式(2年级2) 11-17
java基础知识——java入门与加深二 11-17
jdk核心api--java列表对象的性能分析 01-22
用内嵌类减少 java 程序设计中的混乱 11-17
eclipsecon 2006--bea workshop获最佳商业开发工具 11-17
深入java中文问题及最优解决方法 11-17
确保正确的清除 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息