SWT编写的俄罗期方块

源代码在线查看: block.java

软件大小: 91 K
上传用户: yyjpu
关键词: SWT 编写 方块
下载地址: 免注册下载 普通下载 VIP

相关代码

				/* 
				 * $Id: Block.java, 2007-10-11, 下午02:12:09, Eric. Exp$
				 *
				 * Copyright (c) 2006-2007 Wipro Technologies. All Rights Reserved.
				 * This software program and documentation are copyrighted by Wipro 
				 * Technologies.
				 */
				package eric.block;
				
				import org.eclipse.swt.graphics.Color;
				import org.eclipse.swt.widgets.Display;
				
				
				/**
				 * Class/Interface description
				 * 
				 * @author Eric
				 * @see Another Class
				 * @since 0.1
				 */
				public class Block implements Cloneable
				{
					// L type bolck's tag value
					final static int L_Block_1 = 0;
				
					final static int L_Block_2 = 1;
				
					final static int L_Block_3 = 2;
				
					final static int L_Block_4 = 3;
				
					// J Type block's tag value
					final static int J_BLOCK_1 = 10;
				
					final static int J_BLOCK_2 = 11;
				
					final static int J_BLOCK_3 = 12;
				
					final static int J_BLOCK_4 = 13;
				
					// four block's tag value
					final static int I_BLOCK_1 = 20;
				
					final static int I_BLOCK_2 = 21;
				
					// Z type block's tag value
					final static int Z_BLOCK_1 = 30;
				
					final static int Z_BLOCK_2 = 31;
				
					// 
					final static int S_BLOCK_1 = 40;
				
					final static int S_BLOCK_2 = 41;
				
					final static int diamonds_BLOCK_1 = 50;
				
					// T type blocks tag value
					final static int T_BLOCK_1 = 60;
				
					final static int T_BLOCK_2 = 61;
				
					final static int T_BLOCK_3 = 62;
				
					final static int T_BLOCK_4 = 63;
				
					private int blockType;
				
					private int[] blockX = new int[4];
				
					private int[] blockY = new int[4];
				
					private int x;
				
					private int y;
				
					private Display display;
				
					/**
					 * @param x
					 * @param y
					 * @param blockType
					 * @param display
					 */
					public Block(int x, int y, int blockType, Display display)
					{
						this.x = x;
						this.y = y;
						this.blockType = blockType;
						this.display = display;
				
						init();
					}
				
					private void init()
					{
						switch (blockType)
						{
				
						//		L_BLOCK 4 types
						case L_Block_1:
				
							blockX[0] = 0;
							blockY[0] = -1;
							blockX[1] = 0;
							blockY[1] = 0;
							blockX[2] = 0;
							blockY[2] = 1;
							blockX[3] = 1;
							blockY[3] = 1;
							break;
				
						case L_Block_2:
				
							blockX[0] = -1;
							blockY[0] = 1;
							blockX[1] = -1;
							blockY[1] = 0;
							blockX[2] = 0;
							blockY[2] = 0;
							blockX[3] = 1;
							blockY[3] = 0;
							break;
				
						case L_Block_3:
				
							blockX[0] = -1;
							blockY[0] = 0;
							blockX[1] = 0;
							blockY[1] = 0;
							blockX[2] = 0;
							blockY[2] = 1;
							blockX[3] = 0;
							blockY[3] = 2;
							break;
				
						case L_Block_4:
				
							blockX[0] = -1;
							blockY[0] = 0;
							blockX[1] = 0;
							blockY[1] = 0;
							blockX[2] = 1;
							blockY[2] = 0;
							blockX[3] = 1;
							blockY[3] = -1;
							break;
				
						//			J_BLOCK 4 types
						case J_BLOCK_1:
				
							blockX[0] = 0;
							blockY[0] = -1;
							blockX[1] = 0;
							blockY[1] = 0;
							blockX[2] = 0;
							blockY[2] = 1;
							blockX[3] = -1;
							blockY[3] = 1;
							break;
				
						case J_BLOCK_2:
				
							blockX[0] = -1;
							blockY[0] = -1;
							blockX[1] = -1;
							blockY[1] = 0;
							blockX[2] = 0;
							blockY[2] = 0;
							blockX[3] = 1;
							blockY[3] = 0;
							break;
				
						case J_BLOCK_3:
				
							blockX[0] = 1;
							blockY[0] = -1;
							blockX[1] = 0;
							blockY[1] = -1;
							blockX[2] = 0;
							blockY[2] = 0;
							blockX[3] = 0;
							blockY[3] = 1;
							break;
				
						case J_BLOCK_4:
				
							blockX[0] = -1;
							blockY[0] = 0;
							blockX[1] = 0;
							blockY[1] = 0;
							blockX[2] = 1;
							blockY[2] = 0;
							blockX[3] = 1;
							blockY[3] = 1;
							break;
				
						//			I_BLOCK 2 types
						case I_BLOCK_1:
				
							blockX[0] = 0;
							blockY[0] = -1;
							blockX[1] = 0;
							blockY[1] = 0;
							blockX[2] = 0;
							blockY[2] = 1;
							blockX[3] = 0;
							blockY[3] = 2;
							break;
				
						case I_BLOCK_2:
				
							blockX[0] = -1;
							blockY[0] = 0;
							blockX[1] = 0;
							blockY[1] = 0;
							blockX[2] = 1;
							blockY[2] = 0;
							blockX[3] = 2;
							blockY[3] = 0;
							break;
							
							//	Z_BLOCK 2 types
						case Z_BLOCK_1:
							
							blockX[0] = -1;
							blockY[0] = -1;
							blockX[1] = 0;
							blockY[1] = -1;
							blockX[2] = 0;
							blockY[2] = 0;
							blockX[3] = 1;
							blockY[3] = 0;
							break;	
							
						case Z_BLOCK_2:
							
							blockX[0] = 0;
							blockY[0] = -1;
							blockX[1] = 0;
							blockY[1] = 0;
							blockX[2] = -1;
							blockY[2] = 0;
							blockX[3] = -1;
							blockY[3] = 1;
							break;
							
							//	T_BLOCK 4 types
						case T_BLOCK_1:
							
							blockX[0] = -1;
							blockY[0] = 0;
							blockX[1] = 0;
							blockY[1] = 0;
							blockX[2] = 1;
							blockY[2] = 0;
							blockX[3] = 0;
							blockY[3] = 1;
							break;	
							
						case T_BLOCK_2:
							
							blockX[0] = 0;
							blockY[0] = -1;
							blockX[1] = -1;
							blockY[1] = 0;
							blockX[2] = 0;
							blockY[2] = 0;
							blockX[3] = 0;
							blockY[3] = 1;
							break;
							
						case T_BLOCK_3:
							
							blockX[0] = 0;
							blockY[0] = -1;
							blockX[1] = -1;
							blockY[1] = 0;
							blockX[2] = 0;
							blockY[2] = 0;
							blockX[3] = 1;
							blockY[3] = 0;
							break;
							
						case T_BLOCK_4:
							
							blockX[0] = 0;
							blockY[0] = -1;
							blockX[1] = 0;
							blockY[1] = 0;
							blockX[2] = 0;
							blockY[2] = 1;
							blockX[3] = 1;
							blockY[3] = 0;
							break;
							
						//cute block
						case diamonds_BLOCK_1:
							
							blockX[0] = -1;
							blockY[0] = -1;
							blockX[1] = 0;
							blockY[1] = -1;
							blockX[2] = -1;
							blockY[2] = 0;
							blockX[3] = 0;
							blockY[3] = 0;
							break;	
							
							//Z block
						case S_BLOCK_1:
							
							blockX[0] = -1;
							blockY[0] = 0;
							blockX[1] = 0;
							blockY[1] = 0;
							blockX[2] = 0;
							blockY[2] = -1;
							blockX[3] = 1;
							blockY[3] = -1;
							break;	
						
						case S_BLOCK_2:
							
							blockX[0] = -1;
							blockY[0] = -1;
							blockX[1] = -1;
							blockY[1] = 0;
							blockX[2] = 0;
							blockY[2] = 0;
							blockX[3] = 0;
							blockY[3] = 1;
							break;
						}
					}
				
					/**
					 * @return int[]
					 */
					public int[] getBlockX(){
						return blockX;
					}
					/**
					 * @return int[]
					 */
					public int[] getBlockY(){
						return blockY;
					}
					
					/**
					 * Draw block
					 */
					public void draw(){
						
						int factX = 0;
						int factY = 0;
						for(int i = 0; i < 4; i++){
							factX = blockX[i] * 30 + x;
							factY = blockY[i] * 30 + y;
							new Square(factX, factY, blockType, display).draw();
						}
					}
					
					
					/**
					 * erasure current block
					 */
					public void clear(){
						int factX = 0;
						int factY = 0;
						for(int i = 0; i < 4; i++){
							factX = blockX[i] * 30 + x;
							factY = blockY[i] * 30 + y;
							Square square = new Square(factX, factY, 100, display);
							square.setBorderColor(new Color(display, 0, 0, 0));
							square.draw();
						}
					}
					
					/**
					 * @return int
					 */
					public int getBlockType(){
						return blockType;
					}
				
					/**
					 * @return the x
					 */
					public int getX()
					{
						return x;
					}
				
					/**
					 * @param x the x to set
					 */
					public void setX(int x)
					{
						this.x = x;
					}
				
					/**
					 * @return the y
					 */
					public int getY()
					{
						return y;
					}
				
					/**
					 * @param y the y to set
					 */
					public void setY(int y)
					{
						this.y = y;
					}
				
					/**
					 * Prepare for move block state
					 */
					public Block clone(){
						Block block = null;
						try {
							block = (Block)super.clone();
							block.blockX = (int[])blockX.clone();
							block.blockY = (int[])blockY.clone();
						} catch (CloneNotSupportedException e) {
							e.printStackTrace();
						}
						return block;
					}
					
					/**
					 * Move block down to one level
					 */
					public void moveDown(){
				
						clear();
						y += 30;
						draw();
					}
					
					/**
					 *  Move block down to right row
					 */
					public void moveRight()
					{
						clear();
						x += 30;
						draw();
					}
					
					/**
					 *  Move block down to left row
					 */
					public void moveLeft()
					{
						clear();
						x -= 30;
						draw();
					}
					
					
					/**
					 *  Move block up to one
					 */
					public void moveUp()
					{
						clear();
						y -=30;
						draw();
					}
					
					/**
					 * Deasil draw block
					 * @param x int
					 * @param y int
					 * @param display Display 
					 * @return  Block
					 */
					public static Block getRandomBlock(int x, int y,Display display)
					{
						int[] blockTypes = {
								0, 1, 2, 3,
								10, 11, 12, 13,
								20, 21, 30, 31,
								40, 41,50, 60, 
								61, 62, 63
							};
							int num = (int) (Math.random() * 19);
							
							return new Block(x, y, blockTypes[num], display);
					}
					
					/**
					 * @return Block
					 */
					public Block deasilBlock()
					{
						clear();
						if(blockType >= L_Block_1 && blockType 							if(blockType == L_Block_4){
								blockType = L_Block_1;
								return new Block(x, y, blockType, display);
							}
							else
								return new Block(x, y, ++blockType, display);
						}
						
						else if(blockType >= J_BLOCK_1 && blockType 							
							if(blockType == J_BLOCK_4){
								blockType = J_BLOCK_1;
								
								return new Block(x, y, blockType, display);
							}
							else
								return new Block(x, y, ++blockType, display);
							
								
						}
						
						else if(blockType == I_BLOCK_1 || blockType == I_BLOCK_2){
							if(blockType == I_BLOCK_2){
								blockType = I_BLOCK_1;
								return new Block(x, y, blockType, display);
							}
							else
								return new Block(x, y, ++blockType, display);
						}
						
						else if(blockType == Z_BLOCK_1 || blockType == Z_BLOCK_2){
							if(blockType == Z_BLOCK_2){
								blockType = Z_BLOCK_1;
								return new Block(x, y, blockType, display);
							}
							else
								return new Block(x, y, ++blockType, display);
						}
						
						else if(blockType == S_BLOCK_1 || blockType == S_BLOCK_2){
							if(blockType == S_BLOCK_2){
								blockType = S_BLOCK_1;
								return new Block(x, y, blockType, display);
							}
							else
								return new Block(x, y, ++blockType, display);
						}
				
						else if(blockType >= T_BLOCK_1 && blockType 							if(blockType == T_BLOCK_4){
								blockType = T_BLOCK_1;
								return new Block(x, y, blockType, display);
							}
							else
								return new Block(x, y, ++blockType, display);
						}
						
						else if(blockType == diamonds_BLOCK_1){
							return this;
						}
						else
							return null;
					}
				}
							

相关资源