unity3D 2019.3版本开发的扫雷
生活随笔
收集整理的這篇文章主要介紹了
unity3D 2019.3版本开发的扫雷
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
unity3D 2019.3版本開發(fā)的掃雷
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參數(shù)設(shè)置
總結(jié)
以上是生活随笔為你收集整理的unity3D 2019.3版本开发的扫雷的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 硬盘分区进不去,右键属性显示raw的解决
- 下一篇: 直接将ppt转换成word格式的方法