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

📄 foxcanvas.java

📁 一款java游戏,可以学习参考,类似"是男人就下一百层".
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.util.*;
import com.nttdocomo.io.*;
import com.nttdocomo.ui.*;
import java.io.*;
import javax.microedition.io.*;

public class FoxCanvas
    extends Canvas
    implements Runnable {
  final static int KEY_DOWN = 19;
  final static int KEY_UP = 17;
  final static int KEY_LEFT = 16;
  final static int KEY_RIGHT = 18;
  final static int KEY_MID = 20;
  final static int KEY_LS = 22;
  final static int KEY_RS = 21; //(由于foma要求右确定,左返回,此处键值故意设反)
  public static final int ST_LOGO = 0; //logo状态
  public static final int ST_SPLASH = 1; //开机画面
  public static final int ST_MAINMENU = 2; //主菜单状态
  public static final int ST_RUN = 3; //运行状态
  public static final int ST_GAMEMENU = 4; //游戏中菜单状态
  public static final int ST_HELP = 5; //帮助状态
  public static final int ST_SETTING = 6; //设置状态
  public static final int ST_ABOUT = 7; //奖励信息
  public static final int ST_GAMELEVEL = 8; //游戏难度
  public static final int ST_SCORELEVEL = 9; //排行级别
  public static final int GAME_PLAY = 10; //玩游戏
  public static final int GAME_OVER_DIE = 11; //游戏以狐狸死亡结束
  public static final int GAME_OVER_RESULT = 12; //游戏结束
  public static final int GAME_INPUTNAME = 13; //玩家输入
  public static final int GAME_PAUSE = 15; //暂停游戏
  public static final int ST_SCORE = 14; //查看各级别记录
  public static final int ST_GAMEINFO = 16;
  public static final int ST_KEYINFO = 17;
  private static int preState = -1;
  public int gameState = 0; //全局游戏状态
  private boolean bPlay = false;
  private IApplication midlet;
  FoxRole role;

//图片
  public Image sky = null;
  public Image cloudsBig = null;
  public Image cloudsSmall = null;
  public Image topBoard = null;
  public Image sawtooth = null;
  public Image frame = null;
  public Image framef = null;
  public Image life = null;
  public Image number = null;
  public Image boardSingle = null;
  public Image grassboardSingle = null;
  public Image grassboard = null;
  public Image springboard = null;
  public Image springboardsingle = null;
  public Image nailboard = null;
  public Image scrollboard = null;
  public Image scrollboardf = null;
  public Image ImageSawtooth = null;
  public Image ImageFrame = null;
  public Image ImageFramef = null;
  public Image imgGameOver = null;
  private Image imgMenuFlag = null;
  private Image imgBackFrame = null;
  private Image imgBackSky = null;
  static int boardSpeed = 2; //横板上升速度

  public static final String[] menuMain = { //游戏主菜单列表
      "NEW GAME",
      "OPTION",
      "HELP",
      "HISCORE", //排行榜
      "EXIT"
  };
  public static final String[] menuGameMenu = { //游戏运行过程中菜单
      "CONTINUE",
      "OPTION",
      "HELP",
      "MAIN MENU",
      "EXIT"
  };
  public static final String[] menuSetting = { //设置选项列表
      "\uFF7B\uFF73\uFF9D\uFF84\uFF9E ON",
      "\uFF7B\uFF73\uFF9D\uFF84\uFF9E OFF"
  };
  public static final String[] gameLevel = { //游戏难度,排行榜选项
      "娙扨",
      "晛捠",
      "擄偟偄"
  };

  public final String[] strHelp = {
      "\uFF79\uFF9E\uFF70\uFF91徯夘",
      "憖嶌曽朄"

  };
  public final String[] gameInfo = {
      "\uFF79\uFF9E\uFF70\uFF91徯夘",
      "\uFF74\uFF99\uFF93傪憖嶌偟偰\uFF64埨慡",
      "側\uFF7D\uFF83\uFF6F\uFF8C\uFF9F傪慖戰偟側偑",
      "傜師乆偵壓偵崀傝偰",
      "偄偒傑偟傚偆\uFF61壓偵峴",
      "偗偽峴偔傎偳\uFF8E\uFF9F\uFF72\uFF9D\uFF84偑",
      "壛嶼偝傟偰偄偒傑偡丅",
      "揃偺\uFF7D\uFF83\uFF6F\uFF8C\uFF9F\uFF64偡偖偵夡",
      "傟偰偟傑偆憪偺\uFF7D\uFF83\uFF6F\uFF8C\uFF9F\uFF64",
      "嵍塃偵摦偔\uFF8D\uFF9E\uFF99\uFF84\uFF7A\uFF9D\uFF8D\uFF9E\uFF71丄",
      "\uFF78\uFF99\uFF78\uFF99夞傞\uFF7D\uFF83\uFF6F\uFF8C\uFF9F側偳傪",

      "旔偗丄偟偐偟帪偵偼棙",
      "梡偟偰恑傫偱偄偒傑偟",
      "傚偆\uFF61偁側偨偺敾抐椡",
      "偲恊巜偺尷奅偵挧愴偱",
      "偡!                "

  };
  public final String[] keyInfo = {
      "憖嶌曽朄",
      "嵍\uFF77\uFF70/4\uFF77\uFF70丗嵍偵堏摦",
      "塃\uFF77\uFF70/6\uFF77\uFF70丗塃偵堏摦",
      "嵍\uFF7F\uFF8C\uFF84\uFF77\uFF70丗拞抐/栠傞",
      "塃\uFF7F\uFF8C\uFF84\uFF77\uFF70丗\uFF92\uFF86\uFF6D\uFF70/慖戰",
      "妋掕\uFF77\uFF70丗妋掕      "
  };

  Image imgLogo; //logo图片
  Image imgSplash; //开机画面图片
  private int mainMenuIndex = 0; //主菜单选中项,用于准确返回主菜单
  private int gameMenuIndex = 0; //二级菜单选中项,用于准确返回二级菜单
  private int orderLevelIndex = 0; //排行榜选中项,用于准确返回
  private int soundIndex = 0;
  private int gameLevelIndex = 0;
  private int helpIndex = 0;
  boolean bSound = true; //是否有声音标志
  boolean bRepaint = true; //是否重画标志
  boolean bLogo = true; //logo状态标志
  boolean bSplash = true; //开机画面状态标志
  private long logoStart = 0;
  private long splashStart = 0;
  public static final int WIDTH = 240, HEIGHT = 240;
  private int iButton = 0;
  static Font defFont = Font.getDefaultFont();
  public FoxCanvas() {
    try {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    midlet = IApplication.getCurrentApp();
    setSoftLabel(SOFT_KEY_1, "");
    setSoftLabel(SOFT_KEY_2, "");
    InitDB();
    thread = new Thread(this);
    thread.start();
  }

  //创建图片的函数
  public static Image createImage(String path) {
    Image s = null;
    MediaImage m1 = null;
    m1 = MediaManager.getImage("resource:///res/" + path + ".gif");
    try {
      m1.use();
    }
    catch (ConnectionException ce) {
      ce.printStackTrace();
    }
    catch (UIException uie) {
      uie.printStackTrace();
    }
    s = m1.getImage();
    m1 = null;
    return s;
  }

  //初始化时加载图片
  private void jbInit() throws Exception {
    imgBackSky = createImage("backSky");
    imgBackFrame = createImage("backFrame");
    sky = createImage("sky");
    topBoard = createImage("topBoard");
    cloudsBig = createImage("cloudsBig");
    cloudsSmall = createImage("cloudsSmall");
    sawtooth = createImage("sawtooth");
    frame = createImage("frame");
    framef = createImage("framef");
    life = createImage("life");
    number = createImage("number");
    boardSingle = createImage("boardSingle");
    grassboardSingle = createImage("grassboardSingle");
    grassboard = createImage("grassboard");
    nailboard = createImage("nailboard");
    springboardsingle = createImage("springboardsingle");
    springboard = createImage("springboard");
    scrollboard = createImage("scrollboard");
    scrollboardf = createImage("scrollboardf");
    imgMenuFlag = createImage("menu");
    imgBG = Image.createImage(WIDTH, HEIGHT); //创建背景缓冲区
    Graphics gTemp = imgBG.getGraphics(); //通过缓冲区创建一个图形对象
    ImageFrame = Image.createImage(5, HEIGHT); //创建边框缓冲区
    Graphics tempImageFrame = ImageFrame.getGraphics(); //通过缓冲区创建一个图形对象
    ImageFramef = Image.createImage(5, HEIGHT); //创建边框缓冲区
    Graphics tempImageFramef = ImageFramef.getGraphics(); //通过缓冲区创建一个图形对象
    for (int i = 0; i < WIDTH; i++) {
      gTemp.drawImage(sky, i, 0); //通过缓冲区创建的图形对象在缓冲区里画图
    }
    for (int i = 0; i <= HEIGHT / 11; i++) {
      tempImageFrame.drawImage(frame, 0, 11 * i);
      tempImageFramef.drawImage(framef, 0, 11 * i);
    }
  }

  /**
   * @param g Graphics
   */
  private void DrawMainMenu(Graphics g) {
    if (iButton == 0) {
      iButton = 1;
      setSoftLabel(SOFT_KEY_2, "妋掕");
      setSoftLabel(SOFT_KEY_1, "");
    }
    g.drawImage(imgBackFrame, 0, 0);
    DrawStringSelect(g, menuMain, 90, 64, 30);
    g.drawImage(imgMenuFlag, 70, 52 + mainMenuIndex * 30);
  }

  private void DrawStringSelect(Graphics g, String[] str, int x, int y,
                                int ySize) {
    int len = str.length, posY = y;
    g.setColor(g.getColorOfRGB(255, 255, 0));
    for (int i = 0; i < len; i++) {
      g.drawString(str[i], x, posY);
      posY += ySize;
    }
  }

  /**
   * @param g Graphics
   */
  private void DrawSet(Graphics g) {
    if (iButton == 0) {
      iButton = 1;
      setSoftLabel(SOFT_KEY_2, "妋掕");
      setSoftLabel(SOFT_KEY_1, "栠傞");
    }
    g.drawImage(imgBackFrame, 0, 0);
    g.setColor(g.getColorOfRGB(255, 255, 0));
    g.drawString("OPTION", (WIDTH - defFont.stringWidth("OPTION")) / 2, 85);
    DrawStringSelect(g, menuSetting, 97, 119, 40);
    g.drawImage(imgMenuFlag, 77, 107 + soundIndex * 40);
  }

  /**
   * @param g Graphics
   */
  private int GameInfoPage = 0;
  private void DrawGameInfo(Graphics g) {
    if (iButton == 0) {
      iButton = 1;
      setSoftLabel(SOFT_KEY_2, "");
      setSoftLabel(SOFT_KEY_1, "栠傞");
    }
    g.drawImage(imgBackSky, 0, 0);
    g.setColor(g.getColorOfRGB(0, 0, 255));
    int y = 20;
    if (GameInfoPage == 0) {
      for (int i = 0; i < 11; i++) {
//        defFont.stringWidth("Record Down " + numberList[start]))
        g.drawString(gameInfo[i],
                     (WIDTH - defFont.stringWidth(gameInfo[i])) / 2, y);
//        g.drawString(gameInfo[i], 50, y);
        y += 20;
      }
    }
    else {
      for (int i = 11; i < gameInfo.length; i++) {
        g.drawString(gameInfo[i],
                     (WIDTH - defFont.stringWidth(gameInfo[i])) / 2, y);
//        g.drawString(gameInfo[i], 70, y);
        y += 20;
      }
    }
  }

  /**
   * @param g Graphics
   */
  private void DrawKeyInfo(Graphics g) {
    if (iButton == 0) {
      iButton = 1;
      setSoftLabel(SOFT_KEY_2, "");
      setSoftLabel(SOFT_KEY_1, "栠傞");
    }
    g.drawImage(imgBackSky, 0, 0);
    g.setColor(g.getColorOfRGB(0, 0, 255));
    int y = 30;
    for (int i = 0; i < keyInfo.length; i++) {
//      g.drawString(keyInfo[i], 70, y);
      g.drawString(keyInfo[i],
                   (WIDTH - defFont.stringWidth(keyInfo[i])) / 2, y);
      y += 30;
    }
  }

  /**
   * @param g Graphics
   */
  private void DrawGameMenu(Graphics g) {
    if (iButton == 0) {
      iButton = 1;
      setSoftLabel(SOFT_KEY_2, "妋掕");
      setSoftLabel(SOFT_KEY_1, "");
    }
    g.drawImage(imgBackFrame, 0, 0);
    DrawStringSelect(g, menuGameMenu, 90, 64, 30);
    g.drawImage(imgMenuFlag, 70, 52 + gameMenuIndex * 30);
  }

  /**
   * @param g Graphics
   */
  private void DrawHelp(Graphics g) {
    if (iButton == 0) {
      iButton = 1;
      setSoftLabel(SOFT_KEY_2, "妋掕");
      setSoftLabel(SOFT_KEY_1, "栠傞");
    }

    g.drawImage(imgBackFrame, 0, 0);
    g.setColor(g.getColorOfRGB(255, 255, 0));
    g.drawString("HELP", (WIDTH - defFont.stringWidth("HELP")) / 2, 85);
    DrawStringSelect(g, strHelp, 97, 117, 40);
    g.drawImage(imgMenuFlag, 77, 105 + helpIndex * 40);
  }

  Thread thread;
  public void run() {
    while (true) {
      long start = System.currentTimeMillis();
      repaint();
//      System.gc();
      long timeTaken = System.currentTimeMillis() - start;
      if (timeTaken < 100) {
        try {
          thread.sleep(100 - timeTaken);
        }
        catch (Exception e1) {}
      }
    }
  }

  /**
   * @param g Graphics
   */
  private void DrawGameSelect(Graphics g) {
    if (iButton == 0) {
      iButton = 1;
      setSoftLabel(SOFT_KEY_2, "妋掕");
      setSoftLabel(SOFT_KEY_1, "栠傞");
    }
    g.drawImage(imgBackFrame, 0, 0);
    DrawStringSelect(g, gameLevel, 97, 84, 40);
    g.drawImage(imgMenuFlag, 77, 72 + levelIndex * 40);
  }

  private void DrawScoreSelect(Graphics g) {
    if (iButton == 0) {
      iButton = 1;
      setSoftLabel(SOFT_KEY_2, "妋掕");
      setSoftLabel(SOFT_KEY_1, "栠傞");
    }
    g.drawImage(imgBackFrame, 0, 0);
    g.setColor(g.getColorOfRGB(255, 255, 0));
//    g.setColor(0xFFFF00);
    g.drawString("HISCORE", 92, 70);
    DrawStringSelect(g, gameLevel, 97, 94, 40);
    g.drawImage(imgMenuFlag, 77, 82 + levelListIndex * 40);
  }

  private boolean bBlreadyPause = false;
  void hideNotify() {
    if (gameState == GAME_PLAY) {
      if (bPause) {
        bBlreadyPause = true;
      }
      else {
        bPause = true;
        bBlreadyPause = false;
      }
    }
  }

  private boolean bPause = false;
  void showNotify() {
    if (gameState == GAME_PLAY) {
      if (bBlreadyPause) {
        bBlreadyPause = false;
        bPause = true;
      }
      else {
        bPause = false;
      }
    }
    if (gameState == GAME_PLAY) {
      role.keyTurn = false;
    }
    bLogo = true;
    bSplash = true;
    bGameOver = true;
    iButton = 0;
  }

  public void paint(Graphics g) {
    g.lock();
    switch (gameState) {
      case ST_LOGO: //logo
        g.setColor(g.getColorOfRGB(255, 255, 255));
//        g.setColor(0xffffff);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        if (imgLogo == null) {
          imgLogo = createImage("bbmf");
        }
        g.drawImage(imgLogo, 79, 89);
        if (bLogo) {
          bLogo = false;
          logoStart = System.currentTimeMillis();
        }
        if (System.currentTimeMillis() - logoStart > 2000) {
          gameState = ST_SPLASH;
          bLogo = true;
          imgLogo.dispose();
          System.gc();
        }
        break;
      case ST_SPLASH: //开机画面
        g.setColor(g.getColorOfRGB(255, 255, 255));
//        g.setColor(0xffffff);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        if (imgSplash == null) {
          imgSplash = createImage("splash");
        }
        g.drawImage(imgSplash, 0, 0);
        if (bSplash) {
          bSplash = false;
          splashStart = System.currentTimeMillis();
        }
        if (System.currentTimeMillis() - splashStart > 2000) {
          gameState = ST_MAINMENU;
          bSplash = true;
          bPlay = false;

⌨️ 快捷键说明

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