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

📄 tetris.java

📁 Java写的俄罗斯方块
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	public void repaint() {
		repaint(GlassHeight-1);
		tBlock.repaint();
	}
	public boolean delLine(int pos) {
		if((pos >= GlassHeight) || (pos < 0)) return false;
		lockObject = true;
		try {
			if(pos == 0) {
				for(int x = 0; x < GlassWidth; x++)
					GlassWorkSheet[0][x] = 0;
			} else {
				for(int y = pos; y > 0; y--)
					for(int x = 0; x < GlassWidth; x++)
						GlassWorkSheet[y][x] = GlassWorkSheet[y-1][x];
			}
		} finally {
			lockObject = false;
		}
		return true;
	}
	public int[] getLines() {	//扫描背景,返回所有整行都为1的行的位置
		int MaxCornerNumber = tBlock.getMaxCornerNumber();
		int[] results = new int[MaxCornerNumber];
		int x, y;
		
		for(y = Sheet_y; y < Sheet_y + results.length; y++) {
			if(y >= GlassHeight) break;
			for(x = 0; x < GlassWidth; x++)
				if(GlassWorkSheet[y][x] == 0) break;
			if(x == GlassWidth)
				results[y - Sheet_y] = y;
			else
				results[y - Sheet_y] = 0;
		}
		return results;
	}
	public boolean newObject(Color color, int Figure) {
		Sheet_x = (int)(GlassWidth/2);
		Sheet_y = 0;
		currFigure = Figure;
		if(!canPlace(Sheet_x, Sheet_y, tBlock.getSheet(currFigure)))	//can't place the new object
			return false;

		tBlock.setColor(color);
		tBlock.newObject(Offset_x+Sheet_x*tBlock.getBlockWidth(),
							Offset_y+Sheet_y*tBlock.getBlockHeight(), currFigure);
		return true;
	}
	protected boolean moveObject(int MoveDirect) {
		if(lockObject) return true;
		lockObject = true;
		try {
			switch(MoveDirect) {
	    		//move to left
	    		case 0:
	    			if(!canPlace(Sheet_x-1, Sheet_y, tBlock.getFigureWorkSheet()))
	    				return false;
	    			else
	    				Sheet_x -= 1;
	    			break;
	    		//move down
	    		case 1:
	    			if(!canPlace(Sheet_x, Sheet_y+1, tBlock.getFigureWorkSheet()))
	    				return false;
	    			else
	    				Sheet_y += 1;
	    			break;
	    		//move to right
	    		case 2:
	    			if(!canPlace(Sheet_x+1, Sheet_y, tBlock.getFigureWorkSheet()))
	    				return false;
	    			else
	    				Sheet_x += 1;
	    			break;
		    }
			tBlock.moveObject(MoveDirect);
		} finally {
			lockObject = false;
		}
		return true;
	}
	public void turn() {
		if(lockObject) return;
		lockObject = true;
		try {
			byte[][] Sheet = tBlock.getTurnSheet();
			for(int moveToXX = 0; moveToXX < tBlock.getBlockWidth()-1; moveToXX++)
				if(canPlace(Sheet_x-moveToXX, Sheet_y, Sheet)) {
					Sheet_x -= moveToXX;
					tBlock.setObjectXY(Offset_x+Sheet_x*tBlock.getBlockWidth(),
										Offset_y+Sheet_y*tBlock.getBlockHeight());
					tBlock.turn();
					break;
				}
		} finally {
			lockObject = false;
		}
	}
	private boolean canPlace(int xx, int yy, byte[][] Sheet) {
		int Glass_x, Glass_y;
		int MaxCornerNumber = tBlock.getMaxCornerNumber();
		//xx, yy是否越界?
		if((xx >= 0) && (xx < GlassWidth) && (yy >=0) && (yy < GlassHeight)) {	//没越界
			for(int y = 0; y < MaxCornerNumber; y++)
				for(int x = 0; x < MaxCornerNumber; x++) {
					Glass_x = xx + x;
					Glass_y = yy + y;
					if(Sheet[y][x] == 1) {
						if((Glass_x >= GlassWidth) || (Glass_y >= GlassHeight))
							return false;
						else if(Sheet[y][x] == GlassWorkSheet[Glass_y][Glass_x])
							return false;
					}
				}
			return true;
		} else	//越界
			return false;
	}
	protected void writeGlassWorkSheet(int xx, int yy, byte[][] Sheet) {
		for(int y = 0; y < tBlock.getMaxCornerNumber(); y++)
			for(int x = 0; x < tBlock.getMaxCornerNumber(); x++) {
				if(Sheet[y][x] == 1)
					GlassWorkSheet[yy+y][xx+x] = Sheet[y][x];
			}
	}
}

//方块
class TetrisBlock {
	private int BlockWidth	= 12;	//单元块宽度
	private int BlockHeight	= 12;	//单元块高度
	private int MaxFigureNumber = 7;	//方块种类
	private int MaxCornerNumber = 4;	//最多单元块数(每方块每列/行)
	
	private byte[][]
		Triada = new byte[][]{
			{0, 1, 0, 0},
			{1, 1, 1, 0},
			{0, 0, 0, 0},
			{0, 0, 0, 0}
		},
		LCorner = new byte[][]{	
			{1, 1, 1, 0},
			{1, 0, 0, 0},
			{0, 0, 0, 0},
			{0, 0, 0, 0}
		},
		RCorner = new byte[][]{
			{1, 1, 1, 0},
			{0, 0, 1, 0},
			{0, 0, 0, 0},
			{0, 0, 0, 0}
		},
		LZigzag = new byte[][]{
			{1, 1, 0, 0},
			{0, 1, 1, 0},
			{0, 0, 0, 0},
			{0, 0, 0, 0}
		},
		RZigzag = new byte[][]{
			{0, 1, 1, 0},
			{1, 1, 0, 0},
			{0, 0, 0, 0},
			{0, 0, 0, 0}
		},
		Stick = new byte[][]{
			{1, 1, 1, 1},
			{0, 0, 0, 0},
			{0, 0, 0, 0},
			{0, 0, 0, 0}
		},
		Box = new byte[][]{
			{1, 1, 0, 0},
			{1, 1, 0, 0},
			{0, 0, 0, 0},
			{0, 0, 0, 0}
		};
	private byte[][]
		FigureWorkSheet = new byte[][]{
			{0, 0, 0, 0},
			{0, 0, 0, 0},
			{0, 0, 0, 0},
			{0, 0, 0, 0}
		};

	private byte[][][]
		blockObjects = new byte[][][]{
			Triada, LCorner, RCorner, LZigzag, RZigzag, Stick, Box
		};
		
	private int Figure = 0;
	//private int FirstColor, SecondColor;
	private Color color = Color.blue;
	private boolean lockObject = false;
	private Color backgroundColor;
	private int object_loc_x, object_loc_y;


    //private Graphics g;
    private Tetris tetris;
    
    public TetrisBlock(Tetris tetris) {
        this.tetris = tetris;
        //this.g = tetris.getGraphics();
    }
	public int getBlockWidth() { return BlockWidth;}
	public int getBlockHeight() { return BlockHeight;}
	public int getMaxFigureNumber() { return MaxFigureNumber;}
	public int getMaxCornerNumber() { return MaxCornerNumber;}
	public int getobject_loc_x() { return object_loc_x;}
	public int getobject_loc_y() { return object_loc_y;}
	public byte[][] getSheet(int Figure) {
		if((Figure >= 0) && (Figure < MaxFigureNumber))
			return blockObjects[Figure];
		else
			return null;
	}
	public byte[][] getFigureWorkSheet() { return FigureWorkSheet;}
	
	
	//public void setGraphics(Graphics g) { this.g = g;}
    public void setColor(Color color) {
    	this.color = color;
    }
    public void setBackgroundColor(Color color) {
    	this.backgroundColor = color;
    }
    public void newObject(int loc_x, int loc_y, int Figure) {
    	object_loc_x = loc_x;
    	object_loc_y = loc_y;
    	this.Figure = Figure;
    	FigureWorkSheet = blockObjects[this.Figure];
    	drawObject(object_loc_x, object_loc_y);
    }
    public void moveObject(int MoveDirect) {
    	if(lockObject) return;
    	lockObject = true;
    	try {
	    	clearObject(object_loc_x, object_loc_y);
	    	switch(MoveDirect) {
	    		//move to left
	    		case 0:
	    			object_loc_x -= getBlockWidth();
	    			break;
	    		//move down
	    		case 1:
	    			object_loc_y += getBlockHeight();
	    			break;
	    		//move to right
	    		case 2:
	    			object_loc_x += getBlockWidth();
	    			break;
	    	}
	    	drawObject(object_loc_x, object_loc_y);
	    } finally {
	    	lockObject = false;
	    }
    }
    public void turn() {
    	lockObject = true;
    	try {
	    	clearObject(object_loc_x, object_loc_y);
	    	FigureWorkSheet = getTurnSheet();
	    	drawObject(object_loc_x, object_loc_y);
	    } finally {
    		lockObject = false;
    	}
    }
    public byte[][] getTurnSheet() {
    	//返回逆时钟转90度,值为1的元素向左上角平行靠拢后的方块数组
    	byte[][] Sheet = new byte[MaxCornerNumber][MaxCornerNumber];
    	boolean toBreak = false;
    	int x, y;
    	for(y = 0; y < MaxCornerNumber; y++) {
    		for(x=0; x < MaxCornerNumber; x++)
    			Sheet[MaxCornerNumber-1-x][y] = FigureWorkSheet[y][x];
    	}
    	for(y = 0; y < MaxCornerNumber; y++) {
    		for(x=0; x < MaxCornerNumber; x++)
    			if(Sheet[y][x] == 1) {
    				toBreak = true;
    				break;
    			}
    		if(toBreak) break;
    	}
    	int yy = y;
    	for(; y < MaxCornerNumber; y++) {	//行上移
    		for(x=0; x < MaxCornerNumber; x++)
    			Sheet[y-yy][x] = Sheet[y][x];
    	}
    	for(y = MaxCornerNumber-yy; y < MaxCornerNumber; y++) {		//上移后原位置清零
    		for(x=0; x < MaxCornerNumber; x++)
    			Sheet[y][x] = 0;
    	}
    	return Sheet;
    }
    public void setObjectXY(int loc_x, int loc_y) {
    	clearObject(object_loc_x, object_loc_y);
    	object_loc_x = loc_x;
    	object_loc_y = loc_y;
    	drawObject(object_loc_x, object_loc_y);
    }
    public void repaint() {
    	drawObject(object_loc_x, object_loc_y);
    }
    public void drawMyObject(int loc_x, int loc_y, Graphics g, int Figure) {
    	byte[][] Sheet;
    	if((Sheet = getSheet(Figure)) == null) return;
    	g.clearRect(loc_x, loc_y,
    				getMaxCornerNumber()*getBlockWidth(), getMaxCornerNumber()*getBlockHeight());
    	for(int y=0; y < MaxCornerNumber; y++)
    		for(int x=0; x < MaxCornerNumber; x++) {
    			if(Sheet[y][x] == 1)
        			drawBlock(loc_x+x*getBlockWidth(), loc_y+y*getBlockHeight(),
        						getBlockWidth(), getBlockHeight(),
        						g);
        	}

    }
    private void drawObject(int loc_x, int loc_y) {
    	Graphics g = tetris.getGraphics();
    	g.setColor(color);
    	for(int y=0; y < MaxCornerNumber; y++)
    		for(int x=0; x < MaxCornerNumber; x++) {
    			if(FigureWorkSheet[y][x] == 1)
        			drawBlock(loc_x+x*getBlockWidth(), loc_y+y*getBlockHeight(),
        						getBlockWidth(), getBlockHeight(),
        						g);
        	}
    }
    private void clearObject(int loc_x, int loc_y) {
    	Color oldColor;
    	Graphics g = tetris.getGraphics();
    	oldColor = color;
    	g.setColor(backgroundColor);
    	for(int y=0; y < MaxCornerNumber; y++)
    		for(int x=0; x < MaxCornerNumber; x++) {
    			if(FigureWorkSheet[y][x] == 1)
        			g.fillRect(loc_x+x*getBlockWidth(), loc_y+y*getBlockHeight(),
        						getBlockWidth()-1, getBlockHeight()-1);
        	}
    	g.setColor(oldColor);
    }
    private void drawBlock(int x, int y, int w, int h, Graphics g) {
		g.fill3DRect(x, y, w-1, h-1, true);
    }
}

⌨️ 快捷键说明

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