Android游戏——飞行射击游戏1945最简单的实现

前言:准备考研了,前段时间电脑硬盘坏了,郁闷!!!这几天学习了下不用引擎如何用java做一个小游戏。关卡和菜单什么的没做,主要做了玩起来的部分。
声明:转载请注明出处。

开发环境

  • eclipse+android4.4(测试android6.0运行也没问题)
  • 总目录如下:MainActivity.java是引导类、MainGame.java是游戏总体框架类、Hero.java是玩家的战机、Npc.java是敌人的战机、BulletHero.java和BulletNpc.java是子弹类、GameUtils.java是工具类包括常量也在里面。
    这里写图片描述

框架类介绍
基本大部分游戏的运行流程都如下面框架类所示:永远是先逻辑,在绘画,如此循环而已。首先把logo、Init、Menu、都跑一边,做初始化工作。然后开始游戏后一直循环调用Deal_Paly()和Draw_Paly()。具体看源代。
http://download.csdn.net/detail/sinat_18127633/9396449

package src.com;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;

/*
 * 框架类
 */
public class Game extends View implements Runnable {

    // 游戏的当前状态
    int Game_State;
    // ------------------------------------ 游戏的状态
    // 游戏的Logo 状态
    public static final int GAME_LOGO = 0X0001;
    // 游戏的加载 状态
    public static final int GAME_INIT = 0X0002;
    // 游戏的菜单 状态
    public static final int GAME_MENU = 0X0003;
    // 游戏的进行 状态
    public static final int GAME_PLAY = 0X0004;
    // 游戏的暂停 状态
    public static final int GAME_PAUSE = 0X0005;
    // 游戏的退出 状态
    public static final int GAME_EXIT = 0X0006;

    // 屏幕的宽度和高度
    int Screen_w, Screen_h;

    Paint paint;

    // 线程
    Thread thread;

    // 线程开关
    boolean isRun;

    // ----------------------------- 触屏相关

    int Point_x, Point_y;
    //判断是否触屏
    boolean isPoint;

    /**
     * 框架类的构造方法(是负责给框架类的数据做初始化用的)
     * 
     * @param context
     */
    public Game(Context context, Display display) {
        super(context);
        // ---------------- 获取当前手机的屏幕的宽度和高度
        this.Screen_w = display.getWidth();
        this.Screen_h = display.getHeight();
        // ---------------- 游戏开始时是从游戏的Logo状态开始的
        this.Game_State = GAME_LOGO;
        // ----------------
        this.paint = new Paint();
        // ---------------- 线程相关初始化
        this.isRun = true;
        this.thread = new Thread(this);
        this.thread.start();// 这行代码执行后,系统就会找run()这个方法自动执行
    }

    /**
     * 线程运行方法
     */
    public void run() {
        // 循环( 逻辑--》绘画--》时间的间隔)
        while (this.isRun) {

            // 1.逻辑
            this.Deal();
            // ----------------------
            // 2.调用总绘画
            this.postInvalidate();

            // ------------------ 3.时间的间隔(20分之一秒)
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    // -------------------------------------

    /**
     * 总绘画(画布)
     */
    public void onDraw(Canvas canvas) {
        // --------------
        switch (this.Game_State) {
        case GAME_LOGO:
            this.Draw_Logo(canvas);
            break;
        case GAME_INIT:
            this.Draw_Init(canvas);
            break;
        case GAME_MENU:
            this.Draw_Menu(canvas);
            break;
        case GAME_PLAY:
            this.Draw_Play(canvas);
            break;
        case GAME_PAUSE:
            this.Draw_Pause(canvas);
            break;
        case GAME_EXIT:
            this.Draw_Exit(canvas);
            break;
        }
    }

    /**
     * 绘画_Logo
     * 
     * @param canvas
     */
    public void Draw_Logo(Canvas canvas) {

    }

    /**
     * 绘画_Init
     * 
     * @param canvas
     */
    public void Draw_Init(Canvas canvas) {

    }

    /**
     * 绘画_Menu
     * 
     * @param canvas
     */
    public void Draw_Menu(Canvas canvas) {

    }

    /**
     * 绘画_Play
     * 
     * @param canvas
     */
    public void Draw_Play(Canvas canvas) {

    }

    /**
     * 绘画_Pause
     * 
     * @param canvas
     */
    public void Draw_Pause(Canvas canvas) {

    }

    /**
     * 绘画_Exit
     * 
     * @param canvas
     */
    public void Draw_Exit(Canvas canvas) {

    }

    /**
     * 触屏事件(系统方法)
     */
    public boolean onTouchEvent(MotionEvent me) {
        this.Point_x = (int) me.getRawX();
        this.Point_y = (int) me.getRawY();
        switch (me.getAction()) {
        case MotionEvent.ACTION_DOWN:
            this.isPoint = true;
            break;
        case MotionEvent.ACTION_UP:
            this.isPoint = false;
            break;
        }
        //这里一定要返回true, 否则ACTION_MOVE里获取不到Point_x, Point_y
        return true;
    }

    // -------------------------------------

    /**
     * 总逻辑方法
     */
    public void Deal() {

        switch (this.Game_State) {
        // 如果游戏的当前状态是LOGO状态时
        case GAME_LOGO:
            this.Deal_Logo();
            break;
        // 如果游戏的当前状态是初始化状态时
        case GAME_INIT:
            this.Deal_Init();
            break;
        // 如果游戏的当前状态是菜单状态时
        case GAME_MENU:
            this.Deal_Menu();
            break;
        // 如果游戏的当前状态是进行状态时
        case GAME_PLAY:
            this.Deal_Play();
            break;
        // 如果游戏的当前状态是暂停状态时
        case GAME_PAUSE:
            this.Deal_Pause();
            break;
        // 如果游戏的当前状态是退出状态时
        case GAME_EXIT:
            this.Deal_Exit();
            break;

        }

    }

    // ----------------------

    /**
     * 逻辑_Logo
     */
    public void Deal_Logo() {
        // .....
        // ----------------
        this.Game_State = GAME_INIT;
    }

    /**
     * 逻辑_Init
     */
    public void Deal_Init() {

        this.Game_State = GAME_MENU;
    }

    /**
     * 逻辑_Menu
     */
    public void Deal_Menu() {

        // .....
        // ----------------
        this.Game_State = GAME_PLAY;

    }

    /**
     * 逻辑_Play
     */
    public void Deal_Play() {

    }

    /**
     * 逻辑_Pause
     */
    public void Deal_Pause() {

    }

    /**
     * 逻辑_Exit
     */
    public void Deal_Exit() {

    }
}

这是游戏运行截图
无

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值