Android snake version 1.1 (basic function implementation version)

Source: Internet
Author: User

Let's talk about all the files first.

Desktopview. Java is a map file used to draw desktop files.

Food. Java food files
Rock. Java stone files
Snake. Java snake files
Main activities of snakactivity. Java

Create a file for the snake in the browser. Java

It is stated in advance that this program only implements the simplest function. The following is the interface.

Now, start to put the code.

Package mars.com; import android. app. activity; import android. OS. bundle; import android. view. keyevent; import android. view. windowmanager; public class snail keactivity extends activity {public static final int cell = 20; // one pixel is 20 public static windowmanager; public static int screen_width; // screen width: public static int screen_height; // screen height: public static int number; // The number of horizontal views: Snake view; Snake snake; Food food; rock; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. initvars (); snake = new snake (); Food = new food (); rock = new rock (); snkeview = new snkeview (getapplicationcontext (), Snake, food, rock); snail keview. setfocusable (true); setcontentview (snkeview); snake. start () ;}@ overridepublic Boolean onkeydown (INT keycode, keyevent event) {Switch (keycode) {Case keyevent. keycode_back: Finish (); system. exit (0); break;} return Super. onkeydown (keycode, event);} private void initvars () {windowmanager = snkeactivity. this. getwindowmanager (); screen_height = windowmanager. getdefadisplay display (). getheight (); screen_width = windowmanager. getdefadisplay display (). getwidth (); number = screen_width/cell ;}}

Snake files

Package mars.com; import Java. util. arraylist; import Java. util. permission list; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paint. style; import android. graphics. point; public class snake {// The public static final int up = 1; public static final int down =-1; public static final int left =-2; public static final int right = 2; public static int olddirection = right; public static int newdirection = right; public static rule list <point> snkebody = new rule list <point> (); private point snketail; private Boolean life = true; Public snake () {int x = snail keactivity. number/2; int y = snake activity. number/2; for (INT I = 0; I <3; I ++) {snake body. add (new point (X --, y) ;}} public int getspeed () {return-20 * snail kebody. size (); // The returned time is negative. The longer the body is, the shorter the remaining time is, and the faster the movement} public void eatrock (rock) {arraylist <point> rocks = rock. getrocks (); For (INT I = 0; I <3; I ++) {If (snail kebody. getfirst (). equals (rocks. get (I) {life = false; break ;}} public void eatwall () {If (snkebody. getfirst (). x <0 | snail kebody. getfirst (). Y <0 | snail kebody. getfirst (). X = snail activity. number | snake body. getfirst (). y = snail keactivity. number) {life = false ;}} public void eatfood (Food food, rock) {If (snail kebody. getfirst (). equals (food. eatfood () {snail kebody. addlast (snketail); food. createfood (); rock. createrocks () ;}} public void eathimself () {for (INT I = 1; I <snkebody. size (); I ++) {If (snail kebody. get (I ). equals (snail kebody. getfirst () {life = false; break ;}} public void move () {snketail = snkebody. getlast (); point headpoint = snkebody. getfirst (); point = new point (); // if the new direction and the old direction are in a line if (olddirection = newdirection | olddirection + newdirection = 0) {If (olddirection = up) {point. set (headpoint. x, headpoint. y-1);} else if (olddirection = down) {point. set (headpoint. x, headpoint. Y + 1);} else if (olddirection = left) {point. set (headpoint. x-1, headpoint. y);} else if (olddirection = right) {point. set (headpoint. X + 1, headpoint. y);} snail kebody. addfirst (point); snkebody. removelast (); return;} // if it is not in a straight line, Turn switch (newdirection) {Case up: Point. set (headpoint. x, headpoint. y-1); break; case down: Point. set (headpoint. x, headpoint. Y + 1); break; case left: Point. set (headpoint. x-1, headpoint. y); break; case right: Point. set (headpoint. X + 1, headpoint. y); break;} olddirection = newdirection; snail kebody. addfirst (point); snkebody. removelast ();} class tsnake implements runnable {public void run () {While (LIFE) {move (); try {thread. sleep (500 + getspeed ();} catch (interruptedexception e) {e. printstacktrace () ;}}} public void start () {New thread (New tsnake ()). start ();} public void display (canvas, context) {paint = new paint (); paint. setcolor (color. blue); paint. setstyle (style. fill); For (INT I = 0; I <snail kebody. size (); I ++) {int x = snake body. get (I ). x * snail keactivity. cell; int y = snail kebody. get (I ). y * snail keactivity. cell; canvas. drawrect (X, Y, x + snkeactivity. cell, Y + snail keactivity. cell, paint);} paint. setcolor (color. red); If (LIFE = false) {bitmap image = bitmapfactory. decodestream (context. getresources (). openrawresource (R. drawable. fail); canvas. drawbitmap (image, 200,200, paint );}}}

Files of the snakkeview class

package mars.com;import android.content.Context;import android.graphics.Canvas;public class SnakeView extends DesktopView {private Snake snake;private Food food;private Rock rock;private Context context;public SnakeView(Context context, Snake snake, Food food, Rock rock) {super(context);this.snake = snake;this.food = food;this.rock = rock;this.context = context;}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);snake.display(canvas, context);food.display(canvas);rock.display(canvas);snake.eatFood(food, rock);snake.eatRock(rock);snake.eatHimself();snake.eatWall();}}

Stone files

package mars.com;import java.util.ArrayList;import java.util.Random;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.Style;import android.graphics.Point;public class Rock {public static ArrayList<Point> rocks = new ArrayList<Point>();public int OK = 0;private Point p;public Rock() {init();}public void init() {rocks = new ArrayList<Point>();for (int i = 0; i < 3; i++) {p = new Point();p.x = new Random().nextInt(SnakeActivity.NUMBER);p.y = new Random().nextInt(SnakeActivity.NUMBER);if (rocks.contains(p) == true|| Snake.snakeBody.contains(p) == true) {i--;} else {rocks.add(p);}}}public void createRocks() {init();}public ArrayList<Point> getRocks() {return rocks;}public void display(Canvas canvas) {Paint paint = new Paint();paint.setColor(Color.RED);paint.setStyle(Style.FILL);for (int i = 0; i < 3; i++) {int x = rocks.get(i).x * SnakeActivity.CELL;int y = rocks.get(i).y * SnakeActivity.CELL;canvas.drawRect(x, y, x + SnakeActivity.CELL, y+ SnakeActivity.CELL, paint);}}}

Food files

Package mars.com; import Java. util. random; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paint. style; import android. graphics. point; public class food {private point P = new point (); public food () {Init ();} public point eatfood () {return P;} public void Init () {While (true) {P. X = new random (). nextint (snail keactivity. number); p. y = new random (). nextint (snail keactivity. number); If (snake. snakebody. contains (p) = false) {// jump out of the loop break if not included;} public void createfood () {Init ();} public void display (canvas) {paint = new paint (); paint. setcolor (color. blue); paint. setstyle (style. fill); canvas. drawrect (P. x * snail keactivity. cell, P. y * snail keactivity. cell, (P. X + 1) * snail keactivity. cell, (P. Y + 1) * snail keactivity. cell, paint );}}

Desktop files

Package mars.com; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. view. motionevent; import android. view. view; public class extends topview extends view {private int width; private int height; Public extends topview (context) {super (context) ;}@ overrideprotected void ondraw (canvas) {super. ondraw (canvas); canvas. drawcolor (color. white); // set the canvas color. setviewport (snail keactivity. screen_width, snake activity. screen_width); paint = new paint (); paint. setcolor (color. blue); // draw a horizontal line for (INT I = 0; I <= snkeactivity. number; I ++) canvas. drawline (I * snail keactivity. cell, 0, I * snail keactivity. cell, snail keactivity. cell * snail keactivity. number, paint); // draw a vertical line for (INT I = 0; I <= snkeactivity. number; I ++) canvas. drawline (0, I * snail keactivity. cell, snail keactivity. cell * snail keactivity. number, I * snail keactivity. cell, paint); // start painting button painting. setcolor (color. gray); int x = 0; int y = snake activity. screen_width; width = snake activity. screen_width/7; Height = (snail keactivity. screen_height-Snail keactivity. screen_width)/5; bitmap image = bitmapfactory. decodestream (getresources (). openrawresource (R. drawable. left); canvas. drawbitmap (image, x + width * 3, Y + height * 2, paint); image = bitmapfactory. decodestream (getresources (). openrawresource (R. drawable. right); canvas. drawbitmap (image, x + width * 5, Y + height * 2, paint); image = bitmapfactory. decodestream (getresources (). openrawresource (R. drawable. up); canvas. drawbitmap (image, x + width * 4, Y + height * 1, paint); image = bitmapfactory. decodestream (getresources (). openrawresource (R. drawable. down); canvas. drawbitmap (image, x + width * 4, Y + height * 3, paint); postinvalidate (); // Update page} @ overridepublic Boolean ontouchevent (motionevent event) {float x = event. getx (); float y = event. gety (); bitmap image = bitmapfactory. decodestream (getresources (). openrawresource (R. drawable. left); int imagex = image. getwidth (); int imagey = image. getheight (); // If you press the up key if (x> 4 * width & x <4 * width + imagex & Y> height + snail keactivity. screen_width & Y <imagey + height + snail activity. screen_width) {system. out. println ("*** on"); snake. newdirection = snake. up;} // if it is a downward key, if (x> 4 * width & x <4 * width + imagex & Y> 3 * height + snail keactivity. screen_width & Y <imagey + 3 * height + snail activity. screen_width) {system. out. println ("***"); snake. newdirection = snake. down;} // if it is left-click if (x> 3 * width & x <3 * width + imagex & Y> 2 * height + snail keactivity. screen_width & Y <imagey + 2 * height + snail activity. screen_width) {system. out. println ("*** left"); snake. newdirection = snake. left;} // if you are right-clicking if (x> 5 * width & x <5 * width + imagex & Y> 2 * height + snail keactivity. screen_width & Y <imagey + 2 * height + snail activity. screen_width) {system. out. println ("*** right"); snake. newdirection = snake. right;} return Super. ontouchevent (event );}}

So far, our program is complete.

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.