C#語(yǔ)言使用Unity實(shí)現(xiàn)剪刀石頭布游戲
本文實(shí)例為大家分享了C#語(yǔ)言使用Unity實(shí)現(xiàn)剪刀石頭布游戲的具體代碼,供大家參考,具體內(nèi)容如下
游戲:剪刀石頭布
實(shí)現(xiàn)功能:
1、電腦隨機(jī)出牌(剪刀石頭布)
2、玩家選擇出牌(剪刀石頭布)
3、玩家沒(méi)有出牌時(shí),電腦變幻牌面;
玩家出牌后,電腦出牌,并停止變幻牌面3秒,期間玩家無(wú)法選擇出牌
4、玩家和電腦出牌后,電腦自動(dòng)計(jì)分。
using UnityEngine; using System.Collections; public class hw0310a : MonoBehaviour { // public Rect windows1; public Rect windows2; public Rect btn1; public Rect btn2; public Rect btn3; public Rect box1; public Rect box2; public Rect label1; public Rect label2; public string str1; public string str2; public int score1; public int score2; public Texture[] texture; public int ComputerCp; public int PlayerCp; public GUISkin myskin; public bool notShowTime; public float MaxTime=3; void Start () { windows1=new Rect(100,240,400,120); windows2=new Rect(100,0,400,195); btn1=new Rect(40,20,80,80); btn2=new Rect(160,20,80,80); btn3=new Rect(280,20,80,80); box1=new Rect(50,30,100,100); box2=new Rect(250,30,100,100); label1 = new Rect (95, 150, 50, 50); label2 = new Rect (295, 150, 50, 50); score1 = 0; score2 = 0; str1 = score1.ToString ();//將int類(lèi)型轉(zhuǎn)換為string類(lèi)型,方便計(jì)分 str2 = score2.ToString (); PlayerCp = 4; notShowTime = false; } // Update is called once per frame void Update () { str1 = score1.ToString (); str2 = score2.ToString ();
//控制電腦牌面的變幻條件和時(shí)間 if (notShowTime==false) { computerShow (); } else { MaxTime -= Time.deltaTime; if (MaxTime < 0) { notShowTime = false; MaxTime=3; } } } void computerShow(){ if (ComputerCp < 2) { ComputerCp++; } else { ComputerCp = 0; } } void OnGUI() { windows1=GUI.Window (1, windows1, windowFun1, "Game"); windows2=GUI.Window (2, windows2, windowFun2, "Computer VS Player"); } void windowFun1(int Id) { //電腦牌面停止變幻時(shí),玩家不能出牌 if (GUI.Button (btn1, texture [0])) { if(notShowTime==false) { PlayerCp=0; ComputerOut();//根據(jù)玩家的牌,電腦出牌,電腦永遠(yuǎn)贏 gameLogic();//判斷輸贏并計(jì)分 } } if (GUI.Button (btn2, texture [1])) { if(notShowTime==false) { PlayerCp=1; ComputerOut(); gameLogic(); } } if (GUI.Button (btn3, texture [2])) { if(notShowTime==false) { PlayerCp=2; ComputerOut(); gameLogic(); } } GUI.DragWindow (new Rect(0,0,400,120));//括號(hào)內(nèi)的矩形是可界面拖拽范圍 } void windowFun2(int Id) { // GUI.Button (b4, t4); // GUI.Button (b5, t5); GUI.DragWindow (new Rect(0,0,400,195)); GUI.Box (box1,texture[ComputerCp]); GUI.Box (box2,texture[PlayerCp]); GUI.Label (label1, str1); GUI.Label (label2, str2); } void ComputerOut() { notShowTime = true; int a=PlayerCp; int b = PlayerCp - 2; int c = PlayerCp + 1; if (PlayerCp == 2) { //在確定的兩個(gè)數(shù)中選取隨機(jī)數(shù)的方法 int[] Cp1 = new int[]{a,b}; int i = Random.Range (0, 2); ComputerCp = Cp1[i]; } else { int[] Cp2= new int[]{a,c}; int i = Random.Range (0, 2); ComputerCp = Cp2[i]; } // ComputerCp = Random.Range (0, 3); } void gameLogic() { int a = ComputerCp - PlayerCp; switch (a) { case 0:Debug.Log ("pingju");break; case 1: case -2: { Debug.Log ("Player Lose"); score1=score1+1; break; } case 2: case -1: { Debug.Log ("Player Win"); score2=score2+1; break; } default:break; } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:MVC設(shè)定默認(rèn)路由為指定的Area下的某個(gè)action
欄 目:C#教程
下一篇:C#實(shí)現(xiàn)QQ郵箱發(fā)送郵件
本文標(biāo)題:C#語(yǔ)言使用Unity實(shí)現(xiàn)剪刀石頭布游戲
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4943.html
您可能感興趣的文章
- 01-10C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放
- 01-10C#3.0使用EventLog類(lèi)寫(xiě)Windows事件日志的方法
- 01-10C#使用windows服務(wù)開(kāi)啟應(yīng)用程序的方法
- 01-10c# ArrayList的使用方法小總結(jié)
- 01-10C#使用ADO.Net部件來(lái)訪問(wèn)Access數(shù)據(jù)庫(kù)的方法
- 01-10C#使用Mutex簡(jiǎn)單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10C#編程自學(xué)之開(kāi)篇介紹
- 01-10C#中yield用法使用說(shuō)明
- 01-10C#編程和Visual Studio使用技巧(下)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)