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

📄 gamecanvas.java

📁 为解压缩文件
💻 JAVA
字号:
package pangxie;

import java.util.*;
import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import com.nokia.mid.ui.FullCanvas;

public class GameCanvas extends FullCanvas {
    Image bgImage, startMenu;
    Image logo[] = new Image[2], pointer[] = new Image[2];
    GameLogic logic;
    Vector pangs = new Vector();
    Flower[] follows = new Flower[3];
    Man man;
    RecordStore rs;
    Vector playerList = new Vector();
    Timer displayer, pangxiePointer;
    static int w, h, _Y;
    int status = 1; //状态:1,主菜单;2,游戏;3,Help;-1,game over;
    byte DisplayStatus, imageChanging;
    int colorID = 0;
    int anchor = Graphics.LEFT | Graphics.TOP;
    int color[] = {0xFF0000FF, 0x050000FF};


    public GameCanvas() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        w = this.getWidth();
        h = this.getHeight();

        bgImage = Image.createImage("/res/b.png");
        startMenu = Image.createImage("/res/j001.png");
        pointer[0] = Image.createImage("/res/p11.png");
        pointer[1] = Image.createImage("/res/p10.png");
        logic = new GameLogic(this);

        displayer = new Timer();
        pangxiePointer = new Timer();
        pangxiePointer.schedule(new ChangeImage(), 10, 200);

    }

    protected void paint(Graphics g) {
        switch (status) {



        case 1: //主菜单
            displayer.cancel();
            g.drawImage(startMenu, 0, 0, Graphics.LEFT | Graphics.TOP);
            g.drawImage(PangXie.images[1][imageChanging], 20, 86 + _Y,
                        Graphics.LEFT | Graphics.TOP);
            break;

        case 2: //游戏
            g.drawImage(bgImage, 0, 0, Graphics.LEFT | Graphics.TOP);
            g.setColor(0xffffff);
            g.drawString("Score:" + PangXie.total, 10, 5,
                         Graphics.LEFT | Graphics.TOP);
            g.drawString("Stage: "+GameLogic.level,80,5,Graphics.LEFT|Graphics.TOP);
            if (GameLogic.baoNum > 0) {
                for (int i = 0; i < GameLogic.baoNum; i++) {
                    g.drawImage(pointer[0], 20, 26,
                                Graphics.LEFT | Graphics.TOP);
                g.drawString(" "+GameLogic.baoNum,32,26,Graphics.LEFT|Graphics.TOP);}
            }
            man.paint(g);
            for (int i = 0; i < follows.length; i++) {
                if (follows[i] != null) {
                    follows[i].paint(g);
                }
            }

            for (int i = 0; i < pangs.size(); i++) {
                PangXie px = (PangXie) pangs.elementAt(i);
                px.paint(g);
            }

            break;

        case 3: //help1
            g.setColor(0xffffff);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            g.setColor(0x000000);
            g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
                                   Font.SIZE_LARGE));
            g.drawString("Introduction", 2, 2, anchor);
            g.setFont(Font.getDefaultFont());
            g.drawString("    This is an action game without", 2, 18, anchor);
            g.drawString("level design.In this Summer, a boy", 2, 29, anchor);
            g.drawString("likes enjoying the cool on his own", 2, 40, anchor);
            g.drawString("flat's gazebo.But a great many of ", 2, 51, anchor);
            g.drawString("crabs climb up the flat every nig-", 2, 62, anchor);
            g.drawString("ht. So the boy was boring for alo-", 2, 73, anchor);
            g.drawString("ng time. Tonight,he will fight for", 2, 84, anchor);
            g.drawString("them in his way...", 2, 95, anchor);
            g.setColor(0x0000ff);
            g.drawString("    The player can control a flow-", 2, 108, anchor);
            g.drawString("erpot on the position,then you can", 2, 119, anchor);
            g.drawString("drop off the flowerpot to break d-", 2, 130, anchor);
            g.drawString("own a crab one time. Control:dire-", 2, 141, anchor);
            g.drawString("ctoin keys and select key.just so ", 2, 152, anchor);
            g.drawString("that,enjoy yourself!", 2, 162, anchor);
            break;

        case 4: //help2
            g.setColor(0xffffff);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            g.setColor(0x000000);
            g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
                                   Font.SIZE_LARGE));
            g.drawString("创意", 2, 1, anchor);
            g.drawString("Xuelai", 4, 20, anchor);
            g.drawString("imtrash", 4, 38, anchor);
            g.drawString("程序", 2, 56, anchor);
            g.drawString("imtrash", 4, 76, anchor);
            g.drawString("美工", 4, 96, anchor);
            g.drawString("Xuelai", 2, 114, anchor);
            break;
        case 5: //排行榜
            g.drawImage(bgImage, 0, 0, Graphics.TOP | Graphics.LEFT);
            g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
                                   Font.SIZE_LARGE));
            int length_Y = 10;
            g.setColor(255, 0, 0);
            for (int i = 0; i < playerList.size(); i++) {
                String s = (String) playerList.elementAt(i);

                g.drawString(s, w / 2, +i * 15,
                             Graphics.TOP | Graphics.HCENTER);
            }
            break;

        case 10: //pause
            pauseStatus(g);
            break;

        case -1: //game over
            g.drawImage(bgImage, 0, 0, Graphics.LEFT | Graphics.TOP);
            g.setColor(0xffffff);
            g.drawString("Score:" + PangXie.total, 10, 5,
                         Graphics.LEFT | Graphics.TOP);
            g.setColor(255, 0, 0);
            g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
                                   Font.SIZE_LARGE));
            g.drawString("Game Over", w / 2, 40,
                         Graphics.HCENTER | Graphics.TOP);

            break;
        default:
            break;
        }
    }

    public void closeDb() {
        try {
            rs.closeRecordStore();
        } catch (Exception ex) {
        }
    }

    public void openDb(String rsname) {
        try {
            rs = RecordStore.openRecordStore(rsname, true);
        } catch (Exception e) {
            return;
        }
    }

    public void getPlayerList() {

        try {
            for (int i = 1; i <= rs.getNumRecords(); i++) {
                byte[] data = rs.getRecord(i);
                DataInputStream doSteam = new DataInputStream(new
                        ByteArrayInputStream(
                                data));
                String name = doSteam.readUTF();
                int record = doSteam.readInt();

                playerList.addElement(name + ":" + record);
                doSteam.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addPlayer(String name, int sc) {

        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        DataOutputStream doSteam = new DataOutputStream(bo);
        try {
            doSteam.writeUTF(name);
            doSteam.writeInt(sc);
            byte data[] = bo.toByteArray();
            rs.addRecord(data, 0, data.length);
            doSteam.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (playerList.size() < 5) {
            playerList.addElement(name + ":" + PangXie.total);
        } else {
            playerList.setElementAt(name + ":" + PangXie.total, 4);
        }

    }

    protected void keyPressed(int keyCode) {
        int ga = getGameAction(keyCode);
        logic.handleKeyEvent(ga);
        repaint();
    }




    //任务:主界面活动螃蟹
    class ChangeImage extends TimerTask {
        public void run() {
            if (imageChanging == 0) {
                imageChanging++;
            } else {
                imageChanging--;
            }
            repaint();
        }
    }

    //暂停时画面控制函数
    public void pauseStatus(Graphics gg) {
        status = 10;
        gg.setColor(0x000000);
        gg.drawString("Resume", w / 2, 40,
                      Graphics.HCENTER | Graphics.TOP);
        gg.drawString("Exit", w / 2, 55,
                      Graphics.HCENTER | Graphics.TOP);
        if (logic.i == 1) {
            gg.setColor(0xffffff);
            gg.drawString("Resume", w / 2, 40,
                          Graphics.HCENTER | Graphics.TOP);
        }
        if (logic.i == 2) {
            gg.setColor(0xffffff);
            gg.drawString("Exit", w / 2, 55,
                          Graphics.HCENTER | Graphics.TOP);
        }
    }

}

⌨️ 快捷键说明

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