实例13---点击速度测试机

阅读更多

  在原java me中通过这个例子是想要讲解RecordStore的使用,我在用了另外一种方式来实现同样的效果,就是用SharedPreferences, 然后用触屏点击事件替换了按键点击事件。

下面给出小效果图和源代码:


实例13---点击速度测试机_第1张图片


实例13---点击速度测试机_第2张图片

 MainView

package com.wjh.demon_13;

import com.wjh.midp_me.AndroidSprite;

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.Typeface;
import android.util.Log;
import android.util.MonthDisplayHelper;
import android.view.KeyEvent;
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{
	int keyCode = 0;
	String keyAction = "";
	Thread gameThread = null;
	boolean isGame = true;
	SurfaceHolder holder = null;

	
	Paint backPaint = null;
	Paint forePaint = null;
	
	//具体游戏相关
	public AndroidSprite		m_spButton;				//按钮对象
	public RMSScore 	m_Score;				//记录对象
	public int			m_nTimes = 0;			//管理时间
	public Typeface			m_Font;	
	public MainView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		setFocusable(true);
		getHolder().addCallback(this);
		holder = this.getHolder();
			
			
			backPaint = new Paint();
			backPaint.setColor(Color.BLACK);
			forePaint = new Paint();
			
			m_Score = new RMSScore(context);				//分配内存空间
			m_Score.LoadHighScore();				//读取最高分
			
			m_Font = Typeface.MONOSPACE;
				
			forePaint = new Paint();
			forePaint.setTypeface(m_Font);
			forePaint.setTextSize(20);
			forePaint.setColor(Color.WHITE);
			
			backPaint = new Paint();
			backPaint.setColor(Color.BLACK);
			
			
			Bitmap bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.button);
			m_spButton = new AndroidSprite(bitmap, 25, 25 );
			m_spButton.setFrame( 0 );
			
			
		
	}
	public void StartTime(){					//开始计时
		m_nTimes = 600;							//600*100=60000毫秒=1分钟
		m_Score.m_nCurScore = 0;
	}
	@Override
	public void run() {
		//获取系统当前时间,并将时间换算成以毫秒为单位的数
		long T1 = System.currentTimeMillis();
		long T2 = T1;
		while(isGame){
			T2 = System.currentTimeMillis();
			if( T2 - T1 > 100 ){			    //间隔100毫秒
				T1 = T2;
				input();
				logic();
				doDraw();
			}
		}
		
	}
	//开始游戏主线程
	public void start()
	{
		m_spButton.setPosition( getWidth()/2 - 12, 90 );
		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()
	{
		if( m_nTimes > 0 ){						//计时
			m_nTimes --;
			if( m_nTimes <= 0 ){				//结束游戏
				m_Score.SaveHighScore();
			}
		}
	}
	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);
		
		//设置当前颜色及当前字体
		
		StringBuffer temp 	= new StringBuffer();
		StringBuffer temp2 	= new StringBuffer();
		StringBuffer temp3 	= new StringBuffer();
		
		
		//拼接字符
		temp2.append( "最高分:" );
		temp2.append( m_Score.m_nHighScore );
		temp3.append( "本次得分:" );
		temp3.append( m_Score.m_nCurScore );
		if( m_nTimes <= 0 )
			temp.append( "滑动开始" );	
		else{
			temp.append( "剩余时间" );
			temp.append( m_nTimes / 10 );
			temp.append( "秒" );
		}
		//显示文字信息
		canvas.drawText(temp.toString(), 20, 30,forePaint);
		canvas.drawText( temp2.toString(), 20, 50, forePaint );
		canvas.drawText( temp3.toString(), 20, 70, forePaint );
		//显示按钮
		m_spButton.paint(canvas,forePaint);
		m_spButton.setFrame( 0 );
		
		
	}
	@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();
		
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		Log.d("ddd", ""+m_Score.m_nCurScore);
		if( m_nTimes <= 0 &&event.getAction() == MotionEvent.ACTION_DOWN){					//计时尚未开始
			
			StartTime();
			return false;
		}
		if(event.getAction() == MotionEvent.ACTION_DOWN)
		{
		m_Score.m_nCurScore ++;					//增加分数
		m_spButton.setFrame( 1 );
		return true;
		}else
		{
		return false;
		}
	}
	
	

}

RMSSCore.java

package com.wjh.demon_13;

import android.content.Context;
import android.content.SharedPreferences;

public class RMSScore {
	public int m_nHighScore = 0; // 历史最高分
	public int m_nCurScore = 0;// 当前得分
	private SharedPreferences sharedPreferences = null;
	
	public RMSScore(Context context)
	{
		sharedPreferences = context.getSharedPreferences("RsScore", Context.MODE_PRIVATE);
	}

	/********* 利用RMS系统存储最高得分 ************/
	public void SaveHighScore() {

		if (m_nCurScore <= m_nHighScore) // 如果本次得分低于历史最高分
		{
			return; 
		}// 直接返回
		m_nHighScore = m_nCurScore; // 刷新记录
		
		// 打开SharedPreferences,没有则新建
		
		this.sharedPreferences.edit().putInt("highScore", m_nHighScore).commit(); // 更改第一条记录
	}

	/********* 利用RMS系统读取最高得分 ************/
	public void LoadHighScore() {
		m_nHighScore = this.sharedPreferences.getInt("highScore", -1);
	}
}

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

源代码文件:Demon_13.rar

  • 实例13---点击速度测试机_第3张图片
  • 大小: 2.6 KB
  • 实例13---点击速度测试机_第4张图片
  • 大小: 2.6 KB
  • Demon_13.zip (22 KB)
  • 下载次数: 0
  • Demon_13.rar (72.9 KB)
  • 下载次数: 3
  • 查看图片附件

你可能感兴趣的:(Android,游戏,C,C++,C#)