android--2048 Games

Source: Internet
Author: User
Tags gety clearscore

Always want to play games, first take a simple point of learning, write code to share out, the Great God can guide ~

Directly on the code:

<span style= "FONT-SIZE:12PX;" >import android.app.activity;import android.os.bundle;import Android.view.menu;import Android.widget.TextView; public class Mainactivity extends Activity {public mainactivity () {mainactivity = this;} @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Tvscore = (TextView) Findviewbyid (R.id.tvscore);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} public void ClearScore () {score = 0;showscore ();} public void Showscore () {Tvscore.settext (score+ "");} public void Addscore (int s) {score+=s;showscore ();} private int score = 0;private TextView tvscore;private static mainactivity mainactivity = Null;public static mainactivity Getmainactivity () {return mainactivity;}} </span>

Import Java.util.arraylist;import java.util.list;import android.app.alertdialog;import android.content.Context; Import Android.content.dialoginterface;import Android.graphics.point;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;import Android.widget.gridlayout;public class GameView extends GridLayout {public Gameview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Initgameview ();} Public Gameview (Context context) {super (context); Initgameview ();} Public Gameview (context context, AttributeSet Attrs) {Super (context, attrs); Initgameview ();} private void Initgameview () {setcolumncount (4); SetBackgroundColor (0xffbbada0); Setontouchlistener (new View.ontouchlistener () {private float startx,starty,offsetx,offsety; @Overridepublic boolean OnTouch (View V, Motionevent event) {switch (event.getaction ()) {Case MotionEvent.ACTION_DOWN:startX = Event.getx (); starty = Event.gety ( ); Break;case MotionEvent.ACTION_UP:offsetX = Event.getx()-startx;offsety = Event.gety ()-starty;if (Math.Abs (OffsetX) >math.abs (OffsetY)) {if (offsetx<-5) {swipeleft () ;} else if (offsetx>5) {swiperight ();}} Else{if (offsety<-5) {swipeup ();} else if (offsety>5) {Swipedown ();}} break;} return true;});} @Overrideprotected void onsizechanged (int w, int h, int oldw, int oldh) {super.onsizechanged (W, H, OLDW, OLDH); int Cardwid th = (Math.min (w, h) -10)/4;addcards (cardwidth,cardwidth); Startgame ();} private void addcards (int cardwidth,int cardheight) {Card c;for (int y = 0; y < 4; y++) {for (int x = 0; x < 4; + +) {c = new Card (GetContext ()); C.setnum (0); AddView (c, Cardwidth, cardheight); cardsmap[x][y] = C;}}} private void Startgame () {mainactivity.getmainactivity (). ClearScore (); for (int y = 0, y < 4; y++) {for (int x = 0; x &lt ; 4; X + +) {cardsmap[x][y].setnum (0);}} Addrandomnum (); Addrandomnum ();} private void Addrandomnum () {emptypoints.clear (); for (int y = 0, y < 4; y++) {for (int x = 0; x < 4; + +) {if (CARDSM Ap[x][y].getnum () <=0) {Emptypoints.add (new point (x, Y));}}} Point P = emptypoints.remove ((int) (Math.random () *emptypoints.size ())) Cardsmap[p.x][p.y].setnum (Math.random () > 0.1?2:4);}  private void Swipeleft () {Boolean merge = false;for (int y = 0; y < 4; y++) {for (int x = 0; x < 4; + +) {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].getnum ()); cardsMap[x1][y ].setnum (0); Mainactivity.getmainactivity (). Addscore (Cardsmap[x][y].getnum ()); merge = true;} Break;}}}} if (merge) {addrandomnum (); Checkcomplete ();}} private void Swiperight () {Boolean merge = false;for (int y = 0; y < 4; y++) {for (int x = 3, x >=0; x--) {for (int x 1 = x-1; X1 >=0; 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].getnum ()); cardsMap[x1][y ].setnum (0); Mainactivity.getmainactivity (). Addscore (Cardsmap[x][y].getnum ()); merge = true;} Break;}}}} if (merge) {addrandomnum (); Checkcomplete ();}}  private void Swipeup () {Boolean merge = false;for (int x = 0; x < 4; + +) {for (int y = 0; y < 4, y++) {for (int y1 = y+1; Y1 < 4; y1++) {if (Cardsmap[x][y1].getnum () >0) {if (Cardsmap[x][y].getnum () <=0) {Cardsmap[x][y].setnum (cardsMap[x][y1 ].getnum ()); Cardsmap[x][y1].setnum (0); y--;merge = true;} else if (Cardsmap[x][y].equals (Cardsmap[x][y1])) {Cardsmap[x][y].setnum (Cardsmap[x][y].getnum ()); cardsMap[x][y1 ].setnum (0); Mainactivity.getmainactivity (). Addscore (Cardsmap[x][y].getnum ()); merge = true;} Break;}}}} if (merge) {addrandomnum (); Checkcomplete ();}}  private void Swipedown () {Boolean merge = false;for (int x = 0; x < 4; + +) {for (int y = 3; y >=0; y--) {for (int y1 =Y-1; Y1 >=0; y1--) {if (Cardsmap[x][y1].getnum () >0) {if (Cardsmap[x][y].getnum () <=0) {Cardsmap[x][y].setnum (cardsMap[x][y1 ].getnum ()); Cardsmap[x][y1].setnum (0); y++;merge = true;} else if (Cardsmap[x][y].equals (Cardsmap[x][y1])) {Cardsmap[x][y].setnum (Cardsmap[x][y].getnum ()); cardsMap[x][y1 ].setnum (0); Mainactivity.getmainactivity (). Addscore (Cardsmap[x][y].getnum ()); merge = true;} Break;}}}} if (merge) {addrandomnum (); Checkcomplete ();}} private void Checkcomplete () {Boolean complete = true; all:for (int y = 0; y < 4; y++) {for (int x = 0; x < 4; + +) {if (Cardsmap[x][y].getnum () ==0| | (X>0&&cardsmap[x][y].equals (cardsmap[x-1][y)) | | (X<3&&cardsmap[x][y].equals (cardsmap[x+1][y)) | | (Y>0&&cardsmap[x][y].equals (cardsmap[x][y-1)) | | (Y<3&&cardsmap[x][y].equals (cardsmap[x][y+1]))) {complete = False;break All;}}} if (complete) {New Alertdialog.builder (GetContext ()). Settitle ("Hello"). Setmessage ("Game Over"). Setpositivebutton ("Redo", new Dialoginterface.oncLicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {startgame ();}}). Show ();}} Private card[][] Cardsmap = new Card[4][4];p rivate list<point> emptypoints = new arraylist<point> ();}

Import Android.content.context;import Android.view.gravity;import Android.widget.framelayout;import Android.widget.textview;public class Card extends Framelayout {public Card (context context) {super (context); label = new T Extview (GetContext ()); Label.settextsize (+); Label.setbackgroundcolor (0X33FFFFFF); Label.setgravity ( Gravity.center); Layoutparams LP = new Layoutparams ( -1,-1), Lp.setmargins (0, 0); AddView (label, LP); Setnum (0);} private int num = 0;public int getnum () {return num;} public void setnum (int num) {this.num = Num;if (num<=0) {Label.settext ("");} Else{label.settext (num+ "");}} public boolean equals (Card o) {return getnum () ==o.getnum ();} Private TextView label;}

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "com.example.a2048.MainActivity" android:orientation= "vertical" > <linearlayout android:la    Yout_width= "Fill_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text=        "@string/score"/> <textview android:id= "@+id/gamescore" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/></linearlaYout><com.example.a2048.gameview android:id= "@+id/gameview" android:layout_width= "Fill_parent" Android:layo ut_height= "0DP" android:layout_weight= "1" > </com.example.a2048.gameView></LinearLayout>



android--2048 Games

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.