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

📄 activeshape.java

📁 一个俄罗斯方块的代码,对于j2me的初学者比较适合
💻 JAVA
字号:
import java.util.Random;
import javax.microedition.lcdui.*;
public final class ActiveShape implements Storable {
    private Shape currentShape;
    private int nextShapeIndex;
    private Shape previousShape;
    private int colorIndex;
    private int init_left;
    private int left;
    private int top;
    private int pre_left;
    private int pre_top;
    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];
        data[0] = (byte)Shape.indexOf(currentShape);
        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;
    }
    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;
        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();
        left--;
        int[] data = currentShape.getData();
        for(int i=0; i<0-left; i++) { 
            for(int j=0; j<4; j++) {
                if(data[i+4*j]==1) {
                    left++;
                    return false;
                }
            }
        }
        if(bg.collidesWith(this)) {
            left++;
            return false;
        }
        return true;
    }
    public boolean moveRight(Background bg) {
        lastLocationBeforeMove();
        left++;
        int width = bg.getWidth();
        int[] data = currentShape.getData();
        for(int i=width-left; i<4; i++) {
            for(int j=0; j<4; j++) {
                if(data[i+4*j]==1) {
                    left--;
                    return false;
                }
            }
        }
        if(bg.collidesWith(this)) {
            left--;
            return false;
        }
        return true;
    }
    public boolean moveDown(Background bg) {
        lastLocationBeforeMove();
        top++;
        int height = bg.getHeight();
        int[] data = currentShape.getData();
        for(int i=height-top; i<4; i++) {
            for(int j=0; j<4; j++) {
                if(data[4*i+j]==1) {
                    top--;
                    return false;
                }
            }
        }
        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();
        int width = bg.getWidth();
        int[] data = currentShape.getData();
        for(int i=width-left; i<4; i++) {
            for(int j=0; j<4; j++) {
                if(data[i+4*j]==1) {
                    this.currentShape = backup;
                    return false;
                }
            }
        }
        for(int i=0; i<0-left; i++) {
            for(int j=0; j<4; j++) {
                if(data[i+4*j]==1) {
                    this.currentShape = backup;
                    return false;
                }
            }
        }
        if(bg.collidesWith(this)) {
            this.currentShape = backup;
            return false;
        }
        previousShape = backup;
        return true;
    }
    public void paint(Graphics g, int box_size) {
        if(previousShape==currentShape) {
            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 {
            g.translate(left*box_size, top*box_size);
            previousShape.erase(g, box_size);
            currentShape.paint(g, box_size, Colors.ALL_COLORS[colorIndex]);
            previousShape=currentShape;
        }
    }
    public Shape getNextShape() {
        return Shape.SHAPES[nextShapeIndex];
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -