Android實(shí)戰(zhàn)打飛機(jī)游戲之菜單頁(yè)面設(shè)計(jì)(1)
本文目標(biāo)實(shí)現(xiàn)控制小飛機(jī)的左右移動(dòng)、躲避子彈、打boss。
本節(jié)實(shí)現(xiàn) 開始菜單界面
1、首先 資源文件拷過(guò)來(lái)
2、劃分游戲狀態(tài)
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;// 游戲菜單 // 當(dāng)前游戲狀態(tài)(默認(rèn)初始在游戲菜單界面) public static int gameState = GAME_MENU;
定義五種狀態(tài)
定義完方法后 在繪圖方法中 ,實(shí)體鍵 按下方法 ,抬起方法,觸屏監(jiān)聽,邏輯方法,switch
//在那幾個(gè)方法中加這個(gè)
switch (gameState) {
case GAME_MENU:
break;
case GAMEING:
break;
case GAME_WIN:
break;
case GAME_LOST:
break;
case GAME_PAUSE:
break;
default:
break;
}
下面再聲明一些東西
//聲明一個(gè)Resources實(shí)例便于加載圖片 private Resources res = this.getResources(); //聲明游戲需要用到的圖片資源(圖片聲明) private Bitmap bmpBackGround;//游戲背景 private Bitmap bmpBoom;//爆炸效果 private Bitmap bmpBoosBoom;//Boos爆炸效果 private Bitmap bmpButton;//游戲開始按鈕 private Bitmap bmpButtonPress;//游戲開始按鈕被點(diǎn)擊 private Bitmap bmpEnemyDuck;//怪物鴨子 private Bitmap bmpEnemyFly;//怪物蒼蠅 private Bitmap bmpEnemyBoos;//怪物豬頭Boos private Bitmap bmpGameWin;//游戲勝利背景 private Bitmap bmpGameLost;//游戲失敗背景 private Bitmap bmpPlayer;//游戲主角飛機(jī) private Bitmap bmpPlayerHp;//主角飛機(jī)血量 private Bitmap bmpMenu;//菜單背景 public static Bitmap bmpBullet;//子彈 public static Bitmap bmpEnemyBullet;//敵機(jī)子彈 public static Bitmap bmpBossBullet;//Boss子彈
初始化 游戲
/**
* SurfaceView視圖創(chuàng)建,響應(yīng)此函數(shù)
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
screenW = this.getWidth();
screenH = this.getHeight();
initGame();
flag = true;
// 實(shí)例線程
th = new Thread(this);
// 啟動(dòng)線程
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;
// 按鈕的坐標(biāo)
private int btnX, btnY;
// 按鈕是否按下標(biāo)識(shí)位
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) {
// 獲取當(dāng)前觸控位置
int pointX = (int) event.getX();
int pointyY = (int) event.getY();
// 當(dāng)用戶是按下和移動(dòng)時(shí)
if (event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_MOVE) {
// 判定用戶是否點(diǎn)擊按鈕
if (pointX > btnX && pointX < btnX + bmpButton.getWidth()) {
if (pointyY > btnY && pointyY < btnY + bmpButton.getHeight()) {
isPress = true;
} else {
isPress = false;
}
} else {
isPress = false;
}
// 當(dāng)用于是抬起動(dòng)作時(shí)
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// 判斷抬起時(shí)是否點(diǎn)擊按鈕,防止用戶移動(dòng)到別處
if (pointX > btnX && pointX < btnX + bmpButton.getWidth()) {
if (pointyY > btnY && pointyY < btnY + bmpButton.getHeight()) {
isPress = false;//抬起后重置 還原Button狀態(tài)為未按下狀態(tài)
//改變當(dāng)前游戲狀態(tài)為開始游戲
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 定義游戲狀態(tài)常量
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;// 游戲菜單
// 當(dāng)前游戲狀態(tài)(默認(rèn)初始在游戲菜單界面)
public static int gameState = GAME_MENU;
// 聲明一個(gè)Resources實(shí)例便于加載圖片
private Resources res = this.getResources();
// 聲明游戲需要用到的圖片資源(圖片聲明)
private Bitmap bmpBackGround;// 游戲背景
private Bitmap bmpBoom;// 爆炸效果
private Bitmap bmpBoosBoom;// Boos爆炸效果
private Bitmap bmpButton;// 游戲開始按鈕
private Bitmap bmpButtonPress;// 游戲開始按鈕被點(diǎn)擊
private Bitmap bmpEnemyDuck;// 怪物鴨子
private Bitmap bmpEnemyFly;// 怪物蒼蠅
private Bitmap bmpEnemyBoos;// 怪物豬頭Boos
private Bitmap bmpGameWin;// 游戲勝利背景
private Bitmap bmpGameLost;// 游戲失敗背景
private Bitmap bmpPlayer;// 游戲主角飛機(jī)
private Bitmap bmpPlayerHp;// 主角飛機(jī)血量
private Bitmap bmpMenu;// 菜單背景
public static Bitmap bmpBullet;// 子彈
public static Bitmap bmpEnemyBullet;// 敵機(jī)子彈
public static Bitmap bmpBossBullet;// Boss子彈
public static int screenW;
public static int screenH;
//
private GameMenu gameMenu;
/**
* SurfaceView初始化函數(shù)
*/
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視圖創(chuàng)建,響應(yīng)此函數(shù)
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
screenW = this.getWidth();
screenH = this.getHeight();
initGame();
flag = true;
// 實(shí)例線程
th = new Thread(this);
// 啟動(dòng)線程
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);
//菜單類實(shí)例化
gameMenu = new GameMenu(bmpMenu, bmpButton, bmpButtonPress);
}
/**
* 游戲繪圖
*/
public void myDraw() {
try {
canvas = sfh.lockCanvas();
if (canvas != null) {
canvas.drawColor(Color.WHITE);
// 繪圖函數(shù)根據(jù)游戲狀態(tài)不同進(jìn)行不同繪制
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);
}
}
/**
* 觸屏事件監(jiān)聽
*/
@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;
}
/**
* 按鍵事件監(jiān)聽
*/
@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視圖狀態(tài)發(fā)生改變,響應(yīng)此函數(shù)
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
/**
* SurfaceView視圖消亡時(shí),響應(yīng)此函數(shù)
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
}
}
效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 打飛機(jī)游戲終極BOSS Android實(shí)戰(zhàn)打飛機(jī)游戲完結(jié)篇
- Android實(shí)戰(zhàn)打飛機(jī)游戲之子彈生成與碰撞以及爆炸效果(5)
- Android實(shí)戰(zhàn)打飛機(jī)游戲之怪物(敵機(jī))類的實(shí)現(xiàn)(4)
- Android實(shí)戰(zhàn)打飛機(jī)游戲之無(wú)限循環(huán)的背景圖(2)
- Android實(shí)戰(zhàn)打飛機(jī)游戲之實(shí)現(xiàn)主角以及主角相關(guān)元素(3)
- Android實(shí)現(xiàn)紙飛機(jī)的簡(jiǎn)單操作
相關(guān)文章
Android中LeakCanary檢測(cè)內(nèi)存泄漏的方法
本篇文章主要介紹了Android中LeakCanary檢測(cè)內(nèi)存泄漏的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Kotlin實(shí)現(xiàn)Android系統(tǒng)懸浮窗詳解
大家好,本篇文章主要講的是Kotlin實(shí)現(xiàn)Android系統(tǒng)懸浮窗詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Android進(jìn)程通信之Messenger和AIDL使用詳解
本篇文章主要介紹了Android進(jìn)程通信之Messenger和AIDL使用詳解,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Android實(shí)現(xiàn)滑動(dòng)到頂部懸停的效果
這篇文章給大家介紹一種不常見的實(shí)現(xiàn)Android滑動(dòng)到頂部懸停效果的方式,對(duì)大家開發(fā)Android具有一定的參考借鑒價(jià)值,有需要的朋友們可以來(lái)一起看看。2016-09-09
android中可以通過(guò)兩種方式調(diào)用接口發(fā)送短信
調(diào)用系統(tǒng)短信接口直接發(fā)送短信;調(diào)起系統(tǒng)發(fā)短信功能,本文將給出兩種方式的實(shí)現(xiàn)代碼,感興趣的朋友可以了解下,或許對(duì)你有所幫助2013-02-02
Android開發(fā)歡迎頁(yè)點(diǎn)擊跳過(guò)倒計(jì)時(shí)進(jìn)入主頁(yè)
沒(méi)點(diǎn)擊跳過(guò)自然進(jìn)入主頁(yè),點(diǎn)擊跳過(guò)之后立即進(jìn)入主頁(yè),這個(gè)功能怎么實(shí)現(xiàn)呢,本文通過(guò)實(shí)例代碼給大家介紹Android開發(fā)歡迎頁(yè)點(diǎn)擊跳過(guò)倒計(jì)時(shí)進(jìn)入主頁(yè),感興趣的朋友一起看看吧2023-12-12
Android設(shè)置桌面背景圖片的實(shí)現(xiàn)方法
有時(shí)候我們需要用android設(shè)置桌面背景圖片,這里簡(jiǎn)單分享下,方便需要的朋友2013-06-06
Android開發(fā)筆記XML數(shù)據(jù)解析方法及優(yōu)缺點(diǎn)
XML數(shù)據(jù)是一種常見的數(shù)據(jù)格式,Android開發(fā)中需要對(duì)其進(jìn)行解析。常用的XML解析方式有DOM、SAX、Pull和Json等,每種方式都有其優(yōu)缺點(diǎn)。開發(fā)者可以根據(jù)具體需求選擇合適的解析方式,提高數(shù)據(jù)解析效率和性能2023-05-05
Android 自定義View之倒計(jì)時(shí)實(shí)例代碼
這篇文章主要介紹了Android 自定義View之倒計(jì)時(shí)實(shí)例代碼的相關(guān)資料,大多數(shù)app在注冊(cè)的時(shí)候,都有一個(gè)獲取驗(yàn)證碼的按鈕,點(diǎn)擊后,訪問(wèn)接口,最終用戶會(huì)收到短信驗(yàn)證碼。為了不多次寫這個(gè)獲取驗(yàn)證碼的接口,下面將它自定義成一個(gè)view,方便使用,需要的朋友可以參考下2017-04-04
Android點(diǎn)擊事件派發(fā)機(jī)制源碼分析
這篇文章主要為大家詳細(xì)介紹了Android點(diǎn)擊事件派發(fā)機(jī)制源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08

