Unity3D如何實(shí)現(xiàn)五子棋游戲-創(chuàng)新互聯(lián)

小編這次要給大家分享的是Unity3D如何實(shí)現(xiàn)五子棋游戲,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

創(chuàng)新互聯(lián)專注于平山網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供平山營銷型網(wǎng)站建設(shè),平山網(wǎng)站制作、平山網(wǎng)頁設(shè)計(jì)、平山網(wǎng)站官網(wǎng)定制、小程序開發(fā)服務(wù),打造平山網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供平山網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

Unity3D如何實(shí)現(xiàn)五子棋游戲

1 準(zhǔn)備工作

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

(2)圖片素材準(zhǔn)備:

黑棋子和白棋子

Unity3D如何實(shí)現(xiàn)五子棋游戲Unity3D如何實(shí)現(xiàn)五子棋游戲

棋盤

Unity3D如何實(shí)現(xiàn)五子棋游戲

獲勝提示圖片

Unity3D如何實(shí)現(xiàn)五子棋游戲

Unity3D如何實(shí)現(xiàn)五子棋游戲

2 開發(fā)流程

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

Unity3D如何實(shí)現(xiàn)五子棋游戲

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

Unity3D如何實(shí)現(xiàn)五子棋游戲

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

Unity3D如何實(shí)現(xiàn)五子棋游戲

chess.cs腳本如下:

using UnityEngine;
using System.Collections;
 
public class chess : MonoBehaviour {
 
 //四個(gè)錨點(diǎn)位置,用于計(jì)算棋子落點(diǎn)
 public GameObject LeftTop;
 public GameObject RightTop;
 public GameObject LeftBottom;
 public GameObject RightBottom;
 //主攝像機(jī)
 public Camera cam;
 //錨點(diǎn)在屏幕上的映射位置
 Vector3 LTPos;
 Vector3 RTPos;
 Vector3 LBPos;
 Vector3 RBPos;
 
 Vector3 PointPos;//當(dāng)前點(diǎn)選的位置
 float gridWidth =1; //棋盤網(wǎng)格寬度
 float gridHeight=1; //棋盤網(wǎng)格高度
 float minGridDis; //網(wǎng)格寬和高中較小的一個(gè)
 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 () {
 
 //計(jì)算錨點(diǎn)位置
 LTPos = cam.WorldToScreenPoint(LeftTop.transform.position);
 RTPos = cam.WorldToScreenPoint(RightTop.transform.position);
 LBPos = cam.WorldToScreenPoint(LeftBottom.transform.position);
 RBPos = cam.WorldToScreenPoint(RightBottom.transform.position);
 //計(jì)算網(wǎng)格寬度
 gridWidth = (RTPos.x - LTPos.x) / 14;
 gridHeight = (LTPos.y - LBPos.y) / 14;
 minGridDis = gridWidth < gridHeight &#63; gridWidth : gridHeight;
 //計(jì)算落子點(diǎn)位置
 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);
 }
 }
 //檢測鼠標(biāo)輸入并確定落子狀態(tài)
 if (isPlaying && Input.GetMouseButtonDown(0))
 {
 PointPos = Input.mousePosition;
 for (int i = 0; i < 15; i++)
 {
 for (int j = 0; j < 15; j++)
 { 
 //找到最接近鼠標(biāo)點(diǎn)擊位置的落子點(diǎn),如果空則落子
 if (Dis(PointPos, chessPos[i, j]) < minGridDis / 2 && chessState[i,j]==0)
 {
 //根據(jù)下棋順序確定落子顏色
 chessState[i, j] = chessTurn == turn.black &#63; 1 : -1;
 //落子成功,更換下棋順序
 chessTurn = chessTurn == turn.black &#63; 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;
 } 
 }
 //計(jì)算平面距離函數(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),彈出相應(yīng)的勝利圖片
 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;
 //如果當(dāng)前該白棋落子,標(biāo)定黑棋剛剛下完一步,此時(shí)應(yīng)該判斷黑棋是否獲勝
 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; 
 } 
 }
 }
 }
 //如果當(dāng)前該黑棋落子,標(biāo)定白棋剛剛下完一步,此時(shí)應(yīng)該判斷白棋是否獲勝
 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;
 } 
}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

新聞名稱:Unity3D如何實(shí)現(xiàn)五子棋游戲-創(chuàng)新互聯(lián)
文章路徑:http://bm7419.com/article2/dsdiic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、動態(tài)網(wǎng)站靜態(tài)網(wǎng)站、全網(wǎng)營銷推廣、品牌網(wǎng)站建設(shè)、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)