Snake is a very classic game, these classic games to our childhood added a lot of fun, today we started to a step by stage in the Android device to achieve a snake game, I am also the first time to write this game, there may be wrong, or detours, but finally hope to have a good result, Next we will take a step-by-step touch of the stone development.
First, build a project
Ii. Define your own view (Snake interface)
Package Com.example.crazysnake;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.rectf;import Android.util.attributeset;import Android.view.view;public class Mysnake extends View {private Paint paint;private RectF Rect;public Mysnake (Context context, AttributeSet Attrs) {Super (context, attrs);p aint = new paint (); rect = new RECTF ();} @Overrideprotected void OnDraw (canvas canvas) {Super.ondraw (canvas), Rect.set ((),;p Aint.setcolor (color.red ); Canvas.drawrect (rect, paint);}}
Layout file
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_ Parent " android:layout_height=" fill_parent " android:orientation=" vertical ">< Com.example.crazysnake.MySnake android:layout_width= "match_parent" android:layout_height= "match_parent "/></linearlayout>
Execution effect: We draw a small square on the interface, and this is the basis of our finished snake (as important as the bricks we want to build a building) three, draw a color snake first create a block object
Package Com.example.crazysnake;public class Box {private int x;private int y;public box (int x, int y) {super (); this.x = x; This.y = y;} public int GetX () {return x;} public void SetX (int x) {this.x = x;} public int GetY () {return y;} public void sety (int y) {this.y = y;}}
Draw a row of squares
Package Com.example.crazysnake;import Java.util.arraylist;import Java.util.list;import android.content.Context; Import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.rectf;import Android.util.attributeset;import Android.view.view;public class MySnake extends View { Private Paint paint;private RECTF rect;private int boxsize = 30;private list<box> boxs = new arraylist<box> (); private static final int[] colors = {color.red,color.blue, color.green,color.yellow};p ublic Mysnake (context context, AttributeSet attrs) {Super (context, attrs);p aint = new paint (); rect = new RECTF (); InitData ();} private void InitData () {box box;for (int i=0; i<10; i++) {box = new box (i*boxsize, 0); Boxs.add (Box);}} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); for (int i=0; i<boxs.size (); i++) { Paint.setcolor (colors[i% colors.length]); Rect.set (Boxs.get (i). GetX (), Boxs.get (i). GetY (), Boxs.get (i). GetX () + Boxsize, boxsize); CanvaS.drawrect (rect, paint);}}}
Four, let this color snake move rewrite ontouchevent method
@Overridepublic boolean ontouchevent (Motionevent event) {System.out.println ("OnTouch"); switch (Event.getaction ()) { Case MotionEvent.ACTION_DOWN:mDownX = Event.getx (); mdowny = Event.gety (); break;case MotionEvent.ACTION_UP:float DISX = Event.getx ()-Mdownx;float Disy = Event.gety ()-mdowny; System.out.println ("DISX =" + Disx); System.out.println ("Dixy =" + Disy), if (Math.Abs (DISX) > Math.Abs (disy)) {if (Disx > 0) {System.out.println ("right" ); Move (derectory.right);} Else{move (Derectory.left);}} Else{if (Disy > 0) {}else{}}break;} return true;}
Move method to shift by direction
private void Move (Derectory derectory) {Box box;int bs = 0;switch (derectory) {case Left:case Top:bs =-Boxsize;break;case Right:case bottom:bs = Boxsize;break;} if (derectory = = Derectory.left | | derectory = = derectory.right) {for (int i=0; i<boxs.size (); i++) {box = Boxs.get (i); box . SetX (Box.getx () + BS);}} else{for (int i=0; i<boxs.size (); i++) {box = Boxs.get (i); Box.sety (box.gety () + BS);}} Invalidate ();}
In the above code we have no control of the boundary and need to add code that infers the boundary condition. Let the snake run on its own initiative, let's control it. Start a thread and let the snake run on its own initiative.
Private class Snakethread extends Thread{private boolean stoped = False, @Overridepublic void run () {while (!stoped) {try {Th Read.sleep (1000);} catch (Interruptedexception e) {e.printstacktrace ();} Move ();p ostinvalidate ();}}
All code such as the following:
Package Com.example.crazysnake;import Java.util.arraylist;import Java.util.list;import android.content.Context; Import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.rectf;import Android.util.attributeset;import Android.view.motionevent;import Android.view.View; public class Mysnake extends View {private Paint paint;private rectf rect;private int boxsize = 30;private snakethread SNA Kethread;private list<box> boxs = new arraylist<box> ();p rivate static final int[] colors = {Color.red,color.b LUE, Color.green,color.yellow};p rivate enum Derectory{left,right,top,bottom;} Private Derectory Currentderect = Derectory.right;public Mysnake (context context, AttributeSet Attrs) {Super (context, attrs);p aint = new paint (); rect = new RECTF (); InitData (); Startthread ();} public void Startthread () {if (Snakethread = = null) {Snakethread = new snakethread (); Snakethread.start ();}} private void InitData () {box box;for (int i=0; i<10; i++) {box = New Box (i*boxsize, 0); Boxs.add (Box);}} Private float mdownx;private float Mdowny; @Overridepublic boolean ontouchevent (Motionevent event) {System.out.println ( "OnTouch"), switch (Event.getaction ()) {Case MotionEvent.ACTION_DOWN:mDownX = Event.getx (); mdowny = Event.gety (); ; case MotionEvent.ACTION_UP:float DISX = Event.getx ()-Mdownx;float Disy = Event.gety ()-mdowny; System.out.println ("DISX =" + Disx); System.out.println ("Dixy =" + Disy), if (Math.Abs (DISX) > Math.Abs (disy)) {if (Disx > 0) {currentderect = Derectory.right;} Else{currentderect = Derectory.left;}} Else{if (Disy > 0) {currentderect = Derectory.bottom;} Else{currentderect = Derectory.top;}} break;} return true;} Private class Snakethread extends Thread{private boolean stoped = False, @Overridepublic void run () {while (!stoped) {try {Th Read.sleep (1000);} catch (Interruptedexception e) {e.printstacktrace ();} Move ();p ostinvalidate ();}} private void Move () {Box box;if (boxs.get (0). GetX ()-boxsize < 0) {Currentderect = Derectory.Right;} if (Boxs.get (Boxs.size ()-1). GetX () + 2 * boxsize > GetWidth ()) {currentderect = Derectory.left;} Switch (currentderect) {case left:for (int i=0; i<boxs.size (); i++) {box = Boxs.get (i); Box.setx (Box.getx ()-boxsize);} Break;case right:for (int i=0; i<boxs.size (); i++) {box = Boxs.get (i); Box.setx (Box.getx () + boxsize);} Break;case top:break;case Bottom:break;}} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); for (int i=0; i<boxs.size (); i++) { Paint.setcolor (colors[i% colors.length]); Rect.set (Boxs.get (i). GetX (), Boxs.get (i). GetY (), Boxs.get (i). GetX () + Boxsize, boxsize); Canvas.drawrect (rect, paint);}}
Let's go here today and continue tomorrow ... ^-^
It's a man. 100-storey "fourth floor"--crazy Snake (1)