Apple: first, Apple has its self-directed attribute, that is, the position (X, Y coordinates), and a new Apple is automatically generated after it is eaten by a snake,
Property: X (horizontal), y (vertical)
Method: automatically generate a new Apple (create) with random locations in the room
Package com. Snake. view;
Import java. util. Random;
Import com. Snake. activity. R;
Public class Apple {
// Apple location
Private int X, Y;
Private int applecolor = R. drawable. greenstar;
Public static final int applesize = 24;
// Where Can apple store the largest data?
Private int Maxx, Maxy;
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;
}
Public int getapplecolor (){
Return applecolor;
}
Public void setapplecolor (INT applecolor ){
This. applecolor = applecolor;
}
Public void setmaxx (int mx, int my ){
Maxx = Mx;
Maxy = My;
}
// Regenerate Apple
Public void create (){
System. Out. println ("generate Apple -----------");
Random random = new random ();
Int x = 0; y = 0;
While (x = 0 | Y = 0 ){
X = random. nextint (maxX-1 );
Y = random. nextint (maxY-1 );
}
This. setx (X );
This. sety (y );
}
}
Snake: A snake has a location attribute, as well as a method for moving and eating an apple. You can get the length of the snake body to determine whether it enters the next level.
Package com. Snake. view;
Import java. util. arraylist;
Import com. Snake. activity. R;
Public class snake {
// Initialize the snake body
Public snake (){
Sections = new arraylist <snail kesection> ();
Sections. Add (New snail kesection (, 0 ));
Sections. Add (New snail kesection (, 0 ));
Sections. Add (New snail kesection (3, 1, 0 ));
Sections. Add (New snail kesection (, R. drawable. yellowstar ));
}
Public arraylist <snail kesection> sections;
// Four Directions
Public static final int up = 1;
Public static final int down = 2;
Public static final int left = 3;
Public static final int right = 4;
// The direction of the snake movement. First, move to the right by default.
Private int movedirection = right;
// Obtain the length of the snake body
Public int getsnkelength (){
Return sections. Size ();
}
Public int getmovedirection (){
Return movedirection;
}
Public void setmovedirection (INT movedirection ){
This. movedirection = movedirection;
}
/**
* Snake movement Method
*/
Public void move (INT direction ){
For (snail kesection tion: Sections ){
If (sections. indexof (tion) = sections. Size ()-1 ){
Work (Direction, tion );
} Else {
Snail kesection before = sections. Get (sections. indexof (tion) + 1 );
Tion. Set (before. getx (), before. Gety ());
}
}
}
Private void work (INT direction, snail kesection tion ){
Switch (Direction ){
Case up:
Tion. Set (tion. getx (), tion. Gety ()-1 );
Break;
Case down:
Tion. Set (tion. getx (), tion. Gety () + 1 );
Break;
Case left:
Tion. Set (tion. getx ()-1, tion. Gety ());
Break;
Case right:
Tion. Set (tion. getx () + 1, tion. Gety ());
Break;
}
}
/**
* Eating an apple (turning an apple into a snake)
* @ Param Apple
*/
Public void eat (Apple ){
Sections. Add (0, new snail kesection (Apple. getx (), apple. Gety (), 0 ));
}
}
Class snail kesection {
Public snail kesection (int x, int y, int color ){
This. x = X;
This. Y = y;
If (color! = 0)
This. Color = color;
}
// The current location of the Snake. The default value is the upper left corner.
Private int x = 0, y = 0;
Public int getx (){
Return X;
}
Public void set (int xx, int YY ){
This. x = xx;
This. Y = YY;
}
Public int Gety (){
Return y;
}
Private int color = R. drawable. greenstar;
Public int getcolor (){
Return color;
}
Public void setcolor (INT colorid ){
Color = colorid;
}
}
Room Type: There is a collection of snakes and apples in the room.
Package com. Snake. view;
Import java. util. arraylist;
Import com. Snake. activity. R;
Import com. Snake. activity. Snail keactivity;
Import Android. content. context;
Import Android. Graphics. Bitmap;
Import Android. Graphics. Canvas;
Import Android. Graphics. paint;
Import Android. Graphics. drawable. drawable;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. util. attributeset;
Import Android. util. displaymetrics;
Import Android. View. motionevent;
Import Android. View. view;
Import Android. widget. Toast;
Public class space extends view {
Private handler = new handler (){
@ Override
Public void handlemessage (Message MSG ){
Update ();
Space. This. invalidate ();
Super. handlemessage (MSG );
}
};
Private context c;
Private int displayx, displayy;
Private paint;
Public Space (context, attributeset attrs, int defstyle ){
Super (context, attrs, defstyle );
C = context;
}
Public Space (context, attributeset attrs ){
Super (context, attrs );
C = context;
}
Public Space (context ){
Super (context );
C = context;
}
Private Static int X;
Private Static int y;
Private int offsetx, offsety;
Private int [] [] space;
Private int direction = 4;
// Apple collection
Private arraylist <Apple> apples;
// Snake
Private Snake snake;
@ Override
Protected void onsizechanged (int w, int H, int oldw, int oldh ){
X = W/Apple. applesize;
Y = H/Apple. applesize;
Offsetx = (W-x * apple. applesize)/2;
Offsety = (H-y * apple. applesize)/2;
Space = new int [x] [Y];
Init ();
Snail activity = (snail activity) C;
Displaymetrics dm = new displaymetrics ();
Activity. getwindowmanager (). getdefadisplay display (). getmetrics (DM );
Displayx = DM. widthpixels;
Displayy = DM. heightpixels;
Super. onsizechanged (W, H, oldw, oldh );
}
Private void Init (){
Paint = new paint ();
Apples = new arraylist <Apple> ();
For (INT I = 0; I <2; I ++ ){
// Initialize Apple
Apples. Add (createapple ());
}
// Initialize the snake
Snake = new snake ();
// Original room
Updatespace ();
Handler. obtainmessage (). sendtotarget ();
}
Private void updatespace (){
For (INT I = 0; I <space. length; I ++ ){
For (Int J = 0; j <space [0]. length; j ++ ){
For (snkesection Selection: Snake. Sections ){
If (I = selection. getx () & J = selection. Gety ()){
Space [I] [J] = selection. getcolor ();
}
}
For (Apple: Apples ){
If (I = apple. getx () & J = apple. Gety ()){
Space [I] [J] = apple. getapplecolor ();
}
}
If (I = 0 | j = 0 | I = space. Length-1 | j = space [0]. Length-1)
Space [I] [J] = R. drawable. Redstar;
}
}
}
Private void Update (){
Space = new int [x] [Y];
Snake. Move (Direction );
Snake section lasts = snake. Sections. Get (snake. Sections. Size ()-1 );
If (lasts. getx () = X-1 | lasts. gety () = Y-1 | lasts. getx () = 0 | lasts. gety () = 0)
Toast. maketext (C, "g", Toast. length_short). Show ();
Updatespace ();
For (Apple: Apples ){
Snake section last = snake. Sections. Get (snake. Sections. Size ()-1 );
If (last. getx () = apple. getx () & last. Gety () = apple. Gety ()){
Snake. Eat (Apple );
Apple. Create ();
}
}
Handler. sendmessagedelayed (handler. obtainmessage (), 800 );
}
Public static Apple createapple (){
Apple = new Apple ();
Apple. setmaxx (x, y );
Apple. Create ();
Return apple;
}
Public bitmap createbitmap (int rid ){
Bitmap bitmap = bitmap. createbitmap (Apple. applesize, apple. applesize, bitmap. config. argb_8888 );
Canvas canvas = new canvas (Bitmap );
Drawable tile = C. getresources (). getdrawable (RID );
Tile. setbounds (0, 0, apple. applesize, apple. applesize );
Tile. Draw (canvas );
Return bitmap;
}
@ Override
Public Boolean ontouchevent (motionevent event ){
Switch (event. getaction ()){
Case motionevent. action_down:
Int x = (INT) event. getx ();
Int y = (INT) event. Gety ();
If (displayx/3 <X & x <displayx/3*2 & Y <displayy/5)
Direction = snake. up;
If (displayx/3 <X & x <displayx/3*2 & Y> displayy/5*4)
Direction = snake. down;
If (displayy/3 <Y & Y <displayy/3*2 & x <displayx/3)
Direction = snake. Left;
If (displayy/3 <Y & Y <displayy/3*2 & x> displayx/3*2)
Direction = snake. Right;
Break;
}
Return super. ontouchevent (event );
}
@ Override
Protected void ondraw (canvas ){
For (INT I = 0; I <space. length; I ++ ){
For (Int J = 0; j <space [0]. length; j ++ ){
If (space [I] [J]! = 0)
Canvas. drawbitmap (createbitmap (space [I] [J]), I * apple. applesize + offsetx, J * apple. applesize + offsety, paint );
}
}
Super. ondraw (canvas );
}
}
Source code download
There are children's shoes that you cannot understand.