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

📄 tumbleweedgame.java

📁 /* Title: J2ME Games With MIDP2 Authors: Carol Hamer Publisher: Apress ISBN: 1590593820 */
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
      // the music isn't necessary, so we ignore exceptions.
    }
  }

  /**
   * toggle the music. (pause it if it's going, start it again if it's
   * paused).
   */
  synchronized void toggle() {
    try {
      myShouldPause = !myShouldPause;
      if (myShouldPause) {
        if (myPlayer != null) {
          myPlayer.stop();
        }
      } else if (!myGamePause) {
        // if the player is null, we create a new one.
        if (myPlayer == null) {
          start();
        }
        // start the music.
        myPlayer.start();
      }
    } catch (Exception e) {
      // the music isn't necessary, so we ignore exceptions.
    }
  }

  /**
   * stops the music.
   */
  synchronized void requestStop() {
    try {
      myPlayer.stop();
      // this is called when the game is over, to we close
      // up the player to release the resources.
      myPlayer.close();
    } catch (Exception e) {
      // the music isn't necessary, so we ignore exceptions.
    }
  }

  //----------------------------------------------------------
  //   initialization

  /**
   * start the music.. Here the method is "start" instead of "run" because it
   * is not necessary to create a thread for the Player. the Player runs on
   * its own thread.
   */
  public void start() {
    ToneControl control = null;
    try {
      myPlayer = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
      // do the preliminary set-up:
      myPlayer.realize();
      // set a listener to listen for the end of the tune:
      myPlayer.addPlayerListener(this);
      // get the ToneControl object in order to set the tune data:
      control = (ToneControl) myPlayer.getControl("ToneControl");
      control.setSequence(myTune);
      // set the volume to the highest possible volume:
      VolumeControl vc = (VolumeControl) myPlayer
          .getControl("VolumeControl");
      vc.setLevel(100);
    } catch (Exception e) {
      // the music isn't necessary, so we ignore exceptions.
    }
  }

  //----------------------------------------------------------
  //   implementation of PlayerListener

  /**
   * If we reach the end of the song, play it again...
   */
  public void playerUpdate(Player player, String event, Object eventData) {
    if (event.equals(PlayerListener.END_OF_MEDIA)) {
      if ((!myShouldPause) && (!myGamePause)) {
        try {
          myPlayer.start();
        } catch (Exception e) {
          // the music isn't necessary, so we ignore exceptions.
        }
      }
    }
  }

}
/**
 * This is the class that plays a little tune while you play the game.
 * 
 * @author Carol Hamer
 */

class MusicMaker extends Thread {

  //---------------------------------------------------------
  //   fields

  /**
   * Whether or not the main thread would like this thread to stop.
   */
  public static final int NOTE_LENGTH = 250;

  /**
   * Whether or not the main thread would like this thread to pause.
   */
  private boolean myShouldPause;

  /**
   * If the whole game is paused, we pause the music too..
   */
  private boolean myGamePause;

  /**
   * Whether or not the main thread would like this thread to stop.
   */
  private static boolean myShouldStop;

  /**
   * The tune played by the game, stored as an array of notes and durations.
   * 
   * NOTE: 69 is A. To get other notes, just add or subtract their difference
   * from A on the keyboard including the black keys in the calculation. See
   * the scales below for an idea.
   *  
   */
  private byte[][] myTune = { { 69, 1 }, { 69, 1 }, { 69, 1 }, { 71, 1 },
      { 73, 2 }, { 71, 2 }, { 69, 1 }, { 73, 1 }, { 71, 1 }, { 71, 1 },
      { 69, 4 }, { 69, 1 }, { 69, 1 }, { 69, 1 }, { 71, 1 }, { 73, 2 },
      { 71, 2 }, { 69, 1 }, { 73, 1 }, { 71, 1 }, { 71, 1 }, { 69, 4 },
      { 71, 1 }, { 71, 1 }, { 71, 1 }, { 71, 1 }, { 66, 2 }, { 66, 2 },
      { 71, 1 }, { 69, 1 }, { 68, 1 }, { 66, 1 }, { 64, 4 }, { 69, 1 },
      { 69, 1 }, { 69, 1 }, { 71, 1 }, { 73, 2 }, { 71, 2 }, { 69, 1 },
      { 73, 1 }, { 71, 1 }, { 71, 1 }, { 69, 4 } };

  /**
   * An example "tune" that is just a scale.. not used.
   */
  private byte[][] myScale = { { 69, 1 }, { 71, 1 }, { 73, 1 }, { 74, 1 },
      { 76, 1 }, { 78, 1 }, { 80, 1 }, { 81, 1 } };

  /**
   * An example "tune" that is just a scale.. not used.
   */
  private byte[][] myScale2 = { { 57, 1 }, { 59, 1 }, { 61, 1 }, { 62, 1 },
      { 64, 1 }, { 66, 1 }, { 68, 1 }, { 69, 1 } };

  //----------------------------------------------------------
  //   actions

  /**
   * call this when the game pauses.
   */
  void pauseGame() {
    myGamePause = true;
  }

  /**
   * call this when the game resumes.
   */
  synchronized void resumeGame() {
    myGamePause = false;
    this.notify();
  }

  /**
   * toggle the music. (pause it if it's going, start it again if it's
   * paused).
   */
  synchronized void toggle() {
    myShouldPause = !myShouldPause;
    this.notify();
  }

  /**
   * stops the music.
   */
  synchronized void requestStop() {
    myShouldStop = true;
    this.notify();
  }

  /**
   * start the music..
   */
  public void run() {
    myShouldStop = false;
    myShouldPause = true;
    myGamePause = false;
    int counter = 0;
    while (true) {
      if (myShouldStop) {
        break;
      }
      synchronized (this) {
        while ((myShouldPause) || (myGamePause)) {
          try {
            wait();
          } catch (Exception e) {
          }
        }
      }
      try {
        Manager.playTone(myTune[counter][0], myTune[counter][1]
            * NOTE_LENGTH, 50);
      } catch (Exception e) {
        // the music isn't necessary, so we ignore exceptions.
      }
      synchronized (this) {
        try {
          wait(myTune[counter][1] * NOTE_LENGTH);
        } catch (Exception e) {
        }
      }
      counter++;
      if (counter >= myTune.length) {
        counter = 0;
      }
    }
  }

}
/**
 * This handles the graphics objects.
 * 
 * @author Carol Hamer
 */

class JumpManager extends javax.microedition.lcdui.game.LayerManager {

  //---------------------------------------------------------
  //   dimension fields
  //  (constant after initialization)

  /**
   * The x-coordinate of the place on the game canvas where the LayerManager
   * window should appear, in terms of the coordiantes of the game canvas.
   */
  static int CANVAS_X;

  /**
   * The y-coordinate of the place on the game canvas where the LayerManager
   * window should appear, in terms of the coordiantes of the game canvas.
   */
  static int CANVAS_Y;

  /**
   * The width of the display window.
   */
  static int DISP_WIDTH;

  /**
   * The height of this object's graphical region. This is the same as the
   * height of the visible part because in this game the layer manager's
   * visible part scrolls only left and right but not up and down.
   */
  static int DISP_HEIGHT;

  //   game object fields

  // the player's object.
  private Cowboy myCowboy;

  /**
   * the tumbleweeds that enter from the left.
   */
  private Tumbleweed[] myLeftTumbleweeds;

  /**
   * the tumbleweeds that enter from the right.
   */
  private Tumbleweed[] myRightTumbleweeds;

  /**
   * the object representing the grass in the background..
   */
  private Grass myGrass;

  /**
   * Whether or not the player is currently going left.
   */
  private boolean myLeft;

  /**
   * The leftmost x-coordinate that should be visible on the screen in terms
   * of this objects internal coordinates.
   */
  private int myCurrentLeftX;

  //-----------------------------------------------------
  //    gets/sets

  /**
   * This tells the player to turn left or right.
   * 
   * @param left
   *            whether or not the turn is towards the left..
   */
  void setLeft(boolean left) {
    myLeft = left;
  }

  /**
   * @return a handle to the tumbleweed objects.
   */
  Tumbleweed[] getTumbleweeds() {
    Tumbleweed[] retArray = new Tumbleweed[myLeftTumbleweeds.length
        + myRightTumbleweeds.length];
    for (int i = 0; i < myLeftTumbleweeds.length; i++) {
      retArray[i] = myLeftTumbleweeds[i];
    }
    for (int i = 0; i < myRightTumbleweeds.length; i++) {
      retArray[i + myLeftTumbleweeds.length] = myRightTumbleweeds[i];
    }
    return (retArray);
  }

  //-----------------------------------------------------
  //    initialization and game state changes

  /**
   * Constructor sets the data and constructs the graphical objects..
   * 
   * @param x
   *            The x-coordinate of the place on the game canvas where the
   *            LayerManager window should appear, in terms of the coordiantes
   *            of the game canvas.
   * @param y
   *            The y-coordinate of the place on the game canvas where the
   *            LayerManager window should appear, in terms of the coordiantes
   *            of the game canvas.
   * @param width
   *            the width of the region that is to be occupied by the
   *            LayoutManager.
   * @param height
   *            the height of the region that is to be occupied by the
   *            LayoutManager.
   */
  public JumpManager(int x, int y, int width, int height) throws Exception {
    CANVAS_X = x;
    CANVAS_Y = y;
    DISP_WIDTH = width;
    DISP_HEIGHT = height;
    myCurrentLeftX = Grass.CYCLE * Grass.TILE_WIDTH;
    setViewWindow(0, 0, DISP_WIDTH, DISP_HEIGHT);
    // create the player:
    if (myCowboy == null) {
      myCowboy = new Cowboy(myCurrentLeftX + DISP_WIDTH / 2, DISP_HEIGHT
          - Cowboy.HEIGHT - 2);
      append(myCowboy);
    }
    // create the tumbleweeds to jump over:
    if (myLeftTumbleweeds == null) {
      myLeftTumbleweeds = new Tumbleweed[2];
      for (int i = 0; i < myLeftTumbleweeds.length; i++) {
        myLeftTumbleweeds[i] = new Tumbleweed(true);
        append(myLeftTumbleweeds[i]);
      }
    }
    if (myRightTumbleweeds == null) {
      myRightTumbleweeds = new Tumbleweed[2];
      for (int i = 0; i < myRightTumbleweeds.length; i++) {
        myRightTumbleweeds[i] = new Tumbleweed(false);
        append(myRightTumbleweeds[i]);
      }
    }
    // create the background object:
    if (myGrass == null) {
      myGrass = new Grass();
      append(myGrass);
    }
  }

  /**
   * sets all variables back to their initial positions.
   */
  void reset() {
    if (myGrass != null) {
      myGrass.reset();
    }
    if (myCowboy != null) {
      myCowboy.reset();
    }
    if (myLeftTumbleweeds != null) {
      for (int i = 0; i < myLeftTumbleweeds.length; i++) {
        myLeftTumbleweeds[i].reset();
      }
    }
    if (myRightTumbleweeds != null) {
      for (int i = 0; i < myRightTumbleweeds.length; i++) {
        myRightTumbleweeds[i].reset();
      }
    }
    myLeft = false;
    myCurrentLeftX = Grass.CYCLE * Grass.TILE_WIDTH;
  }

  //-------------------------------------------------------
  //  graphics methods

  /**
   * paint the game graphic on the screen.
   */
  public void paint(Graphics g) {
    setViewWindow(myCurrentLeftX, 0, DISP_WIDTH, DISP_HEIGHT);
    paint(g, CANVAS_X, CANVAS_Y);
  }

  /**
   * If the cowboy gets to the end of the graphical region, move all of the
   * pieces so that the screen appears to wrap.
   */
  private void wrap() {
    if (myCurrentLeftX % (Grass.TILE_WIDTH * Grass.CYCLE) == 0) {
      if (myLeft) {
        myCowboy.move(Grass.TILE_WIDTH * Grass.CYCLE, 0);
        myCurrentLeftX += (Grass.TILE_WIDTH * Grass.CYCLE);
        for (int i = 0; i < myLeftTumbleweeds.length; i++) {
          myLeftTumbleweeds[i]
              .move(Grass.TILE_WIDTH * Grass.CYCLE, 0);
        }
        for (int i = 0; i < myRightTumbleweeds.length; i++) {
          myRightTumbleweeds[i].move(Grass.TILE_WIDTH * Grass.CYCLE,
              0);
        }
      } else {
        myCowboy.move(-(Grass.TILE_WIDTH * Grass.CYCLE), 0);
        myCurrentLeftX -= (Grass.TILE_WIDTH * Grass.CYCLE);
        for (int i = 0; i < myLeftTumbleweeds.length; i++) {
          myLeftTumbleweeds[i].move(-Grass.TILE_WIDTH * Grass.CYCLE,
              0);
        }
        for (int i = 0; i < myRightTumbleweeds.length; i++) {
          myRightTumbleweeds[i].move(-Grass.TILE_WIDTH * Grass.CYCLE,
              0);
        }
      }
    }
  }

  //-------------------------------------------------------
  //  game movements

  /**
   * Tell all of the moving components to advance.
   * 
   * @param gameTicks
   *            the remainaing number of times that the main loop of the game
   *            will be executed before the game ends.
   * @return the change in the score after the pieces have advanced.
   */
  int advance(int gameTicks) {
    int retVal = 0;
    // first we move the view window
    // (so we are showing a slightly different view of
    // the manager's graphical area.)
    if (myLeft) {
      myCurrentLeftX--;
    } else {
      myCurrentLeftX++;
    }
    // now we tell the game objects to move accordingly.
    myGrass.advance(gameTicks);
    myCowboy.advance(gameTicks, myLeft);
    for (int i = 0; i < myLeftTumbleweeds.length; i++) {
      retVal += myLeftTumbleweeds[i].advance(myCowboy, gameTicks, myLeft,
          myCurrentLeftX, myCurrentLeftX + DISP_WIDTH);
      retVal -= myCowboy.checkCollision(myLeftTumbleweeds[i]);
    }
    for (int i = 0; i < myLeftTumbleweeds.length; i++) {
      retVal += myRightTumbleweeds[i].advance(myCowboy, gameTicks,
          myLeft, myCurrentLeftX, myCurrentLeftX + DISP_WIDTH);
      retVal -= myCowboy.checkCollision(myRightTumbleweeds[i]);
    }
    // now we check if we have reached an edge of the viewable
    // area, and if so we move the view area and all of the
    // game objects so that the game appears to wrap.
    wrap();
    return (retVal);
  }

  /**
   * Tell the cowboy to jump..
   */
  void jump() {
    myCowboy.jump();
  }

}

⌨️ 快捷键说明

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