Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android游戲開發——簡單的例子(笑臉)

android游戲開發——簡單的例子(笑臉)

編輯:關於Android編程


程序運行效果圖:

\


<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+tPrC68q1z9ajujwvcD4KPHA+MaGiTWFpbkFjdGl2aXR5PC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:java;">package com.njupt.surface; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MySurfaceView(this)); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { } @Override public boolean dispatchTouchEvent(MotionEvent ev) { GameData.clickX = (int) ev.getX(); GameData.clickY = (int) ev.getY(); switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: GameData.clickType = GameData.DOWN; break; case MotionEvent.ACTION_MOVE: GameData.clickType = GameData.MOVE; break; case MotionEvent.ACTION_UP: GameData.clickType = GameData.UP; break; default: break; } return super.dispatchTouchEvent(ev); } /** * 根據當前activity加載的view的大小有關 */ @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub return super.onTouchEvent(event); } }

2、MySurfaceView

package com.njupt.surface;

import java.util.ArrayList;

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.graphics.Point;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


/**
 * ①展現游戲界面
 * ②控制游戲界面
 */
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable{

	private SurfaceHolder sh;//SurfaceView的管理器,不直接和SurfaceView打交道
	private boolean isDestroy = false;
	private Canvas canvas;//畫紙
	private Paint paint;//畫筆
	private int sW = 0;
	private int sH = 0;
	private ArrayList list = new ArrayList();
	private Resources res;
	
	/**
	 * ①繪制灰色背景
	 * ②繪制fps,幀率:每秒鐘屏幕刷新的次數
	 * ③繪制按鈕
	 * ④繪制小人,實現按鈕的業務
	 * ⑤繪制笑臉
	 * 
	 */
	public MySurfaceView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public MySurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public MySurfaceView(Context context) {
		super(context);
		
		res = context.getResources();
		init();
		sh = this.getHolder();//獲取當前view管理者
		sh.addCallback(this);
		
		paint = new Paint();
		paint.setTextSize(30);
		paint.setAntiAlias(true);//設置抗鋸齒
	}

	private Bitmap[] bm = new Bitmap[10];
	private int w1 = 0;
	private int h1 = 0;
	private int w2 = 0;
	private int h2 = 0;
	
	
	/**
	 * 加載圖片資源
	 */
	private void init() {
		//加載drable下的資源可以使用BitmapFactory
		bm[0] = BitmapFactory.decodeResource(res, R.drawable.left01);
		bm[1] = BitmapFactory.decodeResource(res, R.drawable.left02);
		bm[2] = BitmapFactory.decodeResource(res, R.drawable.top);
		bm[3] = BitmapFactory.decodeResource(res, R.drawable.top2);
		bm[4] = BitmapFactory.decodeResource(res, R.drawable.right01);
		bm[5] = BitmapFactory.decodeResource(res, R.drawable.right02);
		bm[6] = BitmapFactory.decodeResource(res, R.drawable.bottom);
		bm[7] = BitmapFactory.decodeResource(res, R.drawable.bottom2);
		bm[8] = BitmapFactory.decodeResource(res, R.drawable.avatar_boy);//人物
		bm[9] = BitmapFactory.decodeResource(res, R.drawable.rating_small);//笑臉
		
		w1 = bm[0].getWidth();
		h1 = bm[0].getHeight();
		w2 = bm[2].getWidth();
		h2 = bm[2].getHeight();
		
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		
		sW = this.getWidth();//不要在surfaceCreate()方法中那這兩個值,因為那時view可能還沒完全生成...
		sH = this.getHeight();
		
		GameData.screenX = sW;
		GameData.screenY = sH;
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		new Thread(this).start();
		fpsThread();
	}

	private int fps = 0;
	private int count = 0;//計數,屏幕每刷新一次+1
	private void fpsThread() {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(!isDestroy){
					fps = count;
					count = 0;
					
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		isDestroy = true;
	}

	@Override
	public void run() {
		
		while(!isDestroy){
			canvas = sh.lockCanvas();//鎖定畫布,得到畫布
			draw();
			count++;
			sh.unlockCanvasAndPost(canvas);//提交畫好的畫布,顯示到屏幕
		}
	}

	
	/**
	 * 小人的初始位置...
	 */
	private int px = 10;
	private int py = 30;
	
	private int space = 5;//小人移動的距離
	
	/**
	 * 游戲界面的繪制
	 */
	private void draw() {
//		canvas.drawColor(Color.GRAY);
		paint.setColor(Color.GRAY);
		canvas.drawRect(0,0,sW,sH, paint);
		paint.setColor(Color.GREEN);
		canvas.drawText(String.valueOf(fps), 10, sH - 20, paint);
	
	    tickBtn();//應付事件,做相應處理
	    drawBtn();
	    
	    
	    canvas.drawBitmap(bm[8], px,py, null);//繪制小人
	    
	    drawSmile();//繪制笑臉...
	}

	/**
	 * 繪制笑臉...
	 */
	private void drawSmile() {
		
		for(int i = list.size() - 1 ; i >= 0 ; --i){
			Smile smile = list.get(i);
			
			if(smile.isDestroy()){
				list.remove(i);
			}
		}
		
		for(int i = 0 ; i < list.size() ; ++i){
			Smile smile = list.get(i);
			smile.draw(canvas);
		}
	}

	private void tickBtn() {
		switch (GameData.clickType) {
		case GameData.DOWN:
			if(!btn()){
				createSmile();
				GameData.clickType = 0;//防止笑臉一直創建...
			}
			
			break;

		case GameData.MOVE://用於處理手指一開始不在按鈕上,但是滑到了按鈕上的情況...
			btn();
			break;
			
		case GameData.UP:
			left = 0;
			top = 0;
			right = 0;
			bottom = 0;
			
			break;
		default:
			break;
		}
	}

	private void createSmile() {
		Smile smile = new Smile(bm[9],new Point(px+50,py+50),new Point(GameData.clickX,GameData.clickY),0.02f);
	    
		list.add(smile);
	}

	/**
	 * 用於點擊按鈕時,將按鈕的圖片換掉...
	 * 使用boolean作為返回值是為了解決點擊按鈕時也產生笑臉的bug。。。
	 */
	private boolean btn() {
		int x = GameData.clickX;
		int y = GameData.clickY;
		
		//判斷是否點擊了左邊按鈕
		if(10 < x && x < 10+w1 && sH-100 < y && y < sH-100+h1){
			left = 1;//用於將圖片切換掉...
			px -= space;//用於改變小人的位置
			
			return true;
		}
		
		//判斷是否點擊了上邊按鈕
		if(60 < x && x < 60+w2 && sH-150 < y && y < sH-150+h2){
			top = 1;
			py -= space;
			
			return true;
		}
		
		if(110 < x && x < 100+w1 && sH-100 < y && y < sH-100+h1){
			right = 1;
			px += space;
			
			return true;
		}
		
		if(60 < x && x < 60+w2 && sH-60 < y && y < sH-60+h2){
			bottom = 1;
			py += space;
			
			return true;
		}
		
		return false;
	}

	private int left = 0;
	private int top = 0;
	private int right = 0;
	private int bottom = 0;
	private void drawBtn() {
		canvas.drawBitmap(bm[0 + left], 10,sH-100 ,null);//繪制左邊按鈕
		canvas.drawBitmap(bm[2 + top], 60,sH-150 ,null);
		canvas.drawBitmap(bm[4 + right], 110,sH-100 ,null);
		canvas.drawBitmap(bm[6 + bottom], 60,sH-60 ,null);
		
	}

	
	
	
}


3、GameData

package com.njupt.surface;

public class GameData {

	public static int clickX = 0;
	public static int clickY = 0;
	public static int clickType = 0;//記錄當前和屏幕交換的狀態
	
	public final static int DOWN = 1;
	public final static int MOVE = 2;
	public final static int UP = 3;
	
	public static int screenX = 0;
	public static int screenY = 0;
}


4、Smile

package com.njupt.surface;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;

public class Smile {
	private Bitmap bm;
	private Point sp;//笑臉的起點
	private Point ep;//笑臉的終點
	private float space;
	
	public Smile(Bitmap bm, Point sp, Point ep, float space) {
		super();
		this.bm = bm;
		this.sp = sp;
		this.ep = ep;
		this.space = space;
		
		px = sp.x;
		py = sp.y;
	}
	
	private int px = 0;
	private int py = 0;
	public void draw(Canvas canvas){
		canvas.drawBitmap(bm, px, py,null);
		tick();
		
		if(GameData.screenX < px || px < -bm.getWidth() || py < - bm.getHeight() || py > GameData.screenY){
			isDestroy = true;
		}
	}
	
	/**
	 * 用於改變笑臉的位置
	 */
	private void tick() {
		if(ep.x > sp.x){
			px += (ep.x - sp.x)*space;
		}else if(sp.x > ep.x){
			px -= (sp.x - ep.x)*space;
		}
		
		if(ep.y > sp.y){
			py += (ep.y - sp.y)*space;
		}else{
			py -= (sp.y - ep.y)*space;
		}
	}

	
	private boolean isDestroy = false;
	public boolean isDestroy(){
		return isDestroy;
	}
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved