Java-俄罗斯方块最新完善版

趋于完善版

步骤1:

package game;

import java.awt.image.BufferedImage;

public class Cell {
	private int row;
	private int col;
	private BufferedImage image;
	public Cell(){}
	public Cell(int row, int col ,BufferedImage image) {
		super();
		this.row = row;
		this.col = col;
		this.image = image;
	}
	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 void left(){
		col--;
	}
	public void down(){
		row++;
	}
	public void right(){
		col++;
	}
	@Override
	public String toString() {
		return "Cell [row=" + row + ", col=" + col + "]";
	}
	public BufferedImage getImage() {
		return image;
	}
	public void setImage(BufferedImage image) {
		this.image = image;
	}
}

步骤2:

package game;

public class Tetromino {
	Cell[] cells = new Cell[4];
	/**向左移动*/
	public void moveLeft(){
		for(int i=0;i=19){
				return true;
			}
		}
		return false;
	}
	public boolean hitLeft(Cell[] cs) {
		for(int i=0;i

步骤3:

package game;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**TestTetromino 是一个画板*/
public class TestTetromino extends JPanel{
	//常量 矩形的宽度,方格的宽度
	public static final int CELL_SIZE = 26;
	
	//成员变量
	Tetromino currentOne = Tetromino.randomOne();
	Tetromino currentNext = Tetromino.randomOne();
	private Cell[] cs = {};
	private boolean[][] b = new boolean[20][10];
	private int state;
	public static final int RUNNING = 0;
	public static final int GAMEOVER = 1;
	
	
	private int score=0;
	
	public static BufferedImage I;
	public static BufferedImage T;
	public static BufferedImage O;
	public static BufferedImage S;
	public static BufferedImage Z;
	public static BufferedImage J;
	public static BufferedImage L;
	public static BufferedImage tetris;
	public static BufferedImage gameover;
	static{
		try {
			//read读取硬盘上的图片
			//getResource(String path):path是图片的相对当前包的位置
			I = ImageIO.read(TestTetromino.class.getResource("I.png"));
			T = ImageIO.read(TestTetromino.class.getResource("T.png"));
			O = ImageIO.read(TestTetromino.class.getResource("O.png"));
			S = ImageIO.read(TestTetromino.class.getResource("S.png"));
			Z = ImageIO.read(TestTetromino.class.getResource("Z.png"));
			J = ImageIO.read(TestTetromino.class.getResource("J.png"));
			L = ImageIO.read(TestTetromino.class.getResource("L.png"));
			tetris = ImageIO.read(TestTetromino.class.getResource("tetris.png"));
			gameover = ImageIO.read(TestTetromino.class.getResource("gameover.png"));
		} catch (Exception e) {
			//打印栈中的信息
			e.printStackTrace();
		}
		
	}
	/**重写绘制功能,此功能画板默认调用*/
	public void paint(Graphics g){
		g.drawImage(tetris,0, 0, null);
		//平移坐标轴
		g.translate(15, 15);
		paintScore(g);
		panelWall(g);
		paintCurrentOne(g);
		paintCurrentNext(g);
		paintCs(g);
		paintState(g);
	}
	private void paintState(Graphics g) {
		if(state==GAMEOVER){
			g.drawImage(gameover, 0, 0, null);
		}
	}
	private void paintCurrentNext(Graphics g) {
		Cell[] cells = currentNext.cells;
		for(int i=0;i9){
				for(int j=0;j

用到的图片可以自己在网上寻找*_*

你可能感兴趣的:(Java-俄罗斯方块最新完善版)