C#“猜猜看”——物联网工程1122 黄炜彬

这篇博客介绍了使用Unity3D平台和C#脚本语言开发的一款猜谜游戏。主要内容涉及游戏脚本的编写,提供了一个作业下载链接。
摘要由CSDN通过智能技术生成

本次猜猜看游戏是基于Unity 3D平台进行开发,采用C scripts编写游戏脚本语言。

主要代码如下:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ChangeImage : MonoBehaviour {

    public Text name;
    public Image image1;
    public Image image2;
    public Image image3;
    //三个用来接收图片号的数
    static int a;
    static int b;
    static int c;
    static int d;

    


    string[] name2 = {"傻蛋","鸭蛋","茶叶蛋","熊蛋包儿","大混蛋","恐龙蛋",
                         "荷包蛋","小笨蛋","狗蛋儿","小丸子","小黄人",
                         "伊利莎白","皮卡丘","多啦A梦","路飞","维尼" };
    public Sprite[] sprite;
    //Random r = new Random();

    void Start()
    {
        Change_Images();
    } 
    public void Change_Images()
    {
    
        //产生的三个数
        int []result2 = RandomNum(16); 
   
         a = result2[0];
         b = result2[1];
         c = result2[2];
        //随机取出三个名字中的一个
        int[] num = new int[3]{ a, b, c };
        System.Random f = new System.Random();
         d = num[f.Next(3)];

        name.text = name2[d];
        
        //显示图片
        image1.overrideSprite = sprite[a];
        image2.overrideSprite = sprite[b];
        image3.overrideSprite = sprite[c];

        Debug.Log(a);
        Debug.Log(b);
        Debug.Log(c);
        Debug.Log("图片名字为" + d);

        }

    //判断图片是否对应
    public void Click_button1()
    {
        Debug.Log(a);
        int a1 = a;
        if (a1 == d)
        {
            Debug.Log("猜对了");
            //Change_Images();
            Application.LoadLevel(1);

        }
        else
        {
            Application.LoadLevel(2);
            Debug.Log("猜错了");
        }
    }
    public void Click_button2()
    {
        Debug.Log(b);
        int b1 = b;
        if (b1 == d)
        {
            Debug.Log("猜对了");
            Application.LoadLevel(1);
        }
        else
        {
            Debug.Log("猜错了");
            Application.LoadLevel(2);
        }
    }
    public void Click_button3()
    {
        Debug.Log(c);
        int c1 = c;
        if (c1 == d)
        {
            Debug.Log("猜对了");
            Application.LoadLevel(1);
        }
        else
        {
            Debug.Log("猜错了");
            Application.LoadLevel(2);
        }
    }


        //产生不重复的随机数
    public static int[] RandomNum(int length)
    {
     
        int[] index = new int[16];
        for (int i = 0; i < 15; i++)
            index[i] = i;
        //接收产生结果的数组
        int[] result = new int[3];
        int site = 16;
        int id;
        for (int j = 0; j < 3; j++)
        {
            id = Random.Range(1, site - 1);
            result[j] = index[id];
            index[id] = index[site - 1];
            site--;
        }

        return result;

        }

}
切换场景代码:

using UnityEngine;
using System.Collections;

public class BacktoMain : MonoBehaviour {

	public void Back_to_Main()
    {
        Application.LoadLevel(0);
        }
}
游戏运行演示:




作业下载连接:http://pan.baidu.com/s/1i3eWmYp

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值