码迷,mamicode.com
首页 > 移动开发 > 详细

安卓游戏 收集钱袋(自制)

时间:2015-07-27 10:52:52      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

本人对游戏这一块很有兴趣,刚好 又兼着学安卓,所以就学着做了下安卓的 游戏

源码位置

链接: http://pan.baidu.com/s/1eQ4gdI6 密码: g58e

本文章是自己学习和记录用,喜欢的人可以阅读,写的不好的勿喷

开始,安卓基本的东西和环境的搭建就不细说了,先进行游戏的基本分析:

需要的元素:钱 钱袋 画板

小游戏,仅仅是机能的一些测试,所以没有做的很好,

个人喜欢做游戏的原因就是因为游戏和现实的世界非常的贴近,能够用现实的想法去思考游戏

比如本游戏:首先,钱肯定是默认的情况下就不停的运动的,然后钱袋,由玩家去控制,也能进行移动,当钱和钱袋接触,进行加分,并且钱消失,就是这么一段简单的想法

主Activity

package com.rp;



import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//设置全屏
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//显示自定义的SurfaceView视图
		setContentView(new MySurfaceView(this));
	}
}

 钱这个类

package com.rp;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

public class Money {
	//定义钱币
	private Rect money;
	public int moneyX=0,moneyY=0; //钱包的坐标
	public int speed=2;//钱币的下落速度
	public int height;
	private boolean isdeath=false;

	private int cnt;//分数
	
	public Money(int moneyX,int speed,int height){
		this.moneyX  = moneyX;
		this.speed = speed;
		this.height = height;
		this.cnt = speed;
	}
	
	
	public void DrawMoney(Canvas canvas,Paint paint){
		money = new Rect(moneyX, moneyY, moneyX+10, moneyY+20);
		canvas.drawRect(money, paint);
	}
	//钱的逻辑
	/***
	 * 自由下落  当过界的时候重新运动
	 */
	public void logic(){
		
		if(isdeath()){
			moneyY =0;
		}else{
			moneyY +=speed;
		}
		
	}
	/***
	 * 死亡函数
	 * 当钱过界,或者遇到钱袋即死亡
	 */
	public boolean isdeath(){
		if(this.moneyY>=this.height){
			isdeath = true;
		}else{
			isdeath = false;
		}
		return isdeath;
		
	}
	public int getCnt() {
		return cnt;
	}


	public void setCnt(int cnt) {
		this.cnt = cnt;
	}
	

}

  这里加入了一个摇杆系统,没特别作用,好看罢了

package com.rp;

import android.graphics.Canvas;
import android.graphics.Paint;

public class Yaogan {
	//定义两个圆形的中心点坐标与半径
	public float smallCenterX = 40, smallCenterY = 490, smallCenterR = 20;
	public float BigCenterX = 40, BigCenterY = 490, BigCenterR = 40;
	
	
	public void DrawYaogan(Canvas canvas,Paint paint){
		
		//绘制大圆
		canvas.drawCircle(BigCenterX, BigCenterY, BigCenterR, paint);
		//绘制小圆
		canvas.drawCircle(smallCenterX, smallCenterY, smallCenterR, paint);
		
	}
	
	/** 
	 * 小圆针对于大圆做圆周运动时,设置小圆中心点的坐标位置
	 * @param centerX 
	 *            围绕的圆形(大圆)中心点X坐标
	 * @param centerY 
	 *            围绕的圆形(大圆)中心点Y坐标
	 * @param R
	 * 			     围绕的圆形(大圆)半径
	 * @param rad 
	 *            旋转的弧度 
	 */
	public void setSmallCircleXY(float centerX, float centerY, float R, double rad) {
		//获取圆周运动的X坐标   
		smallCenterX = (float) (R * Math.cos(rad)) + centerX;
		//获取圆周运动的Y坐标  
		smallCenterY = (float) (R * Math.sin(rad)) + centerY;
	}
	
	/**
	 * 得到两点之间的弧度
	 * @param px1    第一个点的X坐标
	 * @param py1    第一个点的Y坐标
	 * @param px2    第二个点的X坐标
	 * @param py2    第二个点的Y坐标
	 * @return
	 */
	public double getRad(float px1, float py1, float px2, float py2) {
		//得到两点X的距离  
		float x = px2 - px1;
		//得到两点Y的距离  
		float y = py1 - py2;
		//算出斜边长  
		float Hypotenuse = (float) Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
		//得到这个角度的余弦值(通过三角函数中的定理 :邻边/斜边=角度余弦值)  
		float cosAngle = x / Hypotenuse;
		//通过反余弦定理获取到其角度的弧度  
		float rad = (float) Math.acos(cosAngle);
		//当触屏的位置Y坐标<摇杆的Y坐标我们要取反值-0~-180  
		if (py2 < py1) {
			rad = -rad;
		}
		return rad;
	}

}

 主要调用类

package com.rp;

import java.util.ArrayList;

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.graphics.Rect;
import android.graphics.Region;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

/**
 * 
 * @author sunzhiyan
 * @version 0.1
 *
 */
public class MySurfaceView extends SurfaceView implements Callback, Runnable {
	private SurfaceHolder sfh;
	private Paint paint;
	private Thread th;
	private boolean flag;
	private Canvas canvas;
	private int screenW, screenH=532;
	private int x=100,y=400;//手指触屏的坐标
	private Yaogan yaogan;
	//定义一个音乐播放器
	private MediaPlayer mediaplay;
	//**********************************************
	private Rect rect;
	private Money money;
	private Region reg;
	//定义一个主角
	
	//定义一套钱币
	private ArrayList<Money> arrayList =  new ArrayList<Money>();;
	
	private int mx=0,my=0;
	private int count;
	/**
	 * SurfaceView初始化函数
	 */
	public MySurfaceView(Context context) {
		super(context);
		sfh = this.getHolder();
		sfh.addCallback(this);
		paint = new Paint();
		paint.setColor(Color.RED);
		paint.setAntiAlias(true);
		//调用游戏初始化
		initGame(context);
		setFocusable(true);
	}
		
	//初始化游戏函数
	public void initGame(Context context){
		//实例化一个遥杆
		yaogan = new Yaogan();
		//调用一个音乐文件
		 mediaplay = MediaPlayer.create(context, R.raw.bgmusic);
		//设置循环播放
		mediaplay.setLooping(true);
		//音乐的再次接着播放
		mediaplay.start();
		
		//定义一套钱币
		
		Money money01 = new Money((mx+0)*50,5,screenH);
		Money money02 = new Money((mx+1)*50,15,screenH);
		Money money03 = new Money((mx+2)*50,10,screenH);
		Money money04 = new Money((mx+4)*50,5,screenH);
		Money money05 = new Money((mx+5)*50,5,screenH);
		Money money06 = new Money((mx+6)*50,5,screenH);
		
		arrayList.add(money01);
		arrayList.add(money02);
		arrayList.add(money03);
		arrayList.add(money04);
		arrayList.add(money05);
		arrayList.add(money06);
		
		
		 
	}

	/**
	 * SurfaceView视图创建,响应此函数
	 */
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		
		flag = true;
		//实例线程
		th = new Thread(this);
		//启动线程
		th.start();
	}

	/**
	 * 游戏绘图
	 */
	public void myDraw() {
		try {
			canvas = sfh.lockCanvas();
			if (canvas != null) {
				canvas.drawColor(Color.WHITE);				
				//绘制背景
				//Bitmap background = BitmapFactory.decodeResource(this.getResources(), R.drawable.background);
				//canvas.drawBitmap(background,0,0, paint);
				paint.setAlpha(0x77);
				
				//绘制一个钱币

				//取得鼠标的中心点 绘制主角
				rect = new Rect(x-20, y-20, x+20, y+20);
				reg = new Region(rect);

				for (int i = 0; i < arrayList.size(); i++) {
					//money.logic();
					arrayList.get(i).DrawMoney(canvas, paint);
					if(reg.contains(arrayList.get(i).moneyX, arrayList.get(i).moneyY)){
						count = count+arrayList.get(i).getCnt();
						//目标死亡
						arrayList.get(i).moneyY = 0;
						paint.setColor(Color.BLUE);
					}
				}	
								
/*				for (int i = 0; i < 7; i++) {
					 money = new Money();
					 money.DrawMoney(canvas, paint,(mx+i)*50,i+2);
					 arrayList.add(money);
				}	*/
				
		/*		if(reg.contains(moneyX, moneyY)){
					paint.setColor(Color.BLUE);
				}else{
				}*/

				
				//判断碰撞

				
				canvas.drawRect(rect, paint);
				
				//遥感
				yaogan.DrawYaogan(canvas,paint);

				//不停的对坐标进行监控
				canvas.drawText("x="+x+"y="+y, 100, 480, paint);
				
				//
				canvas.drawText("您的积分是"+count, 230, 500, paint);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (canvas != null)
				sfh.unlockCanvasAndPost(canvas);
		}
	}

	/**
	 * 触屏事件监听
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		//当用户手指抬起,应该恢复小圆到初始位置
		if (event.getAction() == MotionEvent.ACTION_UP) {
			yaogan.smallCenterX = yaogan.BigCenterX;
			yaogan.smallCenterY = yaogan.BigCenterY;
		} else {
			int pointX = (int) event.getX();
			int pointY = (int) event.getY();
			x =  pointX;
			y = pointY;
			//判断用户点击的位置是否在大圆内
			if (Math.sqrt(Math.pow((yaogan.BigCenterX - (int) event.getX()), 2) + Math.pow((yaogan.BigCenterY - (int) event.getY()), 2)) <= yaogan.BigCenterR) {
				//让小圆跟随用户触点位置移动
				yaogan.smallCenterX = pointX;
				yaogan.smallCenterY = pointY;
			} else {
				yaogan.setSmallCircleXY(yaogan.BigCenterX, yaogan.BigCenterY,yaogan.BigCenterR, yaogan.getRad(yaogan.BigCenterX, yaogan.BigCenterY, pointX, pointY));
			}
		}
		return true;
	}



	/**
	 * 按键事件监听
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}


	@Override
	public void run() {
		while (flag) {
			long start = System.currentTimeMillis();
			myDraw();
			logic();
			long end = System.currentTimeMillis();
			try {
				if (end - start < 50) {
					Thread.sleep(50 - (end - start));
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * SurfaceView视图状态发生改变,响应此函数
	 */
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
	}

	/**
	 * SurfaceView视图消亡时,响应此函数
	 */
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		flag = false;
	}
	/**
	 * 游戏逻辑
	 */
	private void logic() {
		for (int i = 0; i < arrayList.size(); i++) {
			//money.logic();
			arrayList.get(i).logic();
		}	
	}
}

基本代码已经给出,也没什么素材,代码本人亲测成功的,后期有打算完善的可能性,当然还是得需要一个美工得协助

安卓游戏 收集钱袋(自制)

标签:

原文地址:http://www.cnblogs.com/sunxun/p/4679252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!