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

📄 canvas1.java~351~

📁 这个是最早期的一个飞行射击游戏 简单实现了地图滚动 子弹碰撞 与 UI等
💻 JAVA~351~
📖 第 1 页 / 共 2 页
字号:
package newgame;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import java.io.*;
import javax.microedition.lcdui.Displayable;
import java.io.IOException;
import javax.microedition.lcdui.Font;

public class Canvas1 extends Canvas implements Runnable {
    public Canvas1() {
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    //變量。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    Thread thread1; //創建線程

    Image img_Logo1; //LOGO1背景
    Image img_Menu; //菜單背景
    Image img_hero; //存儲英雄
    Image img_plane1; //猎虎
    Image img_plane2; //幻影
    Image img_plane3; //天堂

    Image img_buffer; //雙緩沖圖片
    int SCx; //屏幕寬
    int SCy; //屏幕高
    int planeX = 30; //飛機選擇畫面的飛機縱坐標
    boolean key2, key4, key6, key8, key5; //4個按鍵的狀態
    boolean fire = false; //子彈開關
    boolean title; //標題開關(游戲開關)
    boolean choice_LEFT; //選擇飛機時的動畫效果
    boolean choice_RIGHT; //選擇飛機時的動畫效果
    int choice_date_1 = 1; //選擇飛機動畫狀態3向右移動狀態
    int choice_date_2 = 1; //選擇飛機動畫狀態2向左移動狀態
    int choice_date_3 = 1; //選擇飛機動畫狀態3最初中間位置
    int choice_date_4; //選擇飛機動畫狀態4左屏幕外靜止狀態
    int choice_date_5; //選擇飛機動畫狀態5右屏幕外靜止狀態
    int key; //存儲按鍵
    int iSel_Menu; //目錄選項指針
    int mission; //選擇關卡指針
    int viewX = 0; //畫面初始X位置
    int viewY = 400; //畫面初始Y位置
    Font font;
    public static final int GAME_LOGO1 = 0;
    public static final int GAME_LOGO2 = 1;
    public static final int GAME_LOADING = 2; //游戏载入状态
    public static final int GAME_MENU = 3; //菜单下的状态
    public static final int GAME_PLAY = 4; //开始游戏
    public static final int GAME_CONTINUE = 5; //继续游戏
    public static final int GAME_HELP = 6; //游戏帮助
    public static final int GAME_EXIT = 7; //退出游戏
    public static final int GAME_CHOICE = 8; //飞机选择

    public static final int UP = -1;
    public static final int DOWN = -2;
    public static final int LEFT = -3;
    public static final int RIGHT = -4;
    public static final int FIRE = -5;
    Tools tool;
    Graphics Dg; //雙緩沖畫筆
    int Player_count; //飞机选择画面的间隔器
    int Bullet_count; //玩家子彈間隔器
    int Logo1_count; //LOGO1畫面間隔器
    int enemy_count;
    ////////////////////////////////////////////////////////////////////////




    //子弹相关
    HeroBullet bullets1[]; //中间子弹
    HeroBullet bullets2[]; //左
    HeroBullet bullets3[]; //右
    int bullet_index = 0; //子彈序號
    int bullet_type = 3; //子弹类型////////////////////////////////////////////////////////////////
    final int bullet_type_1 = 1; //第一种子弹:图片1单发向上
    final int bullet_type_2 = 2; //第一种子弹:图片1三发倾斜
    final int bullet_type_3 = 3; //第三种子弹:图片2单发向上


    Map map1; //第一關

    /////////////////////////////////////////////////////////////
    //游戲精靈創建

    Hero hero;
    Enemy enemy;
Blast blast;
//...................................構造函數..............................................
    public Canvas1(MIDlet1 MIEDlet) {
        this.setFullScreenMode(true); //2.0
        SCx = this.getWidth();
        SCy = this.getHeight();
        title = true; //游戲開始

        thread1 = new Thread(this);
        this.tool = new Tools(this);

        img_Logo1 = tool.getImage("/logo1.png");
        img_Menu = tool.getImage("/background.png");
        img_plane1 = tool.getImage("/plane1.png");
        img_plane2 = tool.getImage("/plane2.png");
        img_plane3 = tool.getImage("/plane3.png");

        img_buffer = Image.createImage(SCx, SCy);
        Dg = img_buffer.getGraphics();
        font = Font.getFont(font.FACE_SYSTEM, font.STYLE_BOLD, font.SIZE_LARGE);

        this.chageMission(GAME_LOGO1); //進入首個LOGO畫面
        ///////////////////////////////////////////////////////////
        //游戲人物構造
        Hero.type = 2;
blast=new Blast(this);
    }

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    //其他方法
    ////////////////////////////////////////////////////////////////////
    //子弹构造
    public void bullet_construct() {
        // 中间

        this.bullet_index = 0;
        bullets1 = new HeroBullet[15];
        for (int i = 0; i < this.bullets1.length; i++) {
            bullets1[i] = new HeroBullet(this, 1);
        }
        /////////////////////////////////////////
        //左

        this.bullet_index = 0;
        bullets2 = new HeroBullet[15];
        for (int i = 0; i < this.bullets2.length; i++) {
            bullets2[i] = new HeroBullet(this, 2);
        } /////////////////////////////////////////////
//右

        this.bullet_index = 0;
        bullets3 = new HeroBullet[15];
        for (int i = 0; i < this.bullets3.length; i++) {
            bullets3[i] = new HeroBullet(this, 3);
        }

    }


    //關卡初始化
    public void chageMission(int mission) {
        this.mission = mission;
        switch (this.mission) {
        case GAME_PLAY:
            map1 = new Map(22, 22, this);
            map1.readmap(1);

            break;

        }
    }

    /**
     * 发射子弹
     */
    public void heroShot() {
        this.Bullet_count++;
        if (this.Bullet_count > 44) {
            this.Bullet_count = 0;
        }
        switch (bullet_type) {
        case bullet_type_1:
            if (this.Bullet_count % 3 == 0) {

                this.bullets1[this.bullet_index].setPosition(hero.x + 5,
                        hero.y - 10);

                this.bullet_index++;
                if (this.bullet_index > this.bullets1.length - 1) {
                    this.bullet_index = 0;
                }

            }
            break;
        case bullet_type_2:
            if (this.Bullet_count % 3 == 0) {

                this.bullets1[this.bullet_index].setPosition(hero.x + 5,
                        hero.y - 10);
                this.bullets2[this.bullet_index].setPosition(hero.x + 5,
                        hero.y - 10);
                this.bullets3[this.bullet_index].setPosition(hero.x + 5,
                        hero.y - 10);

                this.bullet_index++;
                if (this.bullet_index > this.bullets2.length - 1) {
                    this.bullet_index = 0;
                }

            }

            break;
        case bullet_type_3:
            HeroBullet.frameCntX = 1; //子彈類型指針
            if (this.Bullet_count % 3 == 0) {
                this.bullets1[this.bullet_index].setPosition(hero.x + 5,
                        hero.y - 10);
                this.bullet_index++;
                if (this.bullet_index > this.bullets2.length - 1) {
                    this.bullet_index = 0;
                }
            }
            break;

        }

    }


//啟動線程。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    public void gameStart() {
        thread1.start();
    }

//////////////////////////////////////////////////////////////////////////////
    //邏輯處理
    public void logic() {

        switch (mission) {

        case GAME_LOGO1:
            this.Logo1_count++;
            if (this.Logo1_count == 25) {
                this.chageMission(GAME_MENU);
            }
            break;
        case GAME_HELP:
            break;

        case GAME_MENU:
            break;
        case GAME_CHOICE:

            /////////////////////////////////////////左鍵操作

            if (choice_LEFT) {
                if (choice_date_1 == 1 && choice_date_3 == 1) { //動畫效果:飛機向右移動一小段距離
                    planeX += 6;
                    Player_count++;
                    if (Player_count == 4) {
                        choice_date_2 = 1; //向左移動開始
                        choice_date_1 = 0; //停止you移動
                        choice_date_3 = 0; //[非]靜止狀態
                        Player_count = 0;
                    }
                }
                if (choice_date_2 == 1 && choice_date_1 == 0) { //動畫效果:飛機向左持續移動
                    planeX -= 13; //127
                    if (planeX <= -127) {
                        choice_date_2 = 0;
                        choice_date_4 = 1;
                        choice_date_1 = 1;
                        planeX = SCx; //下一飛機的初始位置
                        Hero.type++;

                        if (Hero.type == 3) {
                            Hero.type = 0;
                        }
                    }
                }
                if (choice_date_4 == 1 && choice_date_1 == 1) { //動畫效果:向左移動
                    planeX -= 15;
                    if (planeX <= 30) {
                        choice_date_4 = 0;
                        choice_date_3 = 1; //回到靜止狀態
                        choice_date_1 = 1;
                        choice_date_2 = 1;
                        choice_LEFT = false; //防止循環
                    }
                }
            }

            /////////////////////////////////////////、、、、、、、、、、、右鍵操作
            if (choice_RIGHT) {
                if (choice_date_2 == 1 && choice_date_3 == 1) { //動畫效果:飛機向左移動一小段距離
                    planeX -= 6;
                    Player_count++;
                    if (Player_count == 4) {
                        choice_date_1 = 1; //向右移動開始
                        choice_date_2 = 0; //停止zuo移動
                        choice_date_3 = 0; //[非]靜止狀態
                        Player_count = 0;
                        //  System.out.println("aaaaaaaaaaa");

                    }
                }
                if (choice_date_1 == 1 && choice_date_2 == 0) { //動畫效果:飛機向右持續移動
                    planeX += 13; //127
                    if (planeX >= SCx) {
                        choice_date_1 = 0;
                        choice_date_5 = 1;
                        choice_date_2 = 1;
                        planeX = -127; //下一飛機的初始位置
                        Hero.type--;
                        if (Hero.type == -1) {
                            Hero.type = 2;
                        }

                        //  System.out.println("bbbbbbbbbbbbbbbbbb");
                        //  System.out.println("type:" + Hero.type);

                    }
                }
                if (choice_date_5 == 1 && choice_date_2 == 1) { //動畫效果:向右移動
                    planeX += 15;
                    if (planeX >= 30) {

                        choice_date_5 = 0;
                        choice_date_3 = 1; //回到靜止狀態
                        choice_RIGHT = false; //初始化

                        choice_date_1 = 1;
                        choice_date_2 = 1;

                    }
                }
            }

            break;
        case GAME_PLAY:
            this.setWindows();

⌨️ 快捷键说明

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