帶我的學長布置的第一個任務是做一個骰子的小游戲~~~~好吧,貌似不太難,我把寫好的源碼拿上來給大家分享下~
可以通過點擊按鈕或者搖一搖來改變骰子的點數~游戲裡有個小BUG,是因為一個參數的問題,我沒改,大家自己去發現吧~
既然是骰子游戲,肯定得有骰子類,按照一貫的慣例,注釋的很詳細,自己看去吧~
Android骰子游戲案例源碼 下載:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2013年資料/12月/3日/Android骰子游戲案例源碼
下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm
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();
}
}
}