<菜鸟上路>俄罗斯方块小游戏的具体实现<中>
来源:互联网 发布:大数据集群服务器配置 编辑:程序博客网 时间:2024/06/05 23:40
Tetromino(四格方块)类的实现:
********************************************************************************************************************************************************************************************
import java.util.Arrays;
import java.util.Random;
public abstract class Tetromino{
// 创建Cell类新数组,定义数组长度为4
protected Cell[] cells=new Cell[4];
//创建 State内部类
protected class State{
int row0,col0,row1,col1,row2,col2,row3,col3;
public State(int row0, int col0,
int row1, int col1, int row2,
int col2, int row3, int col3) {
this.row0 = row0;this.col0 = col0;
this.row1 = row1;this.col1 = col1;
this.row2 = row2;this.col2 = col2;
this.row3 = row3;this.col3 = col3;
}
}
//创建State类新数组
protected State[] states;
//定义旋转状态的序号 states[index%states.length]
protected int index = 10000;
//添加向右旋转方法
public void rotateRight(){
//取得变换的下个数据状态 states[n]
//取得当前轴的 row, col
//旋转以后的数据=(row,col) + states[n]
index++;
State s=states[index%states.length];
Cell o = cells[0];
int row = o.getRow();
int col = o.getCol();
cells[1].setRow(row+s.row1);
cells[1].setCol(col+s.col1);
cells[2].setRow(row+s.row2);
cells[2].setCol(col+s.col2);
cells[3].setRow(row+s.row3);
cells[3].setCol(col+s.col3);
}
//添加向左旋转算法
public void rotateLeft(){
index--;
State s=states[index%states.length];
Cell o = cells[0];
int row = o.getRow();
int col = o.getCol();
cells[1].setRow(row+s.row1);
cells[1].setCol(col+s.col1);
cells[2].setRow(row+s.row2);
cells[2].setCol(col+s.col2);
cells[3].setRow(row+s.row3);
cells[3].setCol(col+s.col3);
}
// 创建工厂方法 随机生成四格方块
public static Tetromino randomOne(){
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 L();
case 3: return new J();
case 4: return new S();
case 5: return new Z();
case 6: return new O();
}
return null;
}
//添加四格方块下落方法
public void softDrop(){
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();
}
}
//重写tostring方法
public String toString(){
//return Arrays.toString(cells);
return "["+cells[0].toString()+","+
cells[1].toString()+","+
cells[2].toString()+","+
cells[3].toString()+"]";
}
}
//开始创建俄罗斯方块中下落的七种四格方块模型,分别用字母o,j,t,l,i,s,z来代替
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);
states = new State[4];
states[0] = new State(0,0, 0,-1, 0,1, 1,0);
states[1] = new State(0,0, -1,0, 1,0, 0,-1);
states[2] = new State(0,0, 0,1, 0,-1, -1,0);
states[3] = new State(0,0, 1,0, -1,0, 0,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);
states = new State[] {new State(0,0, 0,1, 0,-1, 0,-2),
new State(0,0, -1,0, 1,0, 2,0)};
}
}
class S extends Tetromino{
public S() {
cells[0] = new Cell(0, 4, Tetris.S);
cells[1] = new Cell(0, 5, Tetris.S);
cells[2] = new Cell(1, 3, Tetris.S);
cells[3] = new Cell(1, 4, Tetris.S);
states = new State[]{
new State(0,0, 0,1, 1,-1, 1,0),
new State(0,0, -1,0, 1,1, 0,1)};
}
}
class Z extends Tetromino{
public Z() {
cells[0] = new Cell(1, 4, Tetris.Z);
cells[1] = new Cell(0, 3, Tetris.Z);
cells[2] = new Cell(0, 4, Tetris.Z);
cells[3] = new Cell(1, 5, Tetris.Z);
states = new State[]{
new State(0,0, -1,-1, -1,0, 0,1),
new State(0,0, -1,1, 0,1, 1,0)};
}
}
class O extends Tetromino{
public O() {
cells[0] = new Cell(0, 4, Tetris.O);
cells[1] = new Cell(0, 5, Tetris.O);
cells[2] = new Cell(1, 4, Tetris.O);
cells[3] = new Cell(1, 5, Tetris.O);
states = new State[]{
new State(0,0, 0,1, 1,0, 1,1),
new State(0,0, 0,1, 1,0, 1,1)};
}
}
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);
states = 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);
states = 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)};
}
}
//附上旋转算法,感觉比较麻烦。但不知道还有什么其他的比较简便的旋转算法
/*
*
* 0 1 2 3
T0=[2,3][2,2][2,4][3,3]
旋转算法: t0.轴+s1=t1
旋转轴o:[2,3]
S1=[0,0][-1,0][1,0][0,-1]
T1=[2,3][1,3][3,3][2,2]
旋转算法: t1.轴+s2=t2
S2=[0,0][0,1][0,-1][-1,0]
T2=[2,3][2,4][2,2][1,3]
旋转算法: t2.轴+s3=t3
S3=[0,0][1,0][-1,0][0,1]
T3=[2,3][3,3][1,3][2,4]
旋转算法: t3.轴+s0=t0
S0=[0,0][0,-1][0,1][1,0]
T0=[2,3][2,2][2,4][3,3] @author Administrator
*
*/
- <菜鸟上路>俄罗斯方块小游戏的具体实现<中>
- <菜鸟上路>俄罗斯方块小游戏的具体实现<上>
- <菜鸟上路>俄罗斯方块小游戏的具体实现<下>
- Java swing实现的俄罗斯方块小游戏源码
- JavaScript, 实现俄罗斯方块小游戏
- Shell实现俄罗斯方块小游戏
- 俄罗斯方块小游戏的各类
- 使用Java实现小游戏:俄罗斯方块
- 使用java实现俄罗斯方块小游戏
- JTextArea编辑的俄罗斯方块小游戏
- 网页版的俄罗斯方块小游戏
- 俄罗斯方块小游戏
- 小游戏 俄罗斯方块
- 分享一个蛋疼的俄罗斯方块小游戏
- javascript+html做一个俄罗斯方块的小游戏
- 基于MFC的俄罗斯方块小游戏(一)
- 基于MFC的俄罗斯方块小游戏(二)
- 菜鸟上路!!!
- 16-JAVA面向对象-类的构造器
- 由农行取消免费的消息服务,开始收费引发的思考和分析
- POJ 1797 的多种解法
- 分享一个VIM配色方案集合的站点
- Java基础进阶——“Java IO流之三:IO实例”
- <菜鸟上路>俄罗斯方块小游戏的具体实现<中>
- KL距离
- vim/vimdiff+cscope+ctags配置及使用总结
- ultraMaGE 分析一 WinMain流程
- #if
- 如何显示在线人数?
- JAVA程序员必读:基础篇类和继承
- 十进制数与二进制数的转换
- 17-JAVA面向对象-this关健字