欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#语言使用Unity实现剪刀石头布游戏

程序员文章站 2023-11-29 15:28:10
本文实例为大家分享了c#语言使用unity实现剪刀石头布游戏的具体代码,供大家参考,具体内容如下 游戏:剪刀石头布 实现功能: 1、电脑随机出牌(剪刀石头布) 2、...

本文实例为大家分享了c#语言使用unity实现剪刀石头布游戏的具体代码,供大家参考,具体内容如下

游戏:剪刀石头布

实现功能:

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

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

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

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

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

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类型转换为string类型,方便计分
 str2 = score2.tostring ();
 playercp = 4;
 notshowtime = false;
 }
 
 // update is called once per frame
 void update () {
 str1 = score1.tostring ();
 str2 = score2.tostring ();

//控制电脑牌面的变幻条件和时间
 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)
 {  //电脑牌面停止变幻时,玩家不能出牌
 if (gui.button (btn1, texture [0])) 
 {
 if(notshowtime==false)
 {
 playercp=0;
 computerout();//根据玩家的牌,电脑出牌,电脑永远赢
 gamelogic();//判断输赢并计分
 }
 }
 
 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));//括号内的矩形是可界面拖拽范围
 }
 
 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) 
 {  //在确定的两个数中选取随机数的方法
 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;
 }
 
 }
}

C#语言使用Unity实现剪刀石头布游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。