Unity-->实现剪刀石头布的游戏

游戏:剪刀石头布

实现功能:

1、电脑随机出牌(剪刀石头布)

2、玩家选择出牌(剪刀石头布)

3、玩家没有出牌时,电脑变幻牌面;

      玩家出牌后,电脑出牌,并停止变幻牌面3秒,期间玩家无法选择出牌

4、玩家和电脑出牌后,电脑自动计分。

[csharp]  view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class hw0310a : MonoBehaviour {  
  5.     //  
  6.     public Rect windows1;  
  7.     public Rect windows2;  
  8.     public Rect btn1;  
  9.     public Rect btn2;  
  10.     public Rect btn3;  
  11.     public Rect box1;  
  12.     public Rect box2;  
  13.     public Rect label1;  
  14.     public Rect label2;  
  15.     public string str1;  
  16.     public string str2;  
  17.     public int score1;  
  18.     public int score2;  
  19.     public Texture[] texture;  
  20.     public int ComputerCp;  
  21.     public int PlayerCp;  
  22.     public GUISkin myskin;  
  23.     public bool notShowTime;  
  24.     public float MaxTime=3;  
  25.     void Start () {  
  26.         windows1=new Rect(100,240,400,120);  
  27.         windows2=new Rect(100,0,400,195);  
  28.         btn1=new Rect(40,20,80,80);  
  29.         btn2=new Rect(160,20,80,80);  
  30.         btn3=new Rect(280,20,80,80);  
  31.         box1=new Rect(50,30,100,100);  
  32.         box2=new Rect(250,30,100,100);  
  33.         label1 = new Rect (95, 150, 50, 50);  
  34.         label2 = new Rect (295, 150, 50, 50);  
  35.         score1 = 0;  
  36.         score2 = 0;  
  37.         str1 = score1.ToString ();//将int类型转换为string类型,方便计分  
  38.         str2 = score2.ToString ();  
  39.         PlayerCp = 4;  
  40.         notShowTime = false;  
  41.     }  
  42.       
  43.     // Update is called once per frame  
  44.     void Update () {  
  45.         str1 = score1.ToString ();  
  46.         str2 = score2.ToString ();  
[csharp]  view plain copy
  1.        //控制电脑牌面的变幻条件和时间  
  2.         if (notShowTime==false)   
  3.         {  
  4.         computerShow ();  
  5.         }   
  6.         else   
  7.         {  
  8.         MaxTime -= Time.deltaTime;  
  9.         if (MaxTime < 0)  
  10.         {  
  11.         notShowTime = false;  
  12.                 MaxTime=3;  
  13.         }  
  14.         }  
  15.     }  
  16.   
  17.     void computerShow(){  
  18.                 if (ComputerCp < 2) {  
  19.                         ComputerCp++;  
  20.                 } else {  
  21.                         ComputerCp = 0;  
  22.                 }  
  23.         }  
  24.     void OnGUI()  
  25.     {  
  26.         windows1=GUI.Window (1, windows1, windowFun1, "Game");  
  27.         windows2=GUI.Window (2, windows2, windowFun2, "Computer  VS  Player");  
  28.     }  
  29.   
  30.     void windowFun1(int Id)  
  31.     {       //电脑牌面停止变幻时,玩家不能出牌  
  32.         if (GUI.Button (btn1, texture [0]))   
  33.         {  
  34.             if(notShowTime==false)  
  35.             {  
  36.             PlayerCp=0;  
  37.             ComputerOut();//根据玩家的牌,电脑出牌,电脑永远赢  
  38.             gameLogic();//判断输赢并计分  
  39.             }  
  40.         }  
  41.           
  42.         if (GUI.Button (btn2, texture [1]))   
  43.         {  
  44.             if(notShowTime==false)  
  45.             {  
  46.             PlayerCp=1;  
  47.             ComputerOut();  
  48.             gameLogic();  
  49.             }  
  50.         }  
  51.           
  52.         if (GUI.Button (btn3, texture [2]))   
  53.         {  
  54.             if(notShowTime==false)  
  55.             {  
  56.             PlayerCp=2;  
  57.             ComputerOut();  
  58.             gameLogic();  
  59.             }  
  60.         }  
  61.   
  62.   
  63.         GUI.DragWindow (new Rect(0,0,400,120));//括号内的矩形是可界面拖拽范围  
  64.     }  
  65.   
  66.     void windowFun2(int Id)  
  67.     {  
  68. //      GUI.Button (b4, t4);  
  69. //      GUI.Button (b5, t5);  
  70.         GUI.DragWindow (new Rect(0,0,400,195));  
  71.   
  72.         GUI.Box (box1,texture[ComputerCp]);  
  73.         GUI.Box (box2,texture[PlayerCp]);  
  74.   
  75.         GUI.Label (label1, str1);  
  76.         GUI.Label (label2, str2);  
  77.     }  
  78.   
  79.     void ComputerOut()  
  80.     {  
  81.         notShowTime = true;  
  82.         int a=PlayerCp;  
  83.         int b = PlayerCp - 2;  
  84.         int c = PlayerCp + 1;  
  85.         if (PlayerCp == 2)   
  86.         {       //在确定的两个数中选取随机数的方法  
  87.             int[] Cp1 = new int[]{a,b};  
  88.             int i = Random.Range (0, 2);  
  89.             ComputerCp = Cp1[i];  
  90.         }   
  91.         else   
  92.         {  
  93.             int[] Cp2= new int[]{a,c};  
  94.             int i = Random.Range (0, 2);  
  95.             ComputerCp = Cp2[i];  
  96.         }  
  97.         //      ComputerCp = Random.Range (0, 3);  
  98.   
  99.     }  
  100.   
  101.   
  102.     void gameLogic()  
  103.     {  
  104.         int a = ComputerCp - PlayerCp;  
  105.         switch (a)   
  106.         {  
  107.         case 0:Debug.Log ("pingju");break;  
  108.         case 1:  
  109.         case -2:  
  110.         {  
  111.             Debug.Log ("Player Lose");  
  112.             score1=score1+1;  
  113.             break;  
  114.         }  
  115.         case 2:  
  116.         case -1:  
  117.         {  
  118.             Debug.Log ("Player Win");  
  119.             score2=score2+1;  
  120.             break;  
  121.         }  
  122.         default:break;  
  123.         }  
  124.   
  125.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值