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

📄 cgame.java

📁 学习手机j2me的必备程序
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import java.util.*;
import javax.microedition.rms.*;
import java.io.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class CGame extends Canvas implements Runnable, Constance{
    static int[][] map = new int[10][20];
    static int[][] shape;
    static int[][] nextShape;
    static int score;
    static int hightscore = 1000;
    static int shapeX;
    static int shapeY;
    static int nextShapeX = 0;
    static int nextShapeY = 0;
    static int pfX;
    static int pfY;
    static int edgeRight;
    static int edgeLeft;
    static int edgeBottom;
    static int keyPressed;
    static int keyHeld;
    static int speed;
    static int level = 1;
    static int shapeID;
    static int nextShapeID;
    static Random ran = new Random();

    public CGame() {
        try {
            pfX = (SCRW - PFW) / 2;
            pfY = (SCRH - PFH) / 2;
            speed = 8 - level;
            resetGame();
            initRS();

            (new Thread(this)).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    static void drawLevel(Graphics g){
    	g.setColor(0x00ff33);
    	g.drawString("Level " + level, 20, 50, Graphics.TOP|Graphics.HCENTER);
    }
    
    static void drawScore(Graphics g){
    	g.setColor(0xffffff);
    	g.drawString("Score " + score, SCRW / 2, 15, Graphics.TOP|Graphics.HCENTER);
    	g.drawString("High Score " + hightscore, SCRW / 2, 35, Graphics.TOP|Graphics.HCENTER);
    }

    protected void paint(Graphics g) {
        updateInput();
        updateGame();

        drawBG(g);
        drawMap(g);
        drawShape(g);
        drawNextShape(g);
        drawLevel(g);
        drawScore(g);

        keyPressed = 0;
    }

    static void updateInput() {
        if (keyPressed == KEY_CODE_SELECT || keyPressed == KEY_CODE_UP) {
            rotateShape();
        } else if (keyHeld == KEY_CODE_RIGHT) {
            if (checkRight(shape, shapeX + 1, edgeRight))
                shapeX++;
        } else if (keyHeld == KEY_CODE_LEFT) {
            if (checkLeft(shape, shapeX - 1, edgeLeft))
                shapeX--;
        } else if (keyPressed == KEY_CODE_NUM0) {
            saveGame();
        } else if (keyPressed == KEY_CODE_NUM9) {
            loadGame();
        }
    }

    static void updateGame() {
        if (hitGround(shape, edgeBottom)) {
            updateMap();
            shapeX = shapeY = 0;
            shape = nextShape;
            getEdge();
            nextShape = newShape();

            if (hitGround(shape, edgeBottom))
                resetGame();
        }
        if (speed-- < 0 || keyHeld == KEY_CODE_DOWN) {
            shapeY++;
            speed = 8 - level;
        }
    }

    static boolean hitGround(int[][] shape, int edgeBottom) {
        for (int i = 0; i < shape.length; i++) {
            for (int j = 0; j < shape[i].length; j++) {
                if (j + shapeX < 0 || j + shapeX >= map.length)
                    continue;
                if (shapeY + 3 - edgeBottom >= map[0].length - 1)
                    return true;
                if (i + shapeY + 1 > map[0].length - 1)
                    continue;
                if (shape[i][j] != 0 && map[j + shapeX][i + shapeY + 1] != 0)
                    return true;
            }
        }
        return false;
    }

    static void updateMap() {
        for (int i = 0; i < shape.length; i++) {
            for (int j = 0; j < shape[i].length; j++) {
                if (shape[i][j] != 0)
                    map[shapeX + j][shapeY + i] = shapeID + 1;
            }
        }

        checkMap();
    }

    static void checkMap() {
    	int delta = 0;
        outer:
                for (int i = map[0].length - 1; i >= 0; i--) {
            for (int j = 0; j < map.length; j++) {
                if (map[j][i] == 0) {
                    continue outer;
                }
            }
            for (int m = 0; m < map.length; m++) {
                for (int n = i; n > 0; n--) {
                    map[m][n] = map[m][n - 1];
                }
            }
            for (int k = 0; k < map.length; k++)
                map[k][0] = 0;
            i++;
            delta++;
        }
        
        score += delta * 100;
        if(score > hightscore)
        	hightscore = score;
        level = score / 500 + 1;
        if(level == 9)
        	level = 1;
    }

    static boolean checkRight(int[][] s, int x, int edgeright) {
        if (x + s[0].length - edgeright > map.length)
            return false;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (x + j >= map.length)
                    continue;
                if (s[i][j] != 0 &&
                    map[x + j][shapeY + i] != 0)
                    return false;
            }
        }
        return true;
    }

    static boolean checkLeft(int[][] s, int x, int edgeleft) {
        if (x + edgeleft < 0)
            return false;

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (x + j < 0)
                    continue;
                if (s[i][j] != 0 &&
                    map[x + j][shapeY + i] != 0)
                    return false;
            }
        }
        return true;
    }

    static void rotateShape() {
        int[][] res = new int[shape.length][shape[0].length];
        for (int i = 0; i < shape.length; i++) {
            for (int j = 0; j < shape[i].length; j++) {
                if ((i < 2 && j > 1) || (i > 1 && j > 1)) {
                    res[shape[0].length - 1 - j][i] = shape[i][j];
                } else {
                    res[shape.length - 1 - j][i] = shape[i][j];
                }
            }
        }

        int tmpRE = getRightEdge(res);
        int tmpLE = getLeftEdge(res);
        int tmpBE = getBottom(res);

        int backShapeX = shapeX;

        if (shapeX + shape[0].length - tmpRE > map.length)
            shapeX = map.length - shape[0].length + tmpRE;

        if (shapeX + tmpLE < 0)
            shapeX = -tmpLE;

        if (!hitGround(res, tmpBE) && checkRight(res, shapeX, tmpRE) &&
            checkLeft(res, shapeX, tmpLE)) {
            shape = res;
            getEdge();
        } else {
            shapeX = backShapeX;
        }
    }

    static void getEdge() {
        edgeRight = getRightEdge(shape);
        edgeLeft = getLeftEdge(shape);
        edgeBottom = getBottom(shape);
    }

    static int getRightEdge(int[][] shape) {
        int tmp = 0;
        for (int i = 0; i < shape.length; i++) {
            for (int j = 0; j < shape[i].length; j++) {
                if (shape[i][j] != 0 && j > tmp)
                    tmp = j;
            }
        }
        return shape[0].length - 1 - tmp;
    }

    static int getLeftEdge(int[][] s) {
        int tmp = s[0].length;
        for (int i = 0; i < s.length; i++) {
            for (int j = 0; j < s[i].length; j++) {
                if (s[i][j] != 0 && j < tmp)
                    tmp = j;
            }
        }
        return tmp;
    }

    static int getBottom(int[][] s) {
        int tmp = 0;
        for (int i = 0; i < s.length; i++) {
            for (int j = 0; j < s[i].length; j++) {
                if (s[i][j] != 0 && i > tmp)
                    tmp = i;
            }
        }
        return 3 - tmp;
    }

    static void drawBG(Graphics g) {
        g.setColor(0x444444);
        g.fillRect(0, 0, SCRW, SCRH);
        g.setColor(0);
        g.fillRect(pfX, pfY, PFW, PFH);
    }

    static void drawMap(Graphics g) {
        int color = 0;
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                if (map[i][j] != 0) {
                    color = getColor(map[i][j] - 1);
                    drawModule(
                            g, color, 0, pfX + i * CELL_LENGTH,
                            pfY + j * CELL_LENGTH,
                            CELL_LENGTH, CELL_LENGTH);
                }
            }
        }
    }

    static void drawShape(Graphics g) {
        int color = getColor(shapeID);
        for (int i = 0; i < shape.length; i++) {
            for (int j = 0; j < shape[i].length; j++) {
                if (shape[i][j] != 0) {
                    drawModule(g, color, 0, pfX + (shapeX + j) * CELL_LENGTH,
                               pfY + (shapeY + i) * CELL_LENGTH, CELL_LENGTH,
                               CELL_LENGTH);

                }
            }
        }
    }

    static void drawNextShape(Graphics g) {
        int color = getColor(nextShapeID);
        for (int i = 0; i < nextShape.length; i++) {
            for (int j = 0; j < nextShape[i].length; j++) {
                if (nextShape[i][j] != 0) {
                    drawModule(g, color, 0,
                               (nextShapeX + j) * CELL_LENGTH,
                               (nextShapeY + i) * CELL_LENGTH, CELL_LENGTH,
                               CELL_LENGTH);
                }
            }
        }
    }

    static void drawModule(Graphics g, int bgcolor, int frcolor, int x, int y,
                           int w, int h) {
        g.setColor(bgcolor);
        g.fillRect(x, y, w, h);
        g.setColor(FRAME_COLOR);
        g.drawRect(x, y, w, h);
    }

    static int getColor(int id) {
        switch (id) {
        case 0:
            return 0xff0000;
        case 1:
            return 0x00ff00;
        case 2:
            return 0x0000ff;
        case 3:
            return 0x00ffff;
        case 4:
            return 0xff770;
        case 5:
            return 0xff0dcf;
        case 6:
            return 0xffa23f;
        default:
            return 0xffffff;
        }
    }

    static void resetGame() {
        map = new int[10][20];
        shape = newShape();
        nextShape = newShape();
        getEdge();
        level = 1;
        score = 0;
    }

    static int[][] newShape() {
        shapeID = nextShapeID;
        nextShapeID = Math.abs(ran.nextInt() % SHAPES.length);
        return SHAPES[nextShapeID];
    }

    static void initRS(){
        try {
            RecordStore rs = RecordStore.openRecordStore(RECORD_GAME, true);
            if (rs.getNumRecords() == 0) {
                rs.addRecord(new byte[RECORD_MAX_SIZE_GAME], 0,
                             RECORD_MAX_SIZE_GAME);
                rs.closeRecordStore();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    static void saveGame(){
        try{
            RecordStore rs = RecordStore.openRecordStore(RECORD_GAME, true);
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream(
                    RECORD_MAX_SIZE_GAME);
            DataOutputStream out = new DataOutputStream(byteOut);
            out.writeInt(hightscore);
            out.writeInt(level);
            out.writeInt(score);

            byte[] data = byteOut.toByteArray();
            rs.setRecord(RECORD_ID_GAME, data, 0, data.length);
            rs.closeRecordStore();
            byteOut.close();
            out.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    static void loadGame(){
        try{
        RecordStore rs = RecordStore.openRecordStore(RECORD_GAME, true);
            byte[] record = rs.getRecord(RECORD_ID_GAME);
            DataInputStream in = new DataInputStream(new ByteArrayInputStream(record));
            hightscore = in.readInt();
            level = in.readInt();
            score = in.readInt();

            in.close();
            rs.closeRecordStore();
        }
        catch (Exception e) {
        }

    }

    public void run(){
        try{
            while(true){
                Thread.sleep(70);
                repaint();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void keyPressed(int code){
        keyPressed = code;
        keyHeld = code;
    }

    public void keyReleased(int code){
        keyHeld = 0;
    }
}

⌨️ 快捷键说明

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