您好,欢迎访问三七文档
importjava.awt.*;importjava.awt.event.*;importjava.util.*;importjavax.swing.*;publicclassMyPanelextendsJFrame{publicintFPS;publicThreadnewthread;publicstaticbooleanswit;MouseListenerml=newC();KeyListenerkl=newD();JPopupMenujmp;JMenuItemjmi;lettermyletter;Randomr;intisTypedSum;intisOmittedSum;intisWrongTypedSum;intwidth,height;floatpercent;ToolkitKT;publicstaticvoidmain(Stringargs[]){newMyPanel();}publicMyPanel(){KT=this.getToolkit();width=KT.getScreenSize().width;height=KT.getScreenSize().height;this.setSize(newDimension(width,height));this.setContentPane(newA());this.show();FPS=100;isTypedSum=isOmittedSum=isWrongTypedSum=0;percent=0f;r=newRandom();}classAextendsJPanelimplementsRunnable{publicA(){this.setBackground(Color.pink);addComponents();sta();}publicvoidsta(){newthread=newThread(this);newthread.start();myletter=newletter(MyPanel.this);myletter.randomLetters();}publicvoidrun(){while(newthread!=null){this.repaint();try{Thread.sleep(FPS);}catch(InterruptedExceptione){System.out.println(e.toString());}}}publicvoidaddComponents(){MyPanel.this.addKeyListener(kl);jmp=newJPopupMenu();jmi=newJMenuItem(开始游戏);jmi.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){isTypedSum=isOmittedSum=isWrongTypedSum=0;swit=true;sta();}});jmp.add(jmi);jmi=newJMenuItem(结束游戏);jmi.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){stop();swit=false;}});jmp.add(jmi);jmp.addSeparator();jmi=newJMenuItem(增加字母数字);jmi.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){if(myletter.exist_letter_num==9);elsemyletter.exist_letter_num++;myletter.randomLetters();}});jmp.add(jmi);jmi=newJMenuItem(加快下落速度);jmi.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){for(inti=0;imyletter.exist_letter_num;i++)myletter.speed[i]++;}});jmp.add(jmi);jmp.addSeparator();jmi=newJMenuItem(减少字母数字);jmi.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){if(myletter.exist_letter_num==1);elsemyletter.exist_letter_num--;myletter.randomLetters();}});jmp.add(jmi);jmi=newJMenuItem(减缓下落速度);jmi.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){for(inti=0;imyletter.exist_letter_num;i++){if(myletter.speed[i]1)myletter.speed[i]--;}}});jmp.add(jmi);MyPanel.this.addMouseListener(ml);}publicvoidpaintComponent(Graphicsg){super.paintComponent(g);intsum;intshowPercent=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);}}}classCextendsMouseAdapter{publicvoidmousePressed(MouseEvente){showPopup(e);}publicvoidmouseReleased(MouseEvente){showPopup(e);}publicvoidshowPopup(MouseEvente){if(e.isPopupTrigger())jmp.show(e.getComponent(),e.getX(),e.getY());}}classDextendsKeyAdapter{publicvoidkeyPressed(KeyEvente){charkey=e.getKeyChar();if(isTyped(key)){}else{}}publicbooleanisTyped(charkey){for(inti=0;imyletter.exist_letter_num;i++){if((char)(key-32)==myletter.cc[i].charAt(0)){isTypedSum++;myletter.reStart(i);returntrue;}}isWrongTypedSum++;returnfalse;}}publicvoidstop(){newthread=null;}}classletter{MyPanelgame;finalintMax;booleanlet[];intX[];intY[];intspeed[];intexist_letter_num;intXY[];intini;StringBufferc[];Stringcc[];Randomran=newRandom();Colormycolor[]={Color.red,Color.green};intaa[];publicletter(MyPanelgame){Max=9;//将字母最多设置为9个。此数为不可改变的。this.game=game;let=newboolean[Max];XY=newint[Max];ini=50;initArray();exist_letter_num=3;//初始化,刚开始落下字母的个数。}publicvoidinitArray(){for(inti=0;iMax;i++){let[i]=false;XY[i]=ini;ini+=70;}}publicvoidrandomLetters()//随机产生n个不同数字的值。{X=newint[exist_letter_num];Y=newint[exist_letter_num];speed=newint[exist_letter_num];aa=newint[100];for(inti=0,n=0;iexist_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();}publicvoidrandomStrings(){c=newStringBuffer[exist_letter_num];cc=newString[exist_letter_num];while(true){for(inti=0;iexist_letter_num;i++){c[i]=newStringBuffer();cc[i]=newString();c[i].setLength(1);c[i].setCharAt(0,(char)(ran.nextInt(26)+65));cc[i]=+c[i];}if(checkChar(c))break;}}publicbooleancheckChar(StringBufferc[]){if(exist_letter_num==1)returntrue;for(inti=0;iexist_letter_num-1;i++)for(intj=i+1;jexist_letter_num;j++){if(c[i].equals(c[j]))returnfalse;}returntrue;}publicbooleancheck(intaa[],intn){for(inti=0;in;i++)for(intj=i+1;j=n;j++){if(aa[i]==aa[j])returntrue;}returnfalse;}publicvoidpaintLetters(Graphicsg){for(inttemp=0;tempexist_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[te
本文标题:JAVA打字游戏
链接地址:https://www.777doc.com/doc-4805552 .html