Android自学进程—围住神经猫开发

Android自学历程—围住神经猫开发

学习核心内容:
  1. 学会SurfaceView的绘图技巧。
  2. 掌握神经猫的游戏逻辑设计。

 

第一阶段主要完成内容:

  • 并且创建游戏所需的类,搭建基本的代码结构。
  • 创建一个二维数组,用于保存游戏场景,并且将场景初始化。
  • SurfaceView中根据场景数据,在界面中绘制点阵。

先上代码

Android自学进程—围住神经猫开发Android自学进程—围住神经猫开发
 1 package com.ryan.catchcrazycat;
 2 
 3 /**
 4  * Created by air on 15-8-1.
 5  */
 6 public class Dot {
 7 
 8     /*
 9     记录当前点的X,Y坐标
10      */
11     int x,y;
12 
13     /*
14     记录点的状态
15      */
16     int status;
17 
18     public static final int STATUS_ON = 1;
19     public static final int STATUS_OFF = 0;
20     public static final int STATUS_IN = 9;
21 
22     public Dot(int x,int y) {
23         this.x = x;
24         this.y = y;
25         status = STATUS_OFF;
26     }
27 
28     public int getX() {
29         return x;
30     }
31 
32     public int getY() {
33         return y;
34     }
35 
36     public void setX(int x) {
37         this.x = x;
38     }
39 
40     public void setY(int y) {
41         this.y = y;
42     }
43 
44     public void setX(int x,int y) {
45         this.x = x;
46         this.y = y;
47     }
48 
49     public void setStatus(int status){
50         this.status = status;
51     }
52 }
Dot.java
Android自学进程—围住神经猫开发Android自学进程—围住神经猫开发
  1 package com.ryan.catchcrazycat;
  2 
  3 import android.content.Context;
  4 import android.graphics.Canvas;
  5 import android.graphics.Color;
  6 import android.graphics.Paint;
  7 import android.graphics.RectF;
  8 import android.view.SurfaceHolder;
  9 import android.view.SurfaceView;
 10 
 11 /**
 12  * Created by air on 15-8-1.
 13  */
 14 public class playground extends SurfaceView implements SurfaceHolder.Callback{
 15 
 16 
 17 
 18     private static int WIDTH = 40;
 19     private static final int COL = 10;
 20     private static final int ROM = 10;
 21     private static final int BLOCKS = 10;//默认添加的路障数量
 22 
 23     private Dot matrix[][];
 24     private Dot cat;
 25 
 26     public playground(Context context) {
 27         super(context);
 28         getHolder().addCallback(this);
 29         matrix = new Dot[ROM][COL];
 30         for (int i = 0; i < ROM; i++) {
 31             for (int j = 0; j < COL; j++) {
 32                 matrix[i][j] = new Dot(j,i);
 33             }
 34         }
 35 
 36         initGame();
 37     }
 38 
 39 
 40     private void redraw() {
 41         Canvas canvas = getHolder().lockCanvas();
 42         canvas.drawColor(Color.LTGRAY);
 43         Paint paint = new Paint();
 44         paint.setFlags(Paint.ANTI_ALIAS_FLAG);
 45         for (int i = 0; i < ROM; i++) {
 46             int offset = 0; //错位解决
 47             if (i%2==0){
 48                 offset = WIDTH/2;
 49             }
 50             for (int j = 0; j < COL; j++) {
 51                 Dot one = getDot(j,i);
 52                 switch (one.status){
 53                     case Dot.STATUS_OFF:
 54                         paint.setColor(0xFFEEEEEE);
 55                         break;
 56                     case Dot.STATUS_ON:
 57                         paint.setColor(0xFFFFAA00);
 58                         break;
 59                     case Dot.STATUS_IN:
 60                         paint.setColor(0xFFFF0000);
 61                         break;
 62                     default:
 63                         break;
 64                 }
 65                 canvas.drawOval(
 66                         new RectF(one.getX()*WIDTH+offset,one.getY()*WIDTH,(one.getX()+1)*WIDTH+offset,(one.getY()+1)*WIDTH),paint);
 67             }
 68         }
 69         getHolder().unlockCanvasAndPost(canvas);
 70     }
 71 
 72     @Override
 73     public void surfaceCreated(SurfaceHolder holder) {
 74         redraw();
 75     }
 76 
 77     @Override
 78     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
 79         WIDTH = width/(COL+1);
 80         redraw();
 81     }
 82 
 83     @Override
 84     public void surfaceDestroyed(SurfaceHolder holder) {
 85 
 86     }
 87 
 88     private void initGame(){
 89         for (int i = 0; i < ROM; i++) {
 90             for (int j = 0; j < COL; j++) {
 91                 matrix[i][j].setStatus(Dot.STATUS_OFF);
 92             }
 93         }
 94         cat = new Dot(4,5);
 95         getDot(4,5).setStatus(Dot.STATUS_IN);
 96         for (int i = 0; i < BLOCKS; ) {
 97             int x = (int)(Math.random()*1000%COL);
 98             int y = (int)(Math.random()*1000%COL);
 99             if (getDot(x,y).status==Dot.STATUS_OFF){
100                 getDot(x,y).setStatus(Dot.STATUS_ON);
101                 i++;
102                 System.out.println("BLOCKS"+i);
103             }
104         }
105     }
106 
107     private Dot getDot(int x,int y){
108         return matrix[y][x];
109     }
110 
111 }
playground.java
Android自学进程—围住神经猫开发Android自学进程—围住神经猫开发
 1 package com.ryan.catchcrazycat;
 2 
 3 import android.support.v7.app.ActionBarActivity;
 4 import android.os.Bundle;
 5 import android.view.Menu;
 6 import android.view.MenuItem;
 7 
 8 
 9 public class MainActivity extends ActionBarActivity {
10 
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(new playground(this));
15     }
16 
17 
18 }
MainActivity

  Android自学进程—围住神经猫开发来张截图。

 

还没有来得及细细品味,想法感悟心得,后续跟进。