作业2

1. 简答题

  • 解释游戏对象(GameObjects)资源(Assets)的区别与联系。

    区别:
    游戏对象是游戏程序空间中的事物,可能是Empty(空,最有用的事物)、2D、3D、光线、摄像机等;
    游戏资源是构造游戏对象、装饰游戏对象、配置游戏的物体和数据。
    联系:
    游戏资源可以实例为具体的游戏对象,也可以作为某种属性被游戏对象使用。
    
  • 下载几个游戏案例,分别总结资源、对象组织的结构(指资源的目录组织结构与游戏对象树的层次结构)
    游戏案例:在这里插入图片描述
    资源的目录组织结构:
    在这里插入图片描述
    游戏对象树的层次结构:
    在这里插入图片描述
    资源是按照文件夹的形式来组织的,根目录一般是各种类型的资源目录以及说明文件,各资源目录下则是该类型的资源文件。游戏对象树由对象之间的父子关系构成的,父亲对象作为根节点,孩子对象作为子节点。

  • 编写一个代码,使用 debug 语句来验证 MonoBehaviour 基本行为或事件触发的条件

    • 基本行为包括 Awake() Start() Update() FixedUpdate() LateUpdate()
    • 常用事件包括 OnGUI() OnDisable() OnEnable()
    using System.Collections; 	
    using System.Collections.Generic; 	
    using UnityEngine;
    
    public class test : MonoBehaviour 	{
        void Awake()
      	{
           	Debug.Log("This Awake!");
       	}
       	// Start is called before the first frame update
       	void Start()
       	{
           	Debug.Log("This Start!");
       	}
    
       	// Update is called once per frame
       	void Update()
      	 	{
           	Debug.Log("This Update!");
       	}
    
       	void FixedUpdate()
       	{
           	Debug.Log("This FixedUpdate!");
       	}
    
       	void LateUpdate()
       	{
            Debug.Log("This LateUpdate!");
       	}
    
       	void OnGUI()
       	{
           	Debug.Log("This OnGUI!");
       		}
    
       	void OnDisable()
      	 	{
          	 	Debug.Log("This OnDisable!");
       	}
    
       	void OnEnable()
       	{
           	Debug.Log("This OnEnable!");
       	} 	
    }
    

    代码运行结果:

    在这里插入图片描述
    在这里插入图片描述
    Awake():控制脚本被装载时调用;
    Start():在所有Update方法之前被调用一次,表示开始游戏。
    Update():每帧调用一次,用于场景更新。
    FixedUpdate():每固定帧绘制时调用一次,渲染帧执行,渲染效率低下时调用次数减少。
    LateUpdate():每帧执行完毕时调用,在每个Update方法之后。
    OnEnable():当对象变为可用或激活状态时触发。
    OnDisable():当对象变为不可用或非激活状态时触发。
    OnGUI():绘制GUI时触发。一般在这个函数里绘制GUI菜单。

  • 查找脚本手册,了解 GameObject,Transform,Component 对象

    • 分别翻译官方对三个对象的描述(Description)
      GameObject:GameObjects are the fundamental objects in Unity that represent characters, props and scenery. They do not accomplish much in themselves but they act as containers for Components, which implement the real functionality.
      游戏对象是统一的基本对象,代表人物、道具和风景。它们本身并不能完成很多工作,但它们充当组件的容器,这些组件实现了真正的功能。
      
      Transform:The Transform is used to store a GameObject’s position, rotation, scale and parenting state and is thus very important. A GameObject will always have a Transform component attached - it is not possible to remove a Transform or to create a GameObject without one.
      转换用于存储游戏对象的位置、旋转、缩放和父元素状态,因此非常重要。一个游戏对象总是有一个附加的转换组件-不可能移除一个转换或者创建一个没有转换的游戏对象。
      
      Component:Components are the nuts & bolts of objects and behaviors in a game. They are the functional pieces of every GameObject.
      组件是游戏中对象和行为的螺母和螺栓。它们是每个游戏对象的功能部件。
      
    • 描述下图中 table 对象(实体)的属性、table 的 Transform 的属性、 table 的部件
      • 本题目要求是把可视化图形编程界面与 Unity API 对应起来,当你在 Inspector 面板上每一个内容,应该知道对应 API
      • 例如:table 的对象是 GameObject,第一个选择框是 activeSelf 属性。
        在这里插入图片描述
        table对象的属性:activeInHierarchy(表示GameObject是否在场景中处于active状态)、  activeSelf(GameObject的本地活动状态)、
        isStatic(仅编辑器API,指定游戏对象是否为静态)、	layer(游戏对象所在的图层)、scene(游戏对象所属的场景)、tag(游戏对象的标签)、
        transform(游戏对象的转换,最常见的部件)
        table的Transform的属性:Position、Rotation、Scale
        table的部件有:Mesh Filter、Box Collider、Mesh Renderer
        
    • 用 UML 图描述 三者的关系(请使用 UMLet 14.1.1 stand-alone版本出图)
      在这里插入图片描述
  • 资源预设(Prefabs)与 对象克隆 (clone)

    • 预设(Prefabs)有什么好处?
      预设可以提前将设计游戏中所需要的游戏对象进行设计打包,成为一个模板。在设计的过程中,随时可以直接从资源当中加载,成为一个游戏对象。
      
    • 预设与对象克隆 (clone or copy or Instantiate of Unity Object) 关系?
      预设和克隆都是通过复制产生对象。不同的是,预设修改时可以保持所有副本同步,提高使用资源的效率;
      克隆对象修改时其副本独立于被克隆的对象,不会发生变化。
      
    • 制作 table 预制,写一段代码将 table 预制资源实例化成游戏对象
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class test : MonoBehaviour
      {
      	public GameObject table;
      	// Start is called before the first frame update
      	void Start()
      	{
      	 GameObject instance = (GameObject) Instantiate (table, new Vector3(0, Random.Range(5, 10), 0), transform.rotation);
      	}
      
      	// Update is called once per frame
      	void Update()
      	{
      	 
       	}
      }
      
      

2. 编程实践,小游戏

  • 游戏内容: 井字棋 或 贷款计算器 或 简单计算器 等等
  • 技术限制: 仅允许使用 IMGUI 构建 UI
  • 作业目的:
    • 了解 OnGUI() 事件,提升 debug 能力
    • 提升阅读 API 文档能力

这里实现的是井字棋游戏。
了解 IMGUI 请参考unity官方手册关于 IMGUI 的内容

井字棋游戏:两个玩家,一个打圈(O),一个打叉(X),轮流在33的格上打自己的符号,最先以横、直、斜连成一线则为胜。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class jingziqi : MonoBehaviour
{
    private int played = 0;
    private int turn = 1;

    private int[,] chess = new int [3,3];
    
    // Use this for initialization
    void Start()
    {
        reset();
    }
 
    void reset()
    {
        played = 0;
        turn = 1;
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                chess[i,j] = 0;
            }
        } 
    }

    private void OnGUI()
    {
    	//设置字体大小
        GUI.skin.button.fontSize = 20;
        GUI.skin.label.fontSize = 30;

        if(GUI.Button(new Rect(450, 400, 200, 80), "Reset"))
        {
            reset();
        }

        int res = is_win();
        
        if(res == 1)
        {
            GUI.Label(new Rect(500, 20, 100, 50), "O wins!");
        }
        else if(res == 2)
        {
             GUI.Label(new Rect(500, 20, 100, 50), "X wins!");
        }
        else if(res == 3)
        {
             GUI.Label(new Rect(470, 20, 200, 50), "No one wins!");
        }

        for(int i = 0 ; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                if(chess[i,j] == 1) 
                {
                    GUI.Button(new Rect(i * 100 + 400, j * 100 + 80, 100, 100), "O");
                }
                if(chess[i,j] == 2) 
                {
                    GUI.Button(new Rect(i * 100 + 400, j * 100 + 80, 100, 100), "X");
                }
                if(GUI.Button(new Rect(i * 100 + 400, j * 100 + 80, 100, 100), ""))
                {
                    if(res == 0)
                    {
                        if(turn == 1) chess[i,j] = 1;
                        if(turn == 2) chess[i,j] = 2;
                        played++;
                        if(played % 2 == 1) 
                        {
                            turn = 2;
                        } 
                        else
                        {
                            turn = 1;
                        }
                    }
                }

            }
        }
    }

    int is_win()
    {
    	//游戏结束的情况
    	//最中间格子所在的对角线或行或列为同一种棋子时(O或X)
        int temp = chess[1,1];
        if(temp != 0)
        {
            if(temp == chess[0,0] && temp == chess[2,2])
            {
                return temp;
            }
            if(temp == chess[0,2] && temp == chess[2,0])
            {
                return temp;
            }
            if(temp == chess[0,1] && temp == chess[2,1])
            {
                return temp;
            }
            if(temp == chess[1,0] && temp == chess[1,2])
            {
                return temp;
            }
        }
		//左上角格子所在的行或列为同一种棋子时(O或X)
        temp = chess[0,0];
        if(temp != 0)
        {
            if(temp == chess[0,2] && temp == chess[0,1])
            {
                return temp ;
            }
            if(temp == chess[1,0] && temp == chess[2,0])
            {
                return temp;
            }
        }
		//右下角格子所在的行或列为同一种棋子时(O或X)
        temp = chess[2,2];
        if(temp != 0)
        {
            if(temp == chess[2,0] && temp == chess[2,1])
            {
                return temp;
            }
            if(temp == chess[0,2] && temp == chess[1,2])
            {
                return temp;
            }
        }
		//填完所有格子且不属于任何以上任何一种情况为平局
        if(played == 9)
        {
            return 3;
        }
        else
        {
            return 0;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
bupt linux上机作业2 是一项关于Linux操作系统的实践作业,要求对Linux系统进行操作和管理。以下是针对该作业的回答: bupt linux上机作业2 主要涉及以下几个方面的内容: 1. 基本命令的使用:在Linux系统中,我们可以使用一些基本的命令来进行文件和目录的操作,如ls、cd、mkdir、rm等。这些命令的使用是我们平时操作Linux系统的基础,要求掌握它们的基本用法。 2. 文件权限的设置:在Linux系统中,每个文件和目录都有自己的权限,包括读、写、执行等。在这个作业中,我们需要学会如何使用chmod命令来设置文件和目录的权限,以保护系统的安全性。 3. 进程管理和任务调度:Linux系统中的进程管理和任务调度是非常重要的内容。在这个作业中,可以通过使用ps、kill等命令来查看和管理系统中的进程,以及使用cron命令来设置定时任务。 4. 软件包的安装和管理:Linux系统中有很多开源软件包可以供我们使用。在这个作业中,可以学习如何使用apt-get或yum等包管理器来安装和管理软件包,以满足系统中各种软件的需求。 通过完成bupt linux上机作业2,我们可以加深对Linux操作系统的理解和掌握,提升对Linux系统的管理能力。这对于计算机相关专业的学生来说是非常有益的,也是日后工作中可能会用到的技能。希望以上回答能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值