首页 > 代码库 > 练习.打字母游戏

练习.打字母游戏

  1 package com.java7.mychar.main;  2 import java.awt.Color;  3 import java.awt.Frame;  4 import java.awt.Graphics;  5 import java.awt.Panel;  6 import java.awt.event.KeyEvent;  7 import java.awt.event.KeyListener;  8 import javax.swing.JFrame;  9 import javax.swing.JPanel; 10  11 public class MyChar { 12     public static void main(String[] args) { 13         JFrame w = new JFrame(); 14         w.setSize(600, 800); 15          16         MyPanel mp = new MyPanel(); 17         w.add(mp); 18          19         Thread t = new Thread(mp); 20         t.start(); 21          22         w.addKeyListener(mp); 23         mp.addKeyListener(mp); 24          25         w.setVisible(true); 26     } 27 } 28  29 class MyPanel extends JPanel implements Runnable , KeyListener { 30     int x[] = new int[10]; 31     int y[] = new int[10]; 32     char c[] = new char[10]; 33     int score = 1000; 34     // structure method 35     MyPanel() { 36         for(int i = 0; i < 10; i++) { 37             x[i] = (int)(Math.random() * 580); 38             y[i] = (int)(Math.random() * 600); 39             c[i] = (char)(Math.random() * 26 + 97); 40         } 41     } 42     public void paint(Graphics g) { 43         super.paint(g); 44         for(int i = 0; i < 10; i++) { 45             g.setColor(Color.BLUE); 46             g.drawString(new Character(c[i]).toString(), x[i], y[i]); 47         } 48         g.setColor(Color.RED); 49         g.drawString("你的成绩是:" + score, 5, 15); 50     } 51     // 利用线程的repaint方法,让字母由上至下滑落 52     public void run() { 53         while(true) { 54             for(int i = 0; i < 10; i++) { 55                 y[i]++; 56                 // 如字母滑到底部没有被消除,则重新回到顶部生成随机字母,并扣100分 57                 if(y[i] > 700) { 58                     y[i] = 0; 59                     x[i] = (int)(Math.random() * 550); 60                     c[i] = (char)(Math.random() * 26 + 97); 61                     score -= 100; 62                 } 63             } 64             try { 65                 Thread.sleep(30); 66             } catch(Exception e) { 67                  68             } 69             repaint(); 70         } 71     } 72     @Override 73     public void keyPressed(KeyEvent arg0) { 74         // 将用户输入的字符存入keyC中 75         char keyC = arg0.getKeyChar(); 76         int nowY = -1; 77         int nowIndex = -1; 78         // 扫描整个数组,看有没有匹配的字符 79         for(int i = 0; i < 10; i++) {  80             if(keyC == c[i]) { // 如果匹配(可能同时匹配多个) 81                 if(y[i] > nowY) { // 从0-9扫描索引,寻找最大的y坐标 82                     // 若发现更大的y坐标,则将该坐标值赋给nowY,直至nowY => y[i] 83                     nowY = y[i];  84                     nowIndex = i; 85                 } 86             } 87         } 88         // 消除最底下的匹配字母并从顶部重新生成随机字母 89         if(nowIndex != -1) { 90             y[nowIndex] = 0; 91             x[nowIndex] = (int)(Math.random() * 580); 92             c[nowIndex] = (char)(Math.random() * 26 + 97); 93             score += 10; 94         } else { //  95                 score -= 100; 96             }             97     } 98     @Override 99     public void keyTyped(KeyEvent e) {100         // TODO Auto-generated method stub101         102     }103     @Override104     public void keyReleased(KeyEvent e) {105         // TODO Auto-generated method stub106         107     }108 }

 

练习.打字母游戏