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

📄 galaxywarcanvas.java

📁 经典的宇宙大战游戏代码(坦克大战)
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

import java.io.*;
import java.util.*;
import javax.microedition.media.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordStore;


public class GalaxyWarCanvas extends Canvas implements Runnable,
    CommandListener {

    private static final byte MODE_NONE = 0;
    private static final byte MODE_INIT = 1;
    private static final byte MODE_MENU = 2;
    private static final byte MODE_BATTLE = 3;
    private static final byte MODE_LOADING = 4;
    private static final byte MODE_SCORE = 5;
    private static final byte MODE_REPORT = 6;
    private static final byte MODE_PAUSE = 7;
    private static final byte MODE_WRITING_SCORE = 8;
    private static final byte MODE_OPTION = 9;
    private static final byte MODE_HELP = 10;
    private static final byte MODE_ABOUT = 11;
    private static final byte MODE_INTERNET_RANK = 12;
    private static final byte MODE_UPLOAD = 13;
    private static final byte MODE_GAMEOVER = 14;
    //must modify
    private static final byte KEY_NONE = 0;

    private static final byte D_UP = 0;//菜单间的间隔
    private static final byte D_DOWN = 1;//panel 与菜单之间的间隔
    private static final byte D_LEFT = 2;
    private static final byte D_RIGHT = 3;

    private static final byte SOLDIER = 1;
    private static final byte SPY = 2;
    private static final byte DEMON = 3;
    private static final byte BOSS = 4;

    private static final byte IMAGESIZE = 24;//地图元素、玩家图片的长宽
    private static final byte BOSSSIZE = 48;
    private static final byte DIGIT_LEFT = 15;
    private static final int DIGIT_RIGHT = 128;
    private static final byte SPACING = 10;//菜单间的间隔
    private static final byte SPACING2 = 15;//panel 与菜单之间的间隔
    private static final byte SCORELEN = 8;//8 CHAR
    private static final byte SCORE_HEIGHT = 20;
    private static final byte UP_SPACE = 20;
    private static final byte MEDAL_SIZE = 16;  

    private static final byte STEP_SLOW = IMAGESIZE >> 2;
    private static final byte STEP_FAST = STEP_SLOW << 1;
    private static final byte SHOT_RATE = STEP_SLOW + (STEP_SLOW >> 1);
    private static final byte SHOT_AREA = IMAGESIZE * 3;//48 pixel
    private static final byte SEARCH_AREA = IMAGESIZE << 1;
    private static final byte IMPACT = IMAGESIZE >> 2;


    private static final byte MENU_NUM = 8;
    private static final byte PAUSE_MENU_NUM = 4;
    private static final byte OPTION_NUM = 6;
    private static final byte HELP_NUM = 6;
    private static final byte ABOUT_NUM = 9;
    private static final byte CYCLE = 24;
    private static final byte MIDDLE = 12;
    private static final byte CHARWAIT = 12;//FRAME

    private static final byte SMOKE = 5;
    private static final byte CORNER = IMAGESIZE >> 3;
    private static final byte SLEET_TIME = 10;

    private byte imageSize = IMAGESIZE;
    private byte charWaitCounter = 0;
    private boolean isStartWait = false;
    private Thread thread;
    private byte gameState = -1;
    private byte mode = MODE_INIT;
    private byte lastMode;
    private byte lastItem;
    private int key;
    private int gameAction,reGameAction;
    private int frameWait = 40;
    private Random random;
    private byte loadedCounter = -1;
    private Image img, panel,panel_up,mapImg,tank,boss,shot[] = new Image[4]
        ,smoke[] = new Image[SMOKE],cheese,digit,hint,ranking;
    private byte totalLoad;
    private int width = getWidth();
    private int height = getHeight();
    private int viewWidth,viewHeight;//用于地图显示的长宽(像数)
    private int bossWidth,bossHeight;
    private int mapWidth,mapHeight,mapPWidth,mapPHeight;//地图格数、总像数大小
    private int mapOffsetX,mapOffsetY;//地图中心格偏移量
    private int offsetX,offsetY;//屏幕中心点偏移量
    private int osX,osY;//实际像数偏移量
    private int viewMapX,viewMapY;//可显示的Map格数
    private int startMapGx,startMapGy,endMapGx,endMapGy;
    private int startMapPx,startMapPy,endMapPx,endMapPy;
    //private byte viewX,viewY;//中心格
    //private int mX,mY;//中心点
    private Font font;
    private byte currentMenu = 1;
    private byte startMenu = 1;
    private byte map[][];
    private byte menu[];
    private Tank player[];
    private Item item[];
    private int showedNum;
    private boolean isRepaint = true;
    private byte round = 0;
    private byte maxPlayer;
    private byte maxItem;
    private int lastScore = 0;
    private byte step = 0;//需要有调整功能
    private RecordStore rs;
    public static String scoreInfo = "",rankInfo = "";
    public static boolean isReaded = false;
    private String typedInfo = "";
    private int thisChar = 0;
    private byte frameCounter = 0;
    private byte currentScore = 0,currentRank = 0;
    public static int rankNum = 0;
    private byte startScore = 0;
    public static String uploadScore = "0";
    public static String uploadLevel = "1";
    private Command cmdSelect = null,cmdCancel = null;
    private Player ply;
    private InputStream inputStream;

    private boolean isInit = false;
    private boolean isAdded = false;//flag wheather the command have added.
    private boolean isSoundOn = false;
    private boolean isVibration = true;
    private boolean isAutoUpload = false;
    private boolean isFromOption = false;
    public boolean isUploaded = false;
    public static String userID = "",mobile = "";
    public static final String ERRORINFO = "Please make sure your,"
    + "JAVA setting is correct,"
    + "before you upload score,"
    + "or get top ten ranking.,"
    + "Please browse,"
    + "java.mymobilesoft.com,"
    + "or call Motorola hotline,"
    +" 25063888 if you need,"
    + "any help.,";
    public static final String ERRORINFO2 = "Please make sure you"
    + "JAVA setting is correct\n"
    + "before you upload score\n"
    + "or get top ten ranking.\n"
    + "Please browse\n"
    + "java.mymobilesoft.com\n"
    + "or call Motorola hotline\n"
    +" 25063888 if you need\n"
    + "any help.\n";
    //use for game post
    SetForm setForm;
    TextField gameidField, useridField, mobileField, scoreField;
    StringBuffer b = new StringBuffer();

    public static Display display;
    public static final Command exitCommand = new Command("Back To Game"
        , Command.EXIT, 1);
    public static final Command setCommand = new Command("OK", Command.OK, 1);
    public static final Command backCommand = new Command("Back", Command.BACK, 1);


    public GalaxyWarCanvas(Display d) {
        display = d;
        random = new java.util.Random();
        font = Font.getFont(Font.FACE_SYSTEM
            , Font.STYLE_PLAIN, Font.SIZE_MEDIUM);

        byte[] rec = new byte[1];
        int len;

        try {
            //rs.deleteRecordStore("galaxywar");
            rs = RecordStore.openRecordStore("galaxywar", true);
            len = rs.getNumRecords();
            if (len != 4) {
                if (len > 0)
                    for (int i = 1; i <= len; i++) {
                        rs.deleteRecord(i);
                    }
                rec[0] = 0; //round = 0;
                rs.addRecord(rec, 0, rec.length);
                rec[0] = 0;
                rs.addRecord(rec, 0, rec.length);
                rec[0] = 0;
                rs.addRecord(rec, 0, rec.length);
                rec[0] = 2;
                rs.addRecord(rec, 0, rec.length);
            }else {
                readSetting();
                scoreInfo = readScore();
            }
            rs.closeRecordStore();
        }catch(Exception e) {
            System.out.println("init");
        }
    }

    public void run() {
        long timeStart, timeTaken;
        while (true) {
            timeStart = System.currentTimeMillis();
            isRepaint = true;
            switch (mode) {
            case MODE_INIT:
                flowInit();
                break;
            case MODE_MENU:
                flowMenu();
                break;
            case MODE_PAUSE:
                flowPause();
                break;
            case MODE_SCORE:
                flowScore();
                break;
            case MODE_INTERNET_RANK:
                flowInternetRank();
                break;
            case MODE_BATTLE:
                flowBattle();
                break;
            case MODE_LOADING:
                flowLoading();
                break;
            case MODE_WRITING_SCORE:
                flowWritingScore();
                break;
            case MODE_UPLOAD:
                flowInternetRank();
                break;
            case MODE_REPORT:
                flowReport();
                break;
            case MODE_OPTION:
                flowOption();
                break;
            case MODE_HELP:
                flowHelp();
                break;
            case MODE_ABOUT:
                flowAbout();
                break;
            case MODE_GAMEOVER:
                flowGameOver();
                break;
            }
            key = KEY_NONE;
            gameAction = KEY_NONE;
            reGameAction = KEY_NONE;
            if (isRepaint) {
                repaint();
                serviceRepaints();
            }
            addCommand();
            updateCounter();
            System.gc();
            Thread.yield();
            //Frame rate control
            timeTaken = System.currentTimeMillis() - timeStart;
            if (timeTaken >= frameWait)
                continue;
            try {
                Thread.sleep(frameWait - timeTaken);
            } catch (Exception ex) {
                System.out.println("sleep: " + ex);
            }
        }
    }

    public void start() {
        thread = new Thread(this);
        thread.start();
    }

    public void stop() {
        thread = null;
    }

    protected void keyPressed(int keyCode) {
        key = keyCode;
        gameAction = getGameAction(keyCode);
        //if (key != 0)
        //System.out.println("Press key: " + key);
    }

    protected void keyReleased(int keyCode) {
        reGameAction = getGameAction(keyCode);
        //System.out.println("Release key: " + rekey);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdSelect) {
            switch(mode) {
            case MODE_MENU:
                processMenu();
                break;
            case MODE_BATTLE:
                showPauseMenu();
                isRepaint = false;
                break;
            case MODE_PAUSE:
                processPause();
                break;
            case MODE_OPTION:
                processOption();
                break;
            case MODE_REPORT:
                switch(gameState) {
                case 0:
                    restartGame();
                    break;
                case 1:
                    newGame();
                    break;
                case 2:
                    nextGame();
                    break;
                }
                break;
            case MODE_WRITING_SCORE:
                scoreInfoUpdate(player[0].score + " : " + typedInfo);
                updateScore();
                typedInfo = "";
                uploadScore = String.valueOf(player[0].score);
                uploadLevel = String.valueOf(round + 1);
                if (isAutoUpload) {
                    isFromOption = false;
                    setAutoUpload();
                }else {
                    initCommand();
                    mode = MODE_REPORT;
                    isRepaint = false;
                }
                break;
            }
        }
        if (c == cmdCancel) {
            switch(mode) {
            case MODE_PAUSE:
                mode = MODE_BATTLE;
                initCommand();
                isRepaint = false;
                break;
            case MODE_SCORE:
            case MODE_INTERNET_RANK:
                showMainMenu();
                break;
            case MODE_UPLOAD:
                isReaded = false;
                isUploaded = false;
                rankInfo = "";
                initCommand();
                mode = MODE_REPORT;
                isRepaint = false;
                break;
            case MODE_OPTION:
            case MODE_HELP:
            case MODE_ABOUT:
                optionBack();
                break;
            case MODE_REPORT:
                showMainMenu();
                break;
            case MODE_WRITING_SCORE:
                if (typedInfo.length() != 0) {
                    typedInfo = typedInfo.substring(0,typedInfo.length() - 1);
                    thisChar = 0;
                }

⌨️ 快捷键说明

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