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

Android实战打飞机游戏之实现主角以及主角相关元素(3)

程序员文章站 2024-03-05 09:26:12
先看效果图 新建player 类 public class player { private int playerhp = 3; pri...

先看效果图

Android实战打飞机游戏之实现主角以及主角相关元素(3)

新建player 类

public class player {

 private int playerhp = 3;

 private bitmap bmpplayerhp;
 // 主角坐标以及位图
 private int x, y;
 private bitmap bmpplayer;
 // 主角移动速度

 private int speed = 5;
 // 主角移动标识
 private boolean isup, isdown, isleft, isright;

 // 主角的构造函数
 public player(bitmap bmpplayer, bitmap bmpplayerhp) {
  this.bmpplayer = bmpplayer;
  this.bmpplayerhp = bmpplayerhp;
  // 飞机初始位置
  x = mysurfaceview.screenw / 2 - bmpplayer.getwidth() / 2;
  y = mysurfaceview.screenh - bmpplayer.getheight();
 }

 // 主角游戏绘制方法
 public void draw(canvas canvas, paint paint) {

  // 绘制主角
  canvas.drawbitmap(bmpplayer, x, y, paint);
  // 绘制血量

  for (int i = 0; i < playerhp; i++) {
   canvas.drawbitmap(bmpplayerhp, i * bmpplayerhp.getwidth(),
     mysurfaceview.screenh - bmpplayerhp.getheight(), paint);
  }

 }

 /**
  * 按键事件监听
  */
 public void onkeydown(int keycode, keyevent event) {
  if (keycode == keyevent.keycode_dpad_up) {
   isup = true;
  }
  if (keycode == keyevent.keycode_dpad_down) {
   isdown = true;
  }
  if (keycode == keyevent.keycode_dpad_left) {
   isleft = true;
  }
  if (keycode == keyevent.keycode_dpad_right) {
   isright = true;
  }
 }

 public void onkeyup(int keycode, keyevent event) {
  if (keycode == keyevent.keycode_dpad_up) {
   isup = false;
  }
  if (keycode == keyevent.keycode_dpad_down) {
   isdown = false;
  }
  if (keycode == keyevent.keycode_dpad_left) {
   isleft = false;
  }
  if (keycode == keyevent.keycode_dpad_right) {
   isright = false;
  }
 }

 /**
  * 游戏逻辑
  */
 public void logic() {
  if (isup) {
   y -= speed;
  }
  if (isdown) {
   y += speed;
  }
  if (isleft) {
   x -= speed;
  }
  if (isright) {
   x += speed;
  }
  // 判断屏幕x边界
  if (x + bmpplayer.getwidth() >= mysurfaceview.screenw) {
   x = mysurfaceview.screenw - bmpplayer.getwidth();
  } else if (x <= 0) {
   x = 0;
  }
  // 判断屏幕y边界
  if (y + bmpplayer.getheight() >= mysurfaceview.screenh) {
   y = mysurfaceview.screenh - bmpplayer.getheight();
  } else if (y <= 0) {
   y = 0;
  }

 }


 //设置主角血量
 public void setplayerhp(int hp) {
  this.playerhp = hp;
 }

 //获取主角血量
 public int getplayerhp() {
  return playerhp;
 }


}

再在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;

 private player player;

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

  player = new player(bmpplayer,bmpplayerhp);

 }

 /**
  * 游戏绘图
  */
 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);
     player.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:
   player.onkeydown(keycode, event);
   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:
   player.onkeyup(keycode, event);
   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();
   player.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;
 }
}

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