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

📄 creategame.java

📁 一个简单的Java Swing 游戏Blokus~方块圈地~Eclipse3.0 Project
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			if(temp.getPSelected() == false && temp.getOnBoard() == false && temp.getInUse() == true)
				g.drawImage(temp.getPiece(),440 + 12 * 20,180 * 3 + 8 * 20,null);
			temp = (Pieces)ite.next();
			if(temp.getPSelected() == false && temp.getOnBoard() == false && temp.getInUse() == true)
				g.drawImage(temp.getPiece(),440 + 9 * 20,180 * 3 + 3 * 20,null);
			temp = (Pieces)ite.next();
			if(temp.getPSelected() == false && temp.getOnBoard() == false && temp.getInUse() == true)
				g.drawImage(temp.getPiece(),440 + 5 * 20,180 * 3 + 7 * 20,null);
			temp = (Pieces)ite.next();
			if(temp.getPSelected() == false && temp.getOnBoard() == false && temp.getInUse() == true)
				g.drawImage(temp.getPiece(),440 + 15 * 20,180 * 3 + 1 * 20,null);
			temp = (Pieces)ite.next();
			if(temp.getPSelected() == false && temp.getOnBoard() == false && temp.getInUse() == true)
				g.drawImage(temp.getPiece(),440 + 12 * 20,180 * 3 + 4 * 20,null);
			temp = (Pieces)ite.next();
			if(temp.getPSelected() == false && temp.getOnBoard() == false && temp.getInUse() == true)
				g.drawImage(temp.getPiece(),440 + 0 * 20,180 * 3 + 5 * 20,null);
			temp = (Pieces)ite.next();
			if(temp.getPSelected() == false && temp.getOnBoard() == false && temp.getInUse() == true)
				g.drawImage(temp.getPiece(),440 + 17 * 20,180 * 3 + 6 * 20,null);
			temp = (Pieces)ite.next();
			if(temp.getPSelected() == false && temp.getOnBoard() == false && temp.getInUse() == true)
				g.drawImage(temp.getPiece(),440 + 5 * 20,180 * 3 + 2 * 20,null);
			temp = (Pieces)ite.next();
			if(temp.getPSelected() == false && temp.getOnBoard() == false && temp.getInUse() == true)
				g.drawImage(temp.getPiece(),440 + 7 * 20,180 * 3 + 4 * 20,null);
			temp = (Pieces)ite.next();
			if(temp.getPSelected() == false && temp.getOnBoard() == false && temp.getInUse() == true)
				g.drawImage(temp.getPiece(),440 + 7 * 20,180 * 3 + 1 * 20,null);
			temp = (Pieces)ite.next();
			if(temp.getPSelected() == false && temp.getOnBoard() == false && temp.getInUse() == true)
				g.drawImage(temp.getPiece(),440 + 2 * 20,180 * 3 + 5 * 20,null);
			temp = (Pieces)ite.next();
			if(temp.getPSelected() == false && temp.getOnBoard() == false && temp.getInUse() == true)
				g.drawImage(temp.getPiece(),440 + 3 * 20,180 * 3 + 4 * 20,null);
			temp = (Pieces)ite.next();
			if(temp.getPSelected() == false && temp.getOnBoard() == false && temp.getInUse() == true)
				g.drawImage(temp.getPiece(),440 + 12 * 20,180 * 3 + 1 * 20,null);
			temp = (Pieces)ite.next();
			if(temp.getPSelected() == false && temp.getOnBoard() == false && temp.getInUse() == true)
				g.drawImage(temp.getPiece(),440 + 9 * 20,180 * 3 + 6 * 20,null);
		}
	}
	
	//棋子移动函数
	private void createMove(Graphics g){
		if(pColor == 1){
			Iterator itB = bluePieces.getImagePieces().iterator();
			Pieces temp, temp2;
			while(itB.hasNext()){
				temp = (Pieces)itB.next();
				if(temp.getOnBoard() == false && temp.getPSelected() == true){
					if(cir == 0){
						temp1 = temp;
					}
					else if(cir == 1){
						temp2 = makeTemp(temp1);
						setTemp(temp2);
						setCir(2);
					}
					middle(temp1);
					g.drawImage(temp1.getPiece(),x,y,null);
				}
			}
		}
		if(pColor == 2){
			Iterator itY = yellowPieces.getImagePieces().iterator();
			Pieces temp, temp2;
			while(itY.hasNext()){
				temp = (Pieces)itY.next();
				if(temp.getOnBoard() == false && temp.getPSelected() == true){
					if(cir == 0){
						temp1 = temp;
					}
					else if(cir == 1){
						temp2 = makeTemp(temp1);
						setTemp(temp2);
						setCir(2);
					}
					middle(temp1);
					g.drawImage(temp1.getPiece(),x,y,null);
				}
			}
		}
		if(pColor == 3){
			Iterator itR = redPieces.getImagePieces().iterator();
			Pieces temp, temp2;
			while(itR.hasNext()){
				temp = (Pieces)itR.next();
				if(temp.getOnBoard() == false && temp.getPSelected() == true){
					if(cir == 0){
						temp1 = temp;
					}
					else if(cir == 1){
						temp2 = makeTemp(temp1);
						setTemp(temp2);
						setCir(2);
					}
					middle(temp1);
					g.drawImage(temp1.getPiece(),x,y,null);
				}
			}
		}
		if(pColor == 4){
			Iterator itG = greenPieces.getImagePieces().iterator();
			Pieces temp, temp2;
			while(itG.hasNext()){
				temp = (Pieces)itG.next();
				if(temp.getOnBoard() == false && temp.getPSelected() == true){
					if(cir == 0){
						temp1 = temp;
					}
					else if(cir == 1){
						temp2 = makeTemp(temp1);
						setTemp(temp2);
						setCir(2);
					}
					middle(temp1);
					g.drawImage(temp1.getPiece(),x,y,null);
				}
			}
		}
		
	}
	
	//计算棋子中点
	private void middle(Pieces temp){
		int[][] grid;
		int tSize, r1, s1,t;
		float r = 0,s = 0;
		boolean rs = false;
		tSize = temp.getScore(); 
		grid = temp.getGrid();
		for(int i = 0; i < tSize; i ++, rs = false)
			for(int j = 0; j < tSize; j ++)
				if(grid[i][j] == 1){
					if( rs == false){
						r += 1;
						rs = true;
					}
				}
		for(int i = 0; i < tSize; i ++, rs = false)
			for(int j = 0; j < tSize; j ++)
				if(grid[j][i] == 1){
					if( rs == false){
						s += 1;
						rs = true;
					}
				}
		r1 = firstRow(temp);
		s1 = firstColum(temp);
		r2 = (int)(((s / 2) + s1)* 20);
		s2 = (int)(((r / 2) + r1)* 20);
		//System.out.println("r1 = " + r1 + " s1 = " + s1);
		setPx(x - r2);
		setPy(y - s2);
	}
	
	//计算棋子的第一行
	private int firstRow(Pieces t){
		int r = 0;
		for(int i = 0; i < t.getScore(); i ++)
			for(int j = 0; j< t.getScore(); j ++)
				if(t.getGrid()[i][j] == 1){
					r = i;
					return r;
				}
		return 0;
	}
	
	//计算棋子的第一列
	private int firstColum(Pieces t){
		int c = 0;
		for(int i = 0; i < t.getScore(); i ++)
			for(int j = 0; j< t.getScore(); j ++)
				if(t.getGrid()[j][i] == 1){
					c = i;
					return c;
				}
		return 0;
	}
	
	//生成缓存棋子
	private Pieces makeTemp(Pieces t){
		Image im;
		switch(t.getpColor()){
			case 1: im = blue;
				break;
			case 2: im = yellow;
				break;
			case 3: im = red;
				break;
			case 4: im = green;
				break;
			default: im = empty;
				break;
		}
		BufferedImage tempPiece = new BufferedImage( 
												t.getScore() * 20, 
												t.getScore() * 20,
												BufferedImage.TYPE_INT_ARGB);
		Graphics2D dp = tempPiece.createGraphics();
		
		for(int i = 0; i < t.getScore() * 20; i += 20)
			for(int j = 0; j < t.getScore() * 20; j += 20)
				if (t.getGrid()[i / 20][j / 20] == 1){
					dp.drawImage(im,j,i,null);
				}
		Pieces tem = new Pieces(tempPiece, t.getTypeNo(), 
								t.getpColor(), t.getScore(), t.getGrid());
		dp.dispose();
		return tem;
	}
	
	//设置Type值
	public void setPType(int type){
		this.type = type;
	}
	
	//提取Type值
	public int getPType(){
		return type;
	}
	
	//设置棋子X值
	public void setPx(int x){
		this.x = x;
	}
	
	//提取棋子X值
	public int getPx(){
		return x;
	}
	
	//设置棋子Y值
	public void setPy(int y){
		this.y = y;
	}
	
	//提取棋子Y值
	public int getPy(){
		return y;
	}
	
	//设置棋子颜色
	public void setPColor(int pColor){
		this.pColor = pColor;
	}
	
	//提取棋子颜色
	public int getPColor(){
		return pColor;
	}
	
	//提取蓝色棋子
	public CreateBlue getBluePieces(){
		return bluePieces;
	}
	
	//提取黄色棋子
	public CreateYellow getYellowPieces(){
		return yellowPieces;
	}
	
	//提取红色棋子
	public CreateRed getRedPieces(){
		return redPieces;
	}
	
	//提取绿色棋子
	public CreateGreen getGreenPieces(){
		return greenPieces;
	}
	
	//设置棋子是否被点起来
	public void setPOnMouse(boolean onMouse){
		this.onMouse = onMouse;
	}
	
	//提取棋子是否被点起来
	public boolean getPOnMouse(){
		return onMouse;
	}
	
	//提取缓存棋子1
	public Pieces getTemp(){
		return temp1;
	}
	
	//设置缓存棋子1
	public void setTemp(Pieces temp){
		this.temp1 = temp;
	}
	
	//提取棋子位置偏移
	public int getCir(){
		return cir;
	}
	
	//设置棋子位置偏移
	public void setCir(int cir){
		this.cir =cir;
	}
	
	//提取棋子横向位置偏移
	public int getR(){
		return r2;
	}
	
	//提取棋子纵向位置偏移
	public int getS(){
		return s2;
	}
	
	//提取棋盘矩阵状态
	public int[][] getBoardGrid(){
		return boardGrid;
	}
	
	//设置棋盘矩阵状态
	public void setBoardGrid(int[][] boardGrid){
		this.boardGrid = boardGrid;
	}
	
	//设置外框纵向偏移
	public void setYl(int i){
		this.yl = i;
	}
	
	//设置游戏是否已经开始
	public void setStartG(boolean s){
		this.startG = s;
	}
	
	public void setStartC(boolean b){
		this.startC = b;
	}
	
	public boolean getStartC(){
		return this.startC;
	}
	
	public void setOptionC(boolean b){
		this.optionC = b;
	}
	
	public boolean getOptionC(){
		return this.optionC;
	}
	
	public void setExitC(boolean b){
		this.exitC = b;
	}
	
	public boolean getExitC(){
		return this.exitC;
	}
	
	public void setGiveUpC(boolean b){
		this.giveUpC = b;
	}
	
	public boolean getGiveUpC(){
		return this.giveUpC;
	}
	
	//初始化函数
	public void init(){
		blockSize = 20; type = 0; pColor = 0; x = 0; y = 0;
		onMouse = false; startG = false;
		cir = 0;
		yl = 0;
		boardGrid = new int[21][21];
	    for(int i = 0; i < 21; i ++)
	    	for(int j = 0; j < 21; j ++)
	    		boardGrid[i][j] = 0;
	    
	    bluePieces = new CreateBlue();
	    bluePieces.makeBlue();
	    
	    yellowPieces = new CreateYellow();
	    yellowPieces.makeYellow();
	    
	    redPieces = new CreateRed();
	    redPieces.makeRed();
	    
	    greenPieces = new CreateGreen();
	    greenPieces.makeGreen();
		
	}
	
	public void setKeyX(int x){
		this.kx = x;
	}
	
	public void setKeyY(int y){
		this.ky = y;
	}
	
	public int getKeyX(){
		return this.kx;
	}
	
	public int getKeyY(){
		return this.ky;
	}
}

⌨️ 快捷键说明

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