It's a man's next layer. [Layer 4] -- Crazy greedy snakes (3) and crazy greedy snakes

Source: Internet
Author: User
Tags drawtext

It's a man's next layer. [Layer 4] -- Crazy greedy snakes (3) and crazy greedy snakes

In the previous article, Crazy greedy snake (2) is the fourth layer for men to move the greedy snake around the screen. In this article, we will complete all the functions of the greedy snake.

1. Random production of Apple
    private void addAppleBox(){    int randomX = random.nextInt(xMaxBoxCount);        int randomY = random.nextInt(yMaxBoxCount);        for(int i=0; i<boxs.size(); i++){        if(boxs.get(i).getX() == randomX){        addAppleBox();        break;        }        if(boxs.get(i).getY() == randomY){        addAppleBox();        break;        }        appleBox = new Box(randomX, randomY);        }    }
Ii. Determine the Boundary Value
// Determine whether a wall is hit if (headBox. getX () <0 | headBox. getY () <0 | headBox. getX ()> xMaxBoxCount | headBox. getY ()> yMaxBoxCount) {currentState = State. LOSE;} // determine whether the for (int I = 0; I <boxs. size (); I ++) {if (boxs. get (I ). getX () = headBox. getX () & boxs. get (I ). getY () = headBox. getY () {currentState = State. LOSE ;}} isGroup = false; // determines if the Apple has been eaten ~~ For (int I = 0; I <boxs. size (); I ++) {if (boxs. get (I ). getX () = appleBox. getX () & boxs. get (I ). getY () = appleBox. getY () {isGroup = true; addAppleBox ();}}
III. All code
Package com. example. crazysnake; import java. util. arrayList; import java. util. list; import java. util. random; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rectF; import android. OS. handler; import android. OS. message; import android. util. attributeSet; import android. view. motionEvent; import android. view. view ;/* ** CSDN blog: http://blog.csdn.net/dawanganban * @ author sunshine little strong */public class MySnake extends View {private Paint paint; private Paint textPaint; private RectF rect; private static int boxSize = 20; private static int xMaxBoxCount; // The maximum number of boxes in the X axis direction. private static int yMaxBoxCount; // The maximum number of boxes in the Y axis: private List <box> boxs = new ArrayList <Box> (); private Box appleBox; private Random random; private boolean I SGroup = false; private static final int [] colors = {Color. RED, Color. BLUE, Color. GRAY, Color. YELLOW}; private enum derecloud {LEFT, RIGHT, TOP, BOTTOM;} private enum State {READY, // READY for PAUSE, // pause running, // run LOSE // Failed} private derecloud currentDerect = derecloud. LEFT; private State currentState = State. READY; private RefreshHandler mRefreshHandler = new RefreshHandler (); class RefreshHandl Er extends Handler {@ Override public void handleMessage (Message msg) {MySnake. this. update (); MySnake. this. invalidate ();} public void sleep (long delayMillis) {this. removeMessages (0); sendMessageDelayed (obtainMessage (0), delayMillis) ;}} public MySnake (Context context, AttributeSet attrs) {super (context, attrs ); paint = new Paint (); textPaint. setColor (Color. RED); textPaint. SetTextSize (60); rect = new RectF (); random = new Random ();} private void update () {if (currentState = State. RUNNING) {move (); mRefreshHandler. sleep (150) ;}} private void initData () {Box box; for (int I = xMaxBoxCount-5; I <xMaxBoxCount; I ++) {box = new Box (I, 3); boxs. add (box) ;}@ Override protected void onSizeChanged (int w, int h, int oldw, int oldh) {super. onSizeChanged (w, h, oldw, oldh); xMa XBoxCount = (int) Math. floor (w/boxSize); yMaxBoxCount = (int) Math. floor (h/boxSize);} private float mDownX; private float mDownY; @ Override public 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) {if (currentDerect! = Derecloud. LEFT) {currentDerect = derecloud. RIGHT ;}} else {if (currentState! = State. RUNNING) {currentState = State. RUNNING; currentDerect = derecloud. LEFT; boxs. clear (); initData (); addAppleBox (); update ();} else if (currentDerect! = Derecloud. RIGHT) {currentDerect = derecloud. LEFT ;}} else {if (disY> 0) {if (currentDerect! = Derecloud. TOP) {currentDerect = derecloud. BOTTOM ;}} else {if (currentDerect! = Derecloud. BOTTOM) {currentDerect = derecloud. TOP ;}}break;} return true;} private void move () {Box headBox = new Box (0, 0); switch (currentDerect) {case LEFT: headBox = new Box (boxs. get (0 ). getX ()-1, boxs. get (0 ). getY (); break; case RIGHT: headBox = new Box (boxs. get (0 ). getX () + 1, boxs. get (0 ). getY (); break; case TOP: headBox = new Box (boxs. get (0 ). getX (), boxs. get (0 ). getY ()-1); break; c Ase BOTTOM: headBox = new Box (boxs. get (0 ). getX (), boxs. get (0 ). getY () + 1); break;} // determine whether the wall is hit if (headBox. getX () <0 | headBox. getY () <0 | headBox. getX ()> xMaxBoxCount | headBox. getY ()> yMaxBoxCount) {currentState = State. LOSE;} // determine whether the for (int I = 0; I <boxs. size (); I ++) {if (boxs. get (I ). getX () = headBox. getX () & boxs. get (I ). getY () = headBox. getY () {currentState = State. LOSE;} isGr Oup = false; // determines whether an Apple has been eaten ~~ For (int I = 0; I <boxs. size (); I ++) {if (boxs. get (I ). getX () = appleBox. getX () & boxs. get (I ). getY () = appleBox. getY () {isGroup = true; addAppleBox () ;}} boxs. add (0, headBox); if (! IsGroup) {boxs. remove (boxs. size ()-1) ;}} private void addAppleBox () {int randomX = random. nextInt (xMaxBoxCount); int randomY = random. nextInt (yMaxBoxCount); for (int I = 0; I <boxs. size (); I ++) {if (boxs. get (I ). getX () = randomX) {addAppleBox (); break;} if (boxs. get (I ). getY () = randomY) {addAppleBox (); break;} appleBox = new Box (randomX, randomY) ;}@ Override protected void onDraw (Canvas 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 () * boxSize, boxs. get (I ). getY () * boxSize, (boxs. get (I ). getX () + 1) * boxSize, (boxs. get (I ). getY () + 1) * boxSize); canvas. drawRect (rect, paint);} if (appleBox! = Null) {paint. setColor (Color. RED); rect. set (appleBox. getX () * boxSize, appleBox. getY () * boxSize, (appleBox. getX () + 1) * boxSize, (appleBox. getY () + 1) * boxSize); canvas. drawRect (rect, paint);} if (currentState = State. READY) {canvas. drawText ("Slide left", (xMaxBoxCount * boxSize-textPaint. measureText ("Slide left")/2, xMaxBoxCount * boxSize/2, textPaint);} if (currentState = State. LOSE) {canvas. d RawText ("failed! Slide left to continue ", (xMaxBoxCount * boxSize-textPaint. measureText (" failed! Slide left to continue ")/2, (float) xMaxBoxCount * boxSize/2, textPaint); canvas. drawText ("Length:" + boxs. size (), (xMaxBoxCount * boxSize-textPaint. measureText ("Length:" + boxs. size ()/2, (float) xMaxBoxCount * boxSize/4*3, textPaint );}}}


CODE: https://code.csdn.net/lxq_xsyu/crazysnake





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.