Home > Mobile >  Android 2048 games, virtual machine couldn't show the Numbers
Android 2048 games, virtual machine couldn't show the Numbers

Time:09-25

Is such images, sliding and score changes,


Code:

Package com. Example. Administrator. Game2048;

The import android. App. AlertDialog;
The import android. The content. The Context;
Import android. Content. DialogInterface.
The import android. Graphics. Point;
The import android. Util. AttributeSet;
The import android. View. MotionEvent;
The import android. View. The view;
The import android. Widget. GridLayout;

import java.util.ArrayList;
Import the Java. Util. List;

Public class GameView extends GridLayout {
Public GameView (Context Context, AttributeSet attrs, int defStyle) {
Super (context, attrs, defStyle);
InitGameView ();
}
Public GameView (Context Context, AttributeSet attrs) {
super(context, attrs);
InitGameView ();
}

Public GameView Context (Context) {
Super (context);
InitGameView ();
}


//game initialization method
Private void initGameView () {
SetColumnCount (4);
//set the panel into four columns
Xffbbada0 setBackgroundColor (0);
System. The out. Println (" initGameView ");

SetOnTouchListener (new the OnTouchListener () {
/*
* startX: mobile X coordinates on the screen, for the first
* startY: phone Y coordinates on the screen, for the first
* offsetX, offsetY, fingers on the screen of the X, Y offset on
*/
Private float startX, startY, offsetX, offsetY;
@ Override
Public Boolean the onTouch (View v, MotionEvent event) {
The 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) & gt; Math. Abs (offsetY)) {
If (offsetX & lt; 5) {
SwipeLeft ();
System. The out. Println (" Left ");

} else if (offsetX & gt; 5) {
SwipeRight ();
System. The out. Println (" Right ");
}

} else {
If (offsetY & lt; 5) {
SwipeUp ();
System. The out. Println (" Up ");
} else if (offsetY & gt; 5) {
SwipeDown ();
System. The out. Println (" Down ");
}
}
break;
}
return true;
}
});
}
//when the size of the layout change, executes the method
Protected void onSizeChanged (
Int w, int h, int oldw, int oldh) {
Super. OnSizeChanged (w, h, oldw, oldh);
Int cardWidth=(Math. Min (w, h) - 10)/4.
AddCards (cardWidth cardWidth);
The startGame ();
}
The method of adding CARDS//
Private void addCards (int cardWidth, int cardHeight) {
Card c;
//traverse each card
For (int y=0; Y & lt; 4. Y++) {
For (int x=0; x <4. X++) {
C=new Card (getContext ());
C.s. etNum (0);
AddView (c, cardWidth, cardHeight);
CardsMap [x] [y]=c;
}
}
}
//the method of the game
Public void the startGame () {
MainActivity. GetMainActivity (.) clearScore ();
For (int y=0; Y & lt; 4. Y++) {
For (int x=0; x <4. X++) {
CardsMap [x] [y] setNum (0);
EmptyPoint. Add (new Point (x, y));
}
}
AddRandomNum ();
AddRandomNum ();
}
//add a random number
Private void addRandomNum () {
EmptyPoint. The clear ();
For (int y=0; Y & lt; 4. Y++) {
For (int x=0; x <4. X++) {
If (cardsMap [x] [y] getNum () & lt;=0) {
EmptyPoint. Add (new Point (x, y));

}
}
}
Point p=emptyPoint
. Remove ((int) (Math. The random () * emptyPoint in size ()));
CardsMap [p.x] [p.y] setNum (math.h random () & gt; 0.1? 2, 4);
}
//scroll left
Public void swipeLeft () {
Boolean meger=false;
For (int y=0; Y & lt; 4. Y++) {
For (int x=0; x <4. X++) {
For (int x1=x + 1; The x1 & lt; 4. {x1 + +)
If (cardsMap [x1] [y] getNum () & gt; 0 {
If (cardsMap [x] [y] getNum () & lt;=0) {
/*
* to subscript as (x, y) location of the Numbers on the card
* set to coordinate for (x1, y) location on the value of the card;
* the second step, the coordinates (x1, y) location of the Numbers on the card
* is set to 0
* (i.e., into the empty card)
*/
CardsMap [x] [y] setNum (
CardsMap [x1] [y] getNum ());
CardsMap [x1] [y] setNum (0);
X -;
Meger=true;
break;
} else if (cardsMap [x] [y] equals (cardsMap [x1] [y])) {
CardsMap [x] [y] setNum (cardsMap [x] [y] getNum () * 2);
CardsMap [x1] [y] setNum (0);
MainActivity. GetMainActivity ().
AddScore (cardsMap [x] [y] getNum ());
Meger=true;
}
break;
}
}
}
}
If (have) {
AddRandomNum ();
CheckComplete ();
}
}
//right
Public void swipeRight () {
Boolean meger=false;
For (int y=0; Y & lt; 4. Y++) {
For (int x=3; X & gt;=0; X -) {
For (int x1=x - 1; The x1 & gt;=0; {x1 -)
If (cardsMap [x1] [y] getNum () & gt; 0 {
If (cardsMap [x] [y] getNum () & lt;=0) {
/*
* to subscript as (x, y) location of the Numbers on the card
* set to coordinate for (x1, y) location on the value of the card;
* the second step, the coordinates (x1, y) location of the Numbers on the card
* is set to 0
* (i.e., into the empty card)
*/
CardsMap [x] [y] setNum (
CardsMap [x1] [y] getNum ());
CardsMap [x1] [y] setNum (0);
x++;
Meger=true;
break;
} else if (cardsMap [x] [y] equals (cardsMap [x1] [y])) {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related