3D游戏编程与设计hw2——空间与运动

c# 自学

IList,列表抽象行为接口

IList实现是可排序且可按照索引访问其成员的值的集合,它本身实现了ICollection和IEnumerable接口
是所有列表的抽象基类。IList 实现有三种类别:只读、固定大小、可变大小。

List,泛型列表类

泛型的List 类是一个不限长度的集合类型,它内部实际就是一个数组,初始长度是4,每次数组到达限制,就会把现有容量翻倍,它提供用于对集合进行搜索、排序和操作等方法
List是数组链表,数组链表访问快,复杂度O(1),但是添加删除复杂度O(n)

ArrayList,动态任意类型数组类

ArrayList是List接口的可变数组非同步实现,并允许包括null在内的所有元素,相当于List < object>

HashSet 类

HashSet是一个无序的能够保持唯一性的集合,不支持下标访问。

Hashtable 类

Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对
基本概念
Hashtable使用了闭散列法来解决冲突,它通过一个结构体bucket来表示哈希表中的单个元素,这个结构体中有三个成员:
 (1) key :表示键,即哈希表中的关键字。
 (2) val :表示值,即跟关键字所对应值。
 (3) hash_coll :它是一个int类型,用于表示键所对应的哈希码。
哈希表的所有元素存放于一个名称为buckets(又称为数据桶) 的bucket数组之中
优点:
 (1)在使用哈希表保存集合元素(一种键/值对)时,首先要根据键自动计算哈希代码,以确定该元素的保存位置,再把元素的值放入相应位置所指向的存储桶中。在查找时,再次通过键所对应的哈希代码到特定存储桶中搜索,这样将大大减少为查找一个元素进行比较的次数
(2)多线程程序中推荐使用Hashtable,对Hashtable进一步调用Synchronized()方法可以获得完全线程安全的类型
Dictionary< TKey, TValue> 是 Hashtable 的泛型版本,它们之间实现上区别不大,运行效率上有一些差别
Hashtable由于键值类型都object,所以涉及装箱拆箱操作,在添加数据的效率上要差一些,但是频繁使用数据时效率更高,HashTable的优点就在于其索引的方式,速度非常快。如果以任意类型键值访问其中元素会快于其他集合,特别是当数据量特别大的时候,效率差别尤其大。

1、简答并用程序验证【建议做】

游戏对象运动的本质是什么?

游戏对象运动的本质,其实是游戏对象跟随每一帧的变化,空间地变化。这里的空间变化包括了游戏对象的transform属性中的position跟rotation两个属性。一个是绝对或者相对位置的改变,一个是所处位置的角度的旋转变化。

请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)

使用Vector3修改position
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move : MonoBehaviour {
    private float speed = 0.25f;//初速度
    private float acceleration = 0.20f;//加速度
    // Start is called before the first frame update
    void Start() {
    }
    // Update is called once per frame
    void Update() {
        this.transform.position += Vector3.right * speed * Time.time;
        this.transform.position += Vector3.down * acceleration * Time.time * Time.time;
    }
}

使用Translate
void Update () {
	transform.Translate (transform.right * speed * Time.fixedTime, Space.World);
	transform.Translate (transform.down * acceleration * Time.fixedTime * Time.fixedTime, Space.World);
}
计算出移动过程中的Transform.position,直接对其赋值:
void Update () {
    float s = speed * Time.deltaTime;
    transform.position = Vector3.MoveTowards(transform.position, target, s);
}

写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。

using System.Collections;

using System.Collections.Generic;

using UnityEngine;



public class Sun : MonoBehaviour
{



    // Use this for initialization

    void Start()
    {



    }



    // Update is called once per frame

    void Update()
    {

        GameObject.Find("Earth").transform.RotateAround(Vector3.zero, new Vector3(0, 1, 0), 30 * Time.deltaTime);

        GameObject.Find("Earth").transform.Rotate(Vector3.up * Time.deltaTime * 10000);

        GameObject.Find("Mercury").transform.RotateAround(Vector3.zero, new Vector3(1, 1, 0), 25 * Time.deltaTime);

        GameObject.Find("Mercury").transform.Rotate(Vector3.up * Time.deltaTime * 10000);

        GameObject.Find("Venus").transform.RotateAround(Vector3.zero, new Vector3(0, 1, 1), 20 * Time.deltaTime);

        GameObject.Find("Venus").transform.Rotate(Vector3.up * Time.deltaTime * 10000);

        GameObject.Find("Mars").transform.RotateAround(Vector3.zero, new Vector3(2, 1, 0), 45 * Time.deltaTime);

        GameObject.Find("Mars").transform.Rotate(Vector3.up * Time.deltaTime * 10000);

        GameObject.Find("Jupiter").transform.RotateAround(Vector3.zero, new Vector3(1, 2, 0), 35 * Time.deltaTime);

        GameObject.Find("Jupiter").transform.Rotate(Vector3.up * Time.deltaTime * 10000);

        GameObject.Find("Saturn").transform.RotateAround(Vector3.zero, new Vector3(0, 1, 2), 40 * Time.deltaTime);

        GameObject.Find("Saturn").transform.Rotate(Vector3.up * Time.deltaTime * 10000);

        GameObject.Find("Uranus").transform.RotateAround(Vector3.zero, new Vector3(0, 2, 1), 45 * Time.deltaTime);

        GameObject.Find("Uranus").transform.Rotate(Vector3.up * Time.deltaTime * 10000);

        GameObject.Find("Neptune").transform.RotateAround(Vector3.zero, new Vector3(1, 1, 1), 50 * Time.deltaTime);

        GameObject.Find("Neptune").transform.Rotate(Vector3.up * Time.deltaTime * 10000);

    }

}

挂载到sun后运行结果如下

3Dunity制作太阳系

2、编程实践

阅读以下游戏脚本

Priests and Devils
Priests and Devils is a puzzle game in which you will help the Priests and Devils to cross the river within the time limit. There are 3 priests and 3 devils at one side of the river. They all want to get to the other side of this river, but there is only one boat and this boat can only carry two persons each time. And there must be one person steering the boat from one side to the other side. In the flash game, you can click on them to move them and click the go button to move the boat to the other direction. If the priests are out numbered by the devils on either side of the river, they get killed and the game is over. You can try it in many >ways. Keep all priests alive! Good luck!

程序需要满足的要求:

1.play the game ( http://www.flash-game.net/game/2535/priests-and-devils.html )
2.列出游戏中提及的事物(Objects)

牧师,魔鬼,小船,起始岸,目标岸,河水

3.用表格列出玩家动作表(规则表),注意,动作越少越好
动作结果约束
点击牧师牧师上船(牧师下船 )牧师与船在同侧岸,该牧师不在船上,船没有移动(牧师在船上,船没有移动)
点击魔鬼魔鬼上船(魔鬼下船魔鬼与船在同侧岸,船未坐满,该魔鬼不在船上,船没有移动(魔鬼在船上,船未移动)
点击船船移动到对岸船不在移动过程中,船上不为空,游戏未结束
4.请将游戏中对象做成预制

用方块和圆球分别表示牧师与魔鬼,制作左岸右岸、河水、船等对象
在这里插入图片描述

5.在场景控制器 LoadResources 方法中加载并初始化 长方形、正方形、球 及其色彩代表游戏中的对象。

初始化对象色彩
在这里插入图片描述

6.使用 C# 集合类型 有效组织对象

完整代码见GitHub牧师与魔鬼

7.整个游戏仅 主摄像机 和 一个 Empty 对象, 其他对象必须代码动态生成!!! 。 整个游戏不许出现 Find 游戏对象, SendMessage 这类突破程序结构的 通讯耦合 语句。 违背本条准则,不给分
8.请使用课件架构图编程,不接受非 MVC 结构程序

在这里插入图片描述

10.注意细节,例如:船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件!

游戏结果演示:

3D游戏制作——牧师与魔鬼

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值