package activitytest.example.com.catchcat;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class Playground extends SurfaceView {
private static float WIDTH = 40;
private static final int COL = 10;//设置行数
private static final int ROW = 10;//设置列数
private static final int BLOCKS = 15;//默认添加初始时候路障的数量
private Dot matrix[][];//定义二维数组,表示整个棋盘
private Dot cat;//定义猫
public Playground(Context context) {
super(context);
getHolder().addCallback(callback);
matrix = new Dot[ROW][COL];
for(int i=0;i<ROW;i++){
for(int j=0;j<COL;j++){
matrix[i][j] = new Dot(j,i);
}
}
initGame();
}
private Dot getDot(int x,int y){
return matrix[y][x];
}
private void readraw() {//实现界面的绘制
Canvas c = getHolder().lockCanvas();
c.drawColor(Color.LTGRAY);
Paint paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);//抗锯齿
for(int i=0;i<ROW;i++){
int offset = 0;
if(i%2 != 0){
offset = (int) (WIDTH/2);
}
for(int j=0;j<COL;j++){
Dot one = getDot(j,i);
switch(one.getStatus()){
case Dot.STATUS_OFF:
paint.setColor(0XFFEEEEEE);
break;
case Dot.STATUS_ON:
paint.setColor(0XFFFFAA00);
break;
case Dot.STATUS_IN:
paint.setColor(0XFFFF0000);
break;
}
c.drawOval(new RectF(one.getX()*WIDTH+偏移量,one.getY()*WIDTH,(one.getX()+1)*WIDTH+偏移量,(one.getY()+1)*WIDTH),paint);
}
}
getHolder().unlockCanvasAndPost(c);
}
SurfaceHolder.Callback 回调 = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolderholder) {
readraw();
}
@Override
public void surfaceChanged(SurfaceHolderholder, int format, int width, int height) {
WIDTH = width/(COL+1);
readraw();
}
@Override
public void surfaceDestroyed(SurfaceHolderholder) {
}
};
private void initGame(){
for(int i=0;i