Android天天数钱游戏源码

天天数钱游戏Android源码,很有意思的一款游戏,废话不多说,上源码!

天天数钱。基于Android游戏代码

模仿Flash游戏http://www.4399.com/flash/145326_4.htm

源码下载 :http://pan.baidu.com/s/1ntn2Iwx

部分代码 GameView.java

<span style="font-family:Arial;font-size:14px;">package com.sx.view;
 
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
 
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.sx.ttsqian.R;
 
public class GameView extends RelativeLayout implements OnClickListener {
 
	private TimeInterface jiekou;
	private TextView tv_addtime;
	private boolean play_music = true;
	private float userMony = 0.0F;
 
	public void setInterface(TimeInterface _jiekou) {
		this.jiekou = _jiekou;
	}
 
	public boolean setMusicState() {
		play_music = !play_music;
		if (play_music == false) {
 
			if (mp != null && mp.isPlaying()) {
				mp.pause();
			}
		} else {
			if (mp != null) {
				mp.reset();
				mp.release();
			}
			mp = MediaPlayer.create(mContext, R.raw.beijing);
			mp.setLooping(true);
			try {
				mp.prepare();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			mp.start();
		}
 
		return play_music;
	}
 
	private Techniques[] anim = new Techniques[] { Techniques.ZoomOutRight,
			Techniques.ZoomOutLeft, Techniques.ZoomOutUp,
			Techniques.ZoomOutDown, Techniques.SlideOutUp, Techniques.FadeOut,
			Techniques.FadeOutRight, Techniques.RollOut,
			Techniques.RotateOutDownLeft, Techniques.TakingOff };
	private ImageView img_1, img_2, img_3, img_4;
	private Context mContext;
	private LayoutInflater inflater;
	private Matrix matrix = new Matrix();
	private MediaPlayer mp;
	private MediaPlayer mp_great;
	private MediaPlayer mp_error;
	private float[] price = new float[] { 1.0F, 10.0F, 100.0F, 20.0F, 5.0F,
			50.0F, 0.5F, 1.0F, 0.5F, 0.1F, 0.1F, 10.0F, 0.2F, 10.0F };
	private int[] picID = new int[] { R.drawable.kagaz_1, R.drawable.kagaz_10,
			R.drawable.kagaz_100, R.drawable.kagaz_20, R.drawable.kagaz_5,
			R.drawable.kagaz_50, R.drawable.kagaz_5mo, R.drawable.kagaz_kona_1,
			R.drawable.kagaz_5motatur, R.drawable.kagaz_1mo,
			R.drawable.kagaz_1motatur, R.drawable.kagaz_kona10,
			R.drawable.kagaz_2mo, R.drawable.kagaz_10tatur };
	private float allPrice = 0.0f;
	private int TIME = 45 * 1000;
	Timer mTimer;
	MyTimerTask mTimerTask;
	private TextView tv1, tv2, tv3, tv4;
	private ImageView img_good;
 
	@SuppressLint("NewApi")
	public GameView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		initView();
	}
 
	public GameView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		initView();
	}
 
	public GameView(Context context) {
		super(context);
		this.mContext = context;
		initView();
 
	}
 
	class MyTimerTask extends TimerTask {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			mhandler.sendEmptyMessage(-1);
 
		}
 
	}
 
	Handler mhandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
 
			try {
				if (msg.what == 1) {
					jiekou.gameOver(userMony);
 
				} else {
					TIME = TIME - 246;
					if (TIME <= 0) {
						jiekou.updateTime(0);
						mhandler.removeMessages(-1);
						mhandler.sendEmptyMessage(1);
						return;
					}
					jiekou.updateTime(TIME);
					mTimer = new Timer();
					// game over...
					if (mTimer != null) {
						if (mTimerTask != null) {
							mTimerTask.cancel(); // 将原任务从队列中移除
						}
 
						mTimerTask = new MyTimerTask(); // 新建一个任务
						mTimer.schedule(mTimerTask, 246);
					}
				}
			} catch (Exception ex) {
 
			}
 
		}
 
	};
 
	private void initView() {
		userMony = 0.0F;
		LayoutInflater.from(mContext).inflate(R.layout.gameview, this, true);
		mp = MediaPlayer.create(mContext, R.raw.beijing);
		mp_great = MediaPlayer.create(mContext, R.raw.great);
		mp_error = MediaPlayer.create(mContext, R.raw.shibai);
		mp.setLooping(true);
		try {
			mp.prepare();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			mp_great.prepare();
		} catch (Exception ex) {
 
		}
		try {
			mp_error.prepare();
		} catch (Exception ex) {
 
		}
 
		 mp.start();
		img_1 = (ImageView) findViewById(R.id.img_1);
		img_2 = (ImageView) findViewById(R.id.img_2);
		img_3 = (ImageView) findViewById(R.id.img_3);
		img_4 = (ImageView) findViewById(R.id.img_4);
		tv1 = (TextView) findViewById(R.id.tv1);
		tv2 = (TextView) findViewById(R.id.tv2);
		tv3 = (TextView) findViewById(R.id.tv3);
		tv4 = (TextView) findViewById(R.id.tv4);
		tv1.setOnClickListener(this);
		tv2.setOnClickListener(this);
		tv3.setOnClickListener(this);
		tv4.setOnClickListener(this);
 
		img_good = (ImageView) findViewById(R.id.img_good);
		tv_addtime = (TextView) findViewById(R.id.txt_addtime);
		img_good.setVisibility(View.INVISIBLE);
		tv_addtime.setVisibility(View.INVISIBLE);
		CreateNewGameView(false);
	}
 
	public void CreateNewGameView(boolean isOk) {
		if (play_music && isOk) {
			mp_great.start();
		} else if (play_music && !isOk) {
			mp_error.start();
		}
 
		Random r = new Random();
		Random r2 = new Random();
		int MaxValue = 10;
		int index = 0;
		allPrice = 0.0f;
		index = r.nextInt(picID.length);
		Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(
				picID[index])).getBitmap();
		allPrice += price[index];
		// 设置旋转角度
		int r2_value = r2.nextInt(MaxValue);
		if (r2_value % 2 == 0) {
			r2_value = r2_value * (-1);
		}
		matrix.setRotate(r2_value);
		// 重新绘制Bitmap
 
		bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
				bitmap.getHeight(), matrix, true);
		img_1.setImageBitmap(bitmap);
		index = r.nextInt(picID.length);
		bitmap = ((BitmapDrawable) getResources().getDrawable(picID[index]))
				.getBitmap();
		allPrice += price[index];
 
		// 设置旋转角度
		r2_value = r2.nextInt(MaxValue);
		if (r2_value % 2 == 0) {
			r2_value = r2_value * (-1);
		}
		matrix.setRotate(r2_value);
		// 重新绘制Bitmap
		bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
				bitmap.getHeight(), matrix, true);
		img_2.setImageBitmap(bitmap);
		index = r.nextInt(picID.length);
		bitmap = ((BitmapDrawable) getResources().getDrawable(picID[index]))
				.getBitmap();
		allPrice += price[index];
		// 设置旋转角度
		r2_value = r2.nextInt(MaxValue);
		if (r2_value % 2 == 0) {
			r2_value = r2_value * (-1);
		}
		matrix.setRotate(r2_value);
		// 重新绘制Bitmap
		bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
				bitmap.getHeight(), matrix, true);
		img_3.setImageBitmap(bitmap);
		index = r.nextInt(picID.length);
		bitmap = ((BitmapDrawable) getResources().getDrawable(picID[index]))
				.getBitmap();
		allPrice += price[index];
		// 设置旋转角度
		r2_value = r2.nextInt(MaxValue);
		if (r2_value % 2 == 0) {
			r2_value = r2_value * (-1);
		}
		matrix.setRotate(r2_value);
		// 重新绘制Bitmap
		bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
				bitmap.getHeight(), matrix, true);
		img_4.setImageBitmap(bitmap);
		mhandler.sendEmptyMessageDelayed(-1, 500);
		System.out.println("All Price is:" + allPrice);
		ArrayList result = getPrice(allPrice);
		// allPrice=(float)(Math.round(allPrice*100)/100);
		int tv_r_index = r2.nextInt(4);
		if (tv_r_index == 0) {
			tv1.setText(result.get(0) + "元");
			tv2.setText(result.get(1) + "元");
			tv3.setText(result.get(2) + "元");
			tv4.setText(result.get(3) + "元");
		} else if (tv_r_index == 1) {
			tv1.setText(result.get(1) + "元");
			tv2.setText(result.get(0) + "元");
			tv3.setText(result.get(2) + "元");
			tv4.setText(result.get(3) + "元");
		} else if (tv_r_index == 2) {
			tv1.setText(result.get(2) + "元");
			tv2.setText(result.get(1) + "元");
			tv3.setText(result.get(0) + "元");
			tv4.setText(result.get(3) + "元");
		} else if (tv_r_index == 3) {
			tv1.setText(result.get(3) + "元");
			tv2.setText(result.get(1) + "元");
			tv3.setText(result.get(2) + "元");
			tv4.setText(result.get(0) + "元");
		}
 
		// Random r2=new Random();
 
		if (isOk) {
			img_good.setVisibility(View.VISIBLE);
			tv_addtime.setVisibility(View.VISIBLE);
			index = r2.nextInt(anim.length);
			YoYo.with(anim[index]).duration(800).playOn(img_good);
			tv_addtime.setText("+1750");
			YoYo.with(Techniques.SlideOutUp).duration(1000).playOn(tv_addtime);
		} else {
			if (tv_addtime.getVisibility() != View.INVISIBLE) {
				tv_addtime.setText("-750");
				YoYo.with(Techniques.SlideOutUp).duration(1000)
						.playOn(tv_addtime);
			}
		}
	}
 
	private ArrayList getPrice(float price) {
 
		float price4 = price - 0.1F;
		ArrayList result = new ArrayList();
		DecimalFormat decimalFormat = new DecimalFormat(".00");// 构造方法的字符格式这里如果小数不足2位,会以0补足.
		String p = decimalFormat.format(price);// format 返回的是字符串
		System.out.println(p);
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < p.length(); i++) { 			if (p.charAt(i) != ‘.‘ && p.charAt(i) != ‘0‘) { 				buf.append(p.charAt(i)); 			} 		} 		result.add(p); 		String newPrice = buf.toString(); 		float newfloat = Float.parseFloat(newPrice); 		if (newfloat > 400.00F) {
			newfloat = newfloat / 100.0F;
			newPrice = decimalFormat.format(newfloat);
		}
		result.add(newPrice);
		newPrice = "";
		if (buf.length() == 3) {
			try {
				newPrice = buf.substring(1, 1) + buf.substring(2)
						+ buf.substring(0, 1) + "." + buf.substring(0, 1) + "0";
			} catch (Exception ex) {
				System.out.println(ex.getLocalizedMessage());
			}
		} else if (buf.length() == 2) {
			newPrice = buf.substring(1) + (new Random()).nextInt(9)
					+ buf.substring(0, 1) + "." + buf.substring(1) + "0";
		} else if (buf.length() == 4) {
			newPrice = buf.substring(3) + buf.toString().substring(2, 3)
					+ buf.substring(0, 1) + "." + buf.substring(1, 1) + "0";
		} else {
			newPrice = buf.substring(0, 1) + (new Random()).nextInt(9) + "."
					+ (new Random()).nextInt(9) + "0";
		}
		newfloat = Float.parseFloat(newPrice);
		if (newfloat > 400.00F) {
			newfloat = newfloat / 100.0F;
			newPrice = decimalFormat.format(newfloat);
		}
		result.add(newPrice);
		String p4 = decimalFormat.format(price4);
		result.add(p4);
		return result;
	}
 
	public void DestreyView() {
		try {

			mhandler.removeMessages(-1);
			if(mTimerTask!=null)
			{
				mTimerTask.cancel();
			}
			if (mp != null) {
				mp.reset();
				mp.release();
			}
			if (mp_great != null) {
				mp_great.release();
				mp_great = null;
			}
		} catch (Exception ex) {
 
		}
	}
 
	@Override
	public void onClick(View arg0) {
 
		DecimalFormat decimalFormat = new DecimalFormat(".00");// 构造方法的字符格式这里如果小数不足2位,会以0补足.
		String p = decimalFormat.format(allPrice);
		String selectText = "";
		int id = arg0.getId();
		if (id > 0) {
			switch (id) {
			case R.id.tv1:
				selectText = tv1.getText().toString().trim().replace("元", "");
				break;
			case R.id.tv2:
				selectText = tv2.getText().toString().trim().replace("元", "");
				break;
			case R.id.tv3:
				selectText = tv3.getText().toString().trim().replace("元", "");
				break;
			case R.id.tv4:
				selectText = tv4.getText().toString().trim().replace("元", "");
				break;
			default:
				break;
			}
			float newfloat = Float.parseFloat(selectText);
			float current = Float.parseFloat(p);
			if (newfloat == current) {
				CreateNewGameView(true);
				TIME += 1750;
				userMony += current;
				jiekou.updateMony(userMony);
			} else {
				CreateNewGameView(false);
				TIME -= 750;
			}
		}
 
	}
 
}
 </span>
时间: 2024-08-24 09:45:45

Android天天数钱游戏源码的相关文章

Android版Flood it游戏源码

flood_it 流行的Flood it游戏的Android版 游戏玩法: 按下屏幕下方的颜色按钮,左上角的色块就会变颜色.只要把整片色块变成同色就赢了. 效果图<ignore_js_op> <ignore_js_op> 源码项目下载:http://code.662p.com/view/4877.html 详细说明:http://android.662p.com/thread-3245-1-1.html

Android掌中游斗地主游戏源码完整版

源码大放送-掌中游斗地主(完整版),集合了单机斗地主.网络斗地主.癞子斗地主等,有史以来最有参考价值的源码,虽然运行慢了一点但是功能正常,用的是纯java写的. 项目详细说明:http://android.662p.com <ignore_js_op> [/td][td] <ignore_js_op> [/td][td] <ignore_js_op> [/td][td] <ignore_js_op> [/td][td] <ignore_js_op>

android猜猜红桃A游戏源码

一个简单的猜扑克游戏,很适合初学者,点击TextView扑克由反面变为正面,未点击的TextView有透明效果,点击再玩一次按钮,运行洗牌程序undefined <ignore_js_op><ignore_js_op> 详细说明:http://android.662p.com/thread-876-1-1.html android猜猜红桃A游戏源码,布布扣,bubuko.com

飞行熊猫游戏源码android版

这款游戏是前一段时间完成的一个项目,飞行熊猫游戏源码android版,飞行熊猫基于cocos2d游戏引擎开发,包括了谷歌admob广告支持,没有任何版权问题,大家可以自由修改和上传应用商店. 1.本游戏支持无限关卡. 2.源码包也包括了游戏PSD格式资源文件,可自行修改图片. 3.自适应各种屏幕分辨率, 支持android2.1+ 源码下载: http://code.662p.com/view/6682.html <ignore_js_op> <ignore_js_op> <

Android俄罗斯方块游戏源码

Android俄罗斯方块游戏源码,Android经典游戏源码,不容错误,代码详细,适合想要搞游戏开发的人进行学习,直接可以跑,源码有超详细的注释. 玩着俄罗斯方块的的时候有背景歌曲.[最好使用真机测试,不然可能会报错] 简单代码如下,注释可谓详尽至极啊:        Handler m_handler = null;//负责接收消息        Timer timer = null;//负责发送消息        Data data = null;//存放游戏的数据,即二维数组,并负责二维数

非常不错的异形狙杀战android游戏源码

给大家分享一款非常不错的异形狙杀战android游戏源码,异形狙杀战  购买不同的枪支有不同的效果,手雷威力很大不过一局只能用5次,阻止异形入侵的你领土是你的目的来吧 源码下载:http://code.662p.com/view/2269.html    <ignore_js_op> 详细说明:http://android.662p.com/thread-287-1-1.html

继续群发Android游戏源码(再发15款)

续群发Android游戏源码(再发15个,依旧以andengine和libgdx开发的为主.话说开源游戏发布者主要集中在欧美用户群中,而欧美那边Java系又主要用这两款引擎,所以暂时只能群发此二者开发的游戏.另外,Android上SDL开发的游戏源码竟意外的多,有空时小弟再集中发布下SDL的,不过,这货基本算纯C/C++了--) -------------------------- 游戏运行实际效果如下图所示:(一张张传图太麻烦,干脆拼成一幅,但源码包中是按照工程名细分配图的) 下载地址1(微软

android五子棋游戏源码

这个游戏案例是android五子棋游戏源码,也是自己最近做的android五子棋游戏,现在拿出来给大家看看一下.因为初学,所以只做了魅族屏幕的,不过这个没有影响大家的学习和运行,喜欢的朋友可以在这个的基础上进行升级或更新吧. 项目源码下载:http://code.662p.com/view/6853.html <ignore_js_op> <ignore_js_op> 详细说明:http://android.662p.com/thread-5294-1-1.html

Android游戏源码合集(主要是AndEngine和Libgdx的)

近在网络上看到有网友抱怨Android游戏源码找不到,所以小弟收集了一些AndEngine和Libgdx的游戏源码,以Eclipse工程的形式配置好环境,再陆续发出(某引擎避嫌,不在此列). 虽说这些游戏,主要是由Libgdx与AndEngine开发的源码组成.但事实上,能算游戏的,开源的,举凡有点价值的Android游戏源码,小弟也会陆续收集(比如Replicaisland没用任何引擎,可代码有价值,所以这次也放进来了),更会在博客中一一给出. 不过,有两类游戏不在此列,一是游戏源码虽然乍看下