Unity3D笔记(二)使用GUI做井字棋小游戏

第一次使用unity, 做点事情就比较笨。简单的事情也做了许久,主要是对unity这个引擎逻辑不熟悉,对C#这个语言的逻辑也不是很熟,走了许多弯路。


开始想的是真的有九个格子那样的对象,然后有一个中控对象去负责统计结果,然后发现对象之间的通讯好像还有点麻烦,现在没有太多时间去深入学习,所以使用了简单一些的使用方式,全部都是GUI来做,只用一个脚本。
首先就是创建9个格子:

GUI.Button(new Rect(x, y, width, length), ""))

值得注意的是,这个函数,如果这个位置的按钮不存在那么就创建这个按钮,如果这个按钮已经存在了,那么就会返回这个按钮是否被点击。
所以创建按钮和检测按钮是否被点击都写在一起,写在onGUI中。

// draw the chess table on each frame
for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j) {
        switch (state [i, j]) {
            case 0:
                    if (GUI.Button (new Rect (i * 50 + xMove, j * 50 + yMove, 50, 50), ""))
                        click (i, j);
                    break;
                case 1:
                    if (GUI.Button (new Rect (i * 50 + xMove, j * 50 + yMove, 50, 50), "O"))
                        click (i, j);
                    break;
                case 2:
                    if (GUI.Button (new Rect (i * 50 + xMove, j * 50 + yMove, 50, 50), "X"))
                        click (i, j);
                    break;
                default:
                    break;
                }
            }

这里的state是一个3x3二维数组,用于储存当前游戏的状态,同时根据当前游戏状态画出不同的方格。
若被点击则调用click进行处理。
reset按钮就是通过恢复state数组使得下一桢的UI回复到最初的样子。
胜利信息通过GUI.label显示出来。

public static void Label(Rect position, string text);
public static void Label(Rect position, Texture image);
public static void Label(Rect position, GUIContent content);
public static void Label(Rect position, string text, GUIStyle style);
public static void Label(Rect position, Texture image, GUIStyle style);
public static void Label(Rect position, GUIContent content, GUIStyle style);

游戏中的代码如下:

if (whetherEqual) {
            GUI.Label (new Rect (xMove, 170 + yMove, 200, 100), "No winner here, press Restart!");
        }
        else if (whetherEnd) {
            char winner = (turn == 2) ? 'O' : 'X';
            GUI.Label (new Rect (xMove, 170 + yMove, 200, 100), winner + " win! press restart to start over");
        }

至于判断逻辑就不多说了,毕竟不是这里的重点。


附完整代码:
chess.cs 挂在主摄影机上即可运行

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Chess : MonoBehaviour
{
    /* 0 for no chess
     * 1 for player1
     * 2 for player2
     */
    private int[,] state = new int[3, 3];
    /* switch turn
     * 1 for player1
     * 2 for player2
     */
    private int turn = 1;
    private bool whetherEnd = false;
    private bool whetherEqual = false;
    private int xMove = 350;
    private int yMove = 150;
    // Use this for initialization
    void Start ()
    {
        init ();
        Camera view = GetComponent<Camera> ();
        view.backgroundColor = Color.white;
    }

    void OnGUI ()
    {
        GUI.backgroundColor = Color.white;
        // reset button
        if (GUI.Button (new Rect (25 + xMove, 250 + yMove, 100, 50), "Restart")) {
            init ();
            return;
        }
        // draw the chess table on each frame
        for (int i = 0; i < 3; ++i)
            for (int j = 0; j < 3; ++j) {
                switch (state [i, j]) {
                case 0:
                    if (GUI.Button (new Rect (i * 50 + xMove, j * 50 + yMove, 50, 50), ""))
                        click (i, j);
                    break;
                case 1:
                    if (GUI.Button (new Rect (i * 50 + xMove, j * 50 + yMove, 50, 50), "O"))
                        click (i, j);
                    break;
                case 2:
                    if (GUI.Button (new Rect (i * 50 + xMove, j * 50 + yMove, 50, 50), "X"))
                        click (i, j);
                    break;
                default:
                    break;
                }
            }
        GUI.color = Color.black;
        if (whetherEqual) {
            GUI.Label (new Rect (xMove, 170 + yMove, 200, 100), "No winner here, press Restart!");
        }
        else if (whetherEnd) {
            char winner = (turn == 2) ? 'O' : 'X';
            GUI.Label (new Rect (xMove, 170 + yMove, 200, 100), winner + " win! press restart to start over");
        }

    }

    void click (int x, int y)
    {
        if (state [x, y] == 0 && !whetherEnd) {
            state [x, y] = turn;
            checkWhetherWin (x, y);
            turn = (turn == 2) ? 1 : 2;
        }
    }

    void checkWhetherWin (int x, int y)
    {
        // horizontal
        if (state [x, 0] == state [x, 1] && state [x, 1] == state [x, 2]) {
            whetherEnd = true;
            Debug.Log ("win at horizontal");
        }
        // vertical
        if (state [0, y] == state [1, y] && state [1, y] == state [2, y]) {
            whetherEnd = true;
            Debug.Log ("win at vertical");
        }
        // diagonal
        bool flag = true;
        for (int i = 0; i < 3; ++i) {
            if (state [i, i] != state [x, y])
                flag = false;
        }
        if (flag == true) {
            whetherEnd = true;
            Debug.Log ("win at diagonal 1");
        }
        flag = true;
        for (int i = 2; i > -1; --i) {
            if (state [2 - i, i] != state [x, y])
                flag = false;
        }
        if (flag == true) {
            whetherEnd = true;
            Debug.Log ("win at diagonal 2");
        }
        flag = true;
        for (int i = 0; i < 3; ++i)
            for (int j = 0; j < 3; ++j) {
                if (state [i, j] == 0)
                    flag = false;
            }
        if (flag) {
            whetherEnd = true;
            whetherEqual = true;
        }
    }
    // init all things when restart button pressed
    void init ()
    {
        Debug.Log ("chess start");
        for (int i = 0; i < 3; ++i)
            for (int j = 0; j < 3; ++j)
                state [i, j] = 0;
        whetherEnd = false;
        whetherEqual = false;
        turn = 1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值