程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於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;
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved