当前位置: 移动技术网 > IT编程>移动开发>Android > Android实战打飞机游戏之菜单页面设计(1)

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

妙叶艾灸条,公司起名测试,缤购网优惠券

本文目标实现控制小飞机的左右移动、躲避子弹、打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子弹

初始化 游戏

 /**
 * surfaceview视图创建,响应此函数
 */
 @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;

/**
 * 
 * @author liuml
 * @time 2016-5-27 下午5:43:34
 */
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;

 /**
 * surfaceview初始化函数
 */
 public mysurfaceview(context context) {
 super(context);
 sfh = this.getholder();
 sfh.addcallback(this);
 paint = new paint();
 paint.setcolor(color.white);
 paint.setantialias(true);
 setfocusable(true);
 }

 /**
 * surfaceview视图创建,响应此函数
 */
 @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();
  }
 }
 }

 /**
 * surfaceview视图状态发生改变,响应此函数
 */
 @override
 public void surfacechanged(surfaceholder holder, int format, int width,
  int height) {
 }

 /**
 * surfaceview视图消亡时,响应此函数
 */
 @override
 public void surfacedestroyed(surfaceholder holder) {
 flag = false;
 }

}

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网