unity3D 2019.3版本开发的扫雷


1 GameManger

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;public class GameManger : MonoBehaviour
{public GameObject BlockPrefab;public int xColumn =20;public int yRow = 18;public AudioClip bombClip;private float gameTIme = 120;public Text gameText;public Block[,] blockArrays;public CanvasGroup gamePanle;private bool isgame;private void Awake(){blockArrays = new Block[xColumn, yRow];// blocks= Block[xColumn,yRow];for (int i = 0; i < xColumn; i++) {for (int j = 0; j < yRow; j++) {GameObject tempBlock= Instantiate(BlockPrefab,correctPositive(i,j),Quaternion.identity);tempBlock.transform.parent = transform;blockArrays[i, j] = tempBlock.GetComponent<Block>();blockArrays[i, j].Init(i, j, Block.BlockSpriteType.Block, this);}}CreateBoobs();gamePanle.alpha = 0;gamePanle.blocksRaycasts = false;gamePanle.interactable = false;isgame = true;}// Update is called once per framevoid Update(){if (isgame==false){return;}if (gameTIme<=0){isgame = false;gamePanle.alpha = 1;gamePanle.blocksRaycasts = true;gamePanle.interactable = true;}gameText.text = (gameTIme -= Time.deltaTime).ToString("0.0");if (Input.GetMouseButtonDown(1)){Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;bool isCollider = Physics.Raycast(ray, out hit, int.MaxValue, LayerMask.GetMask("Blocks"));if (isCollider){GameObject blockObject= hit.collider.gameObject;Block block = blockObject.GetComponent<Block>();Debug.Log(block.name);if (block.isFlaged == false && block.isOpen == false){block.isFlaged = true;block.setSprite(Block.BlockSpriteType.BlockFlagged);}else{Debug.Log("GetMouseButtonDown(1)");block.isFlaged = false;block.setSprite(Block.BlockSpriteType.Block);}}}}void CreateBoobs(){int count = 0;while (count < 40){int x = Random.Range(0, xColumn);int y = Random.Range(0,yRow);if (blockArrays[x, y].isbombs == false) {blockArrays[x, y].isbombs = true;count++;// blockArrays[x, y].setSprite(Block.BlockSpriteType.Bomb);}}}public Vector3 correctPositive(float x, float y){return new Vector3((transform.position.x - (xColumn) / 2f + x)/2f, (transform.position.y + (yRow-1 ) / 2f - y)/2f, 0);}private ArrayList getNeighbours(Block block) {ArrayList  blocklist =  new ArrayList();if (block.x + 1 < xColumn && block.y + 1 < yRow) {blocklist.Add(blockArrays[block.x + 1, block.y + 1]);}if (block.x + 1 < xColumn &&block.y - 1 >= 0){blocklist.Add(blockArrays[block.x + 1, block.y - 1]);}if (block.x + 1 < xColumn ){blocklist.Add(blockArrays[block.x + 1, block.y]);}if (block.y + 1 < yRow){blocklist.Add(blockArrays[block.x , block.y + 1]);}if (block.y - 1 >= 0){blocklist.Add(blockArrays[block.x, block.y - 1]);}if (block.x - 1 >= 0 && block.y + 1 < yRow){blocklist.Add(blockArrays[block.x - 1, block.y + 1]);}if (block.x - 1 >= 0 ){blocklist.Add(blockArrays[block.x - 1, block.y ]);}if (block.x - 1 >= 0 && block.y - 1 >= 0){blocklist.Add(blockArrays[block.x - 1, block.y - 1]);}return blocklist;}public int getBoobNumber(Block block) {int bombNumber = 0;ArrayList blocks = getNeighbours(block);foreach (Block nblock in blocks) {if (nblock.isbombs == true)bombNumber++;}return bombNumber;}public void buttonPress (Block block){if (Input.GetMouseButtonDown(0)) {if (block.isbombs == false && block.isFlaged == false){if (block.isOpen == false){openBlock(block);}}else if(block.isbombs == true && block.isFlaged == false){AudioSource.PlayClipAtPoint(bombClip,transform.position);block.setSprite(Block.BlockSpriteType.Bomb);Debug.Log("game over");isgame = false;gamePanle.alpha = 1;gamePanle.blocksRaycasts = true;gamePanle.interactable = true;}}   }private void openBlock(Block block) {block.isOpen = true;int bombNumber = getBoobNumber(block);block.setImage(bombNumber);if (bombNumber == 0) {ArrayList blocksList = getNeighbours(block);foreach (Block nblock in blocksList){if (nblock.isOpen == false) {openBlock(nblock);}}}}public void restart(){UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex);}public void QuitGame(){Application.Quit();}
}

2 Block

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Block : MonoBehaviour
{public enum BlockSpriteType{Block,BlockClicked,Bomb,BlockFlagged,one,tow,three,four,five,six,seven,eight}[System.Serializable]public struct BlockSpriteStruct {public BlockSpriteType type;public Sprite sprite;}private GameManger gameManger;public BlockSpriteStruct[] blockSpriteStructs;public int x;public int y;public Dictionary<BlockSpriteType, Sprite> blockSpriteDict;[HideInInspector]public SpriteRenderer spriteRenderer;private void Awake(){spriteRenderer = transform.Find("blockmodel").GetComponent<SpriteRenderer>();blockSpriteDict = new Dictionary<BlockSpriteType, Sprite>();for (int i = 0; i < blockSpriteStructs.Length; i++) {if (!blockSpriteDict.ContainsKey(blockSpriteStructs[i].type)) {blockSpriteDict.Add(blockSpriteStructs[i].type, blockSpriteStructs[i].sprite);}}}[HideInInspector]public BlockSpriteType type;public void Init(int x, int y, BlockSpriteType type, GameManger gameManger){this.x = x;this.y = y;this.type = type;this.gameManger = gameManger;setSprite(type);}public void setImage(int number) {switch (number){case 0:setSprite(BlockSpriteType.BlockClicked);break;case 1:setSprite(BlockSpriteType.one);break;case 2:setSprite(BlockSpriteType.tow);break;case 3:setSprite(BlockSpriteType.three);break;case 4:setSprite(BlockSpriteType.four);break;case 5:setSprite(BlockSpriteType.five);break;case 6:setSprite(BlockSpriteType.six);break;case 8:setSprite(BlockSpriteType.eight);break;case 7:setSprite(BlockSpriteType.seven);break;default:break;}}public  void setSprite(BlockSpriteType type) {this.type = type;spriteRenderer.sprite = blockSpriteDict[type];}public bool isbombs =false;public bool isOpen = false;public bool isFlaged = false;void Start(){if (Input.GetMouseButtonDown(1)){Debug.Log("GetMouseButtonDown(1)");if (isFlaged == false && isOpen == false){isFlaged = true;setSprite(Block.BlockSpriteType.BlockFlagged);}else{isFlaged = false;setSprite(Block.BlockSpriteType.Block);}}}private void OnMouseDown(){Debug.Log(Input.GetMouseButton(1));gameManger.buttonPress(this);}
}

3 GameManger参数设置

unity3D 2019.3版本开发的扫雷相关推荐

  1. Unity3D 2019.3开发的中国象棋的源代码

    Unity3D 2019.3开发的中国象棋的源代码 1 ChessEnum public enum ChessSide {RED,BLACK,Barrier } public enum ChessTy ...

  2. 2019参加Python开发培训靠谱吗?

    在大多数人的眼里,Python 的未来非常光明.这个语言刚刚被 IEEE Spectrum 列为第一大编程语言.而 Packt 最近的调查也显示它是最近一段时期内科技界最为流行的工具.那么2019参加 ...

  3. IntelliJ IDEA 2019.3发布,饱受性能诟病的2019.2版本终于成为过去式

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 白开水不加糖 来源 | 公众号「开源中国」 距 ...

  4. 一键伪装成 Windows 10:Kali Linux 2019.4 版本推出 “Undercover” 模式

     聚焦源代码安全,网罗国内外最新资讯! 编译:奇安信代码卫士团队 上上周,Kali Linux 2019.4 版本发布并推出"Undercover"模式,用于快速将 Kali 桌面 ...

  5. unity3D用什么语言开发好?

    unity3D用什么语言开发好? 一.总结 一句话总结:选c# 同时U3D团队也会把支持的重心转移到C#,也就是说文档和示例以及社区支持的重心都在C#,C#的文档会是最完善的,C#的代码实例会是最详细 ...

  6. (转)火溶CEO王伟峰:Unity3D手机网游开发

    今天看到这篇文章,感觉很不错,尤其是那句"Unity3D的坑我觉得最严重的坑就是没有懂3D的程序员,把Unity当成Office用". 转自http://blog.csdn.net ...

  7. 火溶CEO王伟峰:Unity3D手机网游开发

    http://www.gamelook.com.cn/2013/11/135467 GameLook报道 / 11月2日下午,GameLook游戏开放日活动在上海正式举行,此次会议gamelook邀请 ...

  8. Kinect开发之结合Unity3D进行游戏应用开发

    转自:http://yacare.iteye.com/blog/1950164 最近在用unity3d和Kinect结合开发一个项目,突然间发现了这个博客,感觉其中的Unity3d包太厉害了,挺有意思 ...

  9. Unity3D 2019版-UI技巧,将物体/模型置于UI前,将物体/模型置于UI后,将物体/模型置于UI中

    今天做项目的时候,遇到一个情况UI在全屏显示的情况下,会遮挡住后面在3D空间中的模型.之前处理过类似的情况,这里在记录一下. 当时的第一反应是调整模型shader的渲染层级,但是想了一下这样做太麻烦了 ...

最新文章

  1. 吸引:由《你的知识需要管理》编辑过程想到的
  2. ubuntu sudo apt-get update 失败 解决方法
  3. python里的append怎么用_python中append实例用法总结
  4. Apache ZooKeeper - 集群中 Follow 的作用_非事务请求的处理与 Leader 的选举分析
  5. java ext pagesize_更改透明图像的不透明度/更改extgstate字典的值
  6. 注意,Dubbo 存在高危反序列化漏洞!
  7. 用VBScript实现Zip压缩目录中的所有文件
  8. Eclipse·Maven·构建SpringMVC简单工程-3
  9. discuz点歌台插件
  10. CSDN 赚积分C币方法
  11. Web开发后端框架比较
  12. 消息队列——ActiveMQ使用及原理浅析
  13. 使用 ngrok 进行内网穿透
  14. android vplayer 源码,【关于ffmpeg和Android的种种】【VPlayer不开源】【Android常用调试方法】...
  15. Java学习笔记(五):Java多线程(细致入微,持续更新)
  16. R语言之一元线性回归xt2.15
  17. 生而强悍的iQOO如何在安卓手机阵营成功跑出?
  18. MySQL数据库管理系统原理
  19. IOS端向下滑动骤停
  20. 2020中国技术力量年度榜单正式揭晓,见证创新技术的力量

热门文章

  1. 这两个软件让你实现识图查车牌号
  2. Wow.js常用特效对应名称
  3. access to同义替换_英语近义同义词替换表
  4. ActiveMQ 消息中间件
  5. macbook 终端命令怎么使用_mac 常用的终端命令
  6. pandas读取数组,显示为str,而实际应为list,解决方法
  7. window系统中的系统进程
  8. 对象在内存中是如何存储的
  9. php判断复选框是否被选中的方法
  10. 程序员修炼之道:从小工到专家读书笔记