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;//設(shè)置行數(shù)
private static final int ROW = 10;//設(shè)置列數(shù)
private static final int BLOCKS = 15;//默認(rèn)添加初始時(shí)候路障的數(shù)量
private Dot matrix[][];//定義二維數(shù)組,表示整個(gè)棋盤(pán)
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() {//實(shí)現(xiàn)界面的繪制
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+offset,one.getY()*WIDTH,(one.getX()+1)*WIDTH+offset,(one.getY()+1)*WIDTH),paint);
}
}
getHolder().unlockCanvasAndPost(c);
}
SurfaceHolder.Callback callback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
readraw();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
WIDTH = width/(COL+1);
readraw();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
};
private void initGame(){
for(int i=0;i<ROW;i++){
for(int j=0;j<COL;j++){
matrix[i][j].setStatus(Dot.STATUS_ON);
}
}
cat = new Dot(4,5);
getDot(4,5).setStatus(Dot.STATUS_IN);
for(int i=0;i<BLOCKS;){
int x = (int)(Math.random()*1000)%COL;
int y = (int)(Math.random()*1000)%ROW;
if(getDot(x,y).getStatus() == Dot.STATUS_OFF){
getDot(x,y).setStatus(Dot.STATUS_ON);
i++;
System.out.println("BLOCKS"+i);
}
}
}
}
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)