我的java学习之小游戏_ 打字母01
1 /** 2 程序太乱了 都不想看自己写的程序 还是得看呀 3 做一个打字母游戏 4 要素 5 窗体 JFrame 6 画布 JPanel 7 字母 坐标 数值 Point row cos value 8 下落 线程 9 消除 改变坐标 10 待解决 11 级别和加速那块得调整 12 到了某个级就保持一种加速(不因分数跌落而跌落到下一级) 13 加个倒计时 这一关还有多长时间 需要多少分过... 14 15 */ 16 import java.awt.*; 17 import java.awt.event.*; 18 import javax.swing.*; 19 20 21 public class WordDemo { 22 public static void main(String [] args) { 23 24 JFrame frame = new JFrame(); 25 26 frame.setSize(800, 600); 27 frame.setTitle("呵呵 这是一个练手的小游戏===打字母"); 28 29 WordPanel panel = new WordPanel(); 30 frame.add(panel); 31 32 new Thread(panel).start(); 33 34 //设置监听器 35 frame.addKeyListener(panel); 36 panel.addKeyListener(panel); 37 38 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 39 frame.setVisible(true); 40 } 41 } 42 43 class WordPanel extends JPanel implements Runnable, KeyListener{ 44 45 public WordPanel() { 46 for(int i=0; i<row.length; i++) { 47 row[i] = (int)(Math.random()*(800-(BORDER_SIZE<<1))); 48 cos[i] = (int)(Math.random()*(600-(BORDER_SIZE<<1))); 49 value[i] = (char)(Math.random()*26 + 'a'); 50 } 51 } 52 53 //绘图 54 public void paintComponent(Graphics g) { 55 super.paintComponent(g); 56 57 //画字母 58 g.setFont(new Font("regular", 0, 23)); 59 for(int i=0; i<row.length; i++) { 60 g.drawString(String.valueOf(value[i]), row[i], cos[i]); 61 } 62 63 //画消息提示 64 g.setColor(Color.red); 65 g.setFont(new Font("regular", 0, 35)); 66 g.drawString(returnStr+"此时您的积分是: " + score, 50, 50); 67 } 68 69 //等待线程的轮转 70 public void run() { 71 //少年 用点心 用你的脑袋去写 72 //注意逻辑 1.所有字母下移操作 2.休眠 3.重绘 73 while(true) { 74 for(int i=0; i<cos.length; i++) { 75 //元素下移 76 cos[i]++; 77 if(cos[i]>600){ 78 cos[i] = 0; 79 row[i] = (int)(Math.random()*(800-(BORDER_SIZE<<1))); 80 value[i] = (char)(Math.random()*26 + 'a'); 81 score -= 10; 82 } 83 } 84 85 //线程休眠 86 try{ 87 Thread.sleep(time); 88 }catch(Exception e){e.printStackTrace();} 89 //重绘 90 repaint(); 91 92 //判断级别 -->可修改为数组映射 以后调 93 switch(score / 100) { 94 case -1: 95 JOptionPane.showMessageDialog(null, msg[0], "呵呵", JOptionPane.INFORMATION_MESSAGE); 96 System.exit(0); 97 case 0: 98 returnStr = msg[1]; 99 time = 14; 100 break; 101 case 1: 102 returnStr = msg[2]; 103 time = 10; 104 break; 105 case 2: 106 returnStr = msg[3]; 107 time = 8; 108 break; 109 case 3: 110 returnStr = msg[4]; 111 time = 6; 112 break; 113 } 114 } 115 116 } 117 118 //下落操作-->事件监听 119 public void keyPressed(KeyEvent e) { 120 char receChar = (char)(e.getKeyChar()); 121 //设置标记 122 int index = -1; 123 int indexValue = -1; 124 for(int i=0; i<value.length; i++) { 125 if(receChar == value[i]) { 126 //获取最小cos的索引并保存在index中 127 if(indexValue < cos[i]){ 128 indexValue = cos[i]; 129 index = i; 130 } 131 } 132 } 133 134 if(index != -1) { 135 cos[index] = 0; 136 row[index] = (int)(Math.random()*(800-(BORDER_SIZE<<1))); 137 value[index] = (char)(Math.random()*26 + 'a'); 138 score += 10; 139 }else { 140 score -= 10; 141 } 142 } 143 144 public void keyReleased(KeyEvent e) {} 145 public void keyTyped(KeyEvent e) {} 146 private int row [] = new int[VALUE_LENGTH]; //横坐标 147 private int cos [] = new int[VALUE_LENGTH]; //纵坐标 148 private char value [] = new char[VALUE_LENGTH];//值 149 private static final int VALUE_LENGTH = 10; //字母个数 150 private static final int BORDER_SIZE = 20; //窗体边界长度 151 private int score = 0; //游戏计分项 152 private int level = 0; //游戏等级项 153 private int time = 18; //线程休眠时间 154 private String [] msg = {"你太垃圾了...Game Over", "哦","不错","可以啊","膜拜","不玩了"}; 155 private String returnStr; 156 }
程序太乱了 都不想看自己写的程序 还是得看呀
	做一个打字母游戏
	要素
		  窗体 JFrame   画一个窗体 
		  画布 JPanel   窗体上加载一个面板 然后在面板上作画
		  字母 坐标 数值  Point row cos value
		  下落 线程 加速  
		  消除 改变坐标 
	待解决
		  级别和加速那块得调整
		  到了某个级就保持一种加速(不因分数跌落而跌落到下一级)
		  加个倒计时 这一关还有多长时间 需要多少分过...
                    
                
                
            
        
浙公网安备 33010602011771号