[教程]游戏地图制作
游戏地图制作
package cn.m15.xys;
import java.io.InputStream;
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.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager; public class mapAcitvity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 全屏显示窗口
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSC
REEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 显示自定义的游戏View
setContentView(new MapView(this));
}
public class MapView extends View {
// tile块的宽高
public final static int TILE_WIDTH = 32;
public final static int TILE_HEIGHT = 32;
// tile块的宽高的数量 ,手机分辨率是800*480
public final static int TILE_WIDTH_COUNT = 25;
public final static int TILE_HEIGHT_COUNT = 15;
// 数组元素为0则什么都不画
public final static int TILE_NULL = 0;
// 第一层游戏View地图数组
public int[][] mMapView =
{
{ 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 0, 0, 0, 0, 0, 0, 0 },
{ 21, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 21, 0, 0, 0, 0, 0, 0, 0 },
{ 22, 11, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
103, 103, 103, 103, 11, 21, 0, 0, 0, 0, 0, 0, 0 },
{ 22, 11, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
139, 139, 139, 139, 11, 21, 0, 0, 0, 0, 0, 0, 0 },
{ 22, 11, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
139, 139, 139, 139, 47, 21, 0, 0, 0, 0, 0, 0, 0 },
{ 22, 11, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
139, 139, 139, 139, 47, 21, 0, 0, 0, 0, 0, 0, 0 },
{ 22, 11, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
139, 139, 139, 139, 47, 21, 0, 0, 0, 0, 0, 0, 0 },
{ 22, 11, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
139, 139, 139, 139, 11, 21, 0, 0, 0, 0, 0, 0, 0 },
{ 22, 11, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
139, 139, 139, 139, 11, 21, 0, 0, 0, 0, 0, 0, 0 },
{ 22, 11, 30, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
139, 139, 139, 11, 21, 0, 0, 0, 0, 0, 0, 0 },
{ 22, 19, 30, 30, 30, 30, 30, 30, 30, 30, 30, 26, 26, 26, 26,
26, 26, 21, 0, 0, 0, 0, 0, 0, 0 },
{ 22, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 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, 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, 0, 0 } };
// 第二层游戏实体actor数组
public int[][] mMapAcotor =
{
{ 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, 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, 0, 0, 0, 153, 154, 155, 0, 0, 0, 0, 13, 0, 0,
0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 13, 240, 238, 0, 0, 171, 174, 175, 103, 103, 103,
103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 13, 13, 239, 0, 0, 9, 31, 32, 7, 7, 11, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 13, 0, 9, 9, 9, 11, 39, 40, 9, 9, 11, 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, 0, 9, 9, 9, 13, 13, 13, 13, 9, 11, 0, 13, 13, 0, 0,
0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 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, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0 } };
// 第三层游戏碰撞物理层数组
// 下一章介绍
// ....................
// 游戏地图资源
Bitmap mBitmap = null;
// 资源文件
Resources mResources = null;
// 游戏画笔
Paint mPaint = null;
// 横向纵向tile块的数量
int mWidthTileCount = 0;
int mHeightTileCount = 0;
// 横向纵向tile块的数量
int mBitMapWidth = 0;
int mBitMapHeight = 0;
/**
* 构造方法
*
* @param context
*/
public MapView(Context context)
{
super(context);
mPaint = new Paint();
mBitmap = ReadBitMap(context, R.drawable.map);
mBitMapWidth = mBitmap.getWidth();
mBitMapHeight = mBitmap.getHeight();
mWidthTileCount = mBitMapWidth / TILE_WIDTH;
mHeightTileCount = mBitMapHeight / TILE_HEIGHT;
}
@Override
protected void onDraw(Canvas canvas)
{
DrawMap(canvas, mPaint, mBitmap);
super.onDraw(canvas);
}
private void DrawMap(Canvas canvas, Paint paint, 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];
// 绘制地图第一层
if (ViewID > TILE_NULL)
{
DrawMapTile(ViewID, canvas, paint, bitmap, j
* TILE_WIDTH, i * TILE_HEIGHT);
}
// 绘制地图第二层
if (ActorID > TILE_NULL)
{
DrawMapTile(ActorID, canvas, paint, bitmap, j
* TILE_WIDTH, i * TILE_HEIGHT);
}
}
}
}
/**
* 根据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();
}
}
}
本文档为【[教程]游戏地图制作】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。