📄 background.java
字号:
/*
* Created on 2005-5-18
* Author: Liao Xuefeng, asklxf@163.com
* Copyright (C) 2005, Liao Xuefeng.
*/
package com.crackj2ee.j2me.game.russia;
import javax.microedition.lcdui.*;
/**
* To represent the container of all boxes.
*
* @author Xuefeng
*/
public final class Background implements Storable {
public static final int EMPTY = 99;
// holds width & height:
private int width;
private int height;
// holds boxes, EMPTY or color_index:
private int[][] boxes;
// holds the rows that can be removed.
private boolean[] removingRows;
// make sure cannot instanciate from outside:
public Background(int width, int height) {
this.width = width;
this.height = height;
this.removingRows = new boolean[height];
this.boxes = new int[width][height];
}
public int getWidth() { return this.width; }
public int getHeight() { return this.height; }
/**
* Clean all boxes and can start a new game.
*/
public void init() {
for(int i=0; i<width; i++) {
for(int j=0; j<height; j++)
boxes[i][j] = EMPTY;
}
for(int i=0; i<height; i++)
removingRows[i] = false;
}
public byte[] getRecordData() {
byte[] data = new byte[width*height];
int offset = 0;
for(int r=0; r<height; r++) {
for(int c=0; c<width; c++) {
data[offset] = (byte)boxes[c][r];
offset++;
}
}
return data;
}
public int setRecordData(byte[] data, int offset) {
if((data.length-offset)<width*height) return (-1);
for(int r=0; r<height; r++) {
for(int c=0; c<width; c++) {
boxes[c][r] = data[offset];
offset++;
}
}
return width*height;
}
/**
* Detect if there is a collition between two 4x4 area.
*
* @param active The ActiveShape object.
* @return True if there is a collition.
*/
public boolean collidesWith(ActiveShape active) {
int[] data = active.getCurrentShape().getData();
int left = active.getLeft();
int top = active.getTop();
for(int i1=top, i2=0; i1<top+4; i1++, i2++) {
for(int j1=left, j2=0; j1<left+4; j1++, j2++) {
if(i1>=0 && i1<height && j1>=0 && j1<width)
if(data[4*i2+j2]==1 && boxes[j1][i1]!=EMPTY)
return true;
}
}
return false;
}
public int[][] getBoxes() {
return boxes;
}
/**
* Merge the ActiveShape to the Background.
*
* @param active The ActiveShape object.
*/
public void merge(ActiveShape active) {
int left = active.getLeft();
int top = active.getTop();
int box;
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
box = active.getCurrentShape().getData()[i*4+j];
if(box==1) {
this.boxes[left+j][top+i] = active.getColorIndex();
}
}
}
}
/**
* Mark all rows that can be removed.
*/
public boolean[] markRemovingRows() {
// mark all rows if it can be removed:
for(int i=0; i<height; i++) {
removingRows[i] = true;
for(int j=0; j<width; j++) {
if(boxes[j][i]==EMPTY) {
removingRows[i] = false;
break;
}
}
}
return removingRows;
}
/**
* Remove all marked rows.
*
* @return How many rows removed.
*/
public int doRemove() {
int r = height - 1;
int i = height - 1;
for(i=height-1; i>=0; i--) {
while(r>=0 && removingRows[r])
r--;
if(r==(-1))
break;
copyRow(r, i);
r--;
}
for(int j=i; j>=0; j--)
for(int n=0; n<width; n++)
boxes[n][j] = EMPTY;
return i+1;
}
private void copyRow(int src, int dest) {
for(int j=0; j<width; j++)
boxes[j][dest] = boxes[j][src];
}
private void moveDown(int removing) {
for(int i=removing; i>0; i--) {
// copy row[i-1] to row[i]:
for(int j=0; j<width; j++) {
boxes[j][i] = boxes[j][i-1];
}
}
// clean the top row:
for(int j=0; j<width; j++)
boxes[j][0] = EMPTY;
}
public void paint(Graphics g, int box_size) {
g.setColor(0xffffff); // white
g.drawRect(0, 0, box_size*width+2, box_size*height+2);
g.setColor(0);
g.fillRect(1, 1, box_size*width, box_size*height);
g.translate(1, 1);
// draw each boxes:
for(int i=0; i<width; i++) {
for(int j=0; j<height; j++) {
int color_index = boxes[i][j];
if(color_index!=EMPTY) {
g.setColor(MyCanvas.BORDER_COLOR);
g.drawRect(i*box_size, j*box_size, box_size, box_size);
g.setColor(Colors.ALL_COLORS[color_index]);
g.fillRect(i*box_size+1, j*box_size+1, box_size-2, box_size-2);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -