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

📄 playcanvas.java

📁 初学者游戏框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.microedition.lcdui.*;
import java.util.Random;
import mmae.ui.*;
import mmae.game.Score;
import com.nokia.mid.ui.*;
	
public class PlayCanvas extends XCanvas {

  private static int WIDTH ;
  private static int HEIGHT ;
  boolean gameover;
  ImageFont font;

  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 PlayCanvas()
	{

		/***********************************************
		以下为游戏类初使化内容
		***********************************************/
	      gun[0] = go.getImage("/Gun0.png");
	      gun[1] = go.getImage("/Gun30.png");
	      gun[2] = go.getImage("/Gun60.png");
	      bracket = go.getImage("/Bracket.png");
	      shot[0] = go.getImage("/Shot.png");
	      shot[1] = go.getImage("/Shot.png");
	      shot[2] = go.getImage("/Shot.png");
	      shot[3] = go.getImage("/Shot.png");
	      plane[0] = go.getImage("/Plane_1.png");
	      plane[1] = go.getImage("/Plane_2.png");
	      plane[2] = go.getImage("/Plane_3.png");
	      plane[3] = go.getImage("/Plane_1.png");
	      plane[4] = go.getImage("/Plane_2.png");
	      plane[5] = go.getImage("/Plane_3.png");
	      explode[0] = go.getImage("/Explosion1.png");
	      explode[1] = go.getImage("/Explosion2.png");
	      explode[2] = go.getImage("/Explosion3.png");
	      explode[3] = go.getImage("/Explosion4.png");
	      explode[4] = go.getImage("/Explosion5.png");
	      explode[5] = go.getImage("/Explosion6.png");
	      bomb = go.getImage("/Bomb.png");
	      bg[0] = go.getImage("/Bg1.png");
	      bg[1] = go.getImage("/Bg2.png");
	      bg[2] = go.getImage("/Bg3.png");
	      bg[3] = go.getImage("/Bg1.png");
	      bg[4] = go.getImage("/Bg2.png");
	      bg[5] = go.getImage("/Bg3.png");

		font = new ImageFont(go.getImage("/Font.png"),8);
		font.setOffset(-1);
		
	    offimg = Image.createImage(getWidth(), getHeight());
	    offg = offimg.getGraphics();
	    offg.setFont(PlayCanvas.fs);
	    dg = DirectUtils.getDirectGraphics(offg);

	    WIDTH = getWidth();
	    HEIGHT = getHeight();

	    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();

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

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

	
		start(35);
	}

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

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

	  private void initPlot(){                                                      //初始化关卡
	    //life = 10;                                                                  //每关中 玩家的命数
	    left_plane = 30;                                                         //每关中 玩家需要击落的飞机数
	    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) {
    	g.setClip(0,0,getWidth(),getHeight());
    	
    	
    	if(!isStop())
    	{
    	
			/***********************************************
			以下是游戏进行过程绘图
			***********************************************/    	
			g.setColor(0);
			g.fillRect(0,0,getWidth(),getHeight());

		    offg.drawImage(bg[stage-1], 0, 0, offg.LEFT | offg.TOP);
		     if (isNewStage) {                                                          //新一关
		       //offg.setColor(255,255,255);
		       //offg.drawString(stagelogo+"",WIDTH/2+1,HEIGHT/2+1,g.HCENTER|g.TOP);
		       //offg.setColor(255,255,255);		       
			   //offg.drawString(stagelogo+"",WIDTH/2,HEIGHT/2,g.HCENTER|g.TOP);		       
				
		       font.drawString(offg, stagelogo, getWidth()/2-32, getHeight()/2-10);
		    }
		    else {                                                                      //一般情况
		      offg.setColor(0x000000);                                                  //检测完各飞机的状态 准备再次重画游戏当前的状态值
		      offg.setFont(fs);
		      
		      font.drawString(offg, "SCORE: "+score, 10, 5);
		      font.drawString(offg, "STAGE: "+stage , WIDTH-65, 5);
		      font.drawString(offg, "TASK: "+left_plane, 10, 15);
		      font.drawString(offg, "LIFT: "+life, WIDTH-65, 15);
		      
		      //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);                                              //将临时画布一次画出来,避免屏幕抖动
			
		}
		else if(!isKill())				
		{

			/***********************************************
			以下游戏暂停过程绘图
			***********************************************/    	
			g.drawImage(go.getImage("/Pause.png"),getWidth()/2,getHeight()/2,g.HCENTER|g.VCENTER);		
			
		}
		else
		{

			/***********************************************
			以下游戏结束过程绘图
			***********************************************/    	
			g.drawImage(go.getImage("/Gameover.png"),getWidth()/2,getHeight()/2,g.HCENTER|g.VCENTER);		
					
    	}    	
	}

	public void update() {

		/***********************************************
		以下是游戏线程处理
		***********************************************/    
          checkStage();
          if(!isNewStage) updatePos();
          repaint();                                                            //调用重画repaint()方法
          
		serviceRepaints();
	}

	  private void updatePos() {
	    rotateGun();                                                                //转动大炮的瞄准角度
	    updateShot();                                                               //更新炮弹
	    updatePlane();                                                              //更新飞机的位置
	    updateState();
	  }
	  
	  private void updateState(){
	    for (int i = 0; i < plane.length; i++) {
	      for(int j = 0;j<shot.length;j++){

⌨️ 快捷键说明

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