package wyf.ytl;
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.media.MediaPlayer;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
* 该类是整个程序最主要的类,是主游戏的界面
* 该界面继承自SurfaceView并实现了SurfaceHolder.Callback接口
* 其中包含了一个刷帧的线程类
*
*/
public class GameView extends SurfaceView implements SurfaceHolder.Callback{
private TutorialThread thread;//刷帧的线程
TimeThread timeThread ;
ChessActivity activity;//声明Activity的引用
Bitmap qiPan;//棋盘
Bitmap qizibackground;//棋子的背景图片
Bitmap win;//胜利的图片
Bitmap lost;//失败的图片
Bitmap ok;//确定按钮
Bitmap vs;//黑方红方VS的图片
Bitmap right;//向右的指针
Bitmap left;//向左的指针
Bitmap current;//“当前”文字
Bitmap exit2;//退出按钮图片
Bitmap sound2;//声音按钮图片
Bitmap sound3;//当前是否播放了声音
Bitmap time;//冒号
Bitmap redtime;//红色冒号
Bitmap background;//背景图片
MediaPlayer go;//下棋声音
Paint paint;//画笔
boolean caiPan = true;//是否为玩家走棋
boolean focus = false;//当前是否有选中的棋子
int selectqizi = 0; //当然选中的棋子
int startI, startJ;//记录当前棋子的开始位置
int endI, endJ;//记录当前棋子的目标位置
Bitmap[] heiZi = new Bitmap[7];//黑子的图片数组
Bitmap[] hongZi = new Bitmap[7];//红子的图片数组
Bitmap[] number = new Bitmap[10];//数字的图片数组,用于显示时间
Bitmap[] redNumber = new Bitmap[10];//红色数字的图片,用于显示时间
GuiZe guiZe;//规则类
int status = 0;//游戏状态。0游戏中,1胜利, 2失败
int heiTime = 0;//黑方总共思考时间
int hongTime = 0;//红方总共思考时间
int[][] qizi = new int[][]{//棋盘
{2,3,6,5,1,5,6,3,2},
{0,0,0,0,0,0,0,0,0},
{0,4,0,0,0,0,0,4,0},
{7,0,7,0,7,0,7,0,7},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{14,0,14,0,14,0,14,0,14},
{0,11,0,0,0,0,0,11,0},
{0,0,0,0,0,0,0,0,0},
{9,10,13,12,8,12,13,10,9},
};
public GameView(Context context,ChessActivity activity) {//构造器
super(context);
this.activity = activity;//得到Activity的引用
getHolder().addCallback(this);
go = MediaPlayer.create(this.getContext(), R.raw.go);//加载下棋的声音
this.thread = new TutorialThread(getHolder(), this);//初始化刷帧线程
this.timeThread = new TimeThread(this);//初始化思考时间的线程
init();//初始化所需资源
guiZe = new GuiZe();//初始化规则类
}
public void init(){//初始化方法
paint = new Paint();//初始化画笔
qiPan = BitmapFactory.decodeResource(getResources(), R.drawable.qipan);//棋盘图片
qizibackground = BitmapFactory.decodeResource(getResources(), R.drawable.qizi);//棋子的背景
win = BitmapFactory.decodeResource(getResources(), R.drawable.win);//胜利的图片
lost = BitmapFactory.decodeResource(getResources(), R.drawable.lost);//失败的图片
ok = BitmapFactory.decodeResource(getResources(), R.drawable.ok);//确定按钮图片
vs = BitmapFactory.decodeResource(getResources(), R.drawable.vs);//vs字样的图片
right = BitmapFactory.decodeResource(getResources(), R.drawable.right);//向右的指针
left = BitmapFactory.decodeResource(getResources(), R.drawable.left);//向左的指针
current = BitmapFactory.decodeResource(getResources(), R.drawable.current);//文字“当前”
exit2 = BitmapFactory.decodeResource(getResources(), R.drawable.exit2);//退出按钮图片
sound2 = BitmapFactory.decodeResource(getResources(), R.drawable.sound2);//声音按钮图片
time = BitmapFactory.decodeResource(getResources(), R.drawable.time);//黑色冒号
redtime = BitmapFactory.decodeResource(getResources(), R.drawable.redtime);//红色冒号
sound3 = BitmapFactory.decodeResource(getResources(), R.drawable.sound3);
heiZi[0] = BitmapFactory.decodeResource(getResources(), R.drawable.heishuai);//黑帅
heiZi[1] = BitmapFactory.decodeResource(getResources(), R.drawable.heiju);//黑车
heiZi[2] = BitmapFactory.decodeResource(getResources(), R.drawable.heima);//黑马
heiZi[3] = BitmapFactory.decodeResource(getResources(), R.drawable.heipao);//黑炮
heiZi[4] = BitmapFactory.decodeResource(getResources(), R.drawable.heishi);//黑士
heiZi[5] = BitmapFactory.decodeResource(getResources(), R.drawable.heixiang);//黑象
heiZi[6] = BitmapFactory.decodeResource(getResources(), R.drawable.heibing);//黑兵
hongZi[0] = BitmapFactory.decodeResource(getResources(), R.drawable.hongjiang);//红将
hongZi[1] = BitmapFactory.decodeResource(getResources(), R.drawable.hongju);//红车
hongZi[2] = BitmapFactory.decodeResource(getResources(), R.drawable.hongma);//红马
hongZi[3] = BitmapFactory.decodeResource(getResources(), R.drawable.hongpao);//红砲
hongZi[4] = BitmapFactory.decodeResource(getResources(), R.drawable.hongshi);//红仕
hongZi[5] = BitmapFactory.decodeResource(getResources(), R.drawable.hongxiang);//红相
hongZi[6] = BitmapFactory.decodeResource(getResources(), R.drawable.hongzu);//红卒
number[0] = BitmapFactory.decodeResource(getResources(), R.drawable.number0);//黑色数字0
number[1] = BitmapFactory.decodeResource(getResources(), R.drawable.number1);//黑色数字1
number[2] = BitmapFactory.decodeResource(getResources(), R.drawable.number2);//黑色数字2
number[3] = BitmapFactory.decodeResource(getResources(), R.drawable.number3);//黑色数字3
number[4] = BitmapFactory.decodeResource(getResources(), R.drawable.number4);//黑色数字4
number[5] = BitmapFactory.decodeResource(getResources(), R.drawable.number5);//黑色数字5
number[6] = BitmapFactory.decodeResource(getResources(), R.drawable.number6);//黑色数字6
number[7] = BitmapFactory.decodeResource(getResources(), R.drawable.number7);//黑色数字7
number[8] = BitmapFactory.decodeResource(getResources(), R.drawable.number8);//黑色数字8
number[9] = BitmapFactory.decodeResource(getResources(), R.drawable.number9);//黑色数字9
redNumber[0] = BitmapFactory.decodeResource(getResources(), R.drawable.rednumber0);//红色数字0
redNumber[1] = BitmapFactory.decodeResource(getResources(), R.drawable.rednumber1);//红色数字1
redNumber[2] = BitmapFactory.decodeResource(getResources(), R.drawable.rednumber2);//红色数字2
redNumber[3] = BitmapFactory.decodeResource(getResources(), R.drawable.rednumber3);//红色数字3
redNumber[4] = BitmapFactory.decodeResource(getResources(), R.drawable.rednumber4);//红色数字4
redNumber[5] = BitmapFactory.decodeResource(getResources(), R.drawable.rednumber5);//红色数字5
redNumber[6] = BitmapFactory.decodeResource(getResources(), R.drawable.rednumber6);//红色数字6
redNumber[7] = BitmapFactory.decodeResource(getResources(), R.drawable.rednumber7);//红色数字7
redNumber[8] = BitmapFactory.decodeResource(getResources(), R.drawable.rednumber8);//红色数字8
redNumber[9] = BitmapFactory.decodeResource(getResources(), R.drawable.rednumber9);//红色数字9
background = BitmapFactory.decodeResource(getResources(), R.drawable.bacnground);
}
/**
* 该方法是自己定义的并非重写的
* 该方法是死的,只根据数据绘制屏幕
*/
public void onDraw(Canvas canvas){//自己写的绘制方法
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(background, 0,0, null);//清背景
canvas.drawBitmap(qiPan, 10, 10, null);//绘制棋盘
for(int i=0; i<qizi.length; i++){
for(int j=0; j<qizi[i].length; j++){//绘制棋子
if(qizi[i][j] != 0){
canvas.drawBitmap(qizibackground, 9+j*34, 10+i*35, null);//绘制棋子的背景
if(qizi[i][j] == 1){//为黑帅时
canvas.drawBitmap(heiZi[0], 12+j*34, 13+i*35, paint);
}
else if(qizi[i][j] == 2){//为黑车时
canvas.drawBitmap(heiZi[1], 12+j*34, 13+i*35, paint);
}
else if(qizi[i][j] == 3){//为黑马时
canvas.drawBitmap(heiZi[2], 12+j*34, 13+i*35, paint);
}
else if(qizi[i][j] == 4){//为黑炮时
canvas.drawBitmap(heiZi[3], 12+j*34, 13+i*35, paint);
}
else if(qizi[i
- 1
- 2
- 3
- 4
- 5
- 6
前往页