Android研究之遊戲開發網路攝影機更新

來源:互聯網
上載者:User

Android研究之遊戲開發網路攝影機更新


遊戲中網路攝影機的原理介紹

在遊戲開發中更新網路攝影機的位置可以決定螢幕顯示的內容,尤其是RPG類遊戲網路攝影機有著非常重要的作用,我舉一個例子 有時候我們在玩RPG遊戲的時候進入一個新的情境 觸發一段指令碼後 發現鏡頭開始向上移動 根據鏡頭移動玩家可以大概瀏覽一下這個情境有什麼東西 ,觸發什麼樣的劇情。這個實現的方式就是遊戲網路攝影機原理。上章學習了Android遊戲開發地圖編輯器有需要的可以看下。

:首先網路攝影機顯示的地區也是手機螢幕顯示的地區 如果需要更改網路攝影機的位置 其實是更改背景地圖的位置 利用程式拖動背景地圖 給玩家一種假象讓玩家感覺像是網路攝影機在移動而不是背景地圖在移動。



遊戲中地圖的繪製原理介紹 根據地圖編輯器產生的出來的數組的每一個tile 的 ID 找到每一個tile的地圖資源原始檔案的XY座標 算出來圖片的顯示位置利用程式的切割的方法把每一個tile切割出來顯示在手機螢幕中。 切割圖片的代碼所示:

/** * 繪製圖片中的一部分圖片 * * @param canvas * @param paint * @param bitmap * @param x * @param y * @param src_x * @param src_y * @param src_width * @param src_Height */private void DrawClipImage(Canvas canvas, Paint paint, Bitmap bitmap,int x, int y, int src_x, int src_y, int src_xp, int src_yp) {    canvas.save();    canvas.clipRect(x, y, x + src_xp, y + src_yp);    canvas.drawBitmap(bitmap, x - src_x, y - src_y, paint);    canvas.restore();}

canvas.save();
切割圖片之前先把Canvas儲存起來 然後在切割 繪製結束後
canvas.restore();
在把Canvas的在狀態重設回來 如果不這麼做的話 第一張圖片切割後就會擋住以後所有的圖片,所以大家一定要記住這一點喔。

圖所示:每一張tile的繪製原理就是這樣,說到這裡有些朋友可能就要問 如果我的地圖無限大那根據這個方法豈不是要迴圈無限次?其實螢幕須要繪製的tile數量只需要繪製螢幕顯示地區以內的, 螢幕現實地區以外的我們不用考慮繪製 只需要更新地圖的座標資料就可以,比如我的模擬器螢幕的大小是320X480 那麼我實際繪製的tile數量只是 10 X15 (塊)。其實遊戲開發繪製中還有一個更重要的繪製技術就是雙緩衝技術它可以用來解決螢幕閃爍問題,下一章中我會詳細介紹。

昨天有朋友跟我提出這種用數組的方式來繪製地圖不科學我很同意他的觀點,為什麼不科學? 原因是現在我們只有一個情境我們用一個數組來繪製地圖 萬一我們的遊戲有100個情境 我們豈不是要在程式中寫100個數組了?其實在實際開發中我們是把這些地圖的資訊轉成xml檔案 打到遊戲的包中 玩家在切換遊戲情境的時候便會讀取當前遊戲情境中的地圖xml檔案。其實這些xml檔案中也是儲存這地圖的二位元組資訊 但是這樣做的好處就是資料驅動 程式員不用定義N個數組 做N種判斷 只須要根據當前切換的情境的ID就可以得到地圖的資訊 十分方便 也可以避免代碼中由於筆誤造成的的錯誤 何樂而不為。

但是不管用任何方法處理資料 它的繪製原理都是一樣的。

如何更新遊戲中網路攝影機

:程式取隨機數更新遊戲網路攝影機

目前以每10000毫秒更新一下網路攝影機的位置 (隨機數) 我們有了網路攝影機的位置以後 就可以在算出背景圖片的相對顯示位置 移動背景圖片的位置後就可以給玩家製造出一種網路攝影機在移動的假象了。

地圖塊是我新拼的 長寬的tile塊數是20X20。


使用 CTRL+C 複製,使用 CTRL+V 粘貼。package cn.m15.xys;import java.io.InputStream;
import java.util.Random;import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;public class CameraAcitvity extends Activity { MapView mMapView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 全螢幕顯示視窗
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 擷取螢幕寬高
Display display = getWindowManager().getDefaultDisplay();
// 顯示自訂的遊戲View
mMapView = new MapView(this,display.getWidth(), display.getHeight());
setContentView(mMapView);
} public class MapView extends View { // tile塊的寬高
public final static int TILE_WIDTH = 32;
public final static int TILE_HEIGHT = 32; // tile塊的寬高的數量
public final static int TILE_WIDTH_COUNT = 20;
public final static int TILE_HEIGHT_COUNT = 20; // 地圖的寬高的
public final static int MAP_WIDTH = 640;
public final static int MAP_HEIGHT = 640; //鏡頭移動範圍
public final static int CAMERA_MOVE = 10; // 螢幕的寬高
public int mScreenWidth = 0;
public int mScreenHeight = 0; // 數組元素為0則什麼都不畫
public final static int TILE_NULL = 0;
// 第一層遊戲View地圖數組
public int[][] mMapView = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 1, 1, 1,
1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 137, 1, 1, 1,
1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 1, 1, 1, 1,
1, 1, 1 },
{ 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 137, 137, 137, 1, 1,
1, 1, 1, 1, 1 },
{ 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137,
137, 137, 137, 137, 137, 1, 1, 1 },
{ 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137,
137, 137, 137, 137, 137, 1, 1, 1 },
{ 137, 137, 137, 1, 1, 137, 137, 137, 137, 137, 137, 137, 137,
137, 137, 137, 1, 1, 1, 1 },
{ 1, 137, 137, 1, 1, 1, 137, 137, 137, 137, 137, 137, 137, 137,
137, 137, 137, 1, 137, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 137, 137, 137,
137, 137, 137, 137, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 1, 1, 1, 137, 137,
137, 137, 137, 137 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 1, 1, 1, 137,
137, 137, 137, 1 },
{ 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 137, 137, 1, 1, 1,
1, 137, 137, 137 },
{ 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 1, 1, 1, 1, 1, 1,
1, 137, 137 },
{ 1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 1, 1, 1, 1, 1, 1,
137, 137, 137 },
{ 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 137, 137, 1, 1, 1, 1,
1, 137, 137, 137 },
{ 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 137, 1, 1, 1, 1,
1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 1, 1, 1, 1, 1, 1,
1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 1, 1, 1, 1, 1, 1,
1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 137, 137, 137, 137, 137, 1, 1, 1, 1, 1, 1,
1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 137, 137, 137, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1 } }; // 第二層遊戲實體actor數組
public int[][] mMapAcotor = {
{ 143, 144, 0, 102, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185, 186, 187, 188 },
{ 151, 152, 0, 110, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
193, 194, 195, 196 },
{ 159, 160, 0, 110, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201, 202, 203, 204 },
{ 0, 0, 0, 126, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209, 210, 211, 212 },
{ 0, 0, 0, 134, 136, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 218, 219, 220 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 227, 228 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0 },
{ 102, 103, 103, 103, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0 },
{ 110, 111, 111, 111, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0 },
{ 110, 111, 111, 111, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0 },
{ 126, 127, 127, 127, 128, 0, 0, 0, 0, 0, 0, 0, 165, 166, 0, 0,
0, 0, 0, 0 },
{ 123, 124, 124, 124, 125, 0, 0, 0, 0, 0, 0, 0, 173, 174, 175, 176,
0, 0, 0, 0 },
{ 229, 230, 231, 232, 0, 0, 0, 0, 0, 0, 0, 0, 181, 182, 183, 184,
0, 0, 0, 0 },
{ 237, 238, 239, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0 },
{ 245, 246, 247, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0 },
{ 0, 254, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 233, 234, 235,
236, 0, 0, 0 },
{ 0, 262, 263, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 241, 242, 243,
244, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 250, 251,
0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 258, 259,
0, 0, 143, 144 }
}; // 第三層遊戲碰撞物理層數組
// 下一章介紹
// .................... // 遊戲地圖資源
Bitmap mBitmap = null; // 資源檔
Resources mResources = null; // 遊戲畫筆
Paint mPaint = null; // 橫向縱向tile塊的數量
int mWidthTileCount = 0;
int mHeightTileCount = 0; // 橫向縱向tile塊的數量
int mBitMapWidth = 0;
int mBitMapHeight = 0; //網路攝影機的焦點 0.0點為焦點在中心。
int mCameraPosX = 0;
int mCameraPosY = 0; //地圖左上方錨點座標
int mMapPosX =0;
int mMapPosY =0; //記錄上次時間 private long statrTime = 0; /**
* 構造方法
*
* @param context
*/
public MapView(Context context,int screenWidth, int screenHeight) {
super(context);
mScreenHeight = screenHeight;
mScreenWidth = screenWidth;
mPaint = new Paint();
mBitmap = ReadBitMap(context, R.drawable.map);
mBitMapWidth = mBitmap.getWidth();
mBitMapHeight = mBitmap.getHeight();
mWidthTileCount = mBitMapWidth / TILE_WIDTH;
mHeightTileCount = mBitMapHeight / TILE_HEIGHT;
statrTime = System.currentTimeMillis();
} @Override
protected void onDraw(Canvas canvas) {
UpdateCamera();
DrawMap(canvas, mBitmap);
DrawRectText(canvas);
super.onDraw(canvas);
invalidate();
} private void DrawMap(Canvas canvas, Bitmap bitmap) {
int i, j;
for (i = 0; i < TILE_HEIGHT_COUNT; i++) {
for (j = 0; j < TILE_WIDTH_COUNT; j++) {
int ViewID = mMapView[i][j];
int ActorID = mMapAcotor[i][j];
int x = (j* TILE_WIDTH) + mMapPosX;
int y = (i* TILE_HEIGHT) + mMapPosY;
// 繪製地圖第一層
if (ViewID > TILE_NULL) {
DrawMapTile(ViewID, canvas, mPaint, bitmap, x, y);
} // 繪製地圖第二層
if (ActorID > TILE_NULL) {
DrawMapTile(ActorID, canvas, mPaint, bitmap, x, y);
}
}
}
}
private void DrawRectText(Canvas canvas) {
canvas.clipRect(0, 0,mScreenWidth, 30);
mPaint.setColor(Color.WHITE);
canvas.drawRect(0, 0,mScreenWidth, 30, mPaint);
mPaint.setColor(Color.RED);
canvas.drawText("當前網路攝影機X座標:" + mCameraPosX + "當前網路攝影機Y座標 :" + mCameraPosY, 0, 20, mPaint);
}
private void UpdateCamera() {
long nowTime = System.currentTimeMillis();
//每100毫秒更新一下網路攝影機的位置
if(nowTime - statrTime > 1000) {
//隨機獲得網路攝影機的座標
mCameraPosX = UtilRandom(0,MAP_WIDTH - mScreenWidth);
mCameraPosY = UtilRandom(0,MAP_HEIGHT - mScreenHeight);
//根據網路攝影機的座標更新地圖座標
mMapPosX = -mCameraPosX;
mMapPosY = -mCameraPosY;
statrTime = nowTime;
}
} /**
* 返回一個隨機數
* @param botton
* @param top
* @return
*/
private int UtilRandom(int botton, int top) {
return ((Math.abs(new Random().nextInt()) % (top - botton)) + botton);
}
/**
* 根據ID繪製一個tile塊
*
* @param id
* @param canvas
* @param paint
* @param bitmap
*/
private void DrawMapTile(int id, Canvas canvas, Paint paint,
Bitmap bitmap, int x, int y) {
// 根據數組中的ID算出在地圖資源中的XY 座標
// 因為編輯器預設0 所以第一張tile的ID不是0而是1 所以這裡 -1
id--;
int count = id / mWidthTileCount;
int bitmapX = (id - (count * mWidthTileCount)) * TILE_WIDTH;
int bitmapY = count * TILE_HEIGHT;
DrawClipImage(canvas, paint, bitmap, x, y, bitmapX, bitmapY,
TILE_WIDTH, TILE_HEIGHT);
} /**
* 讀取本地資源的圖片
*
* @param context
* @param resId
* @return
*/
public Bitmap ReadBitMap(Context context, int resId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// 擷取資源圖片
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
} /**
* 繪製圖片中的一部分圖片
*
* @param canvas
* @param paint
* @param bitmap
* @param x
* @param y
* @param src_x
* @param src_y
* @param src_width
* @param src_Height
*/
private void DrawClipImage(Canvas canvas, Paint paint, Bitmap bitmap,
int x, int y, int src_x, int src_y, int src_xp, int src_yp) {
canvas.save();
canvas.clipRect(x, y, x + src_xp, y + src_yp);
canvas.drawBitmap(bitmap, x - src_x, y - src_y, paint);
canvas.restore();
} }
}

最後如果你還是覺得我寫的不夠詳細 看的不夠爽 不要緊我把原始碼的貼出來 歡迎大家一起討論學習。

源碼下載:CameraView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.