⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 piecesselect.java

📁 一个简单的Java Swing 游戏Blokus~方块圈地~Eclipse3.0 Project
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
 * 创建日期 2005-4-12
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package frame;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;

/**
 * @author Robert
 *
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class PiecesSelect implements MouseListener,MouseWheelListener,
										Runnable,KeyListener{
	private CreateGame game;
	private CreateBlue bluePieces;
	private CreateYellow yellowPieces;
	private CreateRed redPieces;
	private CreateGreen greenPieces;
	private Pieces temp;
	
	private Thread startGame;
	
	private int playMode = 5;
	private int turn = 1;
	private boolean start = false;
	private boolean onMouse = false;
	private boolean canPut = false;
	private boolean finishP = false;
	private int blueS = 0, yellowS = 0, redS = 0, greenS = 0; 
	private boolean skipB = false, skipY = false, skipR = false, skipG = false;
	private boolean blueC = false, yellowC = false, redC = false, greenC = false;
	private boolean end =false;
	private int a1 = 1;
	private int a2 = 2;
	private int[] a3 = new int[2];
	private int[] a4 = new int[5];
	private int[] a5 = new int[12];
	private int[] c1 = new int[6]; 
	private int gametype = 0;
	
	//构造函数
	public PiecesSelect(CreateGame game){
		this.game = game;
		a3[0] = 3; a3[1] = 4;
		a4[0] = 5; a4[1] = 6; a4[2] = 7; a4[3] = 8; a4[4] = 9;
		for(int i = 0; i < 12; i ++)
			a5[i] = 10 + i;
		c1[0] = 0; c1[1] = 1; c1[2] = 2; c1[3] = 3; c1[4] = 4; c1[5] = 5;
	}
	
	//鼠标事件
	public void mouseClicked(MouseEvent e){
		int x,y;
		
		//鼠标左键而且没有选择棋子
		if(e.getClickCount() >= 1 && e.getButton() == 1 && onMouse == false){
			//autoRun(1);
			x = e.getX();
			y = e.getY();
			
			if(x > 440 && x < 840 && y > 20 && y < 720 && start == false){
				CreateDialog wD = new CreateDialog('n');
			}
			//System.out.println("x:" + x + " y:" + y);
			if(x >= 20 && x <= 200 && y >= 480 && y <= 540 && start == false){
				startGame = new Thread(this);
				startGame.start();
				start = true;
				game.setStartG(true);
				game.repaint();
			}
				
			if(x >= 240 && x <= 420 && y >= 480 && y <= 540 && start == false){
				CreateDialog optionD = new CreateDialog('o', game, this);
				game.repaint();
				//System.out.println("Options");
			}
				
			if(x >= 20 && x <= 200 && y >= 580 && y<= 640){
				CreateDialog exitD = new CreateDialog('e');
				//System.out.println("Exit");
			}
				
			if(x >= 240 && x <= 420 && y >= 580 && y<= 640 
					&& blueS > 1 && yellowS > 1 && redS > 1 && greenS > 1){
				//System.out.println("Give up!");
				if(turn == 1){
					skipB = true;
					if(skipY == false){
						turn = 2;
						game.setYl(180);
					}
					else{
						if(skipR == false){
							turn = 3;
							game.setYl(360);
						}
						else{
							if(skipG == false){
								turn = 4;
								game.setYl(540);
							}
							else
								end = true;
						}
					}					
				}
				else if(turn == 2){
					skipY = true;
					if(skipR == false){
						turn = 3;
						game.setYl(360);
					}
					else{
						if(skipG == false){
							turn = 4;
							game.setYl(540);
						}
						else{
							if(skipB == false){
								turn = 1;
								game.setYl(0);
							}
							else
								end = true;
						}
					}					
				}
				else if(turn == 3){
					skipR = true;
					if(skipG == false){
						turn = 4;
						game.setYl(540);
					}
					else{
						if(skipB == false){
							turn = 1;
							game.setYl(0);
						}
						else{
							if(skipY == false){
								turn = 2;
								game.setYl(180);
							}
							else
								end = true;
						}
					}					
				}
				else if(turn == 4){
					skipG = true;
					if(skipB == false){
						turn = 1;
						game.setYl(0);
					}
					else{
						if(skipY == false){
							turn = 2;
							game.setYl(180);
						}
						else{
							if(skipR == false){
								turn = 3;
								game.setYl(360);
							}
							else
								end = true;
						}
					}					
				}
				if(skipB == true && skipY ==true && skipR == true && skipG == true){
					end = true;
				}
				
				if(end == true){
					try{
						Thread.sleep(2000);					
						start = false;
						game.setStartG(false);
						CreateDialog endD = new CreateDialog('d', game, this);
					//game.repaint();
					}
					catch(InterruptedException evt){
							
					}
				}
				
				game.repaint();
			}
			if(start == true){	
			if(skipB == false){
				if(x >= 440 && x <= 840 && y >= 20 && y<= 180 && turn == 1 && blueC == false){
					int tn = 0;
					game.setPx(x);
					game.setPy(y);
					game.setKeyX(x);
					game.setKeyY(y);
					//System.out.println("x:" + x + " y:" + y);
					this.bluePieces = game.getBluePieces();
					Iterator ite = bluePieces.getImagePieces().iterator();
						
					tn = selectedPieceType(440, 20, x, y);
					if(tn != 0){
						while(ite.hasNext()){
							Pieces temp = (Pieces)ite.next();
							if(temp.getTypeNo() == tn && temp.getInUse() == true){
								blueS += 1;
								game.setPColor(1);
								temp.setPSelected(true);
								this.setTemp(temp);
								game.setTemp(temp);
								//turn = 2;
								//System.out.println("BlueS:" + blueS);
							}
						}
					}
					game.repaint();
				}
			}
			
			if(skipY == false){
				if(x >= 440 && x <= 840 && y >= 200 && y<= 360 && turn == 2 && yellowC == false){
					int tn = 0;
					game.setPx(x);
					game.setPy(y);
					game.setKeyX(x);
					game.setKeyY(y);
					//System.out.println("x:" + x + " y:" + y);
					this.yellowPieces = game.getYellowPieces();
					Iterator ite = yellowPieces.getImagePieces().iterator();
						
					tn = selectedPieceType(440, 200, x, y);
					if(tn != 0){
						while(ite.hasNext()){
							Pieces temp = (Pieces)ite.next();
							if(temp.getTypeNo() == tn && temp.getInUse() == true){
								yellowS += 1;
								game.setPColor(2);
								temp.setPSelected(true);
								this.setTemp(temp);						
								game.setTemp(temp);
								//turn = 3;
							}
						}
					}
					game.repaint();
				}
			}
			
			if(skipR == false){	
				if(x >= 440 && x <= 840 && y >= 380 && y<= 540 && turn == 3 && redC == false){
					int tn = 0;
					game.setPx(x);
					game.setPy(y);
					game.setKeyX(x);
					game.setKeyY(y);
					//System.out.println("x:" + x + " y:" + y);
					this.redPieces = game.getRedPieces();
					Iterator ite = redPieces.getImagePieces().iterator();
						
					tn = selectedPieceType(440, 380, x, y);
					if(tn != 0){
						while(ite.hasNext()){
							Pieces temp = (Pieces)ite.next();
							if(temp.getTypeNo() == tn && temp.getInUse() == true){
								redS += 1;
								game.setPColor(3);
								temp.setPSelected(true);
								this.setTemp(temp);
								game.setTemp(temp);
								//turn = 4;
							}
						}
					}
						
					game.repaint();
				}
			}
			
			if(skipG == false){
				if(x >= 440 && x <= 840 && y >= 560 && y<= 720 && turn == 4 && greenC == false){
					int tn = 0;
					game.setPx(x);
					game.setPy(y);
					game.setKeyX(x);
					game.setKeyY(y);
					//System.out.println("x:" + x + " y:" + y);
					this.greenPieces = game.getGreenPieces();
					Iterator ite = greenPieces.getImagePieces().iterator();
						
					tn = selectedPieceType(440, 560, x, y);
					if(tn != 0){
						while(ite.hasNext()){
							Pieces temp = (Pieces)ite.next();
							if(temp.getTypeNo() == tn && temp.getInUse() == true){
								greenS += 1;
								game.setPColor(4);
								temp.setPSelected(true);
								this.setTemp(temp);
								game.setTemp(temp);
								//turn = 1;
							}
						}
					}
						
					game.repaint();
				}
			}
			}
		
		}//End 鼠标左键
		//鼠标右键
		else if(e.getClickCount() == 1 && e.getButton() == 3 && onMouse == true){
			this.temp.setPSelected(false);
			this.temp.setGrid(temp.getBak());
			setOnMouse(false);
			game.setPOnMouse(false);
			game.setPColor(0);
			game.setCir(0);
			if(blueS == 1 && turn == 1){
				blueS = 0;
				game.setYl(0);
			}
			else if(yellowS == 1 && turn == 2){
				yellowS = 0;
				game.setYl(180);
			}
			else if(redS == 1 && turn == 3){
				redS = 0;
				game.setYl(360);
			}
			else if(greenS == 1 && turn == 4){
				greenS = 0;
				game.setYl(540);
			}
			
			/*if(turn == 1){
				turn = 4;
				game.setYl(540);
			}
			else{
				turn -= 1;
				game.setYl(180 * (turn - 1));
			}*/
			
			game.repaint();
		}//End 鼠标右键
		//棋盘上的鼠标左键
		else if(e.getClickCount() >= 1 && e.getButton() == 1 && onMouse == true){
			int x1,y1;
			x1 = e.getX();
			y1 = e.getY();
			finishP = false;
			//setCanPut(true);
			if(x1 > 20 && x1 < 420 && y1 > 20 && y1 < 420){
				float rx,cy,r,c;
				float ct;
				int[][] gr = this.temp.getGrid();
				int[][] br = game.getBoardGrid();
				
				r = game.getR();
				c = game.getS();
				rx = (x1 - r)/ 20 - 1;
				cy = (y1 - c)/ 20 - 1;
				if(rx - (int)rx > 0.3)
					rx += 1;
				if(cy - (int)cy > 0.3)
					cy += 1;
				//System.out.println("rx:" + rx + " cy:" + cy + " r:" + r + " sc:" + c);
				
				if(blueS == 1){				
					blueS = firstTurn(1,cy,rx);
					//System.out.println("S1:" + blueS);
					//finishP = true;
					blueS += 1;
				}
				if(yellowS == 1){
					yellowS = firstTurn(2,cy,rx);
					//System.out.println("S2:" + yellowS);
					//finishP = true;
					yellowS += 1;
				}
				if(redS == 1){
					redS = firstTurn(3,cy,rx);
					//finishP = true;
					redS += 1;
				}
				if(greenS == 1){
					greenS = firstTurn(4,cy,rx);
					//finishP = true;
					greenS += 1;
				}
				
				if(blueS >= 2 && yellowS >= 2 && redS >= 2 && greenS >= 2 && finishP == false){
					for(int i = 0; i < temp.getScore(); i ++){
						for(int j = 0; j < temp.getScore(); j ++){
							//System.out.print(gr[i][j] + " ");
							if(gr[i][j] == 1){
								canBePutOn(br,i,j,cy,rx);	
							}
						}
						//System.out.println();
					}
					for(int i = 0; i < temp.getScore(); i ++){
						for(int j = 0; j < temp.getScore(); j ++){
							//System.out.print(gr[i][j] + " ");
							if(gr[i][j] == 1){
								canNotBePutOn(br,i,j,cy,rx);
							}
						}
						//System.out.println();
					}
					
					if(canPut == true){
						for(int i = 0; i < temp.getScore(); i ++){
							for(int j = 0; j < temp.getScore(); j ++){
								//System.out.print(gr[i][j] + " ");
								if(gr[i][j] == 1 && br[(int)cy + i][(int)rx + j] != 0){
									setCanPut(false);
								}
							}
						}
					}
					if(canPut == true){
						for(int i = 0; i < temp.getScore(); i ++){
							for(int j = 0; j < temp.getScore(); j ++){
								//System.out.print(gr[i][j] + " ");
								if(gr[i][j] == 1){
									br[(int)cy + i][(int)rx + j] = temp.getpColor();
								}
							}
						}
						setCanPut(false);
						game.setBoardGrid(br);
						//System.out.println();
						//for(int i = 0; i < 20; i ++){
							//for(int j = 0; j < 10; j ++){
							//	System.out.print(br[i][j] + " ");
							//}
							//System.out.println();
						//}
						
						//game.setBoardGrid(br);
						temp.setOnBoard(true);
						game.setPOnMouse(false);
						setOnMouse(false);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -