关闭

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

标签: Unity3D
502人阅读 评论(2) 收藏 举报
分类:

本次猜猜看游戏是基于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
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4430次
    • 积分:139
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:0篇
    • 译文:0篇
    • 评论:9条
    文章分类
    文章存档