实例9---我的地图

  实例9,在j2me中是使用了图层管理和地图图层,在android并没有这些,所以我用android的只是写了用于 android的图层管理器和地图图层,不过有些东西还没有实现,只实现了一些这个例子会用到的,还有setViewWindow的实现并没有完整,算法上可能会有些问题,虽然我没有测试出,如果有谁测试出来了,请告诉我,我会改正。

 下面给出这个例子的效果图和MainView的代码。


实例9---我的地图


实例9---我的地图
 package com.wjh.demon_8;

import com.wjh.midp_me.AndroidLayerManager;
import com.wjh.midp_me.AndroidSprite;
import com.wjh.midp_me.AndroidTiledLayer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

public class MainView extends SurfaceView implements Callback,Runnable{
	
	int keyCode = 0;
	Thread gameThread = null;
	boolean isGame = true;
	SurfaceHolder holder = null;
	
	Paint backPaint = null;
	Paint forePaint = null;
	//具体游戏相关
	
	public AndroidTiledLayer m_TLayer;				//管理场景
	public int m_aCells[][] =              //存储m_TLayer中各cell的取值
		   {{	2,	2,	3,	2,	2,	2,	2,	2 ,2,	2,	3,	2,	2,	2,	2,	2 },
			{   2,	3,	2,	4,	3,	4,	3,	2 ,2,	2,	3,	2,	2,	2,	2,	2},
			{   2,	3,	3,	3,	4,	2,	4,	2 ,2,	2,	3,	2,	2,	2,	2,	2 },
			{   2,	3,	2,	3,	3,	4,	1,	2 ,2,	2,	3,	2,	2,	2,	2,	2},
			{	2,	4,	3,	3,	3,	3,	4,	2 ,2,	2,	3,	2,	2,	2,	2,	2},
			{	2,	3,	2,	3,	3,	4,	3,	2 ,2,	2,	3,	2,	2,	2,	2,	2},
			{	2,	3,	3,	4,	1,	3,	3,	2 ,2,	2,	3,	2,	2,	2,	2,	2},
			{	4,	2,	2,	4,	2,	2,	4,	4 ,2,	2,	3,	2,	2,	2,	2,	2},
			{	4,	2,	2,	4,	2,	2,	4,	4 ,2,	2,	3,	2,	2,	2,	2,	2},
			{	4,	2,	2,	4,	2,	2,	4,	4 ,2,	2,	3,	2,	2,	2,	2,	2},
			{	4,	2,	2,	4,	2,	2,	4,	4 ,2,	2,	3,	2,	2,	2,	2,	2},
			{	4,	2,	2,	4,	2,	2,	4,	4 ,2,	2,	3,	2,	2,	2,	2,	2},
			{	4,	2,	2,	4,	2,	2,	4,	4 ,2,	2,	3,	2,	2,	2,	2,	2},
			{	4,	2,	2,	4,	2,	2,	4,	4 ,2,	2,	3,	2,	2,	2,	2,	2},
			{	4,	2,	2,	4,	2,	2,	4,	4 ,2,	2,	3,	2,	2,	2,	2,	2},
			{	4,	2,	2,	4,	2,	2,	4,	4 ,2,	2,	3,	2,	2,	2,	2,	2}};
	public AndroidLayerManager m_Manager;			//图层管理器,管理各个图层
	public int m_nX = 0;					//显示区域左上角的横坐标
	public int m_nY = 0;   				//显示区域左上角的纵坐标
	public MainView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		setFocusable(true);
		getHolder().addCallback(this);
		holder = this.getHolder();
		
		backPaint = new Paint();
		backPaint.setColor(Color.BLACK);
		
		forePaint = new Paint();
		//具体游戏相关
		Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.map);
		//创建TiledLayer场景
		m_TLayer = new AndroidTiledLayer( 16, 16, image, 32, 32 ); 
		//设置场景的位置
		m_TLayer.setPosition( 0, 0 );
		
		LoadMap();				
		
		//将各个图层添入m_Manager
		m_Manager = new AndroidLayerManager();
		
		m_Manager.append(m_TLayer);
		//设置m_Manager的显示区域
		m_Manager.setViewWindow( m_nX, m_nY, getWidth(), getHeight() );
	}
	public void LoadMap(){
		//设置m_TLayer中各cell的值
		for( int row = 0; row < 16; row ++ ){
			for( int col = 0; col < 16; col ++ ){
				m_TLayer.setCell( row, col, m_aCells[row][col] );
			}
		}
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		long startTime = System.currentTimeMillis();
		while(isGame)
		{
			input();
			logic();
			doDraw();
			try {
			long endTime = System.currentTimeMillis();
			long sleepTime = endTime - startTime;
				if(sleepTime<100)
				{
					Thread.sleep(sleepTime);
				}
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}
		
	}
	//开始游戏主线程
	public void start()
	{
		if(gameThread == null)
		{
		gameThread = new Thread(this);
		gameThread.start();
		}
	}
	//停止游戏主线程
	public void stop()
	{
		isGame = false;
		if(gameThread != null)
		{
			try {
				gameThread.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//输入判断
	public void input()
	{
		switch(this.keyCode)
		{
		case KeyEvent.KEYCODE_DPAD_UP:
			m_nY -= 5;
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			m_nY += 5;
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			m_nX -= 5;
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			m_nX += 5;
			break;
		}
		m_Manager.setViewWindow(m_nX, m_nY, getWidth(), getHeight());
		this.keyCode = 0;
	}
	//逻辑判断
	public void logic()
	{
	}
	public void doDraw()
	{
		Canvas c = null;
		try
		{
		c = holder.lockCanvas();
		synchronized (holder) {
			paint(c);
		}
		}finally{
			if(c != null)
			{
			holder.unlockCanvasAndPost(c);
			}
		}
	}
	//画图
	public void paint(Canvas canvas)
	{
		//用黑色清屏
		canvas.drawRect(0, 0, getWidth(), getHeight(), forePaint);
		
		//从屏幕的(0,0)点开始显示图像
		m_Manager.paint(canvas, forePaint);
	}
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		start();
		
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		stop();
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		this.keyCode = keyCode;
		return true;
	}


	

	
}
 

apk文件(将后缀改为apk):Demon_8.zip

源代码文件:Demon_8.rar

你可能感兴趣的:(游戏,C++,c,android,C#)