📄 activeshape.java
字号:
/*
* Created on 2005-5-18
* Author: Liao Xuefeng, asklxf@163.com
* Copyright (C) 2005, Liao Xuefeng.
*/
package com.crackj2ee.j2me.game.russia;
import java.util.Random;
import javax.microedition.lcdui.*;
/**
* ActiveShape represent the current falling shape.
*
* @author Xuefeng
*/
public final class ActiveShape implements Storable {
private Shape currentShape; // current shape
private int nextShapeIndex; // next shape
private Shape previousShape; // previous shape
private int colorIndex;
// init location:
private int init_left;
// the location on background:
private int left;
private int top;
// the last location:
private int pre_left;
private int pre_top;
// random generator:
private Random random = new Random();
public ActiveShape(int init_left) {
this.init_left = init_left;
}
public void init() {
currentShape = Shape.SHAPES[0];
previousShape = currentShape;
nextShapeIndex = 1;
colorIndex = 0;
}
public byte[] getRecordData() {
byte[] data = new byte[3];
// current shape index:
data[0] = (byte)Shape.indexOf(currentShape);
// next shape index:
data[1] = (byte)nextShapeIndex;
data[2] = (byte)colorIndex;
return data;
}
public int setRecordData(byte[] data, int offset) {
if(data.length-offset<3) return (-1);
currentShape = Shape.SHAPES[data[offset]];
previousShape = currentShape;
nextShapeIndex = data[offset+1];
colorIndex = data[offset+2];
this.left = init_left;
this.top = Shape.getInitTop(Shape.indexOf(currentShape));
return 3;
}
/**
* If false, that means GAME OVER!
*
* @param shapeIndex The index of the shape.
* @param color The color of the shape.
* @return True if can reset to top.
*/
public boolean reset(Background bg) {
this.currentShape = Shape.SHAPES[nextShapeIndex];
this.previousShape = currentShape;
this.left = init_left;
this.top = Shape.getInitTop(nextShapeIndex);
this.pre_left = left;
this.pre_top = top;
if(bg.collidesWith(this))
return false;
// set next shape:
random.setSeed(System.currentTimeMillis());
int rnd = random.nextInt();
if(rnd<0) rnd=0-rnd;
colorIndex = rnd % Colors.ALL_COLORS.length;
nextShapeIndex = rnd % Shape.SHAPES.length;
return true;
}
public Shape getCurrentShape() { return this.currentShape; }
public int getColorIndex() { return this.colorIndex; }
public int getLeft() { return this.left; }
public int getTop() { return this.top; }
private void lastLocationBeforeMove() {
pre_left = left;
pre_top = top;
}
public boolean moveLeft(Background bg) {
lastLocationBeforeMove();
// move left first:
left--;
// detect if overflow:
int[] data = currentShape.getData();
for(int i=0; i<0-left; i++) { // col
for(int j=0; j<4; j++) { // row
if(data[i+4*j]==1) {
left++;
return false;
}
}
}
// detect if collides with boxes:
if(bg.collidesWith(this)) {
left++;
return false;
}
return true;
}
public boolean moveRight(Background bg) {
lastLocationBeforeMove();
// move right first:
left++;
// detect if overflow:
int width = bg.getWidth();
int[] data = currentShape.getData();
for(int i=width-left; i<4; i++) { // col
for(int j=0; j<4; j++) { // row
if(data[i+4*j]==1) {
left--;
return false;
}
}
}
// detect if collides with boxes:
if(bg.collidesWith(this)) {
left--;
return false;
}
return true;
}
public boolean moveDown(Background bg) {
lastLocationBeforeMove();
// move down first:
top++;
// detect if fall on bottom:
int height = bg.getHeight();
int[] data = currentShape.getData();
for(int i=height-top; i<4; i++) { // row
for(int j=0; j<4; j++) { // col
if(data[4*i+j]==1) {
top--;
return false;
}
}
}
// detect if collides with boxes:
if(bg.collidesWith(this)) {
top--;
return false;
}
return true;
}
public void fallDown(Background bg) {
while(moveDown(bg));
}
public boolean change(Background bg) {
Shape backup = currentShape;
currentShape = currentShape.next();
// test if overflow:
int width = bg.getWidth();
int[] data = currentShape.getData();
for(int i=width-left; i<4; i++) { // col
for(int j=0; j<4; j++) { // row
if(data[i+4*j]==1) {
this.currentShape = backup;
return false;
}
}
}
for(int i=0; i<0-left; i++) { // col
for(int j=0; j<4; j++) { // row
if(data[i+4*j]==1) {
this.currentShape = backup;
return false;
}
}
}
// test if collides with:
if(bg.collidesWith(this)) {
this.currentShape = backup;
return false;
}
previousShape = backup;
return true;
}
public void paint(Graphics g, int box_size) {
if(previousShape==currentShape) {
// just move, not change:
g.translate(pre_left*box_size, pre_top*box_size);
currentShape.erase(g, box_size);
g.translate((left-pre_left)*box_size, (top-pre_top)*box_size);
currentShape.paint(g, box_size, Colors.ALL_COLORS[colorIndex]);
}
else {
// just changed:
g.translate(left*box_size, top*box_size);
previousShape.erase(g, box_size);
// draw new position:
currentShape.paint(g, box_size, Colors.ALL_COLORS[colorIndex]);
// make prev=current until next change:
previousShape=currentShape;
}
}
public Shape getNextShape() {
return Shape.SHAPES[nextShapeIndex];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -