Android Simulation 2048

Source: Internet
Author: User

Use holiday breaks to learn and simulate 2048 games on Android.

Effects such as:

Production ideas:

1, draw 2048 game main interface, according to the mobile phone screen wide height to calculate and draw the size of each block.

1 @Override2     protected voidOnsizechanged (intWintHintOLDW,intOLDH) {3         Super. Onsizechanged (W, H, OLDW, OLDH);4         intCW = (Math.min (w, h)-10)/4;5 6 Addcards (CW, CW);7 8 startgame ();9     }Ten  One     Private voidStartgame () { AMainactivity Aty =mainactivity.getmainactivity (); - Aty.clearscore (); -  the          for(inty = 0; Y < 4; y++) { -              for(intx = 0; x < 4; X + +) { -Cardsmap[x][y].setnum (0); -             } +         } - addrandomnum (); + addrandomnum (); A}

2. Create a two-dimensional array to store the values on the card surface

3. Establish a list<point> to store each card location

    Private card[][] Cardsmap = new Card[4][4];    Private list<point> emptypoints = new arraylist<point> ();    private void Addcards (int cardwidth, int cardheight) {        Card C;        LinearLayout Line;        Linearlayout.layoutparams Linelp;        for (int y = 0, y < 4; y++) {Line            = new LinearLayout (GetContext ());            Linelp = new Layoutparams ( -1, cardheight);            AddView (line, linelp);            for (int x = 0; x < 4; + +) {                c = new Card (GetContext ());                Line.addview (c, Cardwidth, cardheight);                Cardsmap[x][y] = c;}}    }

  

4. At the beginning of the game, add random card data.

Private voidAddrandomnum () {emptypoints.clear ();  for(inty = 0; Y < 4; y++) {             for(intx = 0; x < 4; X + +) {                if(Cardsmap[x][y].getnum () <= 0) {Emptypoints.add (NewPoint (x, y)); }            }        }        if(Emptypoints.size () > 0) {point P= Emptypoints.remove ((int) (Math.random () *emptypoints.size ())); Cardsmap[p.x][p.y].setnum (Math.random ()> 0.1? 2:4); }    }

5. Use the Android Touch event to record your finger in the screen slide direction.

private void Initgameview () {setorientation (linearlayout.vertical);        SetBackgroundColor (0XFFBBADA0);                    Setontouchlistener (New Ontouchlistener () {private float StartX, starty            , OffsetX, OffsetY;                    @Override public boolean OnTouch (View V, motionevent event) {switch (event.getaction ()) {                        Case Motionevent.action_down://When the screen is clicked, record the X and Y values of the Click StartX = Event.getx ();                        Starty = Event.gety ();                    Break Case MotionEvent.ACTION_UP:offsetX = Event.getx ()-startx;//to calculate the hit offset value offs                        Ety = Event.gety ()-starty;                                if (Math.Abs (OffsetX) > Math.Abs (OffsetY)) {if (OffsetX <-5) {            Left                    Swipeleft ();                            } else if (OffsetX > 5) {//right swiperight ();                                }} else {if (OffsetY <-5) {                            Up Swipeup ();                            } else if (OffsetY > 5) {//down swipedown ();                }} break;            } return true;    }        }); }

  

6, according to the direction of the slide to cycle each direction of the card to move the card position and the same card added

    private void Swipeleft () {Boolean merge = false; for (int y = 0, y < 4; y++) {for (int x = 0; x < 4, + x + +) {for (int x1 = x + 1; x1 < 4 ;  x1++) {if (Cardsmap[x1][y].getnum () > 0) {if (Cardsmap[x][y].getnum () <=                            0) {Cardsmap[x][y].setnum (Cardsmap[x1][y].getnum ());                            Cardsmap[x1][y].setnum (0);                            x--;                        Merge = true; } else if (Cardsmap[x][y].equals (Cardsmap[x1][y])) {Cardsmap[x][y].setnum (CARDSMAP[X][Y].GETNU                            M () * 2);                            Cardsmap[x1][y].setnum (0);                            Mainactivity.getmainactivity (). Addscore (Cardsmap[x][y].getnum ());                        Merge = true;                    } break;     }}} if (merge) {           Addrandomnum ();            Checkcomplete (); }        }    }

  

Android Simulation 2048

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.