Unity3d 游戏设计(一)井字棋

3D游戏设计(一)井字棋

运行效果:

实现过程


声明变量:

    public Texture2D O;
    public Texture2D X;
    GUIStyle myStyle;
    private int turn = 1;
    /*记录谁的回合:turn=1为⚪,turn==2为×*/
    int[][] board = new int[3][] { new int[3], new int[3], new int[3] };

reset()函数:用于重新开始游戏

    /*重新开局*/
    void reset() {
        turn = 1;
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                board[i][j] = 0;
            }
        }
        Debug.Log("game reset");
    }

check()函数:检查游戏是否结束:未结束返回0,O胜利返回1,X胜利返回2,平局返回3。

    /*检查胜利条件*/
    int check() {
        /*横线获胜*/
        for (int i = 0; i < 3; i++) {
            if (board[i][0] != 0 && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
                return board[i][0];
            }
        }
        /*纵线获胜*/
        for (int i = 0; i < 3; i++) {
            if (board[0][i] != 0 && board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
                return board[0][i];
            }
        }
        /*斜线获胜*/
        if (board[1][1] != 0 &&
            board[0][0]== board[1][1] && board[2][2] == board[1][1] ||
            board[0][2] == board[1][1] && board[2][0] == board[1][1]) {
            return board[1][1];
        }
        /*对局还没结束*/
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                if (board[i][j] == 0) return 0;
            }
        }
        /*平局*/
        return 3;
    }

display_result()函数:显示目前的游戏状态。

    /*显示对局情况*/
    void display_result(int res) {
        int m = Screen.width / 2;
        if (res == 0) {
            if (turn == 1)
                GUI.Box(new Rect(m - 50, 115, 100, 35), "O is going");
            else 
                GUI.Box(new Rect(m - 50, 115, 100, 35), "X is going");
        }
        else if (res == 1) {
            GUI.Box(new Rect(m - 50, 115, 100, 35), "O WIN");
        }
        else if (res == 2) {
            GUI.Box(new Rect(m - 50, 115, 100, 35), "X WIN");
        }
        else if (res == 3) {
            GUI.Box(new Rect(m - 50, 115, 100, 35), "Draw");
        }
    }

OnGUI()函数:实时渲染,采用预设的O、X图片来表示O、X已经下过的棋子。

    void OnGUI() {
        int m = Screen.width / 2;
        GUI.Box(new Rect(m - 150, 80, 300, 420), "井字棋");
        /*如果reset被点击则重开*/
        if (GUI.Button(new Rect(m - 50, 160, 100, 35), new GUIContent("Reset", "点击重新开始"))) reset();
        GUI.Label(new Rect(m - 150, 170, 150, 35), GUI.tooltip);
        int res = check();
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                if (board[i][j] == 1) {
                    GUI.Button(new Rect(m - 150 + i * 100, 200 + j * 100, 100, 100), O);
                }
                else if (board[i][j] == 2) {
                    GUI.Button(new Rect(m - 150 + i * 100, 200 + j * 100, 100, 100), X);
                }   
                if(GUI.Button(new Rect(m - 150 + i * 100, 200 + j * 100, 100, 100), "")) { 
                    if (res == 0) {
                        if (turn == 1) {
                            board[i][j] = turn;
                            turn = 2;
                        }
                        else {
                            board[i][j] = turn;
                            turn = 1;
                        }
                    }  
                }
            }
        }
        display_result(res);
    }

可能存在的问题及解决方案:

可能存在井字棋显示不全的问题,此时把视野调至标准的1024*768即可。

可能存在预设对象未绑定的问题,此时把O对象绑定图片red_circle,把X对象绑定red_cha即可。

posted @ 2020-09-23 20:45  沐锋丶  阅读(343)  评论(0编辑  收藏  举报