空间与运动
一. 简答并用程序验证【建议做】
1. 游戏对象运动的本质是什么?
游戏对象 position、scale、rotation 属性的变化。
请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)
修改 Transform
public int speed
= 2;
void Update()
{
transform
.position
+= new Vector3(Time
.deltaTime
* speed
, -Time
.deltaTime
* speed
* (2 * transform
.position
.x
+ Time
.deltaTime
* speed
), 0);
}
使用 Transform.translate
public int speedX
= 1;
public int speedY
= 1;
public int t
= 1;
void Update()
{
transform
.Translate(Vector3
.right
* Time
.deltaTime
* xSpeed
+ Vector3
.down
* Time
.deltaTime
* ySpeed
* Time
.deltaTime
* T
);
T
++;
}
使用 Vector3
public int speedX
= 1;
public int speedY
= 1;
public int t
= 1;
void Update()
{
transform
.position
+= Vector3
.right
* Time
.deltaTime
* speedX
;
transform
.position
+= Vector3
.down
* Time
.deltaTime
* speedY
* Time
.deltaTime
* t
;
t
++;
}
3. 写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class move
: MonoBehaviour
{
public Transform Sun
;
public Transform Moon
;
public Transform Mercury
;
public Transform Venus
;
public Transform Earth
;
public Transform Mars
;
public Transform Jupiter
;
public Transform Saturn
;
public Transform Uranus
;
public Transform Neptune
;
void Start
() {
Sun
.position
= Vector3
.zero
;
}
void Update
() {
Mercury
.RotateAround
(Sun
.position
, new Vector3(0, 5, 1), 60 * Time
.deltaTime
);
Mercury
.Rotate
( new Vector3(0, 5, 1) * 10000 / 58 * Time
.deltaTime
);
Venus
.RotateAround
(Sun
.position
, new Vector3(0, 2, 1), 55 * Time
.deltaTime
);
Venus
.Rotate
(new Vector3(0, 2, 1) * 10000/ 243 *Time
.deltaTime
);
Earth
.RotateAround
(Sun
.position
, Vector3
.up
, 50 * Time
.deltaTime
);
Earth
.Rotate
(Vector3
.up
* 30 * Time
.deltaTime
);
Moon
.transform
.RotateAround
(Earth
.position
, Vector3
.up
, 5 * Time
.deltaTime
);
Mars
.RotateAround
(Sun
.position
, new Vector3(0, 12, 5), 45 * Time
.deltaTime
);
Mars
.Rotate
(new Vector3(0, 12, 5) * 10000 * Time
.deltaTime
);
Jupiter
.RotateAround
(Sun
.position
, new Vector3(0, 10, 3), 35 * Time
.deltaTime
);
Jupiter
.Rotate
(new Vector3(0, 10, 3) * 10000/0.3f * Time
.deltaTime
);
Saturn
.RotateAround
(Sun
.position
, new Vector3(0, 3, 1), 20 * Time
.deltaTime
);
Saturn
.Rotate
(new Vector3(0, 3, 1) * 10000/0.4f * Time
.deltaTime
);
Uranus
.RotateAround
(Sun
.position
, new Vector3(0, 10, 1), 15 * Time
.deltaTime
);
Uranus
.Rotate
(new Vector3(0, 10, 1) * 10000/0.6f * Time
.deltaTime
);
Neptune
.RotateAround
(Sun
.position
, new Vector3(0, 8, 1), 10 * Time
.deltaTime
);
Neptune
.Rotate
(new Vector3(0, 8, 1) * 10000/0.7f * Time
.deltaTime
);
}
}
二. 编程实践
1. 阅读以下游戏脚本
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 结构程序注意细节,例如:船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件!
玩家规则表
动作游戏状态结果
点击左侧岸上的角色角色在岸上,船在左岸,船有空位角色上船点击船船上有人船移动到对岸点击左侧岸上的角色角色在船上,船在右岸角色上岸/任意一岸的恶魔大于牧师且牧师数量不为0游戏失败/右侧岸上有三个牧师游戏胜利/时间超过60s游戏失败
代码结构
游戏对象实现做成预制
MVC结构
代码:
Models
Models存储游戏对象的各种数据,只携带初始化函数,每个可能会变更数据的model都分配了一个controller。
(1)RoleModel.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class RoleModel{
public GameObject role
;
public bool isPriest
;
public bool isRight
;
public bool isInBoat
;
public int tag
;
public RoleModel(Vector3 position
, bool isPriest
, int tag
)
{
this.isPriest
= isPriest
;
isRight
= false;
isInBoat
= false;
this.tag
= tag
;
role
= GameObject
.Instantiate(Resources
.Load("Prefabs/"+(isPriest
?"priest":"devil"), typeof(GameObject
))) as GameObject
;
role
.transform
.localScale
= new Vector3(1,1,1);
role
.transform
.position
= position
;
role
.name
= "role" + tag
;
role
.AddComponent<Click>();
role
.AddComponent<BoxCollider>();
}
}
(2)LandModel.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class LandModel
{
public GameObject land
;
public int priestNum
, devilNum
;
public LandModel(string name
, Vector3 position
)
{
priestNum
= devilNum
= 0;
land
= GameObject
.Instantiate(Resources
.Load("Prefabs/land", typeof(GameObject
))) as GameObject
;
land
.name
= name
;
land
.transform
.position
= position
;
land
.transform
.localScale
= new Vector3(13,5,3);
}
}
(3)BoatModel.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class BoatModel {
public GameObject boat
;
public bool isRight
;
public int priestNum
, devilNum
;
public RoleModel
[] roles
;
public BoatModel(Vector3 position
)
{
priestNum
= devilNum
= 0;
roles
= new RoleModel[2];
boat
= GameObject
.Instantiate(Resources
.Load("Prefabs/boat", typeof(GameObject
))) as GameObject
;
boat
.name
= "boat";
boat
.transform
.position
= position
;
boat
.transform
.localScale
= new Vector3(4, (float)1.5, 3);
boat
.AddComponent<BoxCollider>();
boat
.AddComponent<Click>();
isRight
= false;
}
}
(4)RiverModel.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class RiverModel
{
private GameObject river
;
public RiverModel(Vector3 position
)
{
river
= Object
.Instantiate(Resources
.Load("Prefabs/river", typeof(GameObject
))) as GameObject
;
river
.name
= "river";
river
.transform
.position
= position
;
river
.transform
.localScale
= new Vector3(15, 2, 3);
}
}
(5)PositionModel
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class PositionModel
{
public static Vector3 right_land
= new Vector3(15, -4, 0);
public static Vector3 left_land
= new Vector3(-13, -4, 0);
public static Vector3 river
= new Vector3(1, -(float)5.5, 0);
public static Vector3 right_boat
= new Vector3(7, -(float)3.8, 0);
public static Vector3 left_boat
= new Vector3(-5, -(float)3.8, 0);
public static Vector3
[] roles
= new Vector3[]{new Vector3((float)-0.2, (float)0.7, 0) ,new Vector3((float)-0.1, (float)0.7,0),
new Vector3(0, (float)0.7,0),new Vector3((float)0.1, (float)0.7,0),new Vector3((float)0.2, (float)0.7,0),new Vector3((float)0.3, (float)0.7,0)};
public static Vector3
[] boatRoles
= new Vector3[] { new Vector3((float)-0.1, (float)1.2, 0), new Vector3((float)0.2, (float)1.2, 0) };
}
(6)click.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class Click : MonoBehaviour
{
ClickAction clickAction
;
public void setClickAction(ClickAction clickAction
)
{
this.clickAction
= clickAction
;
}
void OnMouseDown()
{
clickAction
.DealClick();
}
}
(7)clickAction.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public interface ClickAction
{
void DealClick();
}
Controllers
Controllers实现游戏的主要逻辑和所有的功能函数,读取修改Models和Views当中的数据。
(1)RoleModelControllers.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class RoleModelController :ClickAction
{
RoleModel roleModel
;
IUserAction userAction
;
public RoleModelController()
{
userAction
= SSDirector
.GetInstance().CurrentSenceController as IUserAction
;
}
public void CreateRole(Vector3 position
, bool isPriest
, int tag
)
{
if (roleModel
!= null)
Object
.DestroyImmediate(roleModel
.role
);
roleModel
= new RoleModel(position
, isPriest
, tag
);
roleModel
.role
.GetComponent<Click>().setClickAction(this);
}
public RoleModel GetRoleModel()
{
return roleModel
;
}
public void DealClick()
{
userAction
.MoveRole(roleModel
);
}
}
(2)BoatController.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class BoatController : ClickAction
{
BoatModel boatModel
;
IUserAction userAction
;
public BoatController()
{
userAction
= SSDirector
.GetInstance().CurrentSenceController as IUserAction
;
}
public void CreateBoat(Vector3 position
)
{
if (boatModel
!= null)
Object
.DestroyImmediate(boatModel
.boat
);
boatModel
= new BoatModel(position
);
boatModel
.boat
.GetComponent<Click>().setClickAction(this);
}
public BoatModel GetBoatModel()
{
return boatModel
;
}
public Vector3 AddRole(RoleModel roleModel
)
{
if (boatModel
.roles
[0] == null)
{
boatModel
.roles
[0] = roleModel
;
roleModel
.isInBoat
= true;
roleModel
.role
.transform
.parent
= boatModel
.boat
.transform
;
if (roleModel
.isPriest
)
boatModel
.priestNum
++;
else
boatModel
.devilNum
++;
return PositionModel
.boatRoles
[0];
}
if (boatModel
.roles
[1] == null)
{
boatModel
.roles
[1] = roleModel
;
roleModel
.isInBoat
= true;
roleModel
.role
.transform
.parent
= boatModel
.boat
.transform
;
if (roleModel
.isPriest
)
boatModel
.priestNum
++;
else
boatModel
.devilNum
++;
return PositionModel
.boatRoles
[1];
}
return roleModel
.role
.transform
.localPosition
;
}
public void RemoveRole(RoleModel roleModel
)
{
if (boatModel
.roles
[0] == roleModel
)
{
boatModel
.roles
[0] = null;
if (roleModel
.isPriest
)
boatModel
.priestNum
--;
else
boatModel
.devilNum
--;
}
if (boatModel
.roles
[1] == roleModel
)
{
boatModel
.roles
[1] = null;
if (roleModel
.isPriest
)
boatModel
.priestNum
--;
else
boatModel
.devilNum
--;
}
}
public void DealClick()
{
if (boatModel
.roles
[0] != null || boatModel
.roles
[1] != null)
userAction
.MoveBoat();
}
}
(3)LandModelController.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class LandModelController
{
private LandModel landModel
;
public void CreateLand(string name
, Vector3 position
)
{
if (landModel
==null)
landModel
= new LandModel(name
, position
);
landModel
.priestNum
= landModel
.devilNum
= 0;
}
public LandModel GetLandModel()
{
return landModel
;
}
public Vector3 AddRole(RoleModel roleModel
)
{
if (roleModel
.isPriest
)
landModel
.priestNum
++;
else
landModel
.devilNum
++;
roleModel
.role
.transform
.parent
= landModel
.land
.transform
;
roleModel
.isInBoat
= false;
return PositionModel
.roles
[roleModel
.tag
];
}
public void RemoveRole(RoleModel roleModel
)
{
if (roleModel
.isPriest
)
landModel
.priestNum
--;
else
landModel
.devilNum
--;
}
}
(4)Move.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class Move : MonoBehaviour
{
public bool isMoving
= false;
public float speed
= 5;
public Vector3 destination
;
public Vector3 mid_destination
;
void Update()
{
if (transform
.localPosition
== destination
)
{
isMoving
= false;
return;
}
isMoving
= true;
if (transform
.localPosition
.x
!= destination
.x
&& transform
.localPosition
.y
!= destination
.y
)
{
transform
.localPosition
= Vector3
.MoveTowards(transform
.localPosition
, mid_destination
, speed
* Time
.deltaTime
);
}
else
{
transform
.localPosition
= Vector3
.MoveTowards(transform
.localPosition
, destination
, speed
* Time
.deltaTime
);
}
}
}
(5)MoveController.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class MoveController
{
private GameObject moveObject
;
public bool GetIsMoving()
{
return (moveObject
!= null && moveObject
.GetComponent<Move>().isMoving
);
}
public void SetMove(Vector3 destination
, GameObject moveObject
)
{
Move test
;
this.moveObject
= moveObject
;
if (!moveObject
.TryGetComponent<Move>(out test
))
moveObject
.AddComponent<Move>();
this.moveObject
.GetComponent<Move>().destination
= destination
;
if (this.moveObject
.transform
.localPosition
.y
> destination
.y
)
this.moveObject
.GetComponent<Move>().mid_destination
= new Vector3(destination
.x
, this.moveObject
.transform
.localPosition
.y
, destination
.z
);
else
this.moveObject
.GetComponent<Move>().mid_destination
= new Vector3(this.moveObject
.transform
.localPosition
.x
, destination
.y
, this.moveObject
.transform
.localPosition
.z
);
}
}
(6)ISceneController.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public interface ISceneController
{
void LoadResources();
}
(7)SSDirector.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class SSDirector : System.Object
{
private static SSDirector _instance
;
public ISceneController CurrentSenceController
{ get; set; }
public static SSDirector GetInstance()
{
if (_instance
== null)
{
_instance
= new SSDirector();
}
return _instance
;
}
}
(8)IUserAction.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public interface IUserAction
{
void MoveBoat();
void MoveRole(RoleModel roleModel
);
void Restart();
void Check();
}
(9)moveCamera.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class MoveCamera : MonoBehaviour
{
void Start()
{
transform
.position
= new Vector3((float)1.11,1, (float)-18.32);
}
}
(10)FirstController.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class FirstController : MonoBehaviour, ISceneController
, IUserAction
{
private LandModelController rightLandRoleController
;
private LandModelController leftLandRoleController
;
private RoleModelController
[] roleModelControllers
;
private MoveController moveController
;
private bool isRuning
;
private float time
;
private RiverModel riverModel
;
private BoatController boatRoleController
;
public void LoadResources()
{
roleModelControllers
= new RoleModelController[6];
for (int i
= 0; i
< 6; i
++)
{
roleModelControllers
[i
] = new RoleModelController();
roleModelControllers
[i
].CreateRole(PositionModel
.roles
[i
], i
< 3 ? true : false, i
);
}
leftLandRoleController
= new LandModelController();
leftLandRoleController
.CreateLand("left_land", PositionModel
.left_land
);
rightLandRoleController
= new LandModelController();
rightLandRoleController
.CreateLand("right_land", PositionModel
.right_land
);
foreach (RoleModelController roleModelController
in roleModelControllers
)
{
roleModelController
.GetRoleModel().role
.transform
.localPosition
= leftLandRoleController
.AddRole(roleModelController
.GetRoleModel());
}
riverModel
= new RiverModel(PositionModel
.river
);
boatRoleController
= new BoatController();
boatRoleController
.CreateBoat(PositionModel
.left_boat
);
moveController
= new MoveController();
isRuning
= true;
time
= 60;
}
public void MoveBoat()
{
if ((!isRuning
) || moveController
.GetIsMoving())
return;
if (boatRoleController
.GetBoatModel().isRight
)
moveController
.SetMove(PositionModel
.left_boat
, boatRoleController
.GetBoatModel().boat
);
else
moveController
.SetMove(PositionModel
.right_boat
, boatRoleController
.GetBoatModel().boat
);
boatRoleController
.GetBoatModel().isRight
= !boatRoleController
.GetBoatModel().isRight
;
}
public void MoveRole(RoleModel roleModel
)
{
if ((!isRuning
) || moveController
.GetIsMoving())
return;
if (roleModel
.isInBoat
)
{
if (boatRoleController
.GetBoatModel().isRight
)
moveController
.SetMove(rightLandRoleController
.AddRole(roleModel
), roleModel
.role
);
else
moveController
.SetMove(leftLandRoleController
.AddRole(roleModel
), roleModel
.role
);
roleModel
.isRight
= boatRoleController
.GetBoatModel().isRight
;
boatRoleController
.RemoveRole(roleModel
);
}
else
{
if (boatRoleController
.GetBoatModel().isRight
== roleModel
.isRight
)
{
if (roleModel
.isRight
)
{
rightLandRoleController
.RemoveRole(roleModel
);
}
else
{
leftLandRoleController
.RemoveRole(roleModel
);
}
moveController
.SetMove(boatRoleController
.AddRole(roleModel
), roleModel
.role
);
}
}
}
public void Restart()
{
time
= 60;
leftLandRoleController
.CreateLand("left_land", PositionModel
.left_land
);
rightLandRoleController
.CreateLand("right_land", PositionModel
.right_land
);
for (int i
= 0; i
< 6; i
++)
{
roleModelControllers
[i
].CreateRole(PositionModel
.roles
[i
], i
< 3 ? true : false, i
);
roleModelControllers
[i
].GetRoleModel().role
.transform
.localPosition
= leftLandRoleController
.AddRole(roleModelControllers
[i
].GetRoleModel());
}
boatRoleController
.CreateBoat(PositionModel
.left_boat
);
isRuning
= true;
}
public void Check()
{
if (!isRuning
)
return;
this.gameObject
.GetComponent<UserGUI>().gameMessage
= "";
if (rightLandRoleController
.GetLandModel().priestNum
== 3)
{
this.gameObject
.GetComponent<UserGUI>().gameMessage
= "You Win!";
isRuning
= false;
}
else
{
int leftPriestNum
, leftDevilNum
, rightPriestNum
, rightDevilNum
;
leftPriestNum
= leftLandRoleController
.GetLandModel().priestNum
+ (boatRoleController
.GetBoatModel().isRight
? 0 : boatRoleController
.GetBoatModel().priestNum
);
leftDevilNum
= leftLandRoleController
.GetLandModel().devilNum
+ (boatRoleController
.GetBoatModel().isRight
? 0 : boatRoleController
.GetBoatModel().devilNum
);
if (leftPriestNum
!= 0 && leftPriestNum
< leftDevilNum
)
{
this.gameObject
.GetComponent<UserGUI>().gameMessage
= "Game Over!";
isRuning
= false;
}
rightPriestNum
= rightLandRoleController
.GetLandModel().priestNum
+ (boatRoleController
.GetBoatModel().isRight
? boatRoleController
.GetBoatModel().priestNum
: 0);
rightDevilNum
= rightLandRoleController
.GetLandModel().devilNum
+ (boatRoleController
.GetBoatModel().isRight
? boatRoleController
.GetBoatModel().devilNum
: 0);
if (rightPriestNum
!= 0 && rightPriestNum
< rightDevilNum
)
{
this.gameObject
.GetComponent<UserGUI>().gameMessage
= "Game Over!";
isRuning
= false;
}
}
}
void Awake()
{
SSDirector
.GetInstance().CurrentSenceController
= this;
LoadResources();
this.gameObject
.AddComponent<UserGUI>();
}
void Update()
{
if (isRuning
)
{
time
-= Time
.deltaTime
;
this.gameObject
.GetComponent<UserGUI>().time
= (int)time
;
if (time
<= 0)
{
this.gameObject
.GetComponent<UserGUI>().time
= 0;
this.gameObject
.GetComponent<UserGUI>().gameMessage
= "Game Over!";
isRuning
= false;
}
}
}
}
Views
UserGUI.cs
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public class UserGUI : MonoBehaviour
{
private IUserAction userAction
;
public string gameMessage
;
public int time
;
void Start()
{
time
= 60;
userAction
= SSDirector
.GetInstance().CurrentSenceController as IUserAction
;
}
void OnGUI()
{
userAction
.Check();
GUIStyle style
= new GUIStyle();
style
.normal
.textColor
= Color
.white
;
style
.fontSize
= 30;
GUIStyle bigStyle
= new GUIStyle();
bigStyle
.normal
.textColor
= Color
.white
;
bigStyle
.fontSize
= 50;
GUI
.Label(new Rect(200, 30, 50, 200), "Priests & Devils", bigStyle
);
GUI
.Label(new Rect(320, 100, 50, 200), gameMessage
, style
);
GUI
.Label(new Rect(0, 0, 100, 50), "Time: " + time
, style
);
if(GUI
.Button(new Rect(340, 160, 100, 50), "Restart"))
{
userAction
.Restart();
}
}
}