1 PackageCom.java7.mychar.main;2 ImportJava.awt.Color;3 ImportJava.awt.Frame;4 ImportJava.awt.Graphics;5 ImportJava.awt.Panel;6 Importjava.awt.event.KeyEvent;7 ImportJava.awt.event.KeyListener;8 9 Public classMyChar {Ten Public Static voidMain (string[] args) { OneFrame W =NewFrame (); AW.setsize (600, 800); - -Mypanel MP =NewMypanel (); the W.add (MP); - -Thread T =NewThread (MP); - T.start (); + - W.addkeylistener (MP); + Mp.addkeylistener (MP); A at w.show (); - } - } - - classMypanelextendsPanelImplementsRunnable, KeyListener { - intX[] =New int[10]; in intY[] =New int[10]; - CharC[] =New Char[10]; to intScore = 1000; + //Structure Method - Mypanel () { the for(inti = 0; I < 10; i++) { *X[i] = (int) (Math.random () * 580); $Y[i] = (int) (Math.random () * 600);Panax NotoginsengC[i] = (Char) (Math.random () * 26 + 97); - } the } + Public voidPaint (Graphics g) { A for(inti = 0; I < 10; i++) { the G.setcolor (color.blue); +g.DrawString (NewCharacter (C[i]). toString (), X[i], y[i]); - } $ G.setcolor (color.red); $g.DrawString ("Your grades are:" + score, 5, 15); - } - //use the thread's Repaint method to slide the letters from top to bottom the Public voidrun () { - while(true) {Wuyi for(inti = 0; I < 10; i++) { they[i]++; - //if the letter slip to the bottom is not eliminated, then return to the top to generate random letters, and buckle 100 points Wu if(Y[i] > 700) { -Y[i] = 0; AboutX[i] = (int) (Math.random () * 550); $C[i] = (Char) (Math.random () * 26 + 97); -Score-= 100; - } - } A Try { +Thread.Sleep (30); the}Catch(Exception e) { - $ } the repaint (); the } the } the @Override - Public voidkeypressed (KeyEvent arg0) { in //deposit user input characters into KEYC the CharKEYC =Arg0.getkeychar (); the intNowy =-1; About intNowindex =-1; the //scan the entire array to see if there are any matching characters the for(inti = 0; I < 10; i++) { the if(KEYC = = C[i]) {//if match (may match multiple simultaneously) + if(Y[i] > Nowy) {//Scan the index from 0-9 to find the maximum y-coordinate - //if a larger y-coordinate is found, assign the coordinate value to Nowy until Nowy = Y[i] theNowy =Y[i];BayiNowindex =i; the } the } - } - //eliminate the bottom match letters and regenerate random letters from the top the if(Nowindex! =-1) { theY[nowindex] = 0; theX[nowindex] = (int) (Math.random () * 580); theC[nowindex] = (Char) (Math.random () * 26 + 97); -Score + = 10; the}Else{// theScore-= 100; the } 94 } the @Override the Public voidkeytyped (KeyEvent e) { the //TODO auto-generated Method Stub98 About } - @Override101 Public voidkeyreleased (KeyEvent e) {102 //TODO auto-generated Method Stub103 104 } the}
Practice. Play Alphabet Games