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

📄 stagecanvas.java

📁 小游戏,MIDP2.0,非常不错的!请试用下!
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
import com.nokia.mid.ui.*;
import java.util.Random;
import javax.microedition.lcdui.*;
import com.nokia.mid.sound.Sound;
import com.nokia.mid.ui.DeviceControl; //.DirectGraphics;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

import java.io.*;

//import javax.microedition.media.Manager;
//import javax.microedition.media.Player;
//import java.io.InputStream;
//import java.util.Hashtable;

public class StageCanvas
    extends FullCanvas {

  public int fps1;
  public int fps2;
  //定义锚点常量

  static final int ANCHOR0_LT = Graphics.LEFT | Graphics.TOP;
  static final Font FONT_SMALL = Font.getFont(Font.FACE_SYSTEM,
                                              Font.STYLE_PLAIN, Font.SIZE_SMALL);

  //  static final int ANCHOR0_CC = Graphics.HCENTER | Graphics.VCENTER;
  // static final int ANCHOR0_RT = Graphics.RIGHT | Graphics.TOP;
  //定义字体常量
  // static final Font FONT_LARGE = Font.getFont(Font.FACE_SYSTEM,
  //                                         Font.STYLE_PLAIN, Font.SIZE_LARGE);
  // static final Font FONT_MEDIUM = Font.getFont(Font.FACE_SYSTEM,
  //                                           Font.STYLE_PLAIN,
  //                                         Font.SIZE_MEDIUM);
  //   static final Font FONT_MEDIUMBOLD = Font.getFont(Font.FACE_SYSTEM,
  //    Font.STYLE_BOLD,
  //   Font.SIZE_MEDIUM);

  //定义色彩常量
  static final int COLOR_RED = 0xFF0000;
  static final int COLOR_GREEN = 0x00FF00;
  static final int COLOR_BLUE = 0x0000FF;
  static final int COLOR_ORG = 0xFF7E00;
  static final int COLOR_YELLOW_RED = 0xFFD200;
  static final int COLOR_BLACK = 0x000000;
  static final int COLOR_WHITE = 0xFFFFFF;

  public int stage_Width; //屏幕宽度:128
  public int stage_Height; //屏幕高度:128
  public int stage_CenterX; //屏幕中心X坐标
  public int stage_CenterY; //屏幕中心y坐标

  private String STR_GRADE[] = {
      "简  单", "普  通", "困  难"}; //游戏的彩单难度
  private String STR_MENU[] = {
      "新游戏", "帮助", "查看得分", "设置", "按键说明", "退出游戏"}; //菜单
  final static String[] imgFileName = {
      "a_logo", "background", "boat", "crocodileBoat", "frame", "gameover",
      "menu", "StoneStubSpraySwirl", "biteBoat"
  };

//定义按键常量
  static final int KEY_UP = -1; //左键
  static final int KEY_DOWN = -2; //右键
  // static final int KEY_FIRE = 53; //5号键

  private String STR_HELP[]; //操作帮助
  private String STR_ABOUT[]; //厂商资讯

  public boolean isRepaint = false; //是否重绘
  public boolean gameOut = false; //游戏是否结束
  private Image images[] = new Image[9];
  private int musicSelectIndex = 0; //游戏music选项
  private int gradeSelectIndex = 0; //游戏的难度选项
  private int playMenuIndex = 0; //play游戏
  private int menuIndex = 0; //主菜单
  private int loseSelectIndex = 0; //游戏失败菜单选择

// private Player player; // = new Player;
//   private Sound sounds[];//声音资源
  // private Image doubleBuffer; //图片缓冲

  //private int stoneY = 120; //石头的坐标
  // private int gameStatus; //游戏全局流程状态
  private static int tempStatus = 0; //loading 临时状态
  int scoreLow = 0; //游戏历史得分
  int scoreMid = 0;
  int scoreHigh = 0;
  int thisMaxScore = 0; //本局得分

  static Random randomDir = new Random(); //船在漩涡中的改变方向

  int fps = 0; //祯数
  int xx = 7; //船的横向速率
  int lowSpeed = 4; //0,6每次上升的像素
  int midSpeed = 6; //1,5每次上升的像素
  int midhighSpeed = 8; //1,5每次上升的像素
  int highSpeed = 15; //每次上升的像素
  public int canKey = 0; //0表示没有按键  1,左 2,右
// public int lastP = 3; //上一个位置 0,1,2,3,4,5,6
  Sprite[] spriteArray = new Sprite[6]; //0,人//1,鳄鱼//2,树//3,激流/4,漩涡//5,小石头//6,中石头//7,大石头

//  boolean isHitLifeValue = false;

  Engine eng; // Stage的控制器
  // public Image imageBuffer;
  private static String sndPath = "/res/sound/";
  private Sound psound;
  boolean isSound = true;
  public StageCanvas(Engine canoeingEngine) { // 设置Stage的控制器

    eng = canoeingEngine;
    try {
      // System.out.println("loadSound ok");
      jbInit();
    }
    catch (Exception e) {
    }
  }

  // Image offScreen;
  /**Component initialization*/
  private void jbInit() throws Exception {
    //   System.out.println(this.hasRepeatEvents());

    //  if(!isDoubleBuffered()){
    // offScreen=Image.createImage(getWidth(),getHeight());
    //}

////////////////////////////////////////////////////////

    /*try{
         sounds[0] = new Sound(initSound("snd-0.wav", 5586), Sound.FORMAT_WAV);
        }
        catch(Exception e){
          e.printStackTrace();
          System.out.println("rsqrsq            rsq");
        }*/

    /////////////////////////////////////////////////////

    stage_Width = this.getWidth();
    stage_Height = this.getHeight();
    stage_CenterX = stage_Width / 2;
    stage_CenterY = stage_Height / 2;

    try {
      for (int i = 0; i < 9; i++) {
        images[i] = Image.createImage("/res/images/" + imgFileName[i] +
                                      ".png");

      }

       loadSound();

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

  }

 // Sound sounds[] = new Sound[0];

  //声音方法     fileName :声音文件名  soundByteNumber:声音文件字节数
 // private byte[] initSound(String fileName, int soundByteNumber) {
   /* byte[] abyte0 = null;
    try {
      InputStream inputstream = getClass().getResourceAsStream("/res/sounds/" +
          fileName);
      abyte0 = new byte[soundByteNumber];
      inputstream.read(abyte0, 0, soundByteNumber);
      inputstream.close();
      //    System.out.println("&&&&&&&&&&&&&&"+abyte0);
    }
    catch (IOException exception) {
      System.out.println("error " + exception);
    }

    return abyte0;*/
 // }

//********************************************************************************
   //得分记录保存

   public void getData() {
     String[] RS_to_del = RecordStore.listRecordStores();
     byte[] info = new byte[6]; //要保留的其它信息
     if (RS_to_del != null) {
       try {
         RecordStore data = RecordStore.openRecordStore("CData", true);
         if (data.getNumRecords() != 0) {
           info = data.getRecord(1);
           this.scoreLow = (int) (info[0]) * 100 + (int) (info[1]);
           this.scoreMid = (int) (info[2]) * 100 + (int) (info[3]);
           this.scoreHigh = (int) (info[4]) * 100 + (int) (info[5]);
         }
         else {
           //data.closeRecordStore();
           data.addRecord(info, 0, info.length);
         }
         data.closeRecordStore();
       }
       catch (RecordStoreException e) {
         e.printStackTrace();
       }
     }
     else {
       this.scoreLow = 0;
       this.scoreMid = 0;
       this.scoreHigh = 0;

     }
   }

  public void saveData() {
    String[] RS_to_del = RecordStore.listRecordStores();
    byte[] info = new byte[6];
    if (RS_to_del != null) {
      try {
        RecordStore data = RecordStore.openRecordStore("CData", true);
        if (data.getNumRecords() != 0) {
          info = data.getRecord(1);
          this.scoreLow = (int) (info[0]) * 100 + (int) (info[1]);
          this.scoreMid = (int) (info[2]) * 100 + (int) (info[3]);
          this.scoreHigh = (int) (info[4]) * 100 + (int) (info[5]);
        }
        else {
          //data.closeRecordStore();
          data.addRecord(info, 0, info.length);
        }
        data.closeRecordStore();
      }
      catch (RecordStoreException e) {
        e.printStackTrace();
      }
    }
    else {
      this.scoreLow = 0;
      this.scoreMid = 0;
      this.scoreHigh = 0;
    }

    switch (this.gradeSelectIndex) {
      case 0:
        if (this.thisMaxScore > this.scoreLow) {
          info[0] = (byte) (this.thisMaxScore / 100);
          info[1] = (byte) (this.thisMaxScore % 100);
        }
        else {
          info[0] = (byte) (this.scoreLow / 100);
          info[1] = (byte) (this.scoreLow % 100);
        }
        break;
      case 1:
        if (this.thisMaxScore > this.scoreMid) {
          info[2] = (byte) (this.thisMaxScore / 100);
          info[3] = (byte) (this.thisMaxScore % 100);

        }
        else {
          info[2] = (byte) (this.scoreMid / 100);
          info[3] = (byte) (this.scoreMid % 100);
        }
        break;
      case 2:
        if (this.thisMaxScore > this.scoreHigh) {
          info[4] = (byte) (this.thisMaxScore / 100);
          info[5] = (byte) (this.thisMaxScore % 100);
        }
        else {

          info[4] = (byte) (this.scoreHigh / 100);
          info[5] = (byte) (this.scoreHigh % 100);
        }
        break;
    }
    try {
      RecordStore data1 = RecordStore.openRecordStore("CData", true);
      data1.closeRecordStore();
      RecordStore.deleteRecordStore("CData");
      data1 = RecordStore.openRecordStore("CData", true);
      data1.addRecord(info, 0, info.length);
      data1.closeRecordStore();
    }
    catch (RecordStoreException e) {
      e.printStackTrace();
    }
  }

//********************************************************************************

//绘画

   int addScore = 0; //每200分加一个生命值
  protected void paint(Graphics g) {
    //  Graphics old = g;
    //  if(offScreen!=null){
    //    g = offScreen.getGraphics();
    //  }

    //sounds[0].play(2);

    int state = this.eng.getGameState(); //根据游戏的状态绘画图像
    boolean isHit = false; //是佛相装
    switch (state) {
      case Engine.STATE_SPLASH:

        this.drawLogoScreen(g); //logo

       if (this.drawLoadingScreen(g)) { // 显示闪屏,进度条
          eng.gameState = Engine.STATE_MENU; //进入主菜单
        }
        break;

      case Engine.STATE_MENU: /////////////////主菜单
        this.drawMenuScreen(g);
        if (images[0] != null) {
          images[0] = null;
        }
        break;
      case Engine.STATE_INTRO: //游戏说明
        this.drawIntroScreen(g);
        break;

      case Engine.STATE_GRADE:
        this.drawGradeScreen(g);
        break;
      case Engine.STATE_SCORE: //得分记录

        this.drawScoreScreen(g);

        break;

      case Engine.STATE_COPYRIGHT: //厂商咨询
        this.drawCopyrightScreen(g);
        break;

      case Engine.STATE_NEW: ////////////////////开始游戏

        this.drawBackground(g);
        this.drawFrame(g);
        this.thisMaxScore = 0; //新游戏初始化成绩值

        this.spriteArrayInit();
        lifeValue = 10;

        //    System.out.println("********************** : " + spriteArray[0].X);
        //       for (int i = 0;i<6;i++){
        //        this.spriteArray[i].sPrint();
        //     }

        for (int i = 1; i < 6; i++) {

          this.drawTypeImages(g, spriteArray[i].T, spriteArray[i].X,
                              spriteArray[i].Y, spriteArray[i].direction);
        }
        this.eng.gameState = this.eng.STATE_PLAY; //*/
        this.isRepaint = true;
        break;

      case Engine.STATE_ISDIE: //鳄鱼咬 船。失败

        // this.drawBiteBoatEffect(g, this.spriteArray[0].Y,
        //                             this.spriteArray[0].X);
        this.drawBiteBoatEffect(g, this.stage_CenterX - 10,
                                this.stage_CenterY - 20);

        //   this.isRepaint = true;
        break;

      case Engine.STATE_PLAY: ///////////////play游戏

        if (this.thisMaxScore - this.addScore > 200) { //每200分加一个生命值
          this.addScore += 200;
          if (this.lifeValue < 10) {
            this.lifeValue++;
          }
        }

        //   gthis.scoreTime=this.resetStopWatch();
        //  imgType:0,人  1,鳄鱼  2,树  3,激流  4,漩涡  5,小石头  6,中石头  7,大石头

        if (isTogether()) {
          isHit = true; //如果真,就不画船
          switch (tWhat) {
            case 1:
              eng.gameState = Engine.STATE_ISDIE;

              break;
            case 2:
              this.drawHitEffect(g, this.spriteArray[0].Y,
                                 this.spriteArray[0].X); //碰的震动效果
              break;
            case 3: //加快
              lowSpeed = 6; //0,6每次上升的像素4  6 8 10
              midSpeed = 10; //1,5每次上升的像素
              midhighSpeed = 15; //1,5每次上升的像素
              highSpeed = 20; //每次上升的像素
              break;
            case 4:
              this.drawSwirlBoat(g, this.spriteArray[0].Y,
                                 this.spriteArray[0].X); //改变方向
              break;
            case 5:
              this.drawHitEffect(g, this.spriteArray[0].Y,
                                 this.spriteArray[0].X); ////碰的震动效果
              break;
            case 6:
              this.drawHitEffect(g, this.spriteArray[0].Y,
                                 this.spriteArray[0].X); ///碰的震动效果
              break;
            case 7:
              this.drawHitEffect(g, this.spriteArray[0].Y,
                                 this.spriteArray[0].X); ///碰的震动效果
              break;
          }

⌨️ 快捷键说明

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