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

📄 lhcanvas.java

📁 手机多玩家游戏
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.*;
import javax.microedition.io.*;

public class LHCanvas extends GameCanvas implements Runnable {
  private Display  display;
  private boolean  sleeping;
  private long     frameDelay;
  private Image[]  background = new Image[2];
  private LHClient client;
  private LHServer server;
  private boolean  isServer;
  private String   status = "";
  private int      mode;  // 0 = none, 1 = dot, 2 = dash
  private int      morseTimer;

  public LHCanvas(Display d, String peerType) {
    super(true);
    display = d;

    // Set the frame rate (30 fps)
    frameDelay = 33;

    // Remember the peer type
    isServer = peerType.equals("Server");
  }
  
  public void start() {
    // Set the canvas as the current screen
    display.setCurrent(this);

    // Initialize the background images
    try {
      background[0] = Image.createImage("/LighthouseOff.png");
      background[1] = Image.createImage("/LighthouseOn.png");
    }
    catch (IOException e) {
      System.err.println("Failed loading images!");
    }

    // Initialize the mode and Morse code timer
    mode = 0;
    morseTimer = 0;

    // Start the networking service
    if (isServer) {
      server = new LHServer(this);
      server.start();
    }
    else {
      client = new LHClient(this);
      client.start();
    }

    // Start the animation thread
    sleeping = false;
    Thread t = new Thread(this);
    t.start();
  }
  
  public void stop() {
    // Stop the animation
    sleeping = true;
  }
  
  public void run() {
    Graphics g = getGraphics();
    
    // The main game loop
    while (!sleeping) {
      update();
      draw(g);
      try {
        Thread.sleep(frameDelay);
      }
      catch (InterruptedException ie) {}
    }
  }

  private void update() {
    // Process user input to issue dots and dashes
    int keyState = getKeyStates();
    if ((keyState & LEFT_PRESSED) != 0) {
      if (isServer)
        server.sendMessage("Dot");
      else
        client.sendMessage("Dot");
      status = "Dot";
    }
    else if ((keyState & RIGHT_PRESSED) != 0) {
      if (isServer)
        server.sendMessage("Dash");
      else
        client.sendMessage("Dash");
      status = "Dash";
    }

    // Update the Morse code timer
    if (mode != 0) {
      morseTimer++;

      // Timeout a dot
      if (mode == 1 && morseTimer > 3)
        mode = 0;
      // Timeout a dash
      else if (mode == 2 && morseTimer > 9)
        mode = 0;
    }
  }

  private void draw(Graphics g) {
    // Draw the background based on the mode
    if (mode == 0)
      g.drawImage(background[0], 0, 0, Graphics.TOP | Graphics.LEFT);
    else
      g.drawImage(background[1], 0, 0, Graphics.TOP | Graphics.LEFT);

    // Draw the status message
    g.setColor(255, 255, 255); // white
    g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
    g.drawString(status, getWidth() / 2, 5, Graphics.TOP | Graphics.HCENTER);

    // Flush the offscreen graphics buffer
    flushGraphics();
  }

  public void setStatus(String s) {
    // Set the status message
    status = s;
  }

  public void receiveMessage(String message) {
    // Set the mode
    if (message.equals("Dash"))
      mode = 2;
    else if (message.equals("Dot"))
      mode = 1;
    else
      mode = 0;

    // Reset the Morse code timer
    morseTimer = 0;

    // Clear the status message
    status = "";
  }
}

⌨️ 快捷键说明

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