Unity3D 消消乐 三消乐回朔递归算法实现

Eirene ·
更新时间:2024-09-20
· 532 次阅读

Unity3D 消消乐 三消乐回朔递归算法实现

无意中看见很久以前的代码, 在这记录一下 ,新建一个空场景,把脚本挂摄像机上运行就行了,思路是四个方向去递归, 具体都写代码里,末尾有下载链接,

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class C624llk : MonoBehaviour { GameObject goal; //意义不大 HashSet arr = new HashSet(); //需要删除的集合 public string text; //无意义变量 int sum = 0; //无意义变量 int Dsum = 0; //无意义变量 GameObject[,] cube; GameObject f; //意义不大 void Start() { UnityEditorInternal.InternalEditorUtility.AddTag("NPC"); //***只能在unity编译器使用的类,不能打包出去 AddCube(); } /// /// 游戏准备 /// void AddCube() { Camera.main.transform.position = new Vector3(0, 4, -35); Camera.main.transform.localEulerAngles = Vector3.zero; GameObject diban = GameObject.CreatePrimitive(PrimitiveType.Plane); diban.name = "地板"; diban.transform.position = new Vector3(0, -2, 0); diban.transform.localScale= new Vector3(20, 20,20); f = new GameObject(); f.name = "游戏画板"; cube = new GameObject[10, 21]; for (int i = 1; i <= 10; i++) { for (int j = -10; j <= 10; j++) { GameObject b = GameObject.CreatePrimitive(PrimitiveType.Cube); b.transform.position = new Vector3(j, i, -20); b.GetComponent().material.color = Random.ColorHSV(); b.transform.tag = "NPC"; b.name = i + "," + j + ""; b.transform.parent = f.transform; int k = Random.Range(1, 6); switch (k) { case 1: b.GetComponent().material.color = Color.blue; break; case 2: b.GetComponent().material.color = Color.white; break; case 3: b.GetComponent().material.color = Color.red; break; case 4: b.GetComponent().material.color = Color.yellow; break; case 5: b.GetComponent().material.color = Color.grey; break; } //b.AddComponent(); //b.GetComponent().constraints = ~RigidbodyConstraints.FreezePositionY; // b.GetComponent().constraints = ~RigidbodyConstraints.FreezeRotation; // b.GetComponent().useGravity = false; cube[i - 1, j + 10] = b; } } } // Update is called once per frame void Update() { sum += arr.Count; if (sum >= Dsum) { Dsum = sum; } Endpd(); text = "计分 " + sum; arr.Clear(); if (Input.GetMouseButtonDown(0)) { goal = MouseObject(); if (goal != null) { Des(goal); print(arr.Count + "***"); foreach (var item in arr) { Decline(item); DestroyImmediate(item); } } } } /// /// 鼠标点击发射线返回对象 /// /// GameObject MouseObject() { Ray r = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit a; if (Physics.Raycast(r, out a) && a.collider.gameObject.tag == "NPC") { return a.collider.gameObject; } return null; } /// /// 四向递归,找到就存起来 /// /// void Des(GameObject s) { Ray r = new Ray(s.transform.position, Vector3.up); Ray r2 = new Ray(s.transform.position, Vector3.down); Ray r3 = new Ray(s.transform.position, Vector3.left); Ray r4 = new Ray(s.transform.position, Vector3.right); RaycastHit temp; if (Physics.Raycast(r, out temp, 1f) && s.GetComponent().material.color == temp.collider.GetComponent().material.color && !arr.Contains(temp.collider.gameObject)) { arr.Add(s); arr.Add(temp.collider.gameObject); Des(temp.collider.gameObject); } if (Physics.Raycast(r2, out temp, 1f) && s.GetComponent().material.color == temp.collider.GetComponent().material.color && !arr.Contains(temp.collider.gameObject)) { arr.Add(s); arr.Add(temp.collider.gameObject); Des(temp.collider.gameObject); } if (Physics.Raycast(r3, out temp, 1f) && s.GetComponent().material.color == temp.collider.GetComponent().material.color && !arr.Contains(temp.collider.gameObject)) { arr.Add(s); arr.Add(temp.collider.gameObject); Des(temp.collider.gameObject); } if (Physics.Raycast(r4, out temp, 1f) && s.GetComponent().material.color == temp.collider.GetComponent().material.color && !arr.Contains(temp.collider.gameObject)) { arr.Add(s); arr.Add(temp.collider.gameObject); Des(temp.collider.gameObject); } else { //没有找到 } }//回朔消除算法 /// /// 计算需不需要移动 /// /// void Decline(GameObject s) { Ray down = new Ray(s.transform.position, Vector3.down); Ray up = new Ray(s.transform.position, Vector3.up); RaycastHit [] up_; RaycastHit[] down_; up_ = Physics.RaycastAll(up, 20); down_ = Physics.RaycastAll(down, 20); //print(s.name + "*-*"); if (up_.Length == 0&& down_.Length == 1) { Zouyidong(s); return; } if (up_.Length == 0) { return; } else { for (int i = 0; i < up_.Length; i++) { up_[i].collider.gameObject.transform.position += Vector3.down;//整列向下移动 } } } /// /// 整列向左移动 /// /// void Zouyidong(GameObject s) { for (int i = 1; i <=10; i++) { Vector3 t = new Vector3(s.transform.position.x, i, s.transform.position.z); Ray r = new Ray(t, Vector3.right); RaycastHit[] temp; temp = Physics.RaycastAll(r,30); for (int j = 0; j < temp.Length; j++) { temp[j].collider.gameObject.transform.position += Vector3.left; } } } /// /// 结束判断 /// void Endpd() { for (int i = 0; i < cube.GetLength(0); i++) { for (int j = 0; j<cube.GetLength(1); j++) { // print(cube[i, j].name); if (cube[i, j] == null) { continue; } Ray r = new Ray(cube[i, j].transform.position, Vector3.right);//向右发出射线 RaycastHit cube2; if (Physics.Raycast(r,out cube2,1f)&& cube[i, j].GetComponent().material.color == cube2.collider.GetComponent().material.color) { return; //如果找到相邻的并且颜色相同,跳出此方法 } r.direction = Vector3.up; //向上发出射线 if (Physics.Raycast(r, out cube2, 1f) && cube[i, j].GetComponent().material.color == cube2.collider.GetComponent().material.color) { return; } } } print("游戏结束"); bool a= UnityEditor.EditorUtility.DisplayDialog("游戏结束,本次得分:"+sum+"历史最高分:"+Dsum, "是否重新开始", "继续", "取消"); //***只能在unity编译器使用的类,不能打包出去 if (a) { sum = 0; arr.Clear(); DestroyImmediate(f); Start(); } else { print("关闭游戏"); } } }

第一次在这写博客, 好紧张 !
//download.csdn.net/download/xiehailiang_/12251536


作者:最咸的那条



unity3d 递归算法 递归 消消乐 算法 unity

需要 登录 后方可回复, 如果你还没有账号请 注册新账号