带我的学长布置的第一个任务是做一个骰子的小游戏~~~~好吧,貌似不太难,我把写好的源码拿上来给大家分享下~
可以通过点击按钮或者摇一摇来改变骰子的点数~游戏里有个小BUG,是因为一个参数的问题,我没改,大家自己去发现吧~
既然是骰子游戏,肯定得有骰子类,按照一贯的惯例,注释的很详细,自己看去吧~
Android骰子游戏案例源码 下载:
具体下载目录在 /2013年资料/12月/3日/Android骰子游戏案例源码
package com.syxy.dice;
import java.util.Random;
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;
public class Dice {
private Bitmap diceBitmap; //骰子的图片引用
private Bitmap tempBitmap; //骰子的临时引用
int diceWidth; //骰子的宽(6个面的骰子在一张图上画)
int diceHeight; //骰子的高
int avgWidth; //骰子图片的平均宽度
// int x, y; //骰子显示的坐标
private int nowNum; //骰子的当前值
private Random random; //获取随即对象
Paint paint; //画笔的引用
public Dice(Resources res){
paint = new Paint();
paint.setAntiAlias(true); //消除画笔的锯齿
initBitmap(res); //初始化骰子图片
diceWidth = diceBitmap.getWidth(); //初始化图片宽度
diceHeight = diceBitmap.getHeight(); //初始化图片高度
avgWidth = diceWidth / 6; //初始化平均宽度
nowNum = -1;
random = new Random();
System.out.println("diceWidth=" + diceWidth +", diceHeight" + diceHeight + ", avgWidth" + avgWidth);
// x = MySurfaceView.screen_width/2 - avgWidth/2;
// y = MySurfaceView.screen_height/3 - diceHeight/2;
}
public void initBitmap(Resources res){
diceBitmap = BitmapFactory.decodeResource(res, R.drawable.dice);
}
private Bitmap getbitmap() { //根据序号截取大图上相应骰子数图片
return Bitmap.createBitmap(diceBitmap, avgWidth*(nowNum+1), 0, avgWidth, diceHeight);
}
public void playDice(Canvas canvas, int x, int y){ //随即绘制骰子
nowNum = random.nextInt(5);
tempBitmap = getbitmap();
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(tempBitmap, x, y, paint);
}
public void DrawDice(Canvas canvas, int x, int y){ //绘制一个固定的骰子,即值为1的骰子,在投骰子的游戏开始前绘制
tempBitmap = getbitmap();
canvas.drawColor(Color.WHITE);
System.out.println("----------------1-----------------");
canvas.drawBitmap(tempBitmap,x, y, paint);
}
}
既然是游戏,那么游戏的视图类总得有的~
package com.syxy.dice;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
public class MySurfaceView extends SurfaceView implements Callback, Runnable {
static int screen_width; //设备屏幕的宽
static int screen_height; //设备屏幕的高
int Dice_x;
int Dice_y;
SurfaceHolder holder; //取得surfaceView的控制类的引用
Thread th; //取得线程类的引用
boolean flag; //线程执行的标志位
boolean isRun; //控制骰子的投掷
Canvas canvas = null;
Dice dice; //骰子的引用
private int sleepSpan = 150; //线程休眠时间30
int i; //用于循环的
Handler mHandler;
public MySurfaceView(Context context, AttributeSet attr) {
super(context, attr);
dice = new Dice(this.getResources());
holder = this.getHolder(); //取得控制类
holder.addCallback(this); //取得surfaceView的监视器
th = new Thread(this); //实例化线程
// TODO Auto-generated constructor stub
}
@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
screen_width = this.getWidth(); //取得屏幕宽度
screen_height = this.getHeight(); //取得屏幕高度
Dice_x =(int)screen_width/2 - (int)dice.avgWidth/2; //计算骰子的X位置
Dice_y =(int)screen_height/3 - (int)dice.diceHeight/2; //计算骰子的Y位置
// Dice_x = 300;
// Dice_y =200;
System.out.println("screen_width=" + screen_width + ", screen_height=" + screen_height + ", Dice_x=" + Dice_x + ", Dice_y=" + Dice_y);
flag = true; //给flag设置初始值
th.start(); //启动线程
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void run(){
while(flag){
if(isRun){ //用判断语句控制是否进入骰子的投掷过程
for(i = 1; i <= 10; i++){ //随机取骰子值10次,完成投掷,建立此循环的目的是为了让线程持续执行,让投掷可控,
canvas = holder.lockCanvas(); //取得surfaceView的canvas
dice.playDice(canvas, Dice_x, Dice_y); //随机投掷骰子
if(canvas != null){
holder.unlockCanvasAndPost(canvas); //释放holder的画布
}
}
isRun = false;
}
}
try{
Thread.sleep(sleepSpan);
}catch(Exception e){
e.printStackTrace();
}
}
}