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

📄 shape.java

📁 手机版俄罗斯方块
💻 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.*;

/**
 * Shape represent a shape. There are totally 19 shapes. 
 * Using <code>Shape[] shapes = Shape.SHAPES;</code> to 
 * get all shapes but do not try to create a shape.
 * 
 * @author Xuefeng
 */
public final class Shape {

    private static final int[] NEXT = { 0,  2,  1,  4,  5,
                                        6,  3,  8,  9, 10,
                                        7, 12, 13, 14, 11,
                                       16, 15, 18, 17
                                      };

    private static final int[] INIT_TOP = {-1, -1,  0,  0, -1,
                                            0,  0,  0,  0,  0,
                                           -1,  0,  0, -1,  0,
                                           -1,  0, -1,  0
                                          };

    public static final Shape[] SHAPES = {

        // Shape of 0, next=0
        // OO
        // OO
        new Shape(0, new int[]{0,0,0,0,
                               0,1,1,0,
                               0,1,1,0,
                               0,0,0,0}),

        // Shape of 1, next=2
        // OOOO
        new Shape(1, new int[]{0,0,0,0,
                               1,1,1,1,
                               0,0,0,0,
                               0,0,0,0}),

        // shape of 2, next=1
        // O
        // O
        // O
        // O
        new Shape(2, new int[]{0,1,0,0,
                               0,1,0,0,
                               0,1,0,0,
                               0,1,0,0}),

        // shape of 3, next=4
        // O
        // O
        // OO
        new Shape(3, new int[]{0,1,0,0,
                               0,1,0,0,
                               0,1,1,0,
                               0,0,0,0}),

        // shape of 4, next=5
        // OOO
        // O
        new Shape(4, new int[]{0,0,0,0,
                               1,1,1,0,
                               1,0,0,0,
                               0,0,0,0}),

        // shape of 5, next=6
        // OO
        //  O
        //  O
        new Shape(5, new int[]{1,1,0,0,
                               0,1,0,0,
                               0,1,0,0,
                               0,0,0,0}),

        // shape of 6, next=3
        //   O
        // OOO
        new Shape(6, new int[]{0,0,1,0,
                               1,1,1,0,
                               0,0,0,0,
                               0,0,0,0}),

        // shape of 7, next=8
        //  O
        //  O
        // OO
        new Shape(7, new int[]{0,1,0,0,
                               0,1,0,0,
                               1,1,0,0,
                               0,0,0,0}),

        // shape of 8, next=9
        // O
        // OOO
        new Shape(8, new int[]{1,0,0,0,
                               1,1,1,0,
                               0,0,0,0,
                               0,0,0,0}),

        // shape of 9, next=10
        // OO
        // O
        // O
        new Shape(9, new int[]{0,1,1,0,
                               0,1,0,0,
                               0,1,0,0,
                               0,0,0,0}),

        // shape of 10, next=7
        // OOO
        //   O
        new Shape(10, new int[]{0,0,0,0,
                                1,1,1,0,
                                0,0,1,0,
                                0,0,0,0}),

        // shape of 11, next=12
        //  O
        // OOO
        new Shape(11, new int[]{0,1,0,0,
                                1,1,1,0,
                                0,0,0,0,
                                0,0,0,0}),

        // shape of 12, next=13
        // O
        // OO
        // O
        new Shape(12, new int[]{0,1,0,0,
                                0,1,1,0,
                                0,1,0,0,
                                0,0,0,0}),

        // shape of 13, next=14
        // OOO
        //  O
        new Shape(13, new int[]{0,0,0,0,
                                1,1,1,0,
                                0,1,0,0,
                                0,0,0,0}),

        // shape of 14, next=11
        //  O
        // OO
        //  O
        new Shape(14, new int[]{0,1,0,0,
                                1,1,0,0,
                                0,1,0,0,
                                0,0,0,0}),

        // shape of 15, next=16
        //  OO
        // OO
        new Shape(15, new int[]{0,0,0,0,
                                0,1,1,0,
                                1,1,0,0,
                                0,0,0,0}),

        // shape of 16, next=15
        // O
        // OO
        //  O
        new Shape(16, new int[]{0,1,0,0,
                                0,1,1,0,
                                0,0,1,0,
                                0,0,0,0}),

        // shape of 17, next=18
        // OO
        //  OO
        new Shape(17, new int[]{0,0,0,0,
                                1,1,0,0,
                                0,1,1,0,
                                0,0,0,0}),

        // shape of 18, next=17
        //  O
        // OO
        // O
        new Shape(18, new int[]{0,0,1,0,
                                0,1,1,0,
                                0,1,0,0,
                                0,0,0,0})
    };

    private int index; // the unique id
    private int[] data;

    private Shape(final int index, final int[] data) {
        this.index = index;
        this.data =  data;
    }

    public int getIndex()  { return index;  }
    public int getWidth()  { return 4;  }
    public int getHeight() { return 4; }
    public int[] getData() { return data;   }

    /**
     * Get the next shape when user pressed "change" key.
     * 
     * @return The next shape that should be display.
     */
    public Shape next() { return SHAPES[NEXT[index]]; }

    static int getInitTop(int index) {
        return INIT_TOP[index];
    }

    public void paint(Graphics g, int box_size, int color) {
        for(int i=0; i<4; i++) { // col
            for(int j=0; j<4; j++) { // row
                if(data[4*j+i]==1) {
                    g.setColor(MyCanvas.BORDER_COLOR);
                    g.drawRect(i*box_size, j*box_size, box_size, box_size);
                    g.setColor(color);
                    g.fillRect(i*box_size+1, j*box_size+1, box_size-2, box_size-2);
                }
            }
        }
    }

    public void erase(Graphics g, int box_size) {
        for(int i=0; i<4; i++) { // col
            for(int j=0; j<4; j++) { // row
                if(data[4*j+i]==1) {
                    g.setColor(0);
                    g.drawRect(i*box_size, j*box_size, box_size, box_size);
                    g.fillRect(i*box_size, j*box_size, box_size, box_size);
                }
            }
        }
    }

    public static int indexOf(Shape shape) {
        for(int i=0; i<SHAPES.length; i++) {
            if(SHAPES[i]==shape) return i;
        }
        return 0;
    }
}

⌨️ 快捷键说明

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