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

📄 ship.java

📁 一个不错的学习游戏,很适合初学者! 希望大家喜欢!
💻 JAVA
字号:
package qian;

import javax.microedition.lcdui.*;
import java.util.*;

public class Ship {
  Image imgLeft; //船
  Image imgRight;
    Image imgf;
  int x = 0;
  int y = 0;
  private int width, height; //屏幕的宽度高度
  private Graphics g; //画布
  private SetShip setp = null; //船移动线程
  private GameCanvas cl = null; //主控
  boolean running;

  Vector booms = new Vector();

  int direct;
  Timer timer;


  public Ship(GameCanvas cl, int width, int height) {
    this.g = cl.bg;
    this.width = width;
    this.height = height;
    x = 0;
    y = 58;
    this.cl = cl;
    try {
      imgLeft = Image.createImage("/res/shipr.png");
      imgRight = Image.createImage("/res/shipl.png");
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

    timer = new Timer();
    timer.schedule(new BoomTask(), 10, 10);
  }

  public void reShip() {
    if (direct == 0) {
      g.drawImage(imgLeft, x, y, Graphics.TOP | Graphics.LEFT);
    }
    else {
      g.drawImage(imgRight, x, y, Graphics.TOP | Graphics.LEFT);
    }

    for (int i = 0; i < booms.size(); i++) {
      Boom boom = (Boom) booms.elementAt(i);
      boom.paint(g);
    }

  }

  public int Xputter() {
    return x + 25;
  }

  public int Yputter() {
    return y + 18;
  }

  public void MoveShip(int b) {
    running = true;
    setp = new SetShip(b);
    setp.start();
  }

  public void StopShip() {
    running = false;
  }

  public void fire() {
    booms.addElement(new Boom(Ship.this));
  }

  public class SetShip
      extends Thread {
    public SetShip(int c) {
      direct = c;
    }

    public void run() {

      while (running) {
        try {
          if (direct == 0) {
            if (x > 0) {
              x = x - 2;
//              if(x%80==0){
//                booms.addElement(new Boom(Ship.this));
//              }

            }

          }
          if (direct == 1) {
            if (x + imgLeft.getWidth() < width) {
              x = x + 2;
//               if(x%20==0){
//                 booms.addElement(new Boom(Ship.this));
//               }
            }
          }
          cl.repaint(); //重刷屏幕

          sleep(10);
        }
        catch (Exception e) {
        }

      }
    }
  }

  class BoomTask
      extends TimerTask {
    public void run() {
      for (int i = 0; i < booms.size(); i++) {
        Boom boom = (Boom) booms.elementAt(i);
        boom.y += 2;
        for (int j = 0; j < cl.qts.size(); j++) {
          QianTing qt = (QianTing) cl.qts.elementAt(j);
          if (boom.crash(qt)) {

            if (qt.isBoomed == false) {
              cl.score += 10;
              qt.isBoomed = true;

              booms.removeElement(boom);
            }

          }
        }
        if (boom.y > cl.getHeight()) {
          booms.removeElementAt(i);
        }
      }
    }
  }

}

⌨️ 快捷键说明

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