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

📄 tetrisboard.java

📁 俄罗斯方块。适用于初学者学习。想念很好
💻 JAVA
字号:
package xn.tetris;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
/**
 * 主面板类
 * 实现方块的添加
 * 落下方块的显示
 * 消行等功能
 * */
public class TetrisBoard {

	final static int EMPTY = -1;
	private int[][] matrix;
	private int x_columns;
	private int y_rows;
	private int removedLines;
	private int fullRowOneTime;
	private GC gc;
	private Display display;
	
	public TetrisBoard(int cols, int rows, Display display){
		this.x_columns = cols;
		this.y_rows = rows;
		this.display = display;
		init();
	}
	public TetrisBoard(int cols, int rows){
		this.x_columns = cols;
		this.y_rows = rows;
		init();
	}
	public TetrisBoard(){
		
	}
	//初始化,将面板二维数组每组下标中的值初始化为-1即EMPTY
	public void init(){
		matrix = new int[x_columns][y_rows];
		for(int cols = 0; cols < x_columns; cols++){
			for(int rows = 0; rows < y_rows; rows++){
				matrix[cols][rows] = EMPTY;
			}
		}
	}
	//得到每次消行的行数,不累加,最多4行
	public int getFullRowOneTime(){
		return fullRowOneTime;
	}
	//得到面板的列数
	public int getX_columns(){
		return x_columns;
	}
	//设置面板的列数,设置完需初始化
	public void setX_columns(int cols){
		
		this.x_columns = cols;
		init();
	}
	//得到面板的行数
	public int getY_rows(){
		return y_rows;
	}
	//设置面板行数,设置完需初始化
	public void setY_rows(int rows){
		this.y_rows = rows;
		init();
	}
	//得到面板二维数组中某一组下标中的值,EMPTY或方块的类型
	public int getPositionValue(int x, int y){
		return matrix[x][y];
	}
	//设置面板二维数组中某一组下标中的值,EMPTY或方块的类型
	public void setPositionValue(int x, int y, int value){
		matrix[x][y] = value;
	}
	//得到消行的总行数
	public int getRemovedLines(){
		return removedLines;
	}
	
	public void setRemovedLines(int lines){
		this.removedLines = lines;
	}
	
	//当方块落到底,向面板中添加方块
	public void addBlock(Block block){
		if(block != null ){
	
			for(int i = 0; i < 4; i++){
				int x = (block.getX() / 30) + block.getBlockX()[i];
				int y = (block.getY() / 30) + block.getBlockY()[i];
				int blockY = block.getY() + block.getBlockY()[i] * 30;

				if(blockY >= 0 && blockY <= 570 && matrix[x][y] == EMPTY)
					matrix[x][y] = block.getBlockType();
					
			}
		}
	}
	//将第y行消去
	public void removeRow(int y){

		if(y > 0 && y < y_rows){
			for(; y > 0; y--){
				for(int x = 0; x < x_columns; x++){
					matrix[x][y] = matrix[x][y - 1];
				}
				
			}
		}
		
		for(int x = 0; x < x_columns; x++){
			matrix[x][0] = EMPTY;
		}
		
	}
	//将所有满行消去
	public void removeFullRows(){
		for(int y = y_rows - 1; y > 0; y--){
			if(isFullRow(y)){
				removeRow(y);
				removedLines++;
				fullRowOneTime++;
				y++;
			}
		}
		
	}
	
	//判断方块是否能向下移动
	public boolean canMoveDown(Block block){
		if(block != null){
			for(int i = 0; i < 4; i++){
				int y = block.getY() / 30 + block.getBlockY()[i];
				int x = block.getX() / 30 + block.getBlockX()[i]; 
				//int blockY = block.getY() + block.getBlockY()[i] * 30;
				if(y++ >= y_rows - 1){
					return false;
				}
				else if(x > -1 && x < x_columns && matrix[x][y++] != EMPTY){
					return false;
				}

			}
		}
		return true;
	}
	//判断方块是否能向左移动	
	public boolean canMoveLeft(Block block){
		if(block != null){
			for(int i = 0; i < 4; i++){
				int y = block.getY() / 30 + block.getBlockY()[i];
				int x = block.getX() / 30 + block.getBlockX()[i]; 

				if(x-- == 0){
					return false;
				}
				else if(y > -1 && matrix[x--][y] != EMPTY){
					return false;
				}

				
			}
		}
		return true;
	}
	//判断方块是否能向右移动
	public boolean canMoveRight(Block block){
		if(block != null){
			for(int i = 0; i < 4; i++){
				int y = block.getY() / 30 + block.getBlockY()[i];
				int x = block.getX() / 30 + block.getBlockX()[i]; 

				if(x++ == x_columns - 1){
					return false;
				}
				
				else if(y > -1 && matrix[x++][y] != EMPTY){
					return false;
				}
			}
		}
		return true;
	}
	//判断方块是否能变形
	public boolean canRotate(Block block){
		if(block != null){
			Block newBlock = (Block) block.clone();
			newBlock = newBlock.rotateClock();
			int blockY = newBlock.getY() / 30;
			int blockX = newBlock.getX() / 30;
			for(int i = 0; i < 4; i++){
				int y = blockY + newBlock.getBlockY()[i];
				int x = blockX + newBlock.getBlockX()[i]; 
				
				if(y >= y_rows - 1 || y < 0 || x < 0 || x > x_columns - 1 ){
					return false;
				}
				else if( matrix[x][y] != EMPTY){
					return false;
				}
			}
			return true;
		}
		return false;
		
		
	}
	//判断整个面板中是否有满行
	public boolean hasFullRow(){
		fullRowOneTime = 0;
		for(int y = y_rows - 1; y >= 0; y--){
			if(isFullRow(y)){
				return true;
			}
				
		}
		return false;
	}
	//判断第y行是否是满行
	public boolean isFullRow(int y){
		 for (int x = 0; x < x_columns; x++) {
	            if (matrix[x][y] == EMPTY) {
	                return false;
	            }
	        }
		 
	        return true;
	}
	//在面板中画出指定显示的消息msg
	public void drawMessage(String msg){
		
		gc = MyGC.getMyGC();
		final Font font = new Font(display, "Helvetica", 18, SWT.NORMAL);
		gc.setFont(font);
		gc.setForeground(new Color(display, 255, 255, 255));
		int x = (x_columns * 30 - (gc.getFontMetrics().getAverageCharWidth() * msg.length())) / 2 ;
		int y = (y_rows * 30) / 2;
		gc.drawString(msg, x, y, true);
		font.dispose();
	}
	
	//将面板数组中不为EMPTY的地方按各自存放的方块类型画出小方块
	public synchronized void draw(){
		for(int y = y_rows - 1; y >= 0; y--){
			for(int x = 0; x < x_columns; x++){
				if(matrix[x][y] != EMPTY){
					int xPos = x * 30;
					int yPos = y * 30;
					new Square(xPos, yPos, matrix[x][y], display).draw();
				}
			}
		}
	}
	
}

⌨️ 快捷键说明

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