[Unity3D+算法]一小时做个2048
原地址:http://blog.csdn.net/dingxiaowei2013/article/details/36462749
048是继FlappyBird之后另一个比较热的轻量级的手游,简单易玩。最近要离职原先的公司——因为我想做游戏,虽然玩游戏不是很多,但还是热爱开发游戏,因此就想去一家游戏公司,感觉对老板有一点愧疚和感激,愿原公司发展越来越好,用灰太狼的话讲,我还会回来的,哈哈!即将入职新公司,听说压力会很大,加班无止境,加班其实我到不怕,乘年轻,还有拼劲,加班算什么,其实只要自己能做出东西,感觉有成就感,倒还是喜欢花更多的时间去做东西,最近处于过渡期,写写之前公司的工作小结,还不是很忙,今天花了一个多小时,自己想了一下2048的算法,然后将其实现,可能算法不是那么优,还望批评交流!
效果图
实现的还比较粗糙,贴出主要逻辑代码,仅供参考,欢迎给出更优算法!
- using UnityEngine;
- using System.Collections;
- public class NewBehaviourScript : MonoBehaviour
- {
- public UILabel valueLabel;
- bool gameover = false;
- void Start()
- {
- gameover = false;
- valueLabel = GameObject.Find("ValueLabel").GetComponentInChildren<UILabel>();
- valueLabel.text = "Game Start";
- valueLabel.color = Color.green;
- }
- // Update is called once per frame
- void Update()
- {
- if (!gameover)
- {
- if (Input.GetKeyDown(KeyCode.D))
- {
- moveR();
- CreateNumber();
- }
- else if (Input.GetKeyDown(KeyCode.A))
- {
- moveL();
- CreateNumber();
- }
- else if (Input.GetKeyDown(KeyCode.W))
- {
- moveU();
- CreateNumber();
- }
- else if (Input.GetKeyDown(KeyCode.S))
- {
- moveD();
- CreateNumber();
- }
- }
- }
- void moveU()
- {
- for (int i = 1; i <= 4; i++)
- {
- bool flag = false;
- for (int j = 2; j <= 4; j++)
- {
- for (int k = j - 1; k >= 1; k--)
- {
- //获取当前元素
- GameObject go = GameObject.Find("L" + (k + 1).ToString() + i.ToString());
- print("当前对象" + go.name);
- UILabel I = go.GetComponentInChildren<UILabel>();
- //获取下一个元素
- GameObject goNext = GameObject.Find("L" + k.ToString() + i.ToString());
- print("下一个对象" + goNext.name);
- UILabel INext = goNext.GetComponentInChildren<UILabel>();
- //比较代码
- if (I.text != "")
- {
- if (INext.text == "")
- {
- INext.text = I.text;
- I.text = "";
- }
- else if (I.text == INext.text)
- {
- if (!flag)
- {
- int a = int.Parse(INext.text) + int.Parse(I.text);
- INext.text = a.ToString();
- I.text = "";
- flag = true;
- }
- }
- }
- }
- }
- }
- }
- void moveD()
- {
- for (int i = 1; i <= 4; i++)
- {
- bool flag = false;
- for (int j = 3; j >= 1; j--)
- {
- for (int k = j + 1; k <= 4; k++)
- {
- //获取当前元素
- GameObject go = GameObject.Find("L" + (k-1).ToString() + i.ToString());
- print("当前对象" + go.name);
- UILabel I = go.GetComponentInChildren<UILabel>();
- //获取下一个元素
- GameObject goNext = GameObject.Find("L" + k.ToString() + i.ToString());
- print("下一个对象" + goNext.name);
- UILabel INext = goNext.GetComponentInChildren<UILabel>();
- //比较代码
- if (I.text != "")
- {
- if (INext.text == "")
- {
- INext.text = I.text;
- I.text = "";
- }
- else if (I.text == INext.text)
- {
- if (!flag)
- {
- int a = int.Parse(INext.text) + int.Parse(I.text);
- INext.text = a.ToString();
- I.text = "";
- flag = true;
- }
- }
- }
- }
- }
- }
- }
- void moveL()
- {
- for (int i = 1; i <= 4; i++)
- {
- bool flag = false;
- for (int j = 2; j <= 4; j++)
- {
- for (int k = j - 1; k >=1 ; k--)
- {
- //获取当前元素
- GameObject go = GameObject.Find("L" + i.ToString() + (k + 1).ToString());
- print("当前对象" + go.name);
- UILabel I = go.GetComponentInChildren<UILabel>();
- //获取下一个元素
- GameObject goNext = GameObject.Find("L" + i.ToString() + k.ToString());
- print("下一个对象" + goNext.name);
- UILabel INext = goNext.GetComponentInChildren<UILabel>();
- //比较代码
- if (I.text != "")
- {
- if (INext.text == "")
- {
- INext.text = I.text;
- I.text = "";
- }
- else if (I.text == INext.text)
- {
- if (!flag)
- {
- int a = int.Parse(INext.text) + int.Parse(I.text);
- INext.text = a.ToString();
- I.text = "";
- flag = true;
- }
- }
- }
- }
- }
- }
- }
- void moveR()
- {
- for (int i = 1; i <= 4; i++)
- {
- bool flag = false;
- for (int j = 3; j >= 1; j--)
- {
- for (int k = j + 1; k <= 4; k++)
- {
- //获取当前元素
- GameObject go = GameObject.Find("L" + i.ToString() + (k - 1).ToString());
- print("当前对象" + go.name);
- UILabel I = go.GetComponentInChildren<UILabel>();
- //获取下一个元素
- GameObject goNext = GameObject.Find("L" + i.ToString() + k.ToString());
- print("下一个对象" + goNext.name);
- UILabel INext = goNext.GetComponentInChildren<UILabel>();
- //比较代码
- if (I.text != "")
- {
- if (INext.text == "")
- {
- INext.text = I.text;
- I.text = "";
- }
- else if (I.text == INext.text)
- {
- if (!flag)
- {
- int a = int.Parse(INext.text) + int.Parse(I.text);
- INext.text = a.ToString();
- I.text = "";
- flag = true;
- }
- }
- }
- }
- }
- }
- }
- void CreateNumber()
- {
- int count = 0;
- bool flag = false;
- for (int i = 1; i <= 4; i++)
- {
- if (!flag)
- {
- for (int j = 1; j <= 4; j++)
- {
- GameObject go = GameObject.Find("L" + i.ToString() + j.ToString());
- UILabel label = go.GetComponentInChildren<UILabel>();
- if (label.text == "")
- {
- int r = Random.Range(1, 10);
- if (r <= 5)
- {
- int value = Random.Range(1, 5);
- if (value < 4)
- label.text = "2";
- else
- label.text = "4";
- flag = true;
- break;
- }
- }
- else
- {
- if (++count == 16)
- {
- valueLabel.text = "Game Over";
- valueLabel.color = Color.red;
- gameover = true;
- }
- }
- }
- }
- else
- break;
- }
- }
- }
[Unity3D+算法]一小时做个2048的更多相关文章
- Unity3D使用碰撞体做触发器实现简单的自己主动开门
在游戏制作中触发器的使用很的方便也很有用. 这一张我们简介一下怎样使用一个简单的触发器来实现自己主动开门关门的效果. 首先确保你已经对门进行了动画的设置. 详细流程例如以下. 选择Window- ...
- 分布式ID系列(5)——Twitter的雪法算法Snowflake适合做分布式ID吗
介绍Snowflake算法 SnowFlake算法是国际大公司Twitter的采用的一种生成分布式自增id的策略,这个算法产生的分布式id是足够我们我们中小公司在日常里面的使用了.我也是比较推荐这一种 ...
- Recommending music on Spotify with deep learning 采用深度学习算法为Spotify做基于内容的音乐推荐
本文参考http://blog.csdn.net/zdy0_2004/article/details/43896015译文以及原文file:///F:/%E6%9C%BA%E5%99%A8%E5%AD ...
- 编程算法 - 不用加减乘除做加法 代码(C)
不用加减乘除做加法 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 写一个函数, 求两个整数之和, 要求在函数体内不得使用+, -, *, /四 ...
- Unity3d 子线程能做的事
一,子线程中能做的事: 1,数据逻辑方面计算: 二,子线程中,不能: 1,加载场景相关事件: Application.LoadLevelAsync.Application.LoadLevel等: 2, ...
- ACM用到的算法。先做个笔记,记一下
ACM 所有算法 数据结构 栈,队列,链表 哈希表,哈希数组 堆,优先队列 双端队列 可并堆 左偏堆 二叉查找树 Treap 伸展树 并查集 集合计数问题 二分图的识别 平衡二叉树 二叉排序树 线段树 ...
- 深入浅出游戏算法(4)-unity3d算法(1)-球转动
球 转动 按以下布局放置好unity3d的各个组件.设置好渲染.位置.光源.大小等 麦好的AI乐园博客全部内容是原创,假设转载请注明来源 http://blog.csdn.net/myhaspl/ 编 ...
- 案例(一) 利用机器算法RFM模型做用户价值分析
一.案例背景 在产品迭代过程中,通常需要根据用户的属性进行归类,也就是通过分析数据,对用户进行归类,以便于在推送及转化过程中获得更大的收益. 本案例是基于某互联网公司的实际用户购票数据为研究对象, ...
- Unity3d 用NGUI制作做新手引导的思路
一.先看下效果 Prefab结构 二.实现思路: 1.prefab上的Panel层级设置成较高 2.背景由5个UISprite拼接起来的,4个(L,R,U,D)当作遮罩,1个镂空(Hollow)当作点 ...
随机推荐
- maven 练习
新建项目: Next next next 新建项目后,MyEclipse会自动从远程仓库中下载支持包,需要几分钟左右时间. 项目结构图: HelloWorld.java public class He ...
- C# 中有关 using 关键字
关于 C# 中的 using 关键字 我们往往只在代码的开头使用 using 关键字来引入名称空间,这是 using 的一个最常见的使用. 但是,using 关键字是否只有这么一处用武之地吗? 下面, ...
- BMP文件格式分析
前两天要做一个读取bmp文件的小程序,顺便查找了一些关于BMP格式的文章,现在post上来. 简介 BMP(Bitmap-File)图形文件是Windows采用的图形文件格式,在Windows环境下运 ...
- exynos 4412 eMMC配置及使用方法
/** ****************************************************************************** * @author Maox ...
- c# 取得ip地址和网关
/// <summary> /// 得到本机IP /// </summary> private string GetLocalIP() { //本机IP地址 string st ...
- 万能的SQLHelper帮助类
/// <summary> /// 数据库帮助类 /// </summary> public class SQLHelper { private static string c ...
- CentOS 6.4 下搭建 MongoDB 2.4.9 环境
一.下载MongoDB2.4.9版 下载MongoDB wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.9.tgz 解压Mo ...
- 给view 添加事件
//绑定图片点击事件 UITapGestureRecognizer *g=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@select ...
- 反编译APK终结教程
现在来教大家如何由网上下载的Android应用反编译为源码.如果你感兴趣,就来看一看吧.前提是你的电脑得已经配置好了java环境,如果没有配置好的话,下面我会附带一提,如果你还是不懂的话,那就上网搜一 ...
- 【转载】Powershell连接世纪互联Office365
$User = "admin@contoso.com" $PWord = ConvertTo-SecureString –String "password" – ...