`
一口三个汉堡
  • 浏览: 115439 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

实例8--《很幼稚的‘小’鸡快跑》

阅读更多

  实例8--《小鸡快跑》,原书用的是java me 提供的sprite,我使用的我自己写的androidSprite。没什么好说的,很幼稚,但是还是要发,我要实现自己的承诺。下面给出效果图:



 mainView.java

package com.wjh.demon_7;

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.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

public class MainView extends SurfaceView implements Callback,Runnable{
	Thread gameThread = null;
	boolean isGame = true;
	SurfaceHolder holder = null;
	//具体游戏相关
	public AndroidSprite m_CockSprite;					//小鸡精灵
	public Bitmap m_BackImg;	//背景图像
	//画笔
	public Paint backPaint = null;
	public Paint forePaint = null;
	public MainView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		setFocusable(true);
		getHolder().addCallback(this);
		holder = this.getHolder();
		// 具体游戏相关代码
			m_BackImg	= BitmapFactory.decodeResource(getResources(), R.drawable.back);
			Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.cock);	        
			//创建小鸡精灵			
			m_CockSprite = new AndroidSprite( image, 95, 85 );	
			//设置精灵当前显示的“帧”编号
			m_CockSprite.setFrame( 0 );      
				
			
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(isGame)
		{
			input();
			logic();
			doDraw();
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}
		
	}
	//开始游戏主线程
	public void start()
	{
		//getWidth与getHeight可分别获取屏幕的宽度和高度	
		int nPosX = getWidth();		
		int nPosY = getHeight() - 90;	
		//设置精灵左上角的位置		
		m_CockSprite.setPosition( nPosX , nPosY );	
		
		// 新建画笔
		backPaint  = new Paint();
		backPaint.setColor(Color.BLACK);
		
		forePaint  = new Paint();
		forePaint.setColor(Color.WHITE);
		
		m_BackImg = Bitmap.createScaledBitmap(m_BackImg, getWidth(),getHeight(), false);
		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()
	{
		
	}
	//逻辑判断
	public void logic()
	{
		int n = m_CockSprite.getFrame();
		n ++;
		//getFrameSequenceLength可获取精灵图像中“帧”的个数
		if( n >= m_CockSprite.getFrameSequenceLength() )
			n = 0;
		m_CockSprite.setFrame(n);
		//设置精灵的位置,如果精灵超出屏幕左侧,则让精灵从右侧重新出现
		int nPosX = m_CockSprite.getX();
		int nPosY = m_CockSprite.getY();
		nPosX -= 6;
		if( nPosX < -95 )
			nPosX = getWidth();
		m_CockSprite.setPosition( nPosX, nPosY );
	}
	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(), backPaint);
		canvas.drawBitmap(m_BackImg, 0, 0,forePaint);
		
		//显示精灵的当前“帧”
		m_CockSprite.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();
		
	}

}

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

源代码:Demon_7.rar

  • 大小: 54.2 KB
  • 大小: 54.2 KB
1
2
分享到:
评论
2 楼 一口三个汉堡 2011-06-08  
mengqingshan 写道
很有启发意义  学习了

呵呵,有用就好。。。
1 楼 mengqingshan 2011-06-08  
很有启发意义  学习了

相关推荐

Global site tag (gtag.js) - Google Analytics