当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:13:00  来源:本站整理

改良后的Java英文字母打字游戏[Java编程]

赞助商链接



  本文“改良后的Java英文字母打字游戏[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

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


  以上是“改良后的Java英文字母打字游戏[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 改良后的Java英文字母打字游戏
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .