这篇文章主要介绍了java 小游戏开发之俄罗斯方块的相关资料,这里实现俄罗斯方块的实例和实现效果给大家看下,学习java基础的朋友的好资料,需要的朋友可以参考下
Java项目 俄罗斯方块
一、心得
二、游戏实例
游戏截图
立即学习“Java免费学习笔记(深入)”;



目录结构

三、代码
1、主界面 Tetris.java
package com.fry.tetris;
import java.util.Arrays;
import java.util.Random;
/**
* 4格方块
*/
public class Tetromino {
protected Cell[] cells = new Cell[4];
/** 保存旋转的相对于轴位置状态 */
protected State[] states;
/** 随机生成 4格方块, 使用简单工厂方法模式!
* randomTetromino 随机生成一个四格方块
* 这个方面的返回值是多态的!
* */
public static Tetromino randomTetromino(){
Random r = new Random();
int type = r.nextInt(7);
switch(type){
case 0: return new T();
case 1: return new I();
case 2: return new J();
case 3: return new L();
case 4: return new O();
case 5: return new S();
case 6: return new Z();
}
return null;
}
public Cell[] getCells() {
return cells;
}
/** 下落 */
public void softDrop(){
for(int i=0; i二、Cell.java
package com.fry.tetris;
import java.awt.Image;
/**
* 格子
* 每一个小格子,就有所在的行 列 和图片
*/
public class Cell {
private int row;
private int col;
//private int color;
private Image image;//格子的贴图
public Cell() {
}
public Cell(int row, int col, Image 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 Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
public void moveRight(){
col++;
//System.out.println("Cell moveRight()" + col);
}
public void moveLeft(){
col--;
}
public void moveDown(){
row++;
}
@Override
public String toString() {
return "["+row+","+col+"]";
}
}三、功能实现 Tetromino.java
package com.fry.tetris;
import java.util.Arrays;
import java.util.Random;
/**
* 4格方块
*/
public class Tetromino {
protected Cell[] cells = new Cell[4];
/** 保存旋转的相对于轴位置状态 */
protected State[] states;
/** 随机生成 4格方块, 使用简单工厂方法模式!
* randomTetromino 随机生成一个四格方块
* 这个方面的返回值是多态的!
* */
public static Tetromino randomTetromino(){
Random r = new Random();
int type = r.nextInt(7);
switch(type){
case 0: return new T();
case 1: return new I();
case 2: return new J();
case 3: return new L();
case 4: return new O();
case 5: return new S();
case 6: return new Z();
}
return null;
}
public Cell[] getCells() {
return cells;
}
/** 下落 */
public void softDrop(){
for(int i=0; i











