本篇文章為大家展示了使用Unity3D怎么制作一個五子棋游戲,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

創(chuàng)新互聯(lián)公司為客戶提供專業(yè)的網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、程序、域名、空間一條龍服務(wù),提供基于WEB的系統(tǒng)開發(fā). 服務(wù)項目涵蓋了網(wǎng)頁設(shè)計、網(wǎng)站程序開發(fā)、WEB系統(tǒng)開發(fā)、微信二次開發(fā)、成都做手機網(wǎng)站等網(wǎng)站方面業(yè)務(wù)。
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;//當(dāng)前點選的位置
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[17, 16];/*原來定義是new int[15, 15],這里將原來數(shù)組chessState上、下和右邊各加一排數(shù)據(jù),
也就相當(dāng)于在棋盤的上、下和右邊各填加一排隱形的棋道。原因后面解釋*/
chessTurn = turn.black;
//計算錨點位置
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 * j, LBPos.y + gridHeight * i);//這里和源程序定義稍有不同,這里i定位行,j為列
}
}
}
void Update()
{
//檢測鼠標(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)點擊位置的落子點,如果空則落子
if (Dis(PointPos, chessPos[i, j]) < minGridDis / 2 && chessState[i + 1, j] == 0)/*這里chessState行要加1,
因為上、下和右邊各多加了一排,要空出來,chessPos的i行對應(yīng)chessState的i+1行*/
{
//根據(jù)下棋順序確定落子顏色
chessState[i + 1, 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 + 1, 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 + 1, 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 + 1, 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);
}
//改寫result函數(shù)
/*解釋:C語言中,這樣的表達式:chessState[i]&&chessState[i+1]&&chessState[i+2]&&chessState[i+3]&&chessState[i+4],如果
* chessState[i]為False,則不管B是真是假或者是異常都不會運行,利用這一點,在chessState的右邊、上邊和下邊各加一行為0的數(shù)據(jù),
* 這樣在判斷連續(xù)五個棋子的狀態(tài)時,就不用擔(dān)心chessState數(shù)組的索引值超出范圍。例如:chessState[i+4]的索引值i+4剛好超出范圍,
* 通過在原來數(shù)組chessState的上、下和右邊個添加一排為0的數(shù),這樣chessState[i+3]==0,于是就可以避免引起異常,從而簡化代碼*/
int result()
{
int flag = 0;
if (chessTurn == turn.white)
{
for (int i = 1; i <= 15; i++)//這里的i從1開始
{
for (int j = 0; j <= 14; j++)//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)//向右橫向
|| (chessState[i, j] == 1 && chessState[i + 1, j] == 1 && chessState[i + 2, j] == 1 && chessState[i + 3, j] == 1 && chessState[i + 4, j] == 1)//向上橫向
|| (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)//向右上斜向
|| (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;
}
}
}
}
else if (chessTurn == turn.black)
{
for (int i = 1; i <= 15; i++)//這里的i從1開始
{
for (int j = 0; j <= 14; 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)
|| (chessState[i, j] == -1 && chessState[i + 1, j] == -1 && chessState[i + 2, j] == -1 && chessState[i + 3, j] == -1 && chessState[i + 4, j] == -1)
|| (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)
|| (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;
}
}上述內(nèi)容就是使用Unity3D怎么制作一個五子棋游戲,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:使用Unity3D怎么制作一個五子棋游戲
本文鏈接:http://chinadenli.net/article26/iidjcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、做網(wǎng)站、、電子商務(wù)、軟件開發(fā)、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)