2048源码(核心算法有,缺少几个anctionbar,以后补上)

2048游戏基本上有四部分组成,
1:主activity,包含游戏块的16个方格,上面统计分数的模块
2:底下的gridview,监听上下左右的滑动,进行事件处理,
3:每一个卡片,里面的内容很简单,只有一个text,记录显示的数字
4:Actionbar,是游戏用重新开始,设置等功能(这个在底下可以下载的代码里面还没有实现)


写代码的流程
1:设计游戏的布局,基本是两块,上面是分数模块,下面是gridview
2:代码实现gridview(组合控件)
3:判断处理使gridview可以监听上下左右滑动的事件
4:实现gridview中的card类,里面就是一个textview(组合控件)
5:在gridview中添加card
6:在游戏的初始时随机添加两个数字
7:实现游戏逻辑,进行上下左右的事件处理
8:计分,实现上面分数统计的模块
9:检查游戏结束
10:优化处理,不同数字具有不同的颜色
11:添加动画效果,我的其他文章中有专门对动画进行的阐述
12:添加anctionbar继续进行优化


主activity的页面布局:activity_main.xml

<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"
    android:background="#EAEAEE"
    tools:context=".MainActivity" >
	    
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="5" >
        
         <LinearLayout
             android:layout_marginLeft="30dp" 
             android:layout_marginTop="30dp"
             android:layout_marginBottom="30dp"
             android:layout_width="120dp"
             android:layout_height="250dp"
             android:background="@drawable/yellowrounded_half_bg">
             
             <TextView
                 android:id="@+id/now_most_score"
	             android:gravity="center"
                 android:layout_width="fill_parent"
                 android:textSize="80px"
                 android:textColor="#FFFFFF" 
                 android:textStyle="bold"
                 android:layout_height="100dp" />

	      </LinearLayout>
	      
         <LinearLayout
             android:layout_marginLeft="170dp" 
             android:layout_marginTop="5dp"
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:orientation="vertical"
             android:background="@drawable/orangerounded_half_bg">
             
             <TextView
                 android:layout_width="fill_parent"
                 android:textSize="26px"
                 android:text="@string/score"
                 android:textColor="#FF6666" 
                 android:textStyle="bold"
                 android:layout_height="40dp" />
             
             <TextView
                 android:id="@+id/now_all_score"
                 android:layout_width="fill_parent"
                 android:textSize="26px"
                 android:textColor="#FFFFFF" 
                 android:textStyle="bold"
                 android:text="0"
                 android:layout_height="40dp" />

	      </LinearLayout>
	      
         <LinearLayout
             android:layout_marginLeft="265dp" 
             android:layout_marginTop="5dp"
             android:layout_width="80dp"
             android:orientation="vertical"
             android:layout_height="80dp"
             android:background="@drawable/orangerounded_half_bg">
             
             <TextView
                 android:layout_width="fill_parent"
                 android:textSize="26px"
                 android:text="@string/main_score"
                 android:textColor="#FF6666" 
                 android:textStyle="bold"
                 android:layout_height="40dp" />
             
             <TextView
                 android:id="@+id/history_most_score"
                 android:layout_width="fill_parent"
                 android:textSize="26px"
                 android:text="0"
                 android:textColor="#FFFFFF" 
                 android:textStyle="bold"
                 android:layout_height="40dp" />

	      </LinearLayout>
	      
         <ImageView 
             android:layout_marginLeft="265dp" 
             android:layout_marginTop="105dp"
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:background="@drawable/sf"   />
         
         <LinearLayout
             android:layout_marginLeft="170dp" 
             android:layout_marginTop="105dp"
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:orientation="vertical"
             android:background="@drawable/orangerounded_half_bg">
             
             <TextView
                 android:layout_width="fill_parent"
                 android:textSize="26px"
                 android:text="@string/author"
                 android:textColor="#FF6666" 
                 android:textStyle="bold"
                 android:layout_height="40dp" />
             
             <TextView
                 android:layout_width="fill_parent"
                 android:textSize="26px"
                 android:textColor="#FFFFFF" 
                 android:textStyle="bold"
                 android:text="@string/author_name"
                 android:layout_height="40dp" />

	      </LinearLayout>
        
    </RelativeLayout>
    
    <com.example.game2048.MyGridView
	    android:id="@+id/girdlayout"
	    android:layout_width="fill_parent"
	    android:layout_height="0dp"
	    android:layout_weight="9"  >
	</com.example.game2048.MyGridView>
	
</LinearLayout>


MainActivity.java
package com.example.game2048;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private TextView now_most_score,now_all_score,history_most_score;
	private int most_score =  0 , all_score = 0;
	private static MainActivity mainactivity;

	public MainActivity() {
		super();
		mainactivity = this;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		now_most_score = (TextView) findViewById(R.id.now_most_score);
		now_all_score = (TextView) findViewById(R.id.now_all_score);
		history_most_score = (TextView) findViewById(R.id.history_most_score);
	}
	
	public static MainActivity getMainActivity(){
		return mainactivity;
	}
	
	public void clearScore(){
		
		most_score =  0;
		all_score = 0;
		
		showScore();
	}
	
	private void showScore(){
		SharedPreferences mySharedPreferences= getSharedPreferences("my2048", Activity.MODE_PRIVATE); 
		SharedPreferences.Editor editor = mySharedPreferences.edit(); 
		now_most_score.setText( most_score + "");
		now_all_score.setText( all_score + "");
		history_most_score.setText(mySharedPreferences.getInt("his_score", 0)+"");
		if(all_score > Integer.parseInt(history_most_score.getText().toString()) ){
			history_most_score.setText(all_score+"");
			editor.putInt("his_score", all_score); 
			//提交当前数据 
			editor.commit(); 
		}
	}
	
	public void addScore(int p,int s){
		most_score = p;
		all_score += s;
		showScore();
	}
	
}


MyGridView.java
package com.example.game2048;

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 MyGridView extends GridLayout {
	
	private MyCard[][] cards = new MyCard[4][4];
	private List<Point> emptyPoints = new ArrayList<Point>();

	public MyGridView(Context context) {
		super(context);
		initGame();
	}
	
	public MyGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initGame();
	}
	
	public MyGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initGame();
	}
	
	private void initGame(){
		setColumnCount(4);
//		setBackgroundColor(0xffFAF9DE);
//		setBackground(getResources().getDrawable(R.drawable.bk_pq));
		
		setOnTouchListener(new OnTouchListener() {
			
			private float startx,starty,offerx,offery;
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				switch (arg1.getAction()) {
				case MotionEvent.ACTION_DOWN:
					startx = arg1.getX();
					starty = arg1.getY();
					break;
				case MotionEvent.ACTION_UP:
					offerx = arg1.getX() - startx;
					offery = arg1.getY() - starty;
					if(Math.abs(offerx) > Math.abs(offery)){
						//横向偏移量大
						if(offerx > 5){
							moveright();
						}else if(offerx < -5){
							moveleft();
						}
					}else{
						//纵向偏移量大
						if(offery > 5){
							movedown();
						}else if(offery < -5){
							moveup();
						}
					}
					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)-10)/4;
		
		addCard(cardwidth,cardwidth);
		
		startGanme();
	}

	private void startGanme() {
		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4; x++) {
				cards[x][y].setNum(0);
			}
		}
		
		addRandom();
		addRandom();
		showScore(0);
	}

	private void addCard(int cardWidth, int cardHeight) {
		
		MyCard c ;
		
		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4; x++) {
				c = new MyCard(getContext());
				c.setNum(2);
				addView(c, cardWidth, cardHeight);
				cards[x][y] = c;
			}
		}
	}
	
	private void addRandom(){
		emptyPoints.clear();
		
		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4; x++) {
				if(cards[x][y].getNum()<=0){
					emptyPoints.add(new Point(x, y));
				}
				cards[x][y].setBackGroundColor(cards[x][y].getNum());
			}
		}
		
		Point p = emptyPoints.remove((int)(Math.random()*emptyPoints.size()));
		cards[p.x][p.y].setNum(Math.random()>0.1?2:4); 
	}

	private void moveup(){
		boolean flag = false;
		for (int x = 0; x < 4; x++) {
			for (int y = 0; y < 4; y++) {
				for (int y1 = y+1; y1 < 4; y1++) {
					if(cards[x][y1].getNum() > 0){
						
						if (cards[x][y].getNum()<=0) {
							cards[x][y].setNum(cards[x][y1].getNum());
							cards[x][y1].setNum(0);
							y--;
							flag = true;
						}else if (cards[x][y].equals(cards[x][y1])){
							cards[x][y].setNum(cards[x][y].getNum()*2);
							cards[x][y1].setNum(0);
							showScore(cards[x][y].getNum());
							flag = true;
						}
						break;
					}
				}
			}
		}
		if(flag){
			addRandom();
			if(checkComplete()){
				showAgainDialog();
			}
		}
	}
	
	private void movedown(){
		boolean flag = false;
		for (int x = 0; x < 4; x++) {
			for (int y = 3; y >= 0; y--) {
				for (int y1 = y-1; y1 >= 0; y1--) {
					if(cards[x][y1].getNum() > 0){
						
						if (cards[x][y].getNum()<=0) {
							cards[x][y].setNum(cards[x][y1].getNum());
							cards[x][y1].setNum(0);
							y++;
							flag = true;
						}else if (cards[x][y].equals(cards[x][y1])){
							cards[x][y].setNum(cards[x][y].getNum()*2);
							cards[x][y1].setNum(0);
							showScore(cards[x][y].getNum());
							flag = true;
						}
						break;
					}
				}
			}
		}
		if(flag){
			addRandom();
			if(checkComplete()){
				showAgainDialog();
			}
		}
	}

	private void moveleft(){
		boolean flag = false;
		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4; x++) {
				for (int x1 = x+1; x1 < 4; x1++) {
					if(cards[x1][y].getNum() > 0){
						
						if (cards[x][y].getNum()<=0) {
							cards[x][y].setNum(cards[x1][y].getNum());
							cards[x1][y].setNum(0);
							x--;
							flag = true;
						}else if (cards[x][y].equals(cards[x1][y])){
							cards[x][y].setNum(cards[x][y].getNum()*2);
							cards[x1][y].setNum(0);
							showScore(cards[x][y].getNum());
							flag = true;
						}
						break;
					}
				}
			}
		}
		if(flag){
			addRandom();
			if(checkComplete()){
				showAgainDialog();
			}
		}
	}

	private void moveright(){
		boolean flag = false;
		for (int y = 0; y < 4; y++) {
			for (int x = 3; x >= 0 ; x--) {
				for (int x1 = x-1; x1 >= 0; x1--) {
					if(cards[x1][y].getNum() > 0){
						
						if (cards[x][y].getNum()<=0) {
							cards[x][y].setNum(cards[x1][y].getNum());
							cards[x1][y].setNum(0);
							x++;
							flag = true;
						}else if (cards[x][y].equals(cards[x1][y])){
							cards[x][y].setNum(cards[x][y].getNum()*2);
							cards[x1][y].setNum(0);
							showScore(cards[x][y].getNum());
							flag = true;
						}
						break;
					}
				}
			}
		}
		if(flag){
			addRandom();
			if(checkComplete()){
				showAgainDialog();
			}
		}
	}

	private void showAgainDialog() {
		new AlertDialog.Builder(getContext()).setTitle("Loser").setMessage("Again").setPositiveButton("Again", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				startGanme();
			}
		}).show();

	}

	private boolean checkComplete() {
		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4 ; x++) {
				if(cards[x][y].getNum()==0 || 
					(x > 0 && cards[x-1][y].equals(cards[x][y])) || 
					(x < 3 && cards[x+1][y].equals(cards[x][y])) || 
					(y > 0 && cards[x][y-1].equals(cards[x][y])) || 
					(y < 3 && cards[x][y+1].equals(cards[x][y])) ){
					return false;
				}
			}
		}
		return true;
	}
	
	private void showScore(int s) {
		int large = 0;
		for (int y = 0; y < 4; y++) {
			for (int x = 0; x < 4 ; x++) {
				if(large < cards[x][y].getNum()){
					large = cards[x][y].getNum();
				}
			}
		}
		MainActivity.getMainActivity().addScore(large,s);
	}

}


MyCard.java
package com.example.game2048;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

public class MyCard extends FrameLayout {
	
	private int num = 0 ;
	private TextView textview;

	public MyCard(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public MyCard(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public MyCard(Context context) {
		super(context);
		init();
	}
	
	private void init(){
		textview = new TextView(getContext());
		textview.setTextSize(32);
		textview.setGravity(Gravity.CENTER);
		textview.setBackgroundColor(0x33ffffff);
		
		LayoutParams lp = new LayoutParams(-1, -1);
		lp.setMargins(10, 10, 0, 0);
		addView(textview, lp);
		setNum(0);
	}
	
	public int getNum(){
		return num;
	}

	public void setNum(int num){
		this.num = num;
		if(num<=0){
			textview.setText("");
		}else{
			textview.setText(num+"");
		}
	}
	
	public boolean equals(MyCard c){
		return getNum() == c.getNum();
	}
	
	public void setBackGroundColor(int num){
		switch (num) {
		case 2:
			textview.setBackgroundColor(0x33ffffff);
			break;
		case 4:
			textview.setBackgroundColor(0x33FFFFCC);
			break;
		case 8:
			textview.setBackgroundColor(0x33FF6666);
			break;
		case 16:
			textview.setBackgroundColor(0x33FF3300);
			break;
		case 32:
			textview.setBackgroundColor(0x33FF33FF);
			break;
		case 64:
			textview.setBackgroundColor(0x330033FF);
			break;
		case 128:
			textview.setBackgroundColor(0x3366FF66);
			break;
		case 256:
			textview.setBackgroundColor(0x3300CC33);
			break;
		case 512:
			textview.setBackgroundColor(0x33009933);
			break;
		case 1024:
			textview.setBackgroundColor(0x33CCCC00);
			break;
		case 2048:
			textview.setBackgroundColor(0x33CC0000);
			break;
		case 4096:
			textview.setBackgroundColor(0x33FFCC99);
			break;
		case 8192:
			textview.setBackgroundColor(0x33FFFF00);
			break;
		default:
			textview.setBackgroundColor(0x33ffffff);
			break;
		}
	}

}

你可能感兴趣的:(2048)