Android self-taught course-surrounded by neural cat development and android

Source: Internet
Author: User
Tags gety

Android self-taught course-surrounded by neural cat development and android

Core content:
1. Learn SurfaceView plotting skills.
2. Master the game logic design of neural cats.

 

The main content of the first phase is as follows:

  • Create the classes required for the game and build the basic code structure.
  • Create a two-dimensional array to save the game scenario and initialize the scenario.
  • Draw a dot matrix in SurfaceView Based on the scene data.

Code first

1 package com. ryan. catchcrazycat; 2 3/** 4 * Created by air on 15-8-1. 5 */6 public class Dot {7 8/* 9 records the X and Y coordinates of the current vertex 10 */11 int x and y; status of 12 13/* 14 record points 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 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; // The default number of roadblocks Added 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; // dislocation solution 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 (0 xFFEEEEEE); 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 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 @ Override12 protected void onCreate (Bundle savedInstanceState) {13 super. onCreate (savedInstanceState); 14 setContentView (new playground (this); 15} 16 17 18}MainActivity

Laizhang.

 

You have not had time to savor your thoughts, learn your thoughts, and follow up later.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.