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

📄 mainpanel.java

📁 俄罗斯方块的原代码,是一个非常好的开发代码
💻 JAVA
字号:
package com.ywb.view;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JPanel;

import com.ywb.model.DownThread;
import com.ywb.model.MyShape;

public class MainPanel extends JPanel{
	private int row,col;
	private static int w=20;
	private JPanel box[][];
	private boolean[][] boxFlag;//标记已经落下的方块位置
	private MyShape shape;
	private int x,y;//定义形状绝对坐标
	private Color boxColor=Color.YELLOW;
	
	public MainPanel(){
		x=3;y=0;//设定初始绝对坐标
		row=16;
		col=9;
		this.setLayout(new GridLayout(row,col,3,3));
		box=new JPanel[row][col];
		this.setBackground(Color.WHITE);
		
		boxFlag=new boolean[row][col];
		
		for(int i=0;i<row;i++){
			for(int j=0;j<col;j++){
				box[i][j]=new JPanel();
				this.add(box[i][j]);
				box[i][j].setBackground(Color.BLACK);
				boxFlag[i][j]=false;
			}
		}
		
		DownThread t=new DownThread(this);
		t.setDaemon(true);
		t.start();
		
		
	}

	public boolean[][] getFlag() {
		return boxFlag;
	}

	public void setFlag(boolean[][] flag) {
		this.boxFlag = flag;
	}

	public MyShape getShape() {
		return shape;
	}

	public void setShape(MyShape shape) {
		this.shape = shape;
	}

	public int getRow() {
		return row;
	}

	public void setRow(int row) {
		this.row = row;
	}

	public int getCol() {
		return col;
	}

	public void setCol(int col) {
		this.col = col;
	}

	public static int getW() {
		return w;
	}

	public static void setW(int w) {
		MainPanel.w = w;
	}

	public JPanel[][] getBox() {
		return box;
	}

	public void setBox(JPanel[][] box) {
		this.box = box;
	}
	//显示形状
	public void setView(){
		int shapenum[]=shape.getShape(shape.getShape(),shape.getType());
//		for(int i:shapenum){
//			System.out.print(i+"  ");
//		}
//		System.out.println("/n  --"+x+"  "+y);
		
		for(int i=0;i<row;i++){
			for(int j=0;j<col;j++){
				box[i][j].setBackground(Color.BLACK);
				for(int index=0;index<4;index++){
				if(shapenum[2*index]+y==i&&shapenum[1+2*index]+x==j){
					box[i][j].setBackground(boxColor);
				}
				}
				if(boxFlag[i][j]){
					box[i][j].setBackground(boxColor);
				}
			}
		}
	}
	//向左移动
	public void moveLeft(){
		x--;
		int tnum[]=shape.getShape(shape.getShape(), shape.getType());
		for(int index=0;index<4;index++){
		if(x<0||boxFlag[y+tnum[index*2]][x+tnum[index*2+1]]){
			x++;
		}
		}
		this.setView();
	}
	//向右移动
	public void moveRight(){
		x++;
		int tnum[]=shape.getShape(shape.getShape(), shape.getType());
		for(int index=0;index<4;index++){
			if(x+tnum[index*2+1]>=col||boxFlag[y+tnum[index*2]][x+tnum[index*2+1]]){
				x--;
				break;
			}
		}
		this.setView();
	}
	//向下移动
	public void moveDown() {
		y++;

		if (isDown()) {
			y--;
			writeBoxFlag();
			isFull();
			shape=new MyShape();
			x=3;y=0;
			
		}
		
		this.setView();
		
		
	}
	//旋转
	public void reShape(){
		shape.reShape();
		this.setView();
	}
	// 判断是否到底
	public boolean isDown() {
		boolean flag = false;
		int tnum[] = shape.getShape(shape.getShape(), shape.getType());
		for (int index = 0; index < 4; index++) {
			if (y + tnum[index * 2] >= row) {
				flag = true;
			}
			int rowt=y + tnum[index * 2];
			int colt=x + tnum[index * 2 + 1];
			if (rowt<row&&boxFlag[rowt][colt])
				flag = true;
		}
		return flag;
	}
	//记录到底的形状
	public void writeBoxFlag(){
		
		int tnum[] = shape.getShape(shape.getShape(), shape.getType());
		for(int i=0;i<row;i++){
			for(int j=0;j<col;j++){
				for (int index = 0; index < 4; index++) {
					if (y + tnum[index * 2]==i&&x+tnum[index*2+1]==j) {
						boxFlag[i][j] = true;
					}
			}
		}
	}
	}
	//判断一行是否满了
	public void isFull(){
		for(int i=0;i<row;i++){
			boolean flag=true;
			for(int j=0;j<col;j++){
				flag=flag&&boxFlag[i][j];
			}
			if(flag){
				//i: 0~i-1  1~i
				for(int k=i-1;k>=0;k--){
					for(int j=0;j<col;j++){
						boxFlag[k+1][j]=boxFlag[k][j];
					}
				}
			}
		}
	}
}

⌨️ 快捷键说明

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