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

📄 gamecanvas.java

📁 飞行类射击小游戏
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

import java.io.InputStream;
import java.util.Random;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Font;
import javax.microedition.media.Player;
import javax.microedition.media.Manager;
import javax.microedition.media.*;
import java.io.*;
import javax.microedition.midlet.*;

public class GameCanvas
    extends Tools
    implements Runnable {
  private Main main;
  private Thread t; //多线程对象
  private Random random; //随机数
  private Font font;
  private Player player; //播放器
  private InputStream in; //数据输入流对象
  private int gameState; //判断游戏状态
  private int menuX;
  private int menuY;
  //游戏的状态
  private final int LOGO1 = 0; //LOGO1
  private final int LOGO2 = 1; //LOGO2
  private final int LOGO3 = 2;
  private final int MENU = 3; //菜单
  private final int LOAD = 4; //载入资源
  private final int CHANGEBULLET = 5;
  private final int GAME = 6; //游戏
  private final int HELP = 7; //帮助
  private final int ABOUT = 8; //关于
  private final int EXIT = 9; //退出
  private final int GAMEOVER=10;//游戏结束
  private final int PAUSE=11;//暂停
  //按键
  private int keyState; //键值
  private final int KEY_UP = -1; //方向键——上
  private final int KEY_DOWN = -2; //方向键——下
  private final int KEY_LEFT = -3; //方向键——左
  private final int KEY_RIGHT = -4; //方向键——右
  private final int KEY_FIRE = -5; //开火键
  private final int LEFT_SOFT = -6; //左软键
  private final int RIGHT_SOFT = -7; //右软键
  private final int KEY_NUM2 = 50; //数字键--2
  private final int KEY_NUM8 = 56; //数字键--8
  private final int KEY_NUM4 = 52; //数字键--4
  private final int KEY_NUM6 = 54; //数字键--6
  private final int KEY_NUM5 = 53; //数字键--5

  //保存键值
  private final int GAME_NUM2 = 1 << 2;
  private final int GAME_NUM8 = 1 << 8;
  private final int GAME_NUM4 = 1 << 4;
  private final int GAME_NUM6 = 1 << 6;
  private final int GAME_NUM5 = 1 << 5;
  private final int GAME_UP = 1 << 11;
  private final int GAME_DOWN = 1 << 12;
  private final int GAME_LEFT = 1 << 13;
  private final int GAME_RIGHT = 1 << 14;
  private final int GAME_FIRE = 1 << 15;
  private final int GAME_SLEFT = 1 << 16;
  private final int GAME_SRIGHT = 1 << 17;

  private Image[] logoImage; //logo图片
  private Image[] backGround; //背景图片
  private Image[] image; //游戏图片
  private Image[] enemyImage; //
  private String[] menuString = {
      "开始", "帮助", "关于", "退出"};
  private String[] pauseMenu={"继续游戏","返回菜单","退出游戏"};
  private int menuChoose;
  private int menuChoose1;
  private int bY; //背景坐标

  //boss属性
  private int bossX;
  private int bossY;
  private int bossHp;
  private int[][] bossBullet;
  private int[][] bossBullet1;
  private int[][] bossBullet2;
  private int[][] bossBullet3;
  private int[][] bossBullet4;
  private long bossTime;
  private long bossTime1;
  private long bossTime2;
  private long bossTime3;
  private long bossTime4;
  private long bossBulletTime;

  //我方飞机属性
  private int dir;
  private int planeX; //飞机坐标X
  private int planeY; //飞机坐标Y
  private int myDir; //飞机移动方向
  private int myHp;
  private final int UP = 0; //飞机方向--上
  private final int LEFT = 1; //飞机方向--左
  private final int RIGHT = 2; //飞机方向--右
  private int bulletState;
  private int chooseX;
  private int[][] bullet1; //
  private int[][]bullet2;
  private int[][] bullet3;
  private int[][]bullet4;
  private int[][]bullet5;
  private long myTime;
  private long myTime1;
  private long myTime2;
  private long myTime3;
  private long myTime4;
  private long myTime5;

  //敌机属性
  private int[][] enemy; //敌机数组
  private int[][] enemy1;
  private int[][]enemy2;
  private int[][] enemyBullet; //敌方子弹数组
  private int[][] enemyBullet1;
  private int[][]enemyBullet2;
  private long enemyTime;
  private long enemyTime1;
  private long enemyTime2;
  private long eBulletTime;
  private long eBulletTime1;
  private long eBulletTime2;
  private int score;//分数
  private int frame;//爆炸帧
  private long explodeTime;
  private int time;
  private String[] helpString = {
      "游戏帮助","飞机自动发弹,方向键", "和'2','4','6','8'控制移动","右软键暂停."};
  private String[] aboutString = {
      "关于游戏","版本号: 1.10", "游戏类型: 射击", "游戏开发: 汪力, 覃琪", "本游戏由XXX公司独家发行", "版权所有,盗版必究!!"};
  public GameCanvas(Main main) {
    this.main = main;
    init1();
    init();
    start();
  }
  private void init1(){
    random = new Random();
     logoImage = new Image[3];
     backGround = new Image[4];
     image = new Image[15];
     enemyImage = new Image[6];
    bullet1 = new int[6][5];
    bullet2 = new int[5][5];
    bullet3 = new int[5][5];
    bullet4 = new int[6][5];
    bullet5 = new int[6][5];
    enemy = new int[6][5];
    enemy1 = new int[6][5];
    enemy2 = new int[6][5];
    enemyBullet = new int[15][5];
    enemyBullet1 = new int[15][5];
    enemyBullet2 = new int[15][5];
    bossBullet = new int[5][5];
    bossBullet1 = new int[5][5];
    bossBullet2 = new int[5][5];
    bossBullet3 = new int[5][5];
    bossBullet4 = new int[5][5];
   }
  //初始化变量
  private void init() {
    menuX = 50;
    menuY = 27;
    menuChoose1=0;
    bossX = 50;
    bossY = -30;
    bossHp = 2000;
    bossBulletTime = 1000;
    planeX = width / 2;
    planeY = height - 30;
    myHp = 100;
    myTime = 0;
    myTime1 = 0;
    myTime2 = 0;
    myTime3 = 0;
    myTime4 = 0;
    myTime5 = 0;
    bulletState = 0;
    chooseX = 60;
    enemyTime = 0;
    enemyTime1 = 0;
    enemyTime2 = 0;
    eBulletTime = 0;
    eBulletTime1 = 0;
    eBulletTime2 = 0;
    bossTime = 0;
    explodeTime = 0;
    score = 0;
    frame = 0;
    time = 0;
  }
  //启动多线程
  private void start() {
    t = new Thread(this);
    t.start();
  }
  //多线程
  public void run() {
    while (true) {
      drawScreen();
      sleep(60);
    }
  }

  private void drawScreen() {
    switch (gameState) {
      case LOGO1:
        logo1();
        break;
      case LOGO2:
        logo2();
        break;
      case LOGO3:
        ctrlLogo();
        logo3();
        break;
      case MENU:
        ctrlMenu();
        menu();
        break;
      case LOAD:
        loading();
        break;
      case CHANGEBULLET:
        ctrlChange();
        changeBullet();
        break;
      case GAME:
        ctrlGame();
        game();
        break;
      case HELP:
        ctrlHelp();
        help();
        break;
      case ABOUT:
        ctrlAbout();
        about();
        break;
      case EXIT:
        exit();
        break;
      case GAMEOVER:
        ctrlGameOver();
        gameover();
        break;
      case PAUSE:
		ctrlPause();
        pause();
        break;
    }
  }

  private void logo1() {
    if (logoImage[0] == null) {
      logoImage[0] = loadImage("/logo1.png");
    }
    if(logoImage[1]==null){
      logoImage[1]=loadImage("/logo2.png");
    }
    if(logoImage[2]==null){
		logoImage[2]=loadImage("/1942.png");
    }
    if (image[12] == null) {
      image[12] = loadImage("/105.png");
    }
    if(image[14]==null){
		image[14]=loadImage("/106.png");
    }
    clearScreen();
    int x = width / 2;
    int w = 0;
    while (x > 0) {
      x--;
      w += 2;
      g.setClip(x, 0, w, height);
      g.drawImage(logoImage[0], (width - logoImage[0].getWidth()) / 2,
                  (height - logoImage[0].getHeight()) / 2, 0);
      repaint();
      sleep(8);
    }
    sleep(500);
    gameState = LOGO2;
  }

  private void logo2() {
    clearScreen();
    g.drawImage(logoImage[1],width/2,height/2,Graphics.HCENTER|Graphics.VCENTER);
    repaint();
    sleep(1000);
    gameState=LOGO3;
  }
	private void logo3(){
		clearScreen();
		g.drawImage(logoImage[2],width/2,height/2,Graphics.HCENTER|Graphics.VCENTER);
		repaint();
	}
  private void menu() {
      init();
    if (player != null) {
      try {
        player.stop();
      }
      catch (MediaException ex) {
        ex.printStackTrace();
      }
    }
    player = null;
    in = null;
    try {
      in = this.getClass().getResourceAsStream("/3.mid");
      player = Manager.createPlayer(in, "audio/midi");
    }
    catch (Exception e) {
      System.out.println(e.getMessage());
    }
    clearScreen();
    g.drawImage(image[12],width/2,30,Graphics.HCENTER|Graphics.VCENTER);
    g.drawImage(image[14],width/2,70+menuString.length*22,Graphics.HCENTER|Graphics.VCENTER);
    font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
    g.setFont(font);
    for (int i = 0; i < menuString.length; i++) {
      if (menuChoose == i)
        g.setColor(0xffff0000);
      else
        g.setColor(0xff333333);
      g.drawString(menuString[i], 60, 50 + i * 22, 0);
    }
    repaint();
  }

  //载入
  private void loading() {
    int w = 0;
    int w1 = 100;
    if (image[0] == null) {
      image[0] = loadImage("/load.png"); //载入界面
    }
    if (image[1] == null) {
      image[1] = loadImage("/img0a1.png"); //我方飞机
    }
    if (image[2] == null) {
      image[2] = loadImage("/img0a0.png");
    }
    if (image[3] == null) {
      image[3] = loadImage("/img0a2.png");
    }
    if (image[4] == null) {
      image[4] = loadImage("/img6a0.png"); //我方子弹
    }
    if (image[5] == null) {
      image[5] = loadImage("/img6a0.png"); //敌方子弹
    }
    if (image[6] == null) {
      image[6] = loadImage("/explode2.png"); //爆炸
    }
    if (image[7] == null) {
      image[7] = loadImage("/ew18.png"); //我方子弹1
    }
    if (image[8] == null) {
      image[8] = loadImage("/e32.png");
    }
    if (image[9] == null) {
      image[9] = loadImage("/ew11.png");
    }
    if(image[10]==null){
		image[10]=loadImage("/gameover.png");
    }
    if(image[13]==null){
      image[13]=loadImage("/ew101.png");//我方子弹3
    }
    if (backGround[0] == null) {
      backGround[0] = loadImage("/seaback.png"); //背景
    }
    if (enemyImage[0] == null) {
      enemyImage[0] = loadImage("/e10.png"); //敌方飞机1
    }
    if (enemyImage[1] == null) {
      enemyImage[1] = loadImage("/e3.png"); //敌方飞机2
    }
    if (enemyImage[2] == null) {
      enemyImage[2] = loadImage("/e26.png"); //敌方飞机3
    }
    if (enemyImage[3] == null) {
      enemyImage[3] = loadImage("/ew0.png"); //敌方飞机1
    }

    String s = "loading... ...";
    clearScreen();
    g.drawImage(image[0], width / 2, height / 2,
                Graphics.HCENTER | Graphics.VCENTER);
    g.drawString(s, (width - w1) / 2,
                 height / 2 + image[0].getHeight() / 2 - 50, 0);
    g.setColor(0xff000000);

⌨️ 快捷键说明

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