Unity实现翻牌消消乐
一、效果演示
二、使用工具
Unity:2019.4.36
VS:2019
三、场景搭建
1、总体预览
四、代码
1、GameControl.cs(挂载在空物体上)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine.EventSystems;public class GameControl : MonoBehaviour
{private static int row = 3; //卡牌的行数private static int col = 4; //卡牌的列数public int[,] map = new int[row, col]; //卡牌数组public Sprite[] cardsSprit; //卡牌的种类public int openCardsNums; //翻开卡牌的数量private Cards[] cards;public GameObject winUI; //胜利UIprivate Transform parentObj;public Text showText;private void Awake(){InitArr(); //初始化数组}// Start is called before the first frame updatevoid Start(){cards = FindObjectsOfType<Cards>();parentObj = GameObject.Find("Cards").transform;winUI.transform.GetChild(2).GetComponent<Button>().onClick.AddListener(GameOver); //点击退出游戏按钮winUI.transform.GetChild(3).GetComponent<Button>().onClick.AddListener(ResetStage); //点击重新开始按钮}// Update is called once per framevoid Update(){if (IsWin()) //判断游戏是否胜利{winUI.SetActive(true); //显示胜利UIFindObjectOfType<TimerControl>().isCounting = false; //暂停计时showText.text = "一共花费了" + (int)FindObjectOfType<TimerControl>().countTime + "秒";}}private void InitArr(){int[] arr = new int[row * col];int length = row * col;for (int i = 0; i < row * col / 2; i++){int t = Random.Range(0, 6);arr[i] = arr[row * col - i - 1] = t;}for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){int t = Random.Range(0, length);map[i, j] = arr[t];for (int k = t; k < length - 1; k++){arr[k] = arr[k + 1];}length--;}}}GameObject[] t_objs = new GameObject[2];int n = 0;public void IsSame() //判断翻开的卡牌是否相同{cards = FindObjectsOfType<Cards>(); if (openCardsNums % 2 == 0){foreach (var card in cards){if (card.GetComponent<Image>().sprite != null && card.GetComponent<Button>().interactable == false){t_objs[n] = card.gameObject;n++;}}if (t_objs[0].GetComponent<Image>().sprite == t_objs[1].GetComponent<Image>().sprite){Invoke("DestroyCards", 1);}else{Invoke("CloseCards", 0.5f);}}}private void DestroyCards() //销毁相同的两张卡牌{t_objs[0].GetComponent<Image>().sprite = null;t_objs[1].GetComponent<Image>().sprite = null;Destroy(t_objs[0].GetComponent<EventTrigger>());Destroy(t_objs[1].GetComponent<EventTrigger>());n = 0;}private void CloseCards() //盖上翻开的两张卡牌{t_objs[0].GetComponent<Button>().interactable = true;t_objs[0].GetComponent<Image>().sprite = cardsSprit[6];t_objs[1].GetComponent<Button>().interactable = true;t_objs[1].GetComponent<Image>().sprite = cardsSprit[6];n = 0;}public bool IsWin() //判断游戏是否胜利{cards = FindObjectsOfType<Cards>();foreach (var card in cards){if(card.GetComponent<Image>().sprite!=null){return false;}}return true;}void GameOver() //退出游戏{#if UNITY_EDITORUnityEditor.EditorApplication.isPlaying = false; //编辑器中退出
#elseApplication.Quit(); //打包成EXE文件退出
#endif}void ResetStage() //重新加载当前场景{SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);}
}
2、Card.cs(挂载在卡牌上)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class Cards : MonoBehaviour
{private int cardType; //卡牌的种类private Sprite originImage; //卡牌原本显示的图片private Sprite currentImage; //卡牌当前显示的图片private int clickNums = 0; //鼠标点击的次数// Start is called before the first frame updatevoid Start(){string[] stringName = transform.name.Split('_'); //分割字符串,通过名字获取卡牌的位置cardType = FindObjectOfType<GameControl>().map[int.Parse(stringName[1]), int.Parse(stringName[2])];originImage = GetComponent<Image>().sprite;currentImage = FindObjectOfType<GameControl>().cardsSprit[cardType];}public void Click(){GetComponent<Image>().sprite = currentImage; //更换卡牌图片if (transform.GetComponent<Button>().interactable) //判断卡牌是否翻开{FindObjectOfType<GameControl>().openCardsNums++; //翻开的卡牌数量加一}transform.GetComponent<Button>().interactable = false; //卡牌翻开后禁用点击FindObjectOfType<GameControl>().IsSame();}
}
3、TimeControl.cs(挂载在时间文本上)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class TimerControl : MonoBehaviour
{public bool isCounting = true; //判断是否在计时public float countTime = 0; //时间private Text timeText;// Start is called before the first frame updatevoid Start(){timeText = GetComponent<Text>();}// Update is called once per framevoid Update(){if(isCounting){countTime += Time.deltaTime;timeText.text = TimeFormatter(countTime); //更新文本}}string TimeFormatter(float time){int hour = (int)time / 3600;int min = (int)(time - hour * 3600) / 60;int sec = (int)(time - hour * 3600 - min * 60);return hour.ToString("D2") + ":" + min.ToString("D2") + ":" + sec.ToString("D2"); //保留两位整数}
}
五、项目链接
链接:https://pan.baidu.com/s/1k_GNqE24D9sUHLprAGrnvg
提取码:sozs
Unity实现翻牌消消乐相关推荐
- 【Unity】消消乐游戏开发流程
消消乐核心功能是消除物品的生成,是一个很有画面感的生成,而不是一下子生成,那么就需要考虑使用协程或Invoke定时调用方法执行生成要消除的物品A(后称'A'),一般都是SpriteRenderer.如 ...
- Unity 3D游戏-消消乐(三消类)教程和源码
Unity 消消乐教程和源码 本文提供全流程,中文翻译. Chinar坚持将简单的生活方式,带给世人! (拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 Start Game ...
- 从零开始制作基于Unity引擎的宝石消消乐(一)
完整项目我已经放到GitHub啦~ GitHub: https://github.com/lucaschen1993/Lukastar 市场上有些消消乐真好玩,比如hxxxxxpop,pxxxxsag ...
- unity消消乐源码
源码地址 http://www.yxkfw.com/?fromuid=63539 提供百度网盘链接: https://pan.baidu.com/s/1dF9V9aL 密码: h9uu 特别声明(务必 ...
- 【游戏开发实战】使用Unity制作水果消消乐游戏教程(二):随机生成水果
文章目录 一.前言 二.导入水果图片素材 三.制作水果预设 四.水果生成器 五.挂FruitSpawner脚本 六.运行测试 一.前言 嗨,大家好,我是新发.下班坐地铁的时候,好几次看到其他人在玩消消 ...
- Unity三消类【水果乐园】消消乐游戏素材+源码
Unity 3D 是目前主流的游戏开发引擎,有数据显示,全球最赚钱的 1000 款手机游戏中,有 30% 是使用 Unity 3D 开发出来的.尤其在 VR 设备中,Unity 3D 游戏开发引擎具有 ...
- 【游戏开发实战】使用Unity制作水果消消乐游戏教程(八):游戏得分加分效果
文章目录 一.前言 二.使用TextMeshPro显示得分 1.导入TTF字体到工程中 2.创建字源txt文件 3.生成字体asset 4.制作得分预设 三.得分数字动画 四.得分代码 五.运行测试 ...
- 【游戏开发实战】使用Unity制作水果消消乐游戏教程(三):水果拖动与交换逻辑
文章目录 一.前言 二.事件管理器 三.水果点击事件 四.水果拖动交换 五.运行测试 一.前言 嗨,大家好,我是新发.下班坐地铁的时候,好几次看到其他人在玩消消乐,既然大家都这么喜欢玩,那我就写个Un ...
- 【游戏开发实战】使用Unity制作水果消消乐游戏教程(七):水果消除特效
文章目录 一.前言 二.导入特效素材 三.制作序列帧动画 四.调整序列帧帧率 五.保存特效序列帧预设 六.特效生成器EffectSpawner 七.动画帧事件,监听动画播放完毕 八.调用特效 九.挂脚 ...
- 基于Unity的糖果消消乐
糖果消消乐的算法实现 定义糖果枚举类型 public enum CandyType { Type1=0,Type2=1,Type3=2,Type4=3,Type5=4,Type6=5,Type7=6, ...
最新文章
- 学习笔记——ArrayList总结
- python pyyaml模块使用示例:读取yaml文件内容
- POJ - 1743 Musical Theme(二分+后缀数组+差分数组)
- Linux系统编程41:多线程之线程池的概念及实现
- SQLyog之MySQL客户端的下载、安装和使用(普通版)
- C#引用AForge调取系统摄像头完成的驿站底单拍照系统
- dota 服务器 无响应,dota2卡死出现无响应蓝屏红字解决方法
- MATLAB求解微分方程
- multisimbcd码_8421BCD码加法器报告1
- Civil 3d-快速转换C3D图形为CAD图形
- Office快捷键大全之一(Word快捷键)
- 南京审计大学计算机考研专业课答案
- 快来天津科技大学找我玩
- 分析加工贸易企业三帐难以平衡的根本原因
- Python.习题五 列表与元组(下)
- 电池寿命问题 (贪心)
- k8s集群coredns无法解析外部域名
- 使用opencv检测视频人脸
- Nacos系列(12)-Nacos1.3.2注册中心nacos.discovery配置参数详解
- android安卓切换音频声道-耳机-外放-蓝牙-实用功能系列
热门文章
- [PCL] 三维图像处理
- 【ubuntu】本地访问ubuntu服务器的jupyter文件,.ipynb误删除找回、恢复
- 《Metasploit 魔鬼训练营》04 Web 应用渗透测试
- Ubuntu16.04安装chrome、JDK、IDEA、MYSQL、node.js和npm、angular、git、maven等软件
- MyBatis13--MyBatis中的二级缓存
- 输入圆柱体的高h和半径r输出圆柱体的表面积s
- PCA主成分分析 理解
- 给定一个字符串s,你可以从中删除一些字符,使得剩下的串是一个回文串。如何删除才能使得回文串最长呢?
- unity3d按照一定比例制作太阳系模型
- 关于nmap的”dnet: Failed to open device eth1“错误解决