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

作者:莹莹carriex 时间:2023-03-01 06:30:18 

本文实例为大家分享了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实现剪刀石头布游戏

来源:https://blog.csdn.net/u013238823/article/details/44183005

标签:C#,Unity,剪刀石头布
0
投稿

猜你喜欢

  • Android本地数据存储Room实践和优化技巧

    2023-11-04 09:01:30
  • C#如何删除指定文件或文件夹

    2022-06-30 21:28:49
  • springboot集成Swagger的方法(让你拥有属于自己的api管理器)

    2023-11-08 20:39:15
  • 浅谈c#表达式树Expression简单类型比较demo

    2023-07-20 06:24:01
  • Java中字符串String的+和+=及循环操作String原理详解

    2023-05-13 15:10:35
  • Json字符串与Object、List、Map的互转工具类

    2023-04-30 16:08:16
  • C# 使用Fiddler捕获本地HttpClient发出的请求操作

    2022-06-28 04:10:34
  • java查询mongodb中的objectid示例

    2022-03-06 08:59:21
  • 使用Java和ffmpeg把音频和视频合成视频的操作方法

    2022-08-22 07:30:20
  • 解决Android 5.1限制外置SD卡写入权限的问题

    2021-08-05 03:54:41
  • IDEA设置maven修改settings.xml配置文件无法加载仓库的解决方案

    2023-08-23 22:39:35
  • 详解Android中IntentService的使用方法

    2021-07-14 06:49:49
  • Java实现二叉堆、大顶堆和小顶堆

    2022-08-20 18:39:33
  • Java 关键字break和continue的使用说明

    2023-07-22 03:50:59
  • Spring 自动装配的二义性实例解析

    2021-12-16 00:37:55
  • 使用Spring Data JDBC实现DDD聚合的示例代码

    2022-05-04 05:11:23
  • Winform实现调用asp.net数据接口实例

    2021-12-16 09:12:48
  • Java探索之Thread+IO文件的加密解密代码实例

    2023-01-26 19:07:03
  • SWT(JFace)体验之复制粘贴

    2022-11-18 13:47:46
  • Android框架Volley使用:ImageRequest请求实现图片加载

    2022-03-01 11:30:06
  • asp之家 软件编程 m.aspxhome.com