欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实战打飞机游戏之无限循环的背景图(2)

程序员文章站 2024-03-06 12:15:20
首先分析下游戏界面内的元素: 无限滚动的背景图, 可以操作的主角,主角的子弹, 主角的血量,两种怪物(敌机),一个boss, boss的爆炸效果. 先看效果图 1...

首先分析下游戏界面内的元素:
无限滚动的背景图, 可以操作的主角,主角的子弹, 主角的血量,两种怪物(敌机),一个boss, boss的爆炸效果.

先看效果图

Android实战打飞机游戏之无限循环的背景图(2)

1、首先实现无限滚动的背景图 原理: 定义两个位图对象 当第一个位图到末尾是 第二个位图从第一个位图的末尾跟上.

public class gamebg {
 // 游戏背景的图片资源
 // 为了循环播放,这里定义两个位图对象,
 // 其资源引用的是同一张图片
 private bitmap bmpbackground1;
 private bitmap bmpbackground2;

 // 游戏背景坐标
 private int bg1x, bg1y, bg2x, bg2y;

 private int speed = 3;

 public gamebg(bitmap bmpbackground) {
  this.bmpbackground1 = bmpbackground;
  this.bmpbackground2 = bmpbackground;
  // 首先让第一张填满屏幕
  bg1y = -math.abs(bmpbackground.getheight() - mysurfaceview.screenh);

  bg2y = bg1y - bmpbackground1.getheight() +50;
 }

 public void draw(canvas canvas,paint paint){

  canvas.drawbitmap(bmpbackground1, bg1x, bg1y, paint);
  canvas.drawbitmap(bmpbackground2, bg2x, bg2y, paint);
 }
 public void logic(){
  bg1y +=speed;
  bg2y +=speed;

  if(bg1y > mysurfaceview.screenh){
   bg1y = bg2y - bmpbackground1.getheight() +50;
  }
  if(bg2y > mysurfaceview.screenh){
   bg2y = bg1y - bmpbackground1.getheight() +50;
  }
 }

}

然后再在mysurfaceview里面调用方法

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;
 private gamebg gamebg;

 /**
  * 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);

  gamebg = new gamebg(bmpbackground);

 }

 /**
  * 游戏绘图
  */
 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:
     gamebg.draw(canvas, paint);
     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:
   gamebg.logic();
   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;
 }
}


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