欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity3D開發(fā)實戰(zhàn)之五子棋游戲

 更新時間:2020年04月16日 12:29:14   作者:雁回晴空  
這篇文章主要為大家詳細介紹了Unity3D開發(fā)實戰(zhàn)之五子棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

前言

經(jīng)過前面《Unity3D入門教程》系列講解,再加上我們自己的探索,相信大家已經(jīng)掌握了Unity3D的相關知識和基本方法。本文將使用前面學到的知識,開發(fā)一款簡單的五子棋程序。本文用到的東西其實不多,非常簡單。在最后我們會把完整工程的源代碼發(fā)布出來,以供初學者參考。先展示一下最后的運行效果吧。

1 準備工作

(1)開發(fā)環(huán)境:Win10 + Unity5.4.1

(2)圖片素材準備:

黑棋子和白棋子

棋盤

獲勝提示圖片

2 開發(fā)流程

上文提到的素材可以直接下載我們給出的這些圖,也可以自己制作。注意黑白棋子要做成PNG格式,以保證顯示的時候棋子四個角是透明的。將用到的圖片素材導入到工程當中。新建一個場景,創(chuàng)建一個Plane,作為MainCamera的子物體。將棋盤貼圖拖動到Plane上,并且將Plane正面面向攝像機。

再創(chuàng)建四個sphere,作為Plane的子物體,分別命名為LeftTop、RightTop、LeftBottom、RightBottom。然后把他們的MeshRenderer勾選掉。這些球是為了計算棋子落點所設置的,所以需要把它們與棋盤的四個角點對準。

然后我們創(chuàng)建一個chess.cs腳本,綁定到MainCamera上。腳本中包含了所有的功能。需要綁定的一些物體如圖所示。

chess.cs腳本如下:

using UnityEngine;
using System.Collections;
 
public class chess : MonoBehaviour {
 
 //四個錨點位置,用于計算棋子落點
 public GameObject LeftTop;
 public GameObject RightTop;
 public GameObject LeftBottom;
 public GameObject RightBottom;
 //主攝像機
 public Camera cam;
 //錨點在屏幕上的映射位置
 Vector3 LTPos;
 Vector3 RTPos;
 Vector3 LBPos;
 Vector3 RBPos;
 
 Vector3 PointPos;//當前點選的位置
 float gridWidth =1; //棋盤網(wǎng)格寬度
 float gridHeight=1; //棋盤網(wǎng)格高度
 float minGridDis; //網(wǎng)格寬和高中較小的一個
 Vector2[,] chessPos; //存儲棋盤上所有可以落子的位置
 int[,] chessState; //存儲棋盤位置上的落子狀態(tài)
 enum turn {black, white } ;
 turn chessTurn; //落子順序
 public Texture2D white; //白棋子
 public Texture2D black; //黑棋子
 public Texture2D blackWin; //白子獲勝提示圖
 public Texture2D whiteWin; //黑子獲勝提示圖
 int winner = 0; //獲勝方,1為黑子,-1為白子
 bool isPlaying = true; //是否處于對弈狀態(tài)
 void Start () {
 chessPos = new Vector2[15, 15];
 chessState =new int[15,15];
 chessTurn = turn.black;
 
 }
 
 void Update () {
 
 //計算錨點位置
 LTPos = cam.WorldToScreenPoint(LeftTop.transform.position);
 RTPos = cam.WorldToScreenPoint(RightTop.transform.position);
 LBPos = cam.WorldToScreenPoint(LeftBottom.transform.position);
 RBPos = cam.WorldToScreenPoint(RightBottom.transform.position);
 //計算網(wǎng)格寬度
 gridWidth = (RTPos.x - LTPos.x) / 14;
 gridHeight = (LTPos.y - LBPos.y) / 14;
 minGridDis = gridWidth < gridHeight ? gridWidth : gridHeight;
 //計算落子點位置
 for (int i = 0; i < 15; i++)
 {
 for (int j = 0; j < 15; j++)
 {
 chessPos[i, j] = new Vector2(LBPos.x + gridWidth * i, LBPos.y + gridHeight * j);
 }
 }
 //檢測鼠標輸入并確定落子狀態(tài)
 if (isPlaying && Input.GetMouseButtonDown(0))
 {
 PointPos = Input.mousePosition;
 for (int i = 0; i < 15; i++)
 {
 for (int j = 0; j < 15; j++)
 { 
 //找到最接近鼠標點擊位置的落子點,如果空則落子
 if (Dis(PointPos, chessPos[i, j]) < minGridDis / 2 && chessState[i,j]==0)
 {
 //根據(jù)下棋順序確定落子顏色
 chessState[i, j] = chessTurn == turn.black ? 1 : -1;
 //落子成功,更換下棋順序
 chessTurn = chessTurn == turn.black ? turn.white : turn.black; 
 }
 }
 }
 //調(diào)用判斷函數(shù),確定是否有獲勝方
 int re = result();
 if (re == 1)
 {
 Debug.Log("黑棋勝");
 winner = 1;
 isPlaying = false;
 }
 else if(re==-1)
 {
 Debug.Log("白棋勝");
 winner = -1;
 isPlaying = false;
 } 
 }
 //按下空格重新開始游戲
 if (Input.GetKeyDown(KeyCode.Space))
 {
 for (int i = 0; i < 15; i++)
 {
 for (int j = 0; j < 15; j++)
 {
 chessState[i, j] = 0;
 }
 }
 isPlaying = true;
 chessTurn = turn.black;
 winner = 0;
 } 
 }
 //計算平面距離函數(shù)
 float Dis(Vector3 mPos, Vector2 gridPos)
 {
 return Mathf.Sqrt(Mathf.Pow(mPos.x - gridPos.x, 2)+ Mathf.Pow(mPos.y - gridPos.y, 2));
 }
 
 void OnGUI()
 { 
 //繪制棋子
 for(int i=0;i<15;i++)
 {
 for (int j = 0; j < 15; j++)
 {
 if (chessState[i, j] == 1)
 {
 GUI.DrawTexture(new Rect(chessPos[i,j].x-gridWidth/2, Screen.height-chessPos[i,j].y-gridHeight/2, gridWidth,gridHeight),black);
 }
 if (chessState[i, j] == -1)
 {
 GUI.DrawTexture(new Rect(chessPos[i, j].x - gridWidth / 2, Screen.height - chessPos[i, j].y - gridHeight / 2, gridWidth, gridHeight), white);
 } 
 }
 }
 //根據(jù)獲勝狀態(tài),彈出相應的勝利圖片
 if (winner == 1)
 GUI.DrawTexture(new Rect(Screen.width * 0.25f, Screen.height * 0.25f, Screen.width * 0.5f, Screen.height * 0.25f), blackWin);
 if (winner == -1)
 GUI.DrawTexture(new Rect(Screen.width * 0.25f, Screen.height * 0.25f, Screen.width * 0.5f, Screen.height * 0.25f), whiteWin);
 
 }
 //檢測是夠獲勝的函數(shù),不含黑棋禁手檢測
 int result()
 {
 int flag = 0;
 //如果當前該白棋落子,標定黑棋剛剛下完一步,此時應該判斷黑棋是否獲勝
 if(chessTurn == turn.white)
 {
 for (int i = 0; i < 11; i++)
 {
 for (int j = 0; j < 15; j++)
 {
 if (j < 4)
 {
 //橫向
 if (chessState[i, j] == 1 && chessState[i, j + 1] == 1 && chessState[i, j + 2] == 1 && chessState[i, j + 3] == 1 && chessState[i, j + 4] == 1)
 {
 flag = 1;
 return flag;
 }
 //縱向
 if (chessState[i, j] == 1 && chessState[i + 1, j] == 1 && chessState[i + 2, j] == 1 && chessState[i + 3, j] == 1 && chessState[i + 4, j] == 1)
 {
 flag = 1;
 return flag;
 }
 //右斜線
 if (chessState[i, j] == 1 && chessState[i + 1, j + 1] == 1 && chessState[i + 2, j + 2] == 1 && chessState[i + 3, j + 3] == 1 && chessState[i + 4, j + 4] == 1)
 {
 flag = 1;
 return flag;
 }
 //左斜線
 //if (chessState[i, j] == 1 && chessState[i + 1, j - 1] == 1 && chessState[i + 2, j - 2] == 1 && chessState[i + 3, j - 3] == 1 && chessState[i + 4, j - 4] == 1)
 //{
 // flag = 1;
 // return flag;
 //}
 }
 else if (j >= 4 && j < 11)
 {
 //橫向
 if (chessState[i, j] == 1 && chessState[i, j + 1] == 1 && chessState[i, j + 2] == 1 && chessState[i, j + 3] == 1 && chessState[i, j + 4] == 1)
 {
 flag = 1;
 return flag;
 }
 //縱向
 if (chessState[i, j] == 1 && chessState[i + 1, j] == 1 && chessState[i + 2, j] == 1 && chessState[i + 3, j] == 1 && chessState[i + 4, j] == 1)
 {
 flag = 1;
 return flag;
 }
 //右斜線
 if (chessState[i, j] == 1 && chessState[i + 1, j + 1] == 1 && chessState[i + 2, j + 2] == 1 && chessState[i + 3, j + 3] == 1 && chessState[i + 4, j + 4] == 1)
 {
 flag = 1;
 return flag;
 }
 //左斜線
 if (chessState[i, j] == 1 && chessState[i + 1, j - 1] == 1 && chessState[i + 2, j - 2] == 1 && chessState[i + 3, j - 3] == 1 && chessState[i + 4, j - 4] == 1)
 {
 flag = 1;
 return flag;
 }
 }
 else
 {
 //橫向
 //if (chessState[i, j] == 1 && chessState[i, j + 1] == 1 && chessState[i, j + 2] == 1 && chessState[i, j + 3] == 1 && chessState[i, j + 4] == 1)
 //{
 // flag = 1;
 // return flag;
 //}
 //縱向
 if (chessState[i, j] == 1 && chessState[i + 1, j] == 1 && chessState[i + 2, j] == 1 && chessState[i + 3, j] == 1 && chessState[i + 4, j] == 1)
 {
 flag = 1;
 return flag;
 }
 //右斜線
 //if (chessState[i, j] == 1 && chessState[i + 1, j + 1] == 1 && chessState[i + 2, j + 2] == 1 && chessState[i + 3, j + 3] == 1 && chessState[i + 4, j + 4] == 1)
 //{
 // flag = 1;
 // return flag;
 //}
 //左斜線
 if (chessState[i, j] == 1 && chessState[i + 1, j - 1] == 1 && chessState[i + 2, j - 2] == 1 && chessState[i + 3, j - 3] == 1 && chessState[i + 4, j - 4] == 1)
 {
 flag = 1;
 return flag;
 }
 }
 
 }
 }
 for (int i = 11; i < 15; i++) 
 {
 for (int j = 0; j < 11; j++) 
 {
 //只需要判斷橫向 
 if (chessState[i, j] == 1 && chessState[i, j + 1] == 1 && chessState[i, j + 2] == 1 && chessState[i, j + 3] == 1 && chessState[i, j + 4] == 1) 
 { 
 flag = 1; 
 return flag; 
 } 
 }
 }
 }
 //如果當前該黑棋落子,標定白棋剛剛下完一步,此時應該判斷白棋是否獲勝
 else if(chessTurn == turn.black)
 {
 for (int i = 0; i < 11; i++)
 {
 for (int j = 0; j < 15; j++)
 {
 if (j < 4)
 {
 //橫向
 if (chessState[i, j] == -1 && chessState[i, j + 1] == -1 && chessState[i, j + 2] == -1 && chessState[i, j + 3] == -1 && chessState[i, j + 4] == -1)
 {
 flag = -1;
 return flag;
 }
 //縱向
 if (chessState[i, j] == -1 && chessState[i + 1, j] == -1 && chessState[i + 2, j] == -1 && chessState[i + 3, j] == -1 && chessState[i + 4, j] == -1)
 {
 flag = -1;
 return flag;
 }
 //右斜線
 if (chessState[i, j] == -1 && chessState[i + 1, j + 1] == -1 && chessState[i + 2, j + 2] == -1 && chessState[i + 3, j + 3] == -1 && chessState[i + 4, j + 4] == -1)
 {
 flag = -1;
 return flag;
 }
 //左斜線
 //if (chessState[i, j] == -1 && chessState[i + 1, j - 1] == -1 && chessState[i + 2, j - 2] == -1 && chessState[i + 3, j - 3] == -1 && chessState[i + 4, j - 4] == -1)
 //{
 // flag = -1;
 // return flag;
 //}
 }
 else if (j >= 4 && j < 11)
 {
 //橫向
 if (chessState[i, j] == -1 && chessState[i, j + 1] == -1 && chessState[i, j + 2] == -1 && chessState[i, j + 3] == -1 && chessState[i, j + 4] ==- 1)
 {
 flag = -1;
 return flag;
 }
 //縱向
 if (chessState[i, j] == -1 && chessState[i + 1, j] == -1 && chessState[i + 2, j] == -1 && chessState[i + 3, j] == -1 && chessState[i + 4, j] == -1)
 {
 flag = -1;
 return flag;
 }
 //右斜線
 if (chessState[i, j] == -1 && chessState[i + 1, j + 1] == -1 && chessState[i + 2, j + 2] == -1 && chessState[i + 3, j + 3] == -1 && chessState[i + 4, j + 4] == -1)
 {
 flag = -1;
 return flag;
 }
 //左斜線
 if (chessState[i, j] == -1 && chessState[i + 1, j - 1] == -1 && chessState[i + 2, j - 2] == -1 && chessState[i + 3, j - 3] == -1 && chessState[i + 4, j - 4] == -1)
 {
 flag = -1;
 return flag;
 }
 }
 else
 {
 //橫向
 //if (chessState[i, j] == -1 && chessState[i, j + 1] ==- 1 && chessState[i, j + 2] == -1 && chessState[i, j + 3] == -1 && chessState[i, j + 4] == -1)
 //{
 // flag = -1;
 // return flag;
 //}
 //縱向
 if (chessState[i, j] == -1 && chessState[i + 1, j] ==- 1 && chessState[i + 2, j] ==- 1 && chessState[i + 3, j] ==- 1 && chessState[i + 4, j] == -1)
 {
 flag = -1;
 return flag;
 }
 //右斜線
 //if (chessState[i, j] == -1 && chessState[i + 1, j + 1] == -1 && chessState[i + 2, j + 2] == -1 && chessState[i + 3, j + 3] == -1 && chessState[i + 4, j + 4] == -1)
 //{
 // flag = -1;
 // return flag;
 //}
 //左斜線
 if (chessState[i, j] == -1 && chessState[i + 1, j - 1] == -1 && chessState[i + 2, j - 2] == -1 && chessState[i + 3, j - 3] == -1 && chessState[i + 4, j - 4] == -1)
 {
 flag = -1;
 return flag;
 }
 }
 }
 }
 for (int i = 11; i < 15; i++) 
 {
 for (int j = 0; j < 11; j++) 
 {
 //只需要判斷橫向 
 if (chessState[i, j] == -1 && chessState[i, j + 1] == -1 && chessState[i, j + 2] == -1 && chessState[i, j + 3] == -1 && chessState[i, j + 4] == -1) 
 { 
 flag = -1; 
 return flag; 
 } 
 }
 }
 } 
 return flag;
 } 
}

運行效果截圖:

小結

本程序實現(xiàn)了五子棋的基本功能,純屬娛樂而作。暫時沒有加入各種UI、網(wǎng)絡模塊等。本程序經(jīng)過了簡單的測試,沒有什么問題,如果大家在使用的時候發(fā)現(xiàn)有什么Bug,請聯(lián)系我改正,謝謝。

下面是工程源碼下載地址

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 客戶端實現(xiàn)藍牙接收(C#)知識總結

    客戶端實現(xiàn)藍牙接收(C#)知識總結

    網(wǎng)上有關藍牙接收的資料很多,使用起來也很簡單,但是我覺得還是有必要把這些知識總結下來,藍牙開發(fā)需要用到一個第三方的庫InTheHand.Net.Personal.dll,感興趣的朋友可以了解下,或許對你有所幫助
    2013-02-02
  • WPF實現(xiàn)窗體亞克力效果的示例代碼

    WPF實現(xiàn)窗體亞克力效果的示例代碼

    這篇文章主要為大家詳細介紹了如何利用WPF實現(xiàn)窗體亞克力效果,文中的示例代碼講解詳細,對我們學習或工作有一定幫助,感興趣的小伙伴可以了解一下
    2022-10-10
  • C# salt+hash 加密

    C# salt+hash 加密

    本文主要介紹了C# salt+hash加密規(guī)則、C# salt產(chǎn)生偽隨機數(shù)原理、hash原理、使用hash來加密的原因等等。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • c# 實現(xiàn)的支付寶支付

    c# 實現(xiàn)的支付寶支付

    這篇文章主要介紹了c# 實現(xiàn)的支付寶支付的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2021-01-01
  • C#實現(xiàn)將網(wǎng)址生成二維碼圖片方法介紹

    C#實現(xiàn)將網(wǎng)址生成二維碼圖片方法介紹

    這篇文章介紹了C#實現(xiàn)將網(wǎng)址生成二維碼圖片的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 利用C#實現(xiàn)訪問遠程硬盤的高效方案

    利用C#實現(xiàn)訪問遠程硬盤的高效方案

    隨著數(shù)據(jù)存儲需求的不斷增加,越來越多的企業(yè)和開發(fā)者開始將文件存儲從本地硬盤轉移到遠程存儲解決方案中,在本篇文章中,我們將深入探討如何使用C#訪問遠程硬盤,需要的朋友可以參考下
    2025-05-05
  • C#?HttpClient超時重試機制詳解

    C#?HttpClient超時重試機制詳解

    超時重試的實現(xiàn)方式可以使用循環(huán)結構,在請求發(fā)起后等待一定時間,若超時未收到響應,則再次發(fā)起請求,循環(huán)次數(shù)可以根據(jù)實際情況進行設置,一般建議不超過三次,這篇文章主要介紹了C#?HttpClient超時重試,需要的朋友可以參考下
    2023-06-06
  • Unity實現(xiàn)刮獎效果

    Unity實現(xiàn)刮獎效果

    這篇文章主要為大家詳細介紹了Unity實現(xiàn)刮獎效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • C#基于FTP協(xié)議的簡易軟件自動升級程序

    C#基于FTP協(xié)議的簡易軟件自動升級程序

    這篇文章主要為大家詳細介紹了C#基于FTP協(xié)議的簡易軟件自動升級程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Unity實現(xiàn)背景圖片淡入淡出效果

    Unity實現(xiàn)背景圖片淡入淡出效果

    這篇文章主要為大家詳細介紹了Unity實現(xiàn)背景圖片淡入淡出,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02

最新評論