3D游戏编程与设计 作业三
1、简答并用程序验证【建议做】
-
游戏对象运动的本质是什么?
游戏对象运动的本质,是游戏对象跟随每一帧的变化自身空间坐标的变换。
-
请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)
方法一:利用position的改变来实现抛物线运动。
抛物线运动在水平方向是匀速的,在竖直方向上是匀加速的,然后将两个方向的运动矢量相加即可,实现代码如下:
方法二:创建Vector3向量。
定义Vector3变量的值,也是竖直方向上是一个均匀增加的数值,水平方向是一个保持不变的数值,然后将游戏对象原本的position属性与该向量相加即可实现抛物线运动,代码如下:
方法三:利用transform中的translate函数来进行改变position。
传入一个Vector3向量的参数,实现position的改变,代码如下:
-
写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Rotate : MonoBehaviour { public Transform sun; public Transform mercury; public Transform venus; public Transform earth; public Transform mars; public Transform jupiter; public Transform saturn; public Transform uranus; public Transform neptune; Vector3[] a = new Vector3[9]; float y, z; // Start is called before the first frame update void Start() { sun.position = Vector3.zero; for(int i = 0; i < 9; i++) { y = Random.Range(1, 360); z = Random.Range(1, 360); a[i] = new Vector3(0, y, z); } } // Update is called once per frame void Update() { mercury.RotateAround(sun.position, a[0], 47 * Time.deltaTime); mercury.Rotate(Vector3.up * 47 * Time.deltaTime); venus.RotateAround(sun.position, a[1], 35 * Time.deltaTime); venus.Rotate(Vector3.up * 35 * Time.deltaTime); earth.RotateAround(sun.position, a[2], 30 * Time.deltaTime); earth.Rotate(Vector3.up * 30 * Time.deltaTime); mars.RotateAround(sun.position, a[3], 24 * Time.deltaTime); mars.Rotate(Vector3.up * 24 * Time.deltaTime); jupiter.RotateAround(sun.position, a[4], 13 * Time.deltaTime); jupiter.Rotate(Vector3.up * 13 * Time.deltaTime); saturn.RotateAround(sun.position, a[5], 11 * Time.deltaTime); saturn.Rotate(Vector3.up * 11 * Time.deltaTime); uranus.RotateAround(sun.position, a[6], 9 * Time.deltaTime); uranus.Rotate(Vector3.up * 9 * Time.deltaTime); neptune.RotateAround(sun.position, a[7], 8 * Time.deltaTime); neptune.Rotate(Vector3.up * 8 * Time.deltaTime); } }
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!
程序需要满足的要求:
-
play the game ( http://www.flash-game.net/game/2535/priests-and-devils.html )
-
列出游戏中提及的事物(Objects)
牧师、魔鬼、船、陆地、河流 -
用表格列出玩家动作表(规则表),注意,动作越少越好
①开船(船不能为空)②牧师上下船③魔鬼上下船 -
请将游戏中对象做成预制
-
在场景控制器 LoadResources 方法中加载并初始化 长方形、正方形、球 及其色彩代表游戏中的对象。
-
使用 C# 集合类型 有效组织对象
-
整个游戏仅 主摄像机 和 一个 Empty 对象, 其他对象必须代码动态生成!!! 。 整个游戏不许出现 Find 游戏对象, SendMessage 这类突破程序结构的 通讯耦合 语句。违背本条准则,不给分
-
请使用课件架构图编程,不接受非 MVC 结构程序
-
注意细节,例如:船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件!
代码如下
boatBehavior.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class boatBehavior : MonoBehaviour
{
public boatAction action;
// Use this for initialization
void Start()
{
action = SSDirector.getInstance().currentSceneController as boatAction;
}
private void OnMouseDown()
{
action.boat_move(this.gameObject);
}
// Update is called once per frame
void Update()
{
}
}
priestBehavior.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class priestBehavior : MonoBehaviour
{
private priestAction action;
// Use this for initialization
void Start()
{
action = SSDirector.getInstance().currentSceneController as priestAction;
}
private void OnMouseDown()
{
action.priest_move(this.gameObject);
}
// Update is called once per frame
void Update()
{
}
}
devilBehavior.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class devilBehavior : MonoBehaviour
{
private devilAction action;
// Use this for initialization
void Start()
{
action = SSDirector.getInstance().currentSceneController as devilAction;
}
private void OnMouseDown()
{
action.devil_move(this.gameObject);
}
// Update is called once per frame
void Update()
{
}
}
UserGUI.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UserGUI : MonoBehaviour
{
private IUserAction action;
// Use this for initialization
void Start()
{
action = SSDirector.getInstance().currentSceneController as IUserAction;
}
void onGUI()
{
}
// Update is called once per frame
void Update()
{
}
}
Interface.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public interface IsceneController
{
void LoadResources();
void Restart();
}
public interface IUserAction
{
}
public interface devilAction
{
void devil_move(GameObject obj);
}
public interface priestAction
{
void priest_move(GameObject obj);
}
public interface boatAction
{
void boat_move(GameObject obj);
}
Director.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SSDirector : System.Object
{
// singlton instance
private static SSDirector _instance;
public IsceneController currentSceneController { get; set; }
public bool running { get; set; }
//get instance anytime anywhere!
public static SSDirector getInstance()
{
if (_instance == null)
{
_instance = new SSDirector();
}
return _instance;
}
public int getFPS()
{
return Application.targetFrameRate;
}
public void setFPS(int fps)
{
Application.targetFrameRate = fps;
}
}
FirstController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SSDirector : System.Object
{
// singlton instance
private static SSDirector _instance;
public IsceneController currentSceneController { get; set; }
public bool running { get; set; }
//get instance anytime anywhere!
public static SSDirector getInstance()
{
if (_instance == null)
{
_instance = new SSDirector();
}
return _instance;
}
public int getFPS()
{
return Application.targetFrameRate;
}
public void setFPS(int fps)
{
Application.targetFrameRate = fps;
}
}