这是我写的一个工厂类 ,你先看吧,不明白的地方再问
翻转我用的是矩阵转置的算法
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class Brick {
int x;
int y;
int style;
int color;
int movable=1;
int BLOCK_W ;
int BLOCK_H ;
//方块类型
//中心对称
private final static int S = 0;
private final int Z = 1;
private final int I = 4;
//完全翻转
private final int L = 2;
private final int J = 3;
private final int T = 6;
//无需翻转
private final int O = 5;
byte [][] matrix;
byte[][] tempMatrix = new byte[4][4];
Matrix mat;
/**方块图片*/
Image imgBigBrick[];
Image imgMiniBrick[];
// tetrisCanvas can = new tetrisCanvas(null);
//键值
//KEY_CODE
final static int KEY_LEFT = 4;
final static int KEY_RIGHT = 32;
final static int KEY_UP = 2;
final static int KEY_DOWN = 64;
public Brick(){
mat = new Matrix();
try {
imgBigBrick = new Image[8];
imgMiniBrick = new Image[8];
for(int j=0;j<8;j++){
// imgBrick[j] = new Image();
imgBigBrick[j] = Image.createImage("/gameres/0"+(j+1)+".png");
imgMiniBrick[j] = Image.createImage("/gameres/00"+(j+1)+".png");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setMatrix(){
switch(this.style){
case S:
this.getMatrix(this.matrix, mat.matrixs,4);
break;
case Z:
this.getMatrix(this.matrix, mat.matrixz,4);
break;
case I:
this.getMatrix(this.matrix, mat.matrixi,4);
break;
case L:
this.getMatrix(this.matrix, mat.matrixl,4);
break;
case J:
this.getMatrix(this.matrix, mat.matrixj,4);
break;
case T:
this.getMatrix(this.matrix, mat.matrixt,4);
break;
case O:
this.getMatrix(this.matrix, mat.matrixo,4);
break;
}
}
//旋转
public void turn(byte[][] scene){
//记录之前的矩阵后转动,若不可转再还原
if(this.style!=O){//如果是O就不翻转
if(this.style == I && this.x == 162){
}else{
this.getMatrix(tempMatrix,this.matrix,4);
this.doTurn(tempMatrix);
}
if(this.checkCollision(scene,tempMatrix,this.x,this.y,0,0)!=1)
this.getMatrix(this.matrix, tempMatrix,4);
}
}
//快速下落到底
public void down(byte[][] scene){
while(this.checkCollision(scene,this.matrix,this.x,this.y+BLOCK_H,0,0)!=1){
this.y +=BLOCK_H;
}
}
//正常下落
public void drop(byte[][] scene){
if(this.checkCollision(scene,this.matrix,this.x,this.y+BLOCK_H,0,0)!=1)
this.y += BLOCK_H;
else this.movable = 0;
}
//平移
public void platlyMove(byte[][] scene,int offset){
//试探左移,检测碰撞
if(this.checkCollision(scene,this.matrix,this.x+offset,this.y,0,0)!=1)
this.x +=offset;
}
//获取场地方块值检测碰撞
public int checkCollision(byte[][] scene,byte[][] matrix,int nextx,int nexty,int offsetX,int offsetY){
int flag = 0;
int x;
int y;
for(int i=0;i<4;i++){
if(flag == 0){
for(int j=0;j<4;j++){
if(matrix[i][j]==1){
x = offsetX+nextx/BLOCK_W+j;
y = offsetY+nexty/BLOCK_H+i;
if(scene[y][x] != 0){
////System.out.println("scene[y][x]=("+y+","+x+")");
flag = 1;
}
}
}
}//if
}
return flag;
}
/**绘制方块*/
public void drawBrick(byte[][] brick,int style,Graphics g,int offsetx,int offsety){
// //System.out.println("offset = "+offsetx);
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(brick[i][j]==1){
if(style == 0)//0表示大方块
g.drawImage(imgBigBrick[this.color],offsetx+this.x+j*this.BLOCK_W, offsety+this.y+i*this.BLOCK_H, 0x4|0x10);
else//1表示小方块
g.drawImage(imgMiniBrick[this.color],offsetx+this.x+j*this.BLOCK_W, offsety+this.y+i*this.BLOCK_H, 0x4|0x10);
// g.setColor(this.color);
// g.fillRect(offsetx+this.x+j*this.BLOCK_W, offsety+this.y+i*this.BLOCK_H, this.BLOCK_W, this.BLOCK_H);
g.setColor(0xffffff);
g.drawRect(offsetx+this.x+j*this.BLOCK_W,offsety+ this.y+i*this.BLOCK_H, this.BLOCK_W, this.BLOCK_H);
}
}
}
}
/**拷贝矩阵*/
public void getMatrix(byte[][] curMatrix,byte[][] oriMatrix,int blockNum){
for(int i=0;i
}
}
}
public void doTurn(byte[][] matrix){
//先将矩阵转置,再将最后一行移至第一行,
byte[][] tempMatrix = new byte[4][4];
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
tempMatrix[i][j] = matrix[3-j][i];
}
}
for(int k=0;k<3;k++){
for(int n=0;n<4;n++){
matrix[k+1][n] = tempMatrix[k][n];
}
}
for(int n=0;n<4;n++){
matrix[0][n] = tempMatrix[3][n];
}
this.tempMatrix = matrix;
}
}
-----------------矩阵----------------------------
byte[][] matrixz = new byte[][]{{0,0,0,0},
{1,1,0,0},
{0,1,1,0},
{0,0,0,0}};
byte[][] matrixs = new byte[][]{{0,0,0,0},
{1,0,0,0},
{1,1,0,0},
{0,1,0,0}};
byte[][] matrixl = new byte[][]{{0,0,0,0},
{0,1,0,0},
{0,1,0,0},
{0,1,1,0}};
byte[][] matrixj = new byte[][]{{0,0,0,0},
{0,1,0,0},
{0,1,0,0},
{1,1,0,0}};
byte[][] matrixt = new byte[][]{
{0,0,0,0},
{0,1,0,0},
{1,1,1,0},
{0,0,0,0}};
byte[][] matrixo = new byte[][]{{0,0,0,0},
{0,1,1,0},
{0,1,1,0},
{0,0,0,0}};
byte[][] matrixi = new byte[][]{{0,0,0,0},
{0,0,0,0},
{1,1,1,1},
{0,0,0,0}};
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class Brick {
int x;
int y;
int style;
int color;
int movable=1;
int BLOCK_W ;
int BLOCK_H ;
//方块类型
//中心对称
private final static int S = 0;
private final int Z = 1;
private final int I = 4;
//完全翻转
private final int L = 2;
private final int J = 3;
private final int T = 6;
//无需翻转
private final int O = 5;
byte [][] matrix;
byte[][] tempMatrix = new byte[4][4];
Matrix mat;
/**方块图片*/
Image imgBigBrick[];
Image imgMiniBrick[];
// tetrisCanvas can = new tetrisCanvas(null);
//键值
//KEY_CODE
final static int KEY_LEFT = 4;
final static int KEY_RIGHT = 32;
final static int KEY_UP = 2;
final static int KEY_DOWN = 64;
public Brick(){
mat = new Matrix();
try {
imgBigBrick = new Image[8];
imgMiniBrick = new Image[8];
for(int j=0;j<8;j++){
// imgBrick[j] = new Image();
imgBigBrick[j] = Image.createImage("/gameres/0"+(j+1)+".png");
imgMiniBrick[j] = Image.createImage("/gameres/00"+(j+1)+".png");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setMatrix(){
switch(this.style){
case S:
this.getMatrix(this.matrix, mat.matrixs,4);
break;
case Z:
this.getMatrix(this.matrix, mat.matrixz,4);
break;
case I:
this.getMatrix(this.matrix, mat.matrixi,4);
break;
case L:
this.getMatrix(this.matrix, mat.matrixl,4);
break;
case J:
this.getMatrix(this.matrix, mat.matrixj,4);
break;
case T:
this.getMatrix(this.matrix, mat.matrixt,4);
break;
case O:
this.getMatrix(this.matrix, mat.matrixo,4);
break;
}
}
//旋转
public void turn(byte[][] scene){
//记录之前的矩阵后转动,若不可转再还原
if(this.style!=O){//如果是O就不翻转
if(this.style == I && this.x == 162){
}else{
this.getMatrix(tempMatrix,this.matrix,4);
this.doTurn(tempMatrix);
}
if(this.checkCollision(scene,tempMatrix,this.x,this.y,0,0)!=1)
this.getMatrix(this.matrix, tempMatrix,4);
}
}
//快速下落到底
public void down(byte[][] scene){
while(this.checkCollision(scene,this.matrix,this.x,this.y+BLOCK_H,0,0)!=1){
this.y +=BLOCK_H;
}
}
//正常下落
public void drop(byte[][] scene){
if(this.checkCollision(scene,this.matrix,this.x,this.y+BLOCK_H,0,0)!=1)
this.y += BLOCK_H;
else this.movable = 0;
}
//平移
public void platlyMove(byte[][] scene,int offset){
//试探左移,检测碰撞
if(this.checkCollision(scene,this.matrix,this.x+offset,this.y,0,0)!=1)
this.x +=offset;
}
//获取场地方块值检测碰撞
public int checkCollision(byte[][] scene,byte[][] matrix,int nextx,int nexty,int offsetX,int offsetY){
int flag = 0;
int x;
int y;
for(int i=0;i<4;i++){
if(flag == 0){
for(int j=0;j<4;j++){
if(matrix[i][j]==1){
x = offsetX+nextx/BLOCK_W+j;
y = offsetY+nexty/BLOCK_H+i;
if(scene[y][x] != 0){
////System.out.println("scene[y][x]=("+y+","+x+")");
flag = 1;
}
}
}
}//if
}
return flag;
}
/**绘制方块*/
public void drawBrick(byte[][] brick,int style,Graphics g,int offsetx,int offsety){
// //System.out.println("offset = "+offsetx);
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(brick[i][j]==1){
if(style == 0)//0表示大方块
g.drawImage(imgBigBrick[this.color],offsetx+this.x+j*this.BLOCK_W, offsety+this.y+i*this.BLOCK_H, 0x4|0x10);
else//1表示小方块
g.drawImage(imgMiniBrick[this.color],offsetx+this.x+j*this.BLOCK_W, offsety+this.y+i*this.BLOCK_H, 0x4|0x10);
// g.setColor(this.color);
// g.fillRect(offsetx+this.x+j*this.BLOCK_W, offsety+this.y+i*this.BLOCK_H, this.BLOCK_W, this.BLOCK_H);
g.setColor(0xffffff);
g.drawRect(offsetx+this.x+j*this.BLOCK_W,offsety+ this.y+i*this.BLOCK_H, this.BLOCK_W, this.BLOCK_H);
}
}
}
}
/**拷贝矩阵*/
public void getMatrix(byte[][] curMatrix,byte[][] oriMatrix,int blockNum){
for(int i=0;i
}
}
}
public void doTurn(byte[][] matrix){
//先将矩阵转置,再将最后一行移至第一行,
byte[][] tempMatrix = new byte[4][4];
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
tempMatrix[i][j] = matrix[3-j][i];
}
}
for(int k=0;k<3;k++){
for(int n=0;n<4;n++){
matrix[k+1][n] = tempMatrix[k][n];
}
}
for(int n=0;n<4;n++){
matrix[0][n] = tempMatrix[3][n];
}
this.tempMatrix = matrix;
}
}
-----------------矩阵----------------------------
byte[][] matrixz = new byte[][]{{0,0,0,0},
{1,1,0,0},
{0,1,1,0},
{0,0,0,0}};
byte[][] matrixs = new byte[][]{{0,0,0,0},
{1,0,0,0},
{1,1,0,0},
{0,1,0,0}};
byte[][] matrixl = new byte[][]{{0,0,0,0},
{0,1,0,0},
{0,1,0,0},
{0,1,1,0}};
byte[][] matrixj = new byte[][]{{0,0,0,0},
{0,1,0,0},
{0,1,0,0},
{1,1,0,0}};
byte[][] matrixt = new byte[][]{
{0,0,0,0},
{0,1,0,0},
{1,1,1,0},
{0,0,0,0}};
byte[][] matrixo = new byte[][]{{0,0,0,0},
{0,1,1,0},
{0,1,1,0},
{0,0,0,0}};
byte[][] matrixi = new byte[][]{{0,0,0,0},
{0,0,0,0},
{1,1,1,1},
{0,0,0,0}};
为回车键添加一个监听器,然后在监听器中写使俄罗斯方块可以变形的方法即可
加入监听,然后按下后对图像进行rapait就可以了