WEB开发网
开发学院WEB开发Jsp 改进后的英文字母打字游戏 阅读

改进后的英文字母打字游戏

 2008-01-05 08:48:46 来源:WEB开发网   
核心提示://以下代码在JDK1.4下通过 //编译:javac MyPanel.java //运行:java MyPanel //Made By Qiukai //注重:启动后,在窗口中点鼠标反键选择开始游戏进行 import java.awt.*; import java.awt.event.*; import java.u

  //以下代码在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;
}
}

Tags:改进 英文 字母

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接