当前位置:首页 > 开发 > 移动开发 > 正文

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

发表于: 2014-09-25   作者:不懂事的小屁孩   来源:转载   浏览:
摘要: 2048游戏基本上有四部分组成, 1:主activity,包含游戏块的16个方格,上面统计分数的模块 2:底下的gridview,监听上下左右的滑动,进行事件处理, 3:每一个卡片,里面的内容很简单,只有一个text,记录显示的数字 4:Actionbar,是游戏用重新开始,设置等功能(这个在底下可以下载的代码里面还没有实现) 写代码的流程 1:设计游戏的布局,基本是两块,上面是分
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源码(核心算法有,缺少几个anctionbar,以后补上)

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
简介 前面讨论的很多文章里,都是针对无向图进行的分析。无向图的一个特性就是其中一旦两个节点a和b
2048游戏最近很火,想看下源码,却不会JavaScript。网上搜了搜安卓版的源码,尝试下来学习。 ubersp
原文出处: 张洋(@敲代码的张洋) 针对目前火爆的2048游戏,有人实现了一个AI程序,可以以较大概率
问题 我最近偶然发现一款叫2048的游戏。你需要通过上、下、左、右的方向移动来合并值相同的方块(Tit
***************************************转载请注明出处:http://blog.csdn.net/lttree***********
转载请标明是引用于 http://blog.csdn.net/chenyujing1234 前段时间在CSDN的资源里载得五子棋VC的源
基于MeanShift的Camshift算法原理详解(整理) 第一篇MeanShift原理和实现 1 MeanShift原理 如下图所
上一篇《是男人就下100层【第五层】——换肤版2048游戏》中阳光小强对2048游戏用自己的方式进行了实
分页控件、查询控件、显示数据的控件和表单控件,终于把这几个控件结合在一起了,和在一起之后就可
本文主要介绍了ASP.NET AJAX的几个核心控件的作用和具体用法。 包括:Scriptmanager,Updatepanal,
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号