Java 小游戏开发之俄罗斯方块
程序员文章站
2023-11-25 17:52:40
java项目 俄罗斯方块
一、心得
二、游戏实例
游戏截图
目录结构
三、代码
1、主界面 tetris.java
package c...
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<cells.length; i++){ cells[i].movedown(); } } public void moveright(){ //system.out.println("moveright()"); for(int i=0; i<cells.length; i++){ this.cells[i].moveright(); } } public void moveleft(){ for(int i=0; i<cells.length; i++){ cells[i].moveleft(); } } private int index = 100000; /** 在 tetromino 上添加方法 */ public void rotateright() { index++;//index = 10001 // index % states.length = 10001 % 4 = 1 state s = states[index%states.length];//s1 // [0] + s1 = [1] cell o = cells[0];//获取当前的轴 //轴与相对位置的和作为旋转以后的格子位置 cells[1].setrow(o.getrow()+s.row1); cells[1].setcol(o.getcol()+s.col1); cells[2].setrow(o.getrow()+s.row2); cells[2].setcol(o.getcol()+s.col2); cells[3].setrow(o.getrow()+s.row3); cells[3].setcol(o.getcol()+s.col3); } /** 在 tetromino 上添加方法 */ public void rotateleft() { index--;//index = 10001 // index % states.length = 10001 % 4 = 1 state s = states[index%states.length];//s1 // [0] + s1 = [1] cell o = cells[0];//获取当前的轴 cells[1].setrow(o.getrow()+s.row1); cells[1].setcol(o.getcol()+s.col1); cells[2].setrow(o.getrow()+s.row2); cells[2].setcol(o.getcol()+s.col2); cells[3].setrow(o.getrow()+s.row3); cells[3].setcol(o.getcol()+s.col3); } @override public string tostring() { return arrays.tostring(cells); } /** tetromino 类中添加的 内部类 用于记录旋转状态 */ 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; } } }//tetromino 类的结束 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[]{ new state(0,0, 0,-1, 0,1, 1, 0), new state(0,0, -1,0, 1,0, 0,-1), new state(0,0, 0,1, 0,-1, -1,0), 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 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 )}; } } 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 )}; } }
二、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<cells.length; i++){ cells[i].movedown(); } } public void moveright(){ //system.out.println("moveright()"); for(int i=0; i<cells.length; i++){ this.cells[i].moveright(); } } public void moveleft(){ for(int i=0; i<cells.length; i++){ cells[i].moveleft(); } } private int index = 100000; /** 在 tetromino 上添加方法 */ public void rotateright() { index++;//index = 10001 // index % states.length = 10001 % 4 = 1 state s = states[index%states.length];//s1 // [0] + s1 = [1] cell o = cells[0];//获取当前的轴 //轴与相对位置的和作为旋转以后的格子位置 cells[1].setrow(o.getrow()+s.row1); cells[1].setcol(o.getcol()+s.col1); cells[2].setrow(o.getrow()+s.row2); cells[2].setcol(o.getcol()+s.col2); cells[3].setrow(o.getrow()+s.row3); cells[3].setcol(o.getcol()+s.col3); } /** 在 tetromino 上添加方法 */ public void rotateleft() { index--;//index = 10001 // index % states.length = 10001 % 4 = 1 state s = states[index%states.length];//s1 // [0] + s1 = [1] cell o = cells[0];//获取当前的轴 cells[1].setrow(o.getrow()+s.row1); cells[1].setcol(o.getcol()+s.col1); cells[2].setrow(o.getrow()+s.row2); cells[2].setcol(o.getcol()+s.col2); cells[3].setrow(o.getrow()+s.row3); cells[3].setcol(o.getcol()+s.col3); } @override public string tostring() { return arrays.tostring(cells); } /** tetromino 类中添加的 内部类 用于记录旋转状态 */ 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; } } }//tetromino 类的结束 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[]{ new state(0,0, 0,-1, 0,1, 1, 0), new state(0,0, -1,0, 1,0, 0,-1), new state(0,0, 0,1, 0,-1, -1,0), 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 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 )}; } } 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 )}; } }
以上就是java实现俄罗斯方块的实例,如有疑问请留言或者到本站社区讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!