文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2022-06-06 07:59

关注

先看效果图

新建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;
 
 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);
  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();
   }
  }
 }
 
 @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实战打飞机游戏之菜单页面设计(1)Android实现纸飞机的简单操作


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯