iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android实战打飞机游戏之菜单页面设计(1)
  • 691
分享到

Android实战打飞机游戏之菜单页面设计(1)

菜单单页Android 2022-06-06 07:06:03 691人浏览 独家记忆
摘要

本文目标实现控制小飞机的左右移动、躲避子弹、打boss。 本节实现 开始菜单界面 1、首先 资源文件拷过来 2、划分游戏状态 public static final int

本文目标实现控制小飞机的左右移动、躲避子弹、打boss。

本节实现 开始菜单界面

1、首先 资源文件拷过来

2、划分游戏状态


 public static final int GAME_MENU = 0;// 游戏菜单
 public static final int GAMEING = 1;// 游戏中
 public static final int GAME_WIN = 2;// 游戏胜利
 public static final int GAME_LOST = 3;// 游戏失败
 public static final int GAME_PAUSE = -1;// 游戏菜单
 // 当前游戏状态(默认初始在游戏菜单界面)
 public static int gameState = GAME_MENU;

定义五种状态

 定义完方法后 在绘图方法中 ,实体键 按下方法 ,抬起方法,触屏监听,逻辑方法,switch


//在那几个方法中加这个
  switch (gameState) {
  case GAME_MENU:
   break;
  case GAMEING:
   break;
  case GAME_WIN:
   break;
  case GAME_LOST:
   break;
  case GAME_PAUSE:
   break;
  default:
   break;
  }

下面再声明一些东西


//声明一个Resources实例便于加载图片
 private Resources res = this.getResources();
 //声明游戏需要用到的图片资源(图片声明)
 private Bitmap bmpBackGround;//游戏背景
 private Bitmap bmpBoom;//爆炸效果
 private Bitmap bmpBoosBoom;//Boos爆炸效果
 private Bitmap bmpButton;//游戏开始按钮
 private Bitmap bmpButtonPress;//游戏开始按钮被点击
 private Bitmap bmpEnemyDuck;//怪物鸭子
 private Bitmap bmpEnemyFly;//怪物苍蝇
 private Bitmap bmpEnemyBoos;//怪物猪头Boos
 private Bitmap bmpGameWin;//游戏胜利背景
 private Bitmap bmpGameLost;//游戏失败背景
 private Bitmap bmpPlayer;//游戏主角飞机
 private Bitmap bmpPlayerHp;//主角飞机血量
 private Bitmap bmpMenu;//菜单背景
 public static Bitmap bmpBullet;//子弹
 public static Bitmap bmpEnemyBullet;//敌机子弹
 public static Bitmap bmpBossBullet;//Boss子弹

初始化 游戏


 
 @Override
 public void surfaceCreated(SurfaceHolder holder) {
 screenW = this.getWidth();
 screenH = this.getHeight();
 initGame();
 flag = true;
 // 实例线程
 th = new Thread(this);
 // 启动线程
 th.start();
 }

 
 private void initGame() {
 //加载游戏资源
 bmpBackGround = BitmapFactory.decodeResource(res, R.drawable.background);
 bmpBoom = BitmapFactory.decodeResource(res, R.drawable.boom);
 bmpBoosBoom = BitmapFactory.decodeResource(res, R.drawable.boos_boom);
 bmpButton = BitmapFactory.decodeResource(res, R.drawable.button);
 bmpButtonPress = BitmapFactory.decodeResource(res, R.drawable.button_press);
 bmpEnemyDuck = BitmapFactory.decodeResource(res, R.drawable.enemy_duck);
 bmpEnemyFly = BitmapFactory.decodeResource(res, R.drawable.enemy_fly);
 bmpEnemyBoos = BitmapFactory.decodeResource(res, R.drawable.enemy_pig);
 bmpGameWin = BitmapFactory.decodeResource(res, R.drawable.gamewin);
 bmpGameLost = BitmapFactory.decodeResource(res, R.drawable.gamelost);
 bmpPlayer = BitmapFactory.decodeResource(res, R.drawable.player);
 bmpPlayerHp = BitmapFactory.decodeResource(res, R.drawable.hp);
 bmpMenu = BitmapFactory.decodeResource(res, R.drawable.menu);
 bmpBullet = BitmapFactory.decodeResource(res, R.drawable.bullet);
 bmpEnemyBullet = BitmapFactory.decodeResource(res, R.drawable.bullet_enemy);
 bmpBossBullet = BitmapFactory.decodeResource(res, R.drawable.boosbullet);
 }

菜单类 GameMenu

菜单类
包括 初始化绘制按钮和背景图


package com.gsf;
import Android.graphics.Bitmap;
import android.graphics.canvas;
import android.graphics.Paint;
import android.view.MotionEvent;

public class GameMenu {
 // 菜单背景图
 private Bitmap bmpMenu;
 // 按钮图片资源(按下和未按下图)
 private Bitmap bmpButton, bmpButtonPress;
 // 按钮的坐标
 private int btnX, btnY;
 // 按钮是否按下标识位
 private Boolean isPress;
 // 菜单初始化
 public GameMenu(Bitmap bmpMenu, Bitmap bmpButton, Bitmap bmpButtonPress) {
 this.bmpMenu = bmpMenu;
 this.bmpButton = bmpButton;
 this.bmpButtonPress = bmpButtonPress;
 // X居中,Y紧接屏幕底部
 btnX = MySurfaceView.screenW / 2 - bmpButton.getWidth() / 2;
 btnY = MySurfaceView.screenH - bmpButton.getHeight();
 isPress = false;
 }
 public void draw(Canvas canvas, Paint paint) {
 // 绘制菜单背景图
 canvas.drawBitmap(bmpMenu, 0, 0, paint);
 if (isPress) {
  canvas.drawBitmap(bmpButtonPress, btnX, btnY, paint);
 } else {
  canvas.drawBitmap(bmpButton, btnX, btnY, paint);
 }
 }
 public void onTouchEvent(MotionEvent event) {
 // 获取当前触控位置
 int pointX = (int) event.getX();
 int pointyY = (int) event.getY();
 // 当用户是按下和移动时
 if (event.getAction() == MotionEvent.ACTION_DOWN
  || event.getAction() == MotionEvent.ACTION_MOVE) {
  // 判定用户是否点击按钮
  if (pointX > btnX && pointX < btnX + bmpButton.getWidth()) {
  if (pointyY > btnY && pointyY < btnY + bmpButton.getHeight()) {
   isPress = true;
  } else {
   isPress = false;
  }
  } else {
  isPress = false;
  }
  // 当用于是抬起动作时
 } else if (event.getAction() == MotionEvent.ACTION_UP) {
  // 判断抬起时是否点击按钮,防止用户移动到别处
  if (pointX > btnX && pointX < btnX + bmpButton.getWidth()) {
  if (pointyY > btnY && pointyY < btnY + bmpButton.getHeight()) {
   isPress = false;//抬起后重置 还原Button状态为未按下状态
   //改变当前游戏状态为开始游戏
   MySurfaceView.gameState = MySurfaceView.GAMEING;
  }
  }
 }
 }
}

然后 在MySurfaceView中使用 GameMenu 使用菜单类


public class MySurfaceView extends SurfaceView implements Callback, Runnable {
 private SurfaceHolder sfh;
 private Paint paint;
 private Thread th;
 private boolean flag;
 private Canvas canvas;
 // 1 定义游戏状态常量
 public static final int GAME_MENU = 0;// 游戏菜单
 public static final int GAMEING = 1;// 游戏中
 public static final int GAME_WIN = 2;// 游戏胜利
 public static final int GAME_LOST = 3;// 游戏失败
 public static final int GAME_PAUSE = -1;// 游戏菜单
 // 当前游戏状态(默认初始在游戏菜单界面)
 public static int gameState = GAME_MENU;
 // 声明一个Resources实例便于加载图片
 private Resources res = this.getResources();
 // 声明游戏需要用到的图片资源(图片声明)
 private Bitmap bmpBackGround;// 游戏背景
 private Bitmap bmpBoom;// 爆炸效果
 private Bitmap bmpBoosBoom;// Boos爆炸效果
 private Bitmap bmpButton;// 游戏开始按钮
 private Bitmap bmpButtonPress;// 游戏开始按钮被点击
 private Bitmap bmpEnemyDuck;// 怪物鸭子
 private Bitmap bmpEnemyFly;// 怪物苍蝇
 private Bitmap bmpEnemyBoos;// 怪物猪头Boos
 private Bitmap bmpGameWin;// 游戏胜利背景
 private Bitmap bmpGameLost;// 游戏失败背景
 private Bitmap bmpPlayer;// 游戏主角飞机
 private Bitmap bmpPlayerHp;// 主角飞机血量
 private Bitmap bmpMenu;// 菜单背景
 public static Bitmap bmpBullet;// 子弹
 public static Bitmap bmpEnemyBullet;// 敌机子弹
 public static Bitmap bmpBossBullet;// Boss子弹
 public static int screenW;
 public static int screenH;
 //
 private GameMenu gameMenu;
 
 public MySurfaceView(Context context) {
 super(context);
 sfh = this.getHolder();
 sfh.addCallback(this);
 paint = new Paint();
 paint.setColor(Color.WHITE);
 paint.setAntiAlias(true);
 setFocusable(true);
 }
 
 @Override
 public void surfaceCreated(SurfaceHolder holder) {
 screenW = this.getWidth();
 screenH = this.getHeight();
 initGame();
 flag = true;
 // 实例线程
 th = new Thread(this);
 // 启动线程
 th.start();
 }
 
 private void initGame() {
 // 加载游戏资源
 bmpBackGround = BitmapFactory
  .decodeResource(res, R.drawable.background);
 bmpBoom = BitmapFactory.decodeResource(res, R.drawable.boom);
 bmpBoosBoom = BitmapFactory.decodeResource(res, R.drawable.boos_boom);
 bmpButton = BitmapFactory.decodeResource(res, R.drawable.button);
 bmpButtonPress = BitmapFactory.decodeResource(res,
  R.drawable.button_press);
 bmpEnemyDuck = BitmapFactory.decodeResource(res, R.drawable.enemy_duck);
 bmpEnemyFly = BitmapFactory.decodeResource(res, R.drawable.enemy_fly);
 bmpEnemyBoos = BitmapFactory.decodeResource(res, R.drawable.enemy_pig);
 bmpGameWin = BitmapFactory.decodeResource(res, R.drawable.gamewin);
 bmpGameLost = BitmapFactory.decodeResource(res, R.drawable.gamelost);
 bmpPlayer = BitmapFactory.decodeResource(res, R.drawable.player);
 bmpPlayerHp = BitmapFactory.decodeResource(res, R.drawable.hp);
 bmpMenu = BitmapFactory.decodeResource(res, R.drawable.menu);
 bmpBullet = BitmapFactory.decodeResource(res, R.drawable.bullet);
 bmpEnemyBullet = BitmapFactory.decodeResource(res,
  R.drawable.bullet_enemy);
 bmpBossBullet = BitmapFactory
  .decodeResource(res, R.drawable.boosbullet);
 //菜单类实例化
 gameMenu = new GameMenu(bmpMenu, bmpButton, bmpButtonPress);
 }
 
 public void myDraw() {
 try {
  canvas = sfh.lockCanvas();
  if (canvas != null) {
  canvas.drawColor(Color.WHITE);
  // 绘图函数根据游戏状态不同进行不同绘制
  switch (gameState) {
  case GAME_MENU:
   gameMenu.draw(canvas, paint);
   break;
  case GAMEING:
   break;
  case GAME_WIN:
   break;
  case GAME_LOST:
   break;
  case GAME_PAUSE:
   break;
  default:
   break;
  }
  }
 } catch (Exception e) {
  // TODO: handle exception
 } finally {
  if (canvas != null)
  sfh.unlockCanvasAndPost(canvas);
 }
 }
 
 @Override
 public boolean onTouchEvent(MotionEvent event) {
 switch (gameState) {
 case GAME_MENU:
  gameMenu.onTouchEvent(event);
  break;
 case GAMEING:
  break;
 case GAME_WIN:
  break;
 case GAME_LOST:
  break;
 case GAME_PAUSE:
  break;
 }
 return true;
 }
 
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
 switch (gameState) {
 case GAME_MENU:
  break;
 case GAMEING:
  break;
 case GAME_WIN:
  break;
 case GAME_LOST:
  break;
 case GAME_PAUSE:
  break;
 }
 return super.onKeyDown(keyCode, event);
 }
 @Override
 public boolean onKeyUp(int keyCode, KeyEvent event) {
 switch (gameState) {
 case GAME_MENU:
  break;
 case GAMEING:
  break;
 case GAME_WIN:
  break;
 case GAME_LOST:
  break;
 case GAME_PAUSE:
  break;
 }
 return super.onKeyUp(keyCode, event);
 }
 
 private void logic() {
 switch (gameState) {
 case GAME_MENU:
  break;
 case GAMEING:
  break;
 case GAME_WIN:
  break;
 case GAME_LOST:
  break;
 case GAME_PAUSE:
  break;
 }
 }
 @Override
 public void run() {
 while (flag) {
  long start = System.currentTimeMillis();
  myDraw();
  logic();
  long end = System.currentTimeMillis();
  try {
  if (end - start < 50) {
   Thread.sleep(50 - (end - start));
  }
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
 }
 }
 
 @Override
 public void surfaceChanged(SurfaceHolder holder, int fORMat, int width,
  int height) {
 }
 
 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
 flag = false;
 }
}

效果图:

您可能感兴趣的文章:打飞机游戏终极BOSS Android实战打飞机游戏完结篇Android实战打飞机游戏之子弹生成与碰撞以及爆炸效果(5)Android实战打飞机游戏之怪物(敌机)类的实现(4)Android实战打飞机游戏之无限循环的背景图(2)Android实战打飞机游戏之实现主角以及主角相关元素(3)Android实现纸飞机的简单操作


--结束END--

本文标题: Android实战打飞机游戏之菜单页面设计(1)

本文链接: https://www.lsjlt.com/news/24500.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作