Card.class
package com.example.myroom.androidgame2048;
import android.content.Context;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.w3c.dom.Text;
/**
Created by myroom on 2016/12/2.
*/
public class Card extends FrameLayout {
public Card(Context context){
super(context);
label=new TextView(getContext());
label.setTextSize(32);
label.setBackgroundColor(0x33ffffff);
label.setGravity(Gravity.CENTER);
LayoutParams lp= new LayoutParams(-1,-1);
// GridLayout.LayoutParams ll=new GridLayout.LayoutParams(lp);
lp.setMargins(20,20,0,0);
addView(label,lp);
setNum(0);
}
private int num=0;
private TextView label;
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 card) {
return getNum()==card.getNum();//判断数字是否相同
}
}
GameView.class
package com.example.myroom.androidgame2048;
import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridLayout;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
Created by myroom on 2016/12/2.
*/
public class GameView extends GridLayout {
public GameView(Context context) {
super(context);
InitGameView();
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
InitGameView();
}
public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
InitGameView();
}
private void InitGameView(){
setColumnCount(4);//指明是4列
setBackgroundColor(0xffbbada0);
setOnTouchListener(new View.OnTouchListener() {
private float startX,startY,offsetX,offsetY;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = motionEvent.getX();
startY = motionEvent.getY();
break;
case MotionEvent.ACTION_UP:
offsetX = motionEvent.getX() - startX;
offsetY = motionEvent.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;
}
});
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {//位置变化时
super.onSizeChanged(w, h, oldw, oldh);
int cardWidth=(Math.min(w,h)-20)/4;//像素间隔
addCard(cardWidth,cardWidth);
startgame();
}
private void startgame() {
for(int y=0;y<4;y++){
for(int x=0;x<4;x++){
cardsmap[x][y].setNum(0);
}
}
addRandomNum();
addRandomNum();
addRandomNum();
addRandomNum();
addRandomNum();
addRandomNum();
addRandomNum();
addRandomNum();
addRandomNum();
addRandomNum();
addRandomNum();
}
private void addCard(int cardWidth,int cardheight){
Card c;
for(int y=0;y<4;y++){//添加16张卡片
for(int x=0;x<4;x++){
c=new Card(getContext());
c.setNum(0);
addView(c,cardWidth,cardheight);
cardsmap[x][y]=c;//记录数据
}
}
}
private void addRandomNum() {
emptyPoints.clear();
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
if (cardsmap[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; 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].getNum()*2);
cardsmap[x1][y].setNum(0);
merge=true;
}
break;
}
}
}
}
if(merge){
addRandomNum();
}
}
private void swipeRight(){
Toast.makeText(getContext(),"right",Toast.LENGTH_SHORT).show();
for (int y = 0; y < 4; y++) {
for (int x = 3; x >=0; x--) {
for(int x1=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++;
break;
}else if(cardsmap[x][y].equals(cardsmap[x1][y])){
cardsmap[x][y].setNum(cardsmap[x][y].getNum()*2);
cardsmap[x1][y].setNum(0);
break;
}
}
}
}
}
}
private void swipeup(){
Toast.makeText(getContext(),"up",Toast.LENGTH_SHORT).show();
for (int x = 0; x < 4; x++) {
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--;
break;
}else if(cardsmap[x][y].equals(cardsmap[x][y1])){
cardsmap[x][y].setNum(cardsmap[x][y].getNum()*2);
cardsmap[x][y1].setNum(0);
break;
}
}
}
}
}
}
private void swipeDown(){
Toast.makeText(getContext(),"down",Toast.LENGTH_SHORT).show();
for (int x = 0; x < 4; x++) {
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++;
break;
}else if(cardsmap[x][y].equals(cardsmap[x][y1])){
cardsmap[x][y].setNum(cardsmap[x][y].getNum()*2);
cardsmap[x][y1].setNum(0);
break;
}
}
}
}
}
}
private Card[][] cardsmap=new Card[4][4];
private List<Point>emptyPoints=new ArrayList<Point>();
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context="com.example.myroom.androidgame2048.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:longClickable="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/score"/>
<TextView
android:id="@+id/tvScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<com.example.myroom.androidgame2048.GameView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/gameView"></com.example.myroom.androidgame2048.GameView>
</LinearLayout>