unity教程之制作连连看小教程

哈哈,周末了总算有时间完善一下做的连连看了,顺便写个小教程分享给大家,哇哈哈~

文章出处【狗刨学习网】

开始正题:


连连看的规则大家应该都知道,选中的两个图片相同,并且不能多于两个拐点能连在一块,这两个图片就可以消掉;
连通的类型:
直线型;
一个拐点;
两个拐点;
下面开始介绍这三种连通类型
直线型:
直线型分两种,一种是横向,一种是竖向;

首先是横向连接




A,B两点的x坐标相同,图片类型相同,从A点开始到B点检测,如果AB两点之间没有其他图片就销毁AB两个图片,竖向的和横向的类似
一个拐点:



AB两点的x坐标和y坐标都不相同的时候开始检测一个拐点是否可以连接,通过AB两点计算出CD两点,然后分别检测AC,BC,AD,BD是否可以通过直线型连接到一起,显然AB两点可以通过A>C,C>B连接到一起,


两个拐点:



AB两点,从A开始,横向和竖向把所有和A能直线连接的点找出来,用这些点和B点做一个拐点的检测,显然A点下边的那个点可以通过绿色的那个点可以通过一个拐点的方式和B点连接起来,

哈哈,是不是很简单啊,上边的就是连连看的核心内容

接下来详细介绍一下各个脚本的作用,(哇哈哈,以注释的形式给大家介绍吧)
GameManager.cs  游戏的核心代码,产生图片,判断是否可以销毁等

  1. [color=#008ef1][font=宋体]using UnityEngine;[/font][/color]
  2. using System.Collections;
  3. using System.Collections.Generic;

  4. public class GameManager : MonoBehaviour
  5. {
  6.     public DrawLine drawLine;//画线
  7.     public GameObject tilePrefab;//tile的预制
  8.     public List<Tile> tiles;//开始实例化的时候存放tile
  9.     public List<Tile> _tiles;//存放随机摆放的tile
  10.     public List<Tile> tilesEdge;//为了边界处可以有拐点,把棋盘四周的tile放在这里,这里的tile看不到
  11.     public int x, y;//棋牌的大小,两个数必须是偶数
  12.     private Tile tileA;
  13.     private Tile tileB;
  14.     private bool destroy;
  15.     private Vector3 mousePos;
  16.     private enum stepType//控制游戏的状态
  17.     {
  18.         one,
  19.         two,
  20.         three
  21.     }
  22.     private stepType _stepType;

  23.     void Start ()
  24.     {
  25.         this.gameObject.transform.position = Vector3.zero;
  26.         Spawn ();
  27.         _stepType = stepType.one;
  28.     }
  29.      
  30.     private void Spawn ()//实例化tile
  31.     {
  32.         float num = (x * y - (2 * x + 2 * y - 4)) * 0.5f;
  33.         for (int i = 0; i <num; i ++) {
  34.             int idTex = Random.Range (20, 45);
  35.             GameObject obj = Instantiate (tilePrefab) as GameObject;
  36.             GameObject obj2 = Instantiate (tilePrefab) as GameObject;
  37.             Tile tile = obj.GetComponent<Tile> ();
  38.             Tile tile2 = obj2.GetComponent<Tile> ();
  39.             tile.Init (idTex);
  40.             tile2.Init (idTex);
  41.             tiles.Add (tile);
  42.             tiles.Add (tile2);
  43.         }
  44.         for (int i = 0; i<((2*x+2*y) -4); i++) {//实例化边缘的tile
  45.             GameObject obj = Instantiate (tilePrefab) as GameObject;
  46.             obj.name = "edage";
  47.             Tile tile = obj.GetComponent<Tile> ();
  48.             tilesEdge.Add (tile);
  49.         }
  50.         CreatTile ();
  51.         for (int i = 0; i < _tiles.Count; i++) {
  52.             _tiles [i].transform.name = i.ToString ();
  53.             _tiles [i].id = i;
  54.         }
  55.         this.transform.position = new Vector3 (-(x / 2.0f - 0.5f), -(y / 2.0f - 0.5f), 0);
  56.     }
  57.      
  58.     private void CreatTile ()//随机摆放tile,如果是边缘的就放在边缘位置
  59.     {
  60.         int idTex = 0;
  61.         float _y = 0.0f;
  62.         for (int i = 0; i < y; i ++) {
  63.             float _x = 0.0f;
  64.             for (int j = 0; j < x; j ++) {
  65.                 if (i == 0 || j == 0 || i == y - 1 || j == x - 1) {
  66.                     tilesEdge [0].transform.position = new Vector3 (_x, _y, 0);
  67.                     tilesEdge [0].pos = new Vector2 (_x, _y);
  68.                     tilesEdge [0].transform.rotation = new Quaternion (0, 0, 180, 0);
  69.                     tilesEdge [0].transform.parent = this.gameObject.transform;
  70.                     _tiles.Add (tilesEdge [0]);
  71.                     tilesEdge [0].transform.localScale = Vector3.zero;
  72.                     tilesEdge [0].type = false;
  73.                     tilesEdge.RemoveAt (0);
  74.                 } else {
  75.                     int id = Mathf.FloorToInt (Random.Range (0, tiles.Count));
  76.                     tiles [id].transform.position = new Vector3 (_x, _y, 0);
  77.                     tiles [id].pos = new Vector2 (_x, _y);
  78.                     tiles [id].transform.rotation = new Quaternion (0, 0, 180, 0);
  79.                     tiles [id].transform.parent = this.gameObject.transform;
  80.                     _tiles.Add (tiles [id]);
  81.                     tiles.RemoveAt (id);
  82.                 }
  83.                 _x += 1;
  84.             }
  85.             _y += 1;
  86.         }
  87.     }
  88.      
  89.     private void SelectTile ()//开始选择图片,通过射线方式选中,如果tileA和tileB不相同,则tileA等于tileB开始下一个检测
  90.     {
  91.         Ray ray = Camera.mainCamera.ScreenPointToRay (mousePos);
  92.         RaycastHit hit;
  93.         int mask = 1 << 8;
  94.         if (Physics.Raycast (ray, out hit, mask)) {
  95.             if (tileA == null) {
  96.                 tileA = hit.transform.GetComponent<Tile> ();
  97.                 tileA.SetTileTexture (1);
  98. //                print ("tileA = hit.transform.GetComponent<Tile> ();" + tileA.transform.name);
  99.             } else {
  100.                 tileB = hit.transform.GetComponent<Tile> ();
  101.                 tileB.SetTileTexture (1);
  102. //                print ("tileB = hit.transform.GetComponent<Tile> ();" + tileB.transform.name);
  103.                 Compare (tileA, tileB);
  104.                 if (tileA == null ;; tileB == null) {
  105.                      
  106. //                    print ("a and b is null");
  107.                 }
  108.             }
  109. //            hit.transform.GetComponent
  110.         }
  111.     }
  112.      
  113.     private void Compare (Tile tile1, Tile tile2)//比较两个点是否可以连接到一起
  114.     { 
  115.         // same card
  116.         _stepType = stepType.one;
  117.         drawLine.waypoints.Add (tile1.transform); //第一个选择的tile是画线的起始位置,
  118.         drawLine.transform.position = tile1.transform.position;
  119.         destroy = false;
  120.         print ("compare");
  121.         if (tile1.pos.x == tile2.pos.x ;; tile1.pos.y == tile2.pos.y) {如果选中的是同一个图片返回
  122.             tileA.SetTileTexture (0);
  123. //            tileB.SetTileTexture (0);
  124.             tileA = tileB;
  125.             tileB = null;
  126. //            tileA.SetTileTexture (1);
  127.             return;
  128.         } else if (tile1.pos.x == tile2.pos.x ;; tile1.pos.y != tile2.pos.y) {//如果两点的x相等,竖向检测
  129.             print ("check y");
  130.             destroy = CheckY (tile1, tile2);
  131.             if (destroy)
  132.                 drawLine.waypoints.Add (tile2.transform);
  133.         } else if (tile1.pos.x != tile2.pos.x ;; tile1.pos.y == tile2.pos.y) {//如果两点的y相等,横向检测
  134.             print ("check x");
  135.             destroy = CheckX (tile1, tile2);
  136.             if (destroy)
  137.                 drawLine.waypoints.Add (tile2.transform);
  138.         }
  139.         if (!destroy) {//不符合直线连接方式的开始进行一个拐点的检测
  140.             _stepType = stepType.two;
  141.             destroy = CheckTwoStep (tile1, tile2);
  142. //            print ("destroy = CheckTwoStep (tile1, tile1);:" + destroy);
  143.             print ("check two step");
  144.             if (!destroy) {//不符合直线和一个拐点检测的开始进行两个拐点的检测
  145.                 _stepType = stepType.three;
  146.                 destroy = CheckThreeStep (tile1, tile2);
  147.                 print ("check three:" + destroy);
  148.                 print ("tile1.idTex:" + tile1.idTex + "tile1.idTex:" + tile1.idTex);
  149.             }
  150.         }
  151.         if (destroy) {//如果符合销毁条件销毁图片,并开始画线
  152.             tile1.transform.localScale = Vector3.zero;
  153.             tile2.transform.localScale = Vector3.zero;
  154.             tile1.type = false;
  155.             tile2.type = false;
  • 1
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值