回首大学四年,自己一事无成.毕业之际,来到Java领域,开始了java菜鸟路.
今天带来的是java入门级小项目,俄罗斯方块.
首先介绍一下思路吧,也算是个人经历,初学者,难免会有一种拿到一个小项目,没有任何思绪,完全不知道从何下手.我也一样.
那么,俄罗斯方块要如何去实现呢,首先我们先不去考虑界面问题,就先研究一下它的结构,该如何去实现方块的功能.
方块-->点,方块是由点组成的,一个方块四个点.所以我们首先要建立一个Cell类,用来实现点的功能(表示一个点的属性和实现一个点的功能).
代码如下:
package com.dsc.tetris;
import java.awt.image.BufferedImage;
//格子类
public class Cell {
private int row;
private int col;
private BufferedImage image;
public Cell(int row, int col, BufferedImage image) {
super();
this.row = row;
this.col = col;
this.image = image;
}
public void drop(){
row ++;
}
public void moveRight(){
col ++;
}
public void moveLeft(){
col --;
}
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 BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
public String toString(){
return row + "," + col;
}
}
这个类的主要功能就是,实现点的表示和向四个方向的移动.
package com.dsc.tetris;
import java.awt.image.BufferedImage;
//格子类
public class Cell {
private int row;
private int col;
private BufferedImage image;
public Cell(int row, int col, BufferedImage image) {
super();
this.row = row;
this.col = col;
this.image = image;
}
public void drop(){
row ++;
}
public void moveRight(){
col ++;
}
public void moveLeft(){
col --;
}
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 BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
public String toString(){
return row + "," + col;
}
}
那么有了点功能的实现我们就能表示一个方块了,四个点表示一个方块(例如:I型方块坐标为(0,4)(1,4)(2,4)(3,4)).
有了思路我们开始下一步,初始化方块.
方块的类型共有7种(I,O,Z,S,L,O,J).它们在俄罗斯方块项目中都有共同的属性,实际为4个点,有上下左右的移动.所以我们为了避免过多的代码重复建立一个父类,表示它们共有的属性.然后在7中方块类中分别继承它来获取共有的属性.
代码如下:(其中包括后续的,初始化和随机出现方块的算法,以及方块旋转的算法(我后面会讲,先一起放着))
package com.dsc.tetris;
import java.util.Random;
//父类,抽象类
public abstract class Tetromino {
protected Cell[] cells = new Cell[4];
protected State[] state;
//旋转初始化
class State{
int row0,col0;
int row1,col1;
int row2,col2;
int row3,col3;
public State(int row0, int col0, int row1, int col1, int row2,
int col2, int row3, int col3) {
super();
this.row0 = row0;
this.col0 = col0;
this.row1 = row1;
this.col1 = col1;
this.row2 = row2;
this.col2 = col2;
this.row3 = row3;
this.col3 = col3;
}
}
//方块旋转算法
protected int index = 100;
public void rotateRight(){
index ++;
State states = state[index % state.length];
int x = cells[0].getRow();
int y = cells[0].getCol();
cells[1].setRow(x + states.row1);
cells[1].setCol(y + states.col1);
cells[2].setRow(x + states.row2);
cells[2].setCol(y + states.col2);
cells[3].setRow(x + states.row3);
cells[3].setCol(y + states.col3);
}
public void rotateLeft(){
index --;
State states =state[index % state.length];
int x = cells[0].getRow();
int y = cells[0].getCol();
cells[1].setRow(x + states.row1);
cells[1].setCol(y + states.col1);
cells[2].setRow(x + states.row2);
cells[2].setCol(y + states.col2);
cells[3].setRow(x + states.row3);
cells[3].setCol(y + states.col3);
}
public void drop(){
for(int i = 0;i < cells.length;i++)
cells[i].drop();
}
public void moveRight(){
for(int i = 0;i < cells.length;i++)
cells[i].moveRight();
}
public void moveLeft(){
for(int i = 0;i < cells.length;i++)
cells[i].moveLeft();
}
//随机产生一个方块
public static Tetromino random(){
Random random = new Random();
int type = random.nextInt(7);
switch(type){
case 0:return new T();
case 1:return new I();
case 2:return new J();
case 3:return new S();
case 4:return new L();
case 5:return new Z();
case 6:return new O();
}
return null;
}
}
class T extends Tetromino{
public T(){
cells[0] = new Cell(0,4,Tetris.T);
cells[1] = new Cell(0,3,Tetris.T);
cells[2] = new Cell(0,5,Tetris.T);
cells[3] = new Cell(1,4,Tetris.T);
//格子的四种旋转状态
state = new State[4];
state[0] = new State(0,0,0,-1,0,1,1,0);
state[1] = new State(0,0,-1,0,1,0,0,-1);
state[2] = new State(0,0,0,1,0,-1,-1,0);
state[3] = new State(0,0,1,0,-1,0,0,1);
}
}
class O extends Tetromino{
public O(){
cells[0] = new Cell(0,4,Tetris.O);
cells[1] = new Cell(0,3,Tetris.O);
cells[2] = new Cell(1,3,Tetris.O);
cells[3] = new Cell(1,4,Tetris.O);
state = new State[]{new State(0,0,0,1,1,0,1,1)
,new State(0,0,0,1,1,0,1,1)};
}
}
class S extends Tetromino{
public S(){
cells[0] = new Cell(1,4,Tetris.S);
cells[1] = new Cell(1,3,Tetris.S);
cells[2] = new Cell(0,4,Tetris.S);
cells[3] = new Cell(0,5,Tetris.S);
state = new State[]{new State(0,0,0,-1,-1,0,-1,1),
new State(0,0,-1,0,0,1,1,1)};
}
}
class Z extends Tetromino{
public Z(){
cells[0] = new Cell(1,4,Tetris.Z);
cells[1] = new Cell(1,5,Tetris.Z);
cells[2] = new Cell(0,3,Tetris.Z);
cells[3] = new Cell(0,4,Tetris.Z);
state = new State[]{new State(0,0,-1,-1,-1,0,0,1)
,new State(0,0,-1,1,0,1,1,0)};
}
}
class L extends Tetromino{
public L(){
cells[0] = new Cell(0,4,Tetris.L);
cells[1] = new Cell(0,3,Tetris.L);
cells[2] = new Cell(0,5,Tetris.L);
cells[3] = new Cell(1,3,Tetris.L);
state = new State[]{new State(0,0,0,-1,0,1,1,-1)
,new State(0,0,-1,0,1,0,-1,-1)
,new State(0,0,0,1,0,-1,-1,1)
,new State(0,0,1,0,-1,0,1,1)};
}
}
class J extends Tetromino{
public J(){
cells[0] = new Cell(0,4,Tetris.J);
cells[1] = new Cell(0,3,Tetris.J);
cells[2] = new Cell(0,5,Tetris.J);
cells[3] = new Cell(1,5,Tetris.J);
state = new State[]{new State(0,0,0,-1,0,1,1,1)
,new State(0,0,-1,0,1,0,1,-1)
,new State(0,0,0,1,0,-1,-1,-1)
,new State(0,0,1,0,-1,0,-1,1)};
}
}
class I extends Tetromino{
public I(){
cells[0] = new Cell(0,4,Tetris.I);
cells[1] = new Cell(0,3,Tetris.I);
cells[2] = new Cell(0,5,Tetris.I);
cells[3] = new Cell(0,6,Tetris.I);
state = new State[]{new State(0,0,0,-1,0,1,0,2),
new State(0,0,-1,0,1,0,2,0)};
}
}
package com.dsc.tetris;
import java.util.Random;
//父类,抽象类
public abstract class Tetromino {
protected Cell[] cells = new Cell[4];
protected State[] state;
//旋转初始化
class State{
int row0,col0;
int row1,col1;
int row2,col2;
int row3,col3;
public State(int row0, int col0, int row1, int col1, int row2,
int col2, int row3, int col3) {
super();
this.row0 = row0;
this.col0 = col0;
this.row1 = row1;
this.col1 = col1;
this.row2 = row2;
this.col2 = col2;
this.row3 = row3;
this.col3 = col3;
}
}
//方块旋转算法
protected int index = 100;
public void rotateRight(){
index ++;
State states = state[index % state.length];
int x = cells[0].getRow();
int y = cells[0].getCol();
cells[1].setRow(x + states.row1);
cells[1].setCol(y + states.col1);
cells[2].setRow(x + states.row2);
cells[2].setCol(y + states.col2);
cells[3].setRow(x + states.row3);
cells[3].setCol(y + states.col3);
}
public void rotateLeft(){
index --;
State states =state[index % state.length];
int x = cells[0].getRow();
int y = cells[0].getCol();
cells[1].setRow(x + states.row1);
cells[1].setCol(y + states.col1);
cells[2].setRow(x + states.row2);
cells[2].setCol(y + states.col2);
cells[3].setRow(x + states.row3);
cells[3].setCol(y + states.col3);
}
public void drop(){
for(int i = 0;i < cells.length;i++)
cells[i].drop();
}
public void moveRight(){
for(int i = 0;i < cells.length;i++)
cells[i].moveRight();
}
public void moveLeft(){
for(int i = 0;i < cells.length;i++)
cells[i].moveLeft();
}
//随机产生一个方块
public static Tetromino random(){
Random random = new Random();
int type = random.nextInt(7);
switch(type){
case 0:return new T();
case 1:return new I();
case 2:return new J();
case 3:return new S();
case 4:return new L();
case 5:return new Z();
case 6:return new O();
}
return null;
}
}
class T extends Tetromino{
public T(){
cells[0] = new Cell(0,4,Tetris.T);
cells[1] = new Cell(0,3,Tetris.T);
cells[2] = new Cell(0,5,Tetris.T);
cells[3] = new Cell(1,4,Tetris.T);
//格子的四种旋转状态
state = new State[4];
state[0] = new State(0,0,0,-1,0,1,1,0);
state[1] = new State(0,0,-1,0,1,0,0,-1);
state[2] = new State(0,0,0,1,0,-1,-1,0);
state[3] = new State(0,0,1,0,-1,0,0,1);
}
}
class O extends Tetromino{
public O(){
cells[0] = new Cell(0,4,Tetris.O);
cells[1] = new Cell(0,3,Tetris.O);
cells[2] = new Cell(1,3,Tetris.O);
cells[3] = new Cell(1,4,Tetris.O);
state = new State[]{new State(0,0,0,1,1,0,1,1)
,new State(0,0,0,1,1,0,1,1)};
}
}
class S extends Tetromino{
public S(){
cells[0] = new Cell(1,4,Tetris.S);
cells[1] = new Cell(1,3,Tetris.S);
cells[2] = new Cell(0,4,Tetris.S);
cells[3] = new Cell(0,5,Tetris.S);
state = new State[]{new State(0,0,0,-1,-1,0,-1,1),
new State(0,0,-1,0,0,1,1,1)};
}
}
class Z extends Tetromino{
public Z(){
cells[0] = new Cell(1,4,Tetris.Z);
cells[1] = new Cell(1,5,Tetris.Z);
cells[2] = new Cell(0,3,Tetris.Z);
cells[3] = new Cell(0,4,Tetris.Z);
state = new State[]{new State(0,0,-1,-1,-1,0,0,1)
,new State(0,0,-1,1,0,1,1,0)};
}
}
class L extends Tetromino{
public L(){
cells[0] = new Cell(0,4,Tetris.L);
cells[1] = new Cell(0,3,Tetris.L);
cells[2] = new Cell(0,5,Tetris.L);
cells[3] = new Cell(1,3,Tetris.L);
state = new State[]{new State(0,0,0,-1,0,1,1,-1)
,new State(0,0,-1,0,1,0,-1,-1)
,new State(0,0,0,1,0,-1,-1,1)
,new State(0,0,1,0,-1,0,1,1)};
}
}
class J extends Tetromino{
public J(){
cells[0] = new Cell(0,4,Tetris.J);
cells[1] = new Cell(0,3,Tetris.J);
cells[2] = new Cell(0,5,Tetris.J);
cells[3] = new Cell(1,5,Tetris.J);
state = new State[]{new State(0,0,0,-1,0,1,1,1)
,new State(0,0,-1,0,1,0,1,-1)
,new State(0,0,0,1,0,-1,-1,-1)
,new State(0,0,1,0,-1,0,-1,1)};
}
}
class I extends Tetromino{
public I(){
cells[0] = new Cell(0,4,Tetris.I);
cells[1] = new Cell(0,3,Tetris.I);
cells[2] = new Cell(0,5,Tetris.I);
cells[3] = new Cell(0,6,Tetris.I);
state = new State[]{new State(0,0,0,-1,0,1,0,2),
new State(0,0,-1,0,1,0,2,0)};
}
}
先写到这,后面的明天补上.