先看效果图
新建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;// 子d public static Bitmap bmpEnemyBullet;// 敌机子d public static Bitmap bmpBossBullet;// Boss子d 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; }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android实战打飞机游戏之实现主角以及主角相关元素(3)全部内容,希望文章能够帮你解决Android实战打飞机游戏之实现主角以及主角相关元素(3)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)