案例:打字遊戲

代碼奉上 

package day08;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 打字遊戲
 */
public class CharGame {
	public static void main(String [] args){
		//1.窗體
		JFrame frame = new JFrame();
		frame.setTitle("CharGame");
		frame.setSize(500,600);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//2.面板
		MyCharGamePanel mcgp = new MyCharGamePanel();
		mcgp.setBackground(new Color(180,247,235));//RGB
		frame.add(mcgp);
		//調用init方法
		mcgp.init();
		//添加鍵盤監聽
		mcgp.addKeyListener(mcgp);
		frame.addKeyListener(mcgp);
		frame.setVisible(true);
		//啓動字母的下落
		mcgp.run();
	}
}
//自定義面板類
class MyCharGamePanel extends JPanel implements KeyListener{
	//定義Random對象
	Random random = new Random();
	//定義保存字母的數組
	char [] cs = new char[15];
	//定義保存x座標的數組
	int [] xx = new int[15];
	//定義保存y座標的數組
	int [] yy = new int[15];
	//定義保存顏色的數組
	Color [] colors = new Color[15];
	//定義保存字母方向的數組
	int [] dirs = new int[15];
	//定義遊戲的分數
	int score = 1000 ;
	//定義初始化數據的方法
	public void init(){
		// 65   97+26
		for(int i=0 ;i 0?score:0), 160, 30);
		//畫結束狀態
		if(score<0){
			g.setColor(Color.RED);
			g.setFont(new Font(Font.SERIF,Font.BOLD,35));
			g.drawString("分數:"+0, 160, 30);
			g.setFont(new Font(Font.SERIF,Font.BOLD,90));
			g.drawString("Game Over", 20, 300);
		}
		
	}
	//將字母下落的邏輯封裝成一個方法
	public void run(){
		while(score>=0){
			//判斷方向,控制字母的座標
			for(int i=0 ;i=470){
					dirs[i] =1;
				}
				//判斷出界
				if(yy[i]>600){
					//減分
					score-=100;
					//重新生成
					cs[i]=(char)(random.nextInt(26)+97);
					//初始化x,y座標
					xx[i] = random.nextInt(450)+20;//[0,460)+20=[20,480)
					yy[i] = 0;
					//RGB
					colors[i]=new Color(
							random.nextInt(256),
							random.nextInt(256),
							random.nextInt(256));
					//dirs
					// 0垂直下落  1左下  2 右下 
					dirs[i]= random.nextInt(3);
				}
			}
			//重繪
			repaint();
			//休息一會
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		repaint();
	}
	//鍵盤按下
	public void keyPressed(KeyEvent e) {
		//獲取按下的字符
		char c = e.getKeyChar();
		System.out.println("c:"+c);
		//判斷是否擊中字母
		//定義保存打中字母的下標
		int nowIndex = -1 ;
		//定義保存打中字母的y座標
		int nowY = -1 ;
		
		for(int i=0;inowY){
					nowIndex = i;
					nowY = yy[i];
				}
			}
		}
		//通過isShoot的結果判斷加減分
		if(nowIndex !=-1){
			score +=10;
			//
//			重新生成
			cs[nowIndex]=(char)(random.nextInt(26)+97);
			//初始化x,y座標
			xx[nowIndex] = random.nextInt(450)+20;//[0,460)+20=[20,480)
			yy[nowIndex] = 0;
			//RGB
			colors[nowIndex]=new Color(
					random.nextInt(256),
					random.nextInt(256),
					random.nextInt(256));
			//dirs
			// 0垂直下落  1左下  2 右下 
			dirs[nowIndex]= random.nextInt(3);
		}else{
			score -=100;
		}
	}
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章