【Unity】简单的扫雷代码,适合初学者

来源:互联网 发布:花草茶品牌 知乎 编辑:程序博客网 时间:2024/05/08 09:37
using UnityEngine;using System.Collections;public class Grid{    public int x;                   // 记录x坐标    public int y;                // 记录y坐标    public bool hasBoom;        // 是否有雷    public int boomNumber;        // 周边雷数    public bool opend = false;  // 是否打开}public class Boom : MonoBehaviour{    public int row = 10;    // 行数    public int column = 10; // 列数    private int size = 50;    // 格子大小    private Grid[,] map;    // 格子数组    private float minTime;  // 最佳时间    private bool over = false; // 游戏是否结束    void CreateMap ()    {        map = new Grid[row, column];    // 创建初始化数组        for (int i = 0; i < row; i++) {            for (int j = 0; j < column; j++) {                map [i, j] = new Grid ();// 实例化格子                map [i, j].x = i;       // 记录坐标                map [i, j].y = j;            }        }    }    void ComputerNumber () // 遍历数组,查找每个格子周围雷的个数    {        for (int i = 0; i < row; i++) {            for (int j = 0; j < column; j++) {                map [i, j].boomNumber = CountBoom (i, j);            }        }    }    int CountBoom (int x, int y)  // 计算每个方格的周围雷数(九宫格形状,除去中心位置的)    {        int count = 0;        for (int i = -1; i <= 1; i++) {            for (int j = -1; j <= 1; j++) {                if (!(i == 0 && j == 0)) {                    int n = x + i;                    int m = y + j;                    if (n >= 0 && n < row && m >= 0 && m < column) { // 如果是雷,计数加1                        if (map [n, m].hasBoom) {                            count++;                        }                    }                }            }        }        return count;    }    void Open (int x, int y)  // 打开雷数为0的格子    {        map [x, y].opend = true;        if (map [x, y].boomNumber == 0) {            for (int i = -1; i <= 1; i++) {                for (int j = -1; j <= 1; j++) {                    if (!(i == 0 && j == 0)) {                        int n = x + i;                        int m = y + j;                        if (n >= 0 && n < row && m >= 0 && m < column && map [n, m].opend == false) {                            Open (n, m);                            audio.Play ();  // 播放打开格子的音效                        }                    }                }            }        }    }    void boomcount (int boomCount)  // 为地图中添加的雷数    {        for (int i = 0; i < boomCount; i++) {            map [Random.Range (0, row), Random.Range (0, column)].hasBoom = true;        }    }    void Start ()    {        CreateMap ();  // 初始化地图    }    void Update ()    {        int sum = 0;        if (!over)        for (int i = 0; i < 10; i++) {  // 遍历数组,计算已经打开的格子和雷数相加是否等于100            for (int j = 0; j < 10; j++) {                if (map [i, j].opend) {                    sum = sum + 1;                }                if (map [i, j].hasBoom) {                    sum = sum + 1;                }                if (sum == 100) {                    over = true;                    if ( Time.realtimeSinceStartup < PlayerPrefs.GetFloat("keepTime")  ) {                    PlayerPrefs.SetFloat("keepTime",Time.realtimeSinceStartup);                    minTime = PlayerPrefs.GetFloat("keepTime");                    } else if (PlayerPrefs.GetFloat("keepTime") == 0) {                        PlayerPrefs.SetFloat("keepTime",Time.realtimeSinceStartup);                    }                    Debug.Log ("You Win!!");                    Debug.Log ("你用了" +  Time.realtimeSinceStartup.ToString ("0.00") + "秒!");                }            }        }    }    void OnGUI ()    {        GUI.Button(new Rect(600,150,100,50),"最好成绩 " + PlayerPrefs.GetFloat("keepTime").ToString("0.00"));        if (GUI.Button (new Rect (600, 0, 100, 50), "容易")) {            boomcount (4);            ComputerNumber (); // 遍历每个格子的雷数属性               }        if (GUI.Button (new Rect (600, 50, 100, 50), "简单")) {            boomcount (6);            ComputerNumber ();               }        if (GUI.Button (new Rect (600, 100, 100, 50), "困难")) {            boomcount (10);            ComputerNumber ();                }        for (int i = 0; i < row; i++) {            for (int j = 0; j < column; j++) {                // 显示出坐标和雷数                string tip = map [i, j].x + "," + map [i, j].y + "\n";                tip += map [i, j].boomNumber;                // 有雷的显示为红色,没有雷的显示为黄色                if (map [i, j].hasBoom) {                    GUI.backgroundColor = Color.red;                } else {                    GUI.backgroundColor = Color.yellow;                }                if (map [i, j].opend) {                    GUI.Box (new Rect (j * size, i * size, size, size), tip);                } else if (GUI.Button (new Rect (j * size, i * size, size, size), tip)) {                                      if (map [i, j].hasBoom) {                        // 当鼠标悬停在按钮上的时候,字体蓝色高亮                        GUI.skin.button.hover.textColor = Color.blue;                        print ("Over!");                    } else {                        Open (i, j);                    }                }            }        }    }}


1 0
原创粉丝点击