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

实例17--无敌抢钱鸡

 
阅读更多

  随着转化的进行,难度也在增加,这次的实例我弄了一天,当然有些时间是在弄其他的,随着代码量的增加,要更加理解源代码的结构,我这次采用uml弄出他们的各个类图和结构,这样让我更了解原作者的思想...然后再用android的方法的实现,当然因为都是java开发的,有些类是直接用的,这次还是使用了我本来的写的android_midp包,在androidSprite类中添加了碰撞方法.

下面是效果图:



 

 

 

MainView.java

package com.wjh.demon_17;

import java.util.Random;

import com.wjh.midp_me.GameView;

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

public class MainView extends SurfaceView implements Callback,Runnable{
	
	
	public static final int GAME_UI		= 0;	//进入用户界面
	public static final int GAME_GAMING	= 1;	//进行游戏
	public static final int GAME_END		= 2;	//游戏结束
	public static final int GAME_STATE_NUM = 3;	//状态总数
	private int m_nState	= GAME_UI;				//存储当前的游戏状态
	
	private boolean		m_bRunning;				//控制线程运行
	private MyBack			m_Back;					//游戏背景
	private MyUI			m_UI;					//用户界面	
	private ChickSprite	m_Chick;				//管理小鸡对象
	private GoodsSprite	m_Goods[];				//管理物品对象
	private int			m_nScore = 0;			//当前得分
	private int			m_nTime = 60 * 10;		//游戏总时间
	private Random			m_Random;
	private int screenStartX = 0;//游戏界面开始坐标X
	private int screenStartY = 0;//游戏界面开始坐标Y
	private int screenWidth = 0;//游戏界面宽度
	private int screenHeight =0;//游戏界面高度
	
	private Context context ;
	int keyState = 0;
	Thread gameThread = null;
	SurfaceHolder holder = null;
	public MainView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
		this.context = context;
		setFocusable(true);
		setFocusableInTouchMode(true);
		holder = this.getHolder();
		holder.addCallback(this);	
	}
	private void initView()
	{
		m_Random = new Random();
		m_Back = new MyBack(context);
		m_UI = new MyUI(context);
		screenStartX = (getWidth() - m_Back.getBackImageWidth())/2;
		screenStartY = (getHeight()-m_Back.getBackImageHeight())/2;
		screenWidth = screenStartX+m_Back.getBackImageWidth();
		screenHeight = screenStartY+m_Back.getBackImageHeight();
		
		try{
			//读取小鸡精灵图片,并设置小鸡的初始位置
			Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.chick);
			m_Chick = new ChickSprite( img, 31, 29);
			m_Chick.setRefPixelPosition( getWidth() / 2, screenHeight- 18 );
			
			//读取物品图片,屏幕上最多同时掉落6个物品
			img = BitmapFactory.decodeResource(getResources(), R.drawable.goods);
			m_Goods = new GoodsSprite[6];
			for( int n = 0; n < m_Goods.length; n ++ )
				m_Goods[n] = new GoodsSprite( img, 19, 19 );
		}
		catch (Exception ex){} 
		setState(GAME_UI);							//设置游戏状态
	}
	private void setState( int state ){			//设置当前的游戏状态
		if( state < 0 || state >= GAME_STATE_NUM )
			return;
		m_nState = state;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(m_bRunning)
		{
			input();
			logic();
			doDraw();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	//开始游戏主线程
	public void start()
	{
		if(gameThread == null)
		{
		gameThread = new Thread(this);
		gameThread.start();
		m_bRunning = true;
		}
	}
	//停止游戏主线程
	public void stop()
	{
		m_bRunning = false;
		if(gameThread != null)
		{
			try {
				gameThread.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	private int getKeyStates()
	{
		return keyState;
	}
	private void setKeyStates(int keyStates)
	{
		this.keyState = keyStates;
	}
	//输入判断
	public void input()
	{
		//根据不同的游戏状态,处理按键操作
				int keyStates = getKeyStates();
				switch( m_nState ){
				case GAME_UI:								//进入用户界面
					m_UI.Input(keyStates);
					if( m_UI.getState() == MyUI.UI_HIDE )	//当用户界面被隐藏时进入游戏
						setState(GAME_GAMING);
					break;
				case GAME_GAMING:							//进入游戏
					if( m_nTime <= 0 )						//如果游戏超时
						return;	
					m_Chick.Input(keyStates,screenStartX,screenStartY,screenWidth,screenStartY);
					break;
				}
	}
	//逻辑判断
	public void logic()
	{
		//根据不同的游戏状态,处理逻辑操作
				switch( m_nState ){
				case GAME_UI:								//进入用户界面
					break;
				case GAME_GAMING:							//进入游戏
					m_Chick.Logic();
					for( int n = 0; n < m_Goods.length; n ++ )
					m_Goods[n].Logic(screenHeight-10);
					Collides();								//碰撞检测
					CreateGoods();							//随机生成新物品
					m_nTime --;
					if( m_nTime <= 0 )						//如果游戏超时
						setState(GAME_END);
					break;
				}
	}
	
	private void Collides(){							//碰撞检测
		for( int n = 0; n < m_Goods.length; n ++ ){
			if( m_Chick.collidesWith(m_Goods[n]) )
			{//如果小鸡与物品发生碰撞,即小鸡接到了物品
				switch( m_Goods[n].getType() ){
				case GoodsSprite.GODDS_GOLD:			//接到金元宝
					m_nScore += 5;						//增加5个积分点
					break;
				case GoodsSprite.GODDS_DIAMOND:			//接到钻石
					m_nScore += 10;						//增加10个积分点
					break;
				case GoodsSprite.GODDS_FRUIT:			//接到圆形水果会被砸倒
					m_Chick.setState( ChickSprite.CHICK_DOWN );
					break;
				}
				m_Goods[n].setVisible(false);			//让物品消失
			}
		}
	}
	private void CreateGoods(){						//随机产生新物品
		//以1比20的概率产生新物品
		int rand = m_Random.nextInt() % 10;				//rand在-10~10之间
		if( rand != 0 )							
			return;
		for( int n = 0; n < m_Goods.length; n ++ ){
			if( m_Goods[n].isVisible() == false ){
				//随机设置新物品的种类及初始位置
				int type = Math.abs( m_Random.nextInt());
				type = type % GoodsSprite.GODDS_TYPE_NUM;
				m_Goods[n].setType(type);
				
				int x = m_Random.nextInt() % (screenWidth-screenStartX-10);
				x = Math.abs(x)+screenStartX;
				m_Goods[n].StartDrop( x,screenStartY+10);
				break;
			}
		}
	}
	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)
	{
		//用黑色清屏
		Paint backpaint = new Paint();
		backpaint.setColor(Color.BLACK);
		canvas.drawRect(0, 0, getWidth(), getHeight(), backpaint);
		
		Paint forePaint = new Paint();
		forePaint.setColor(Color.RED);
		forePaint.setTextSize(20);
		switch( m_nState ){
		case GAME_UI:									//显示标题界面
			m_UI.Paint(canvas, forePaint,getWidth(), getHeight());
			break;
		case GAME_GAMING:								//显示游戏画面
			m_Back.Paint(canvas, forePaint,getWidth(), getHeight());
			m_Chick.paint(canvas,forePaint);
			for( int n = 0; n < m_Goods.length; n ++ )
				m_Goods[n].paint(canvas,forePaint);
			//显示文字信息
			StringBuffer strText = new StringBuffer();
			strText.append("Time:");
			strText.append(m_nTime / 10);
			strText.append("     Score:");
			strText.append(m_nScore);
			canvas.drawText( strText.toString(),20,20,forePaint);
			break;
		case GAME_END:									//显示结束信息
			m_Back.Paint(canvas,forePaint, getWidth(), getHeight());
			StringBuffer strEndText = new StringBuffer();
			strEndText.append("游戏结束,本次得分:");
			strEndText.append(m_nScore);
			canvas.drawText( strEndText.toString(), 20, getHeight()/2,forePaint );
			break;
		}
	}
	@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
		initView();
		start();
	
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		stop();
		
	}
	
	float x1 = 0;
	float x2 = 0;
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub	
		switch(event.getAction())
		{
		case MotionEvent.ACTION_DOWN:
			setKeyStates(GameView.ACTION_FIRE);
			x1 = event.getX();
			x2 = 0;
			break;
		case MotionEvent.ACTION_UP:
			x2 =event.getX();
			if(x2-x1>50)
			{
				setKeyStates(GameView.ACTION_RIGHT);
			}else if(x1-x2>50)
			{
				setKeyStates(GameView.ACTION_LEFT);
			}
			x1= 0;
			x2 = 0;
			break;
		default :
			break;
		}
		return true;
	}


	
	
}

 源代码:Demon_17.rar

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

  • 大小: 12.1 KB
  • 大小: 7.7 KB
  • 大小: 7.8 KB
  • 大小: 7 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics