Swing simple typing game source code, swing typing game
The Code is as follows:
Import java. awt. *; import java. awt. event. *; public class Main {public static Frame f = new Frame (); public static void main (String [] agrs) {f. setSize (500,400); f. setLocation (Toolkit. getdefatooltoolkit (). getScreenSize (). width-f.getSize (). width)/2, (Toolkit. getdefatooltoolkit (). getScreenSize (). height-f.getSize (). height)/2); f. setTitle ("swing simple typing game"); f. setBackground (Color. black); Mypanel mp = new Mypane L (); f. add (mp); Thread t = new Thread (mp); t. start (); f. addKeyListener (mp); mp. addKeyListener (mp); mp. addMouseListener (mp); f. setVisible (true); f. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {System. exit (0) ;}}) ;}} class Mypanel extends Panel implements Runnable, KeyListener, MouseListener {private static final long serialVersionUID = 1L; int x [] = new int [20]; int y [] = New int [20]; char c [] = new char [20]; int score = 1000; int level = 1; boolean flag = true; public Mypanel () {for (int I = 0; I <20; I ++) {x [I] = (int) (Math. random () * 1020); y [I] = (int) (Math. random () * 768)-700; c [I] = (char) (Math. random () * 26 + 65);} public void paint (Graphics g) {g. setColor (Color. BLUE); g. setFont (new Font ("", Font. BOLD, 30); if (flag = false) {g. setColor (Color. GREEN); g. setFont (new Font ("", Font. B OLD, 50); g. drawString ("Over", Main. f. getSize (). width/2, Main. f. getSize (). height/2);} for (int I = 0; I <20; I ++) {g. drawString (c [I] + "", x [I], y [I]);} g. drawString ("score:" + score, 20, 50); g. drawString ("level:" + level, 50);} public void run () {while (flag) {for (int I = 0; I <20; I ++) {y [I] ++; if (y [I]> 768) {y [I] = 0; x [I] = (int) (Math. random () * (Main. f. getSize (). width); c [I] = (char) (Math. random () * 26 + 65); score-= 100; if (SC Ore <= 0) {score = 0; flag = false; c = new char [20] ;}} try {Thread. sleep (20-3 * level);} catch (InterruptedException e) {e. printStackTrace ();} this. repaint () ;}} public void mouseClicked (MouseEvent e) {System. out. println (e. getX (); System. out. println (e. getY ();} public void keyPressed (KeyEvent e) {int maxY =-1000; int index =-1; if (e. getKeyCode ()> = e. VK_A & e. getKeyCode () <= e. VK_Z) {for (int I = 0; I <20; I ++) {if (e. getKeyCode () = c [I]) {if (y [I]> maxY) {maxY = y [I]; index = I ;}} if (index! =-1) {y [index] = 0; x [index] = (int) (Math. random () * (Main. f. getSize (). width); c [index] = (char) (Math. random () * 26 + 65); score + = 50; if (score-500)/500> level) {level ++ ;}} else {score-= 100; if (score <= 0) {score = 0; flag = false; c = new char [20];} repaint () ;}} public void keyReleased (KeyEvent arg0) {} public void keyTyped (KeyEvent arg0) {} public void mouseEntered (MouseEvent arg0) {} public void mouseExited (MouseEvent arg0) {} public void mousePressed (MouseEvent arg0) {} public void mouseReleased (MouseEvent arg0 ){}}
<Ignore_js_op>
Http://java.662p.com/thread-3684-1-1.html