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

📄 gamecanvas.java

📁 初学者游戏框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package game.biggun;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.lang.*;
import java.util.*;
import com.nokia.mid.ui.*;

public class  GameCanvas extends FullCanvas implements Runnable{

  private static int WIDTH ;
  private static int HEIGHT ;
  Thread th ;

  private boolean killthread;
  private boolean stopthread;
  boolean showexit;
  boolean showpause;
  boolean gameover,showover;

  private Score highscore = null;

  private static boolean gun_left;
  private static boolean gun_right;
  private static boolean fire[] = new boolean[4];
  private static boolean win = false;

  private static int gun_x;                                                     //大炮的X坐标
  private static int gun_y;                                                     //大炮的Y坐标

  private static int shot_x[] = new int[4];                                     //炮弹的X坐标
  private static int shot_y[] = new int[4];                                     //炮弹的Y坐标
  private int shotNum;
  private static int gun_width;
  private static int gun_height;
  private static int cdegree = 0;
  private static int tdegree[] = {0,0,0,0};                                     //temp degree

  private static Image[] gun = new Image[3];                                    //大炮
  private static Image cgun;                                                    //当前角度的大炮
  private static Image bracket;                                                 //支架
  private static Image plane[] = new Image[6];
  private static Image shot[] = new Image[4];                                   //炮弹
  private static Image bomb;                                                    //炸弹
  private static Image[] explode = new Image[6];                                //爆炸效果
  private static Image[] bg = new Image[6];

  private static final Font fs = Font.getFont(64, 0, 8);
  private static final Font fl = Font.getFont(64, 1, 16);

  private static int stage = 0;
  private static int score ;
  private static int life ;
  private static int left_plane ;
  private static int degree ;
  int rotatedelaycount = 0;
  int planeSpeed ;
  int bombX ;
  Random rand;
  int fly;                                                                      //whitch fly
  int tfly;
  int interval;
  int fireinterval;
  private static boolean [] isFly = {false,false,false,false,false,false};      //fly flag
  private static boolean [] isSendBomb = {false,false,false,false,false,false};
  private static boolean [] isBombShot = {false,false,false,false,false,false};
  private static boolean [] isBombGun = {false,false,false,false,false,false};
  private static boolean [] isBombGround = {false,false,false,false,false,false};
  private static boolean [] isShotPlane = {false,false,false,false,false,false};
  private static boolean [] isShotBomb = {false,false,false,false,false,false};

  private static int [] hitPlane = {0,0,0,0,0,0};
  private static int [] hitBomb = {0,0,0,0,0,0};
  private static int [] hitGun = {0,0,0,0,0,0};
  private static int [] hitGround = {0,0,0,0,0,0};
  private static int [] tBombX = {0,0,0,0,0,0};

  int difficulty;

  private static int flip;
  private static int start_tx;

  private static boolean showscore = false ;
  private static boolean isNewStage = false ;
  private String stagelogo;
  boolean out[] = {false,false,false,false,false,false};
  int start_x;
  int step_x;
  int bomb_move_step_h;
  int explodecount;
  boolean rotate = true;
  private int stageinterval;
  private int maxstage;
  private Image offimg;
  private Graphics offg;
  private DirectGraphics dg;

  //分别对应飞机起飞的初始X,Y,投炸弹的初始X,Y,是否投弹(用0,1表示),飞机飞行的速度,飞机是否爆炸,炸弹是否爆炸(炸弹可见),爆炸效果图计数
  //                   0,1            2,3,   4,                 5,              6,          7                 8
  int[][] plane_XY = {
      //     0  1  2  3  4  5  6  7  8
        {WIDTH, 0, 0, 0, 0, 0, 0, 0, 0}
      , {0,     0, 0, 0, 0, 0, 0, 0, 0}
      , {WIDTH, 0, 0, 0, 0, 0, 0, 0, 0}
      , {0,     0, 0, 0, 0, 0, 0, 0, 0}
      , {WIDTH, 0, 0, 0, 0, 0, 0, 0, 0}
      , {0,     0, 0, 0, 0, 0, 0, 0, 0}
   };

  public GameCanvas() {                                                         //start here
    try {
      gun[0] = Image.createImage("/game/biggun/res/gun0.png");
      gun[1] = Image.createImage("/game/biggun/res/gun30.png");
      gun[2] = Image.createImage("/game/biggun/res/gun60.png");
      bracket = Image.createImage("/game/biggun/res/bracket.png");
      shot[0] = Image.createImage("/game/biggun/res/shot.png");
      shot[1] = Image.createImage("/game/biggun/res/shot.png");
      shot[2] = Image.createImage("/game/biggun/res/shot.png");
      shot[3] = Image.createImage("/game/biggun/res/shot.png");
      plane[0] = Image.createImage("/game/biggun/res/plane_1.png");
      plane[1] = Image.createImage("/game/biggun/res/plane_2.png");
      plane[2] = Image.createImage("/game/biggun/res/plane_3.png");
      plane[3] = Image.createImage("/game/biggun/res/plane_1.png");
      plane[4] = Image.createImage("/game/biggun/res/plane_2.png");
      plane[5] = Image.createImage("/game/biggun/res/plane_3.png");
      explode[0] = Image.createImage("/game/biggun/res/explosion1.png");
      explode[1] = Image.createImage("/game/biggun/res/explosion2.png");
      explode[2] = Image.createImage("/game/biggun/res/explosion3.png");
      explode[3] = Image.createImage("/game/biggun/res/explosion4.png");
      explode[4] = Image.createImage("/game/biggun/res/explosion5.png");
      explode[5] = Image.createImage("/game/biggun/res/explosion6.png");
      bomb = Image.createImage("/game/biggun/res/bomb.png");
      bg[0] = Image.createImage("/game/biggun/res/bg1.png");
      bg[1] = Image.createImage("/game/biggun/res/bg2.png");
      bg[2] = Image.createImage("/game/biggun/res/bg3.png");
      bg[3] = Image.createImage("/game/biggun/res/bg4.png");
      bg[4] = Image.createImage("/game/biggun/res/bg5.png");
      bg[5] = Image.createImage("/game/biggun/res/bg6.png");
    }
    catch (Exception e) {
    }

    offimg = Image.createImage(getWidth(), getHeight());
    offg = offimg.getGraphics();
    offg.setFont(GameCanvas.fs);
    dg = DirectUtils.getDirectGraphics(offg);

    WIDTH = getWidth();
    HEIGHT = getHeight();
    highscore = new Score();

    gun_width = gun[0].getWidth();
    gun_height = gun[0].getHeight();
    for(int j=0;j<shot.length;j++){
      shot_x[j] = WIDTH / 2;
      shot_y[j] = HEIGHT * 4 / 5 + 5;
    }
    rand = new Random();
    th = new Thread(this);

    init();                                                                     //初始化游戏
    initPlot();                                                                 //初始化游戏关数

    difficulty = 84;                                                            //难度调节数
    interval = 50;                                                              //线程的时间间隔
    maxstage = 6;                                                               //最大关数
    th.start();

  }
  public void init(){                                                           //初始化游戏
          win = false;
          showexit = false;
          stopthread = false;
          killthread = false;

          gameover = false;
          showover = false;
          isNewStage = true;
          stage = 0;
          score = 0;
          bomb_move_step_h = 1;
  }

  private void initPlot(){                                                      //初始化关卡
    life = 10;                                                                  //每关中 玩家的命数
    left_plane = 50;                                                            //每关中 玩家需要击落的飞机数
    degree = 0;                                                                 //大炮在每关中初始的角度 0表示朝上
    isNewStage = true;                                                          //新一关的开始 控制着过关后 显示下一关是第几关
    difficulty -= 12;                                                           //每过一次 难度的修改值 通过改变飞机出场的频率来实现
    stageinterval = 0;                                                          //用于每关时延的临时计数
    stage++;                                                                    //过关后 关数递增
    shotNum = 0;
    bomb_move_step_h ++;                                                        //炸弹下落时的速度 每一关加一
    fireinterval = 0;
    for(int j=0;j<shot.length;j++){
      fire[j] = false;                                                          //控制是否开火
      shotReset(j);
    }
    for (int i = 0; i < plane.length; i++) {
      planeReset(i);
    }
    StringBuffer sb = new StringBuffer("Mission ");
    sb.append(String.valueOf(stage));
    stagelogo=sb.toString();
  }

  public void paint(Graphics g) {
    offg.drawImage(bg[stage-1], 0, 0, offg.LEFT | offg.TOP);
     if (isNewStage) {                                                          //新一关
       offg.setColor(255,0,0);
       offg.drawString(stagelogo+"",WIDTH/2,HEIGHT/2,g.HCENTER|g.TOP);
    }
    else {                                                                      //一般情况
      offg.setColor(0x000000);                                                  //检测完各飞机的状态 准备再次重画游戏当前的状态值
      offg.setFont(fs);
      offg.drawString("Score: " + score , 10, 5, offg.LEFT | offg.TOP);         //顶部的状态行
      offg.drawString("Stage: " + stage , WIDTH-10, 5, offg.RIGHT | offg.TOP);
      offg.drawString("Task: " + left_plane , 10, 20,offg.LEFT | offg.TOP);
      offg.drawString("Life: " + life , WIDTH-10, 20, offg.RIGHT | offg.TOP);

      //for(int i=0;i<plane.length;i++){
      //  offg.drawString(i+"F:" + isFly[i] + "BS:"+isBombShot[i] + " G:"+isBombGun[i] + " R:"+isBombGround[i] + " S:"+isSendBomb[i], 0, (3+i)*12,offg.LEFT | offg.TOP);
      //}
      //炮弹
      for (int j = 0; j < shot.length; j++) {
        if (fire[j]) {
          offg.drawImage(shot[j], shot_x[j], shot_y[j], offg.HCENTER | offg.TOP);
        }
      }

      offg.drawImage(bracket, WIDTH / 2 - 5, HEIGHT * 4 / 5 + 10, offg.HCENTER | offg.TOP);   //画大炮支架及大炮
      dg.drawImage(cgun, WIDTH / 2, HEIGHT * 4 / 5, offg.HCENTER | offg.TOP, cdegree);
      offg.drawImage(bracket, WIDTH / 2, HEIGHT * 4 / 5 + 10, offg.HCENTER | offg.TOP);

      //飞机  炸弹
      //打中炮弹  大炮  地面  后的爆炸效果
      for (int i = 0; i < plane.length; i++) {
        if (i % 2 == 0) flip = 0; else flip = 8192;

        if(isFly[i]) {
             dg.drawImage(plane[i], plane_XY[i][0], plane_XY[i][1],
                          offg.TOP | offg.HCENTER, flip);
           }

          if (isShotPlane[i]) {
            if (hitPlane[i] < explode.length) {
              offg.drawImage(explode[hitPlane[i]], plane_XY[i][0],
                             plane_XY[i][1], offg.TOP | offg.HCENTER);          //画出爆炸效果图
              if (hitPlane[i] == 0)
                updateLeftPlane(i);
              hitPlane[i]++;
            }
            else if (hitBomb[i] >= explode.length) {
              //hitPlane[i] = 0;
              //isShotPlane[i] = false;
            }
          }

          if (isSendBomb[i]) {

            if (isBombShot[i]) {
              if (hitBomb[i] < explode.length) {
                offg.drawImage(explode[hitBomb[i]], plane_XY[i][2],
                               plane_XY[i][3], offg.TOP | offg.HCENTER);        //画出爆炸效果图
                hitBomb[i]++;
              }else if (hitBomb[i] >= explode.length) {
                //hitBomb[i] = 0;
                isBombShot[i]=false;
                isSendBomb[i] = false;
                }
              } else

              if (isBombGun[i]) {
                if (hitGun[i] < explode.length) {
                  offg.drawImage(explode[hitGun[i]], plane_XY[i][2],
                                 plane_XY[i][3], offg.TOP | offg.HCENTER);      //画出爆炸效果图
                  if(hitGun[i]==1) life--;
                  hitGun[i]++;
                }else if (hitGun[i] >= explode.length) {
                  //hitGun[i] = 0;
                  isBombGun[i]=false;
                  isSendBomb[i] = false;
                  }
                }else

                if (isBombGround[i]) {
                  if (hitGround[i] < explode.length) {
                    offg.drawImage(explode[hitGround[i]], plane_XY[i][2],
                                   plane_XY[i][3], offg.TOP | offg.HCENTER);    //画出爆炸效果图
                  if(hitGround[i]==1) life--;
                    hitGround[i]++;
                  }else if (hitGround[i] >= explode.length) {
                    //hitGround[i] = 0;
                    isBombGround[i]= false;
                    isSendBomb[i] = false;
                    }
                  } else
                  {
                    offg.drawImage(bomb, plane_XY[i][2], plane_XY[i][3], offg.TOP | offg.HCENTER);
                  }

            }
        }
    }                                                                           //end  一般情况
    g.drawImage(offimg, 0, 0, 20);                                              //将临时画布一次画出来,避免屏幕抖动
    if(showover){
      if(win)
        g.drawImage(Resources.img[Resources.IMG_WIN],WIDTH/2,HEIGHT/2,g.HCENTER|g.VCENTER);
      else
        g.drawImage(Resources.img[Resources.IMG_GAMEOVER],WIDTH/2,HEIGHT/2,g.HCENTER|g.VCENTER);
    }
    if(showexit)
      g.drawImage(Resources.img[Resources.IMG_SHOWEXIT],WIDTH/2,HEIGHT/2,g.HCENTER|g.VCENTER);
  }

  public void run(){
    while(!killthread){
      while (!stopthread){
        try {
          th.sleep(interval);
          checkStage();
          if(!isNewStage) updatePos();
          repaint();                                                            //调用重画repaint()方法
          if(showexit) stopthread();
          if(gameover&&showover){
            th.sleep(5000);
            stopthread();
            BigGun.changeCanvas(Resources.CVS_MENU, true);
          }
        } catch (InterruptedException ie) {

⌨️ 快捷键说明

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