⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 background.java

📁 一个俄罗斯方块的代码,对于j2me的初学者比较适合
💻 JAVA
字号:
import javax.microedition.lcdui.*;
public final class Background implements Storable {
    public static final int EMPTY = 99;
    private int width;
    private int height;
    private int[][] boxes;
    private boolean[] removingRows;
    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; }
    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;
    }
    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;
    }
    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();
                }
            }
        }
    }
    public boolean[] markRemovingRows() {
        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;
    }
    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--) {
            for(int j=0; j<width; j++) {
                boxes[j][i] = boxes[j][i-1];
            }
        }
        for(int j=0; j<width; j++)
            boxes[j][0] = EMPTY;
    }
    public void paint(Graphics g, int box_size) {
        g.setColor(0xffffff);
        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);
        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 + -