3D游戏编程与设计_HW3

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

  • 游戏对象运动的本质是什么?
    • 游戏对象运动的本质是对象随着每一帧的空间位置的变化,空间位置的变化有两个属性分别是 position 和 rotation,position 表示游戏对象的绝对坐标或者相对坐标,rotation 表示物体绕着某一旋转轴旋转的角度。这两个属性就能描述出游戏对象运动。

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

    • using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class Move1: MonoBehaviour {
      	public float speed = 1;
      	// Use this for initialization
      	void Start () {
      		
      	}
      	// Update is called once per frame
      	void Update () {
      		this.transform.position += Vector3.down*Time.deltaTime*(speed/10);
      		this.transform.position += Vector3.right*Time.deltaTime*5;
              speed++;
      
      	}
      	
      
      
      }
      

      方法2:

    • using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class Move2 : MonoBehaviour {
      	public float speed=1;
      	// Use this for initialization
      	void Start () {
      		
      	}
      	
      	// Update is called once per frame
      	void Update () {
      		Vector3 change= new Vector3(Time.deltaTime*5,-Time.deltaTime*(speed/10),0);
      		this.transform.position += change;
      
      	}
      }
      

      方法3:

    • using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class Move2 : MonoBehaviour {
      	public float speed=1;
      	// Use this for initialization
      	void Start () {
      		
      	}
      	
      	// Update is called once per frame
      	void Update () {
      		Vector3 change= new Vector3(Time.deltaTime*5,-Time.deltaTime*(speed/10),0);
      		 transform.Translate(change);
              speed++;
      
      	}
      }
      

       

  • 写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。
    • 先做好预设,添加好每个行星

    • 之后,创建一个空对象,然后编写启动脚本,并将脚本挂载在空对象上。启动脚本代码如下:

    • using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class Controller : MonoBehaviour {
          void LoadResources()
          {
              GameObject sunset = Instantiate<GameObject>(
                                      Resources.Load<GameObject>("prefabs/sun"),
                                      Vector3.zero, Quaternion.identity);
              sunset.name = "sunset";
              Debug.Log("load sunset ...\n");
          }
          // Use this for initialization
          private void Awake()
          {
              LoadResources(); 
          }
      }
      

      然后要做的就是编写每个行星的运动脚本,这里为了避免每次开始都要手动挂载对象对应到每个行星,所以采取,编写好脚本,挂载好每个对象之后再将其存为预设。运动脚本代码如下:

    • using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class RoundSun : MonoBehaviour {
      	public Transform sun;
      	public Transform moon;
          public Transform Mercury;
          public Transform Venus;
          public Transform earth;
          public Transform Mars;
          public Transform Jupite;
          public Transform Saturn;
          public Transform Neptune;
          public Transform Uranus;
          // Use this for initialization
         void LoadResources()
          {
              GameObject sunset = Instantiate<GameObject>(
                                      Resources.Load<GameObject>("prefabs/sun"),
                                      Vector3.zero, Quaternion.identity);
              sunset.name = "sunset";
              Debug.Log("load sunset ...\n");
          }
      
      
      
          void Start () {
      		sun.position = Vector3.zero;
      		earth.position = new Vector3 (-3, 0,-3 );
      		moon.position = new Vector3 (-3, 0, -2);
              Mercury.position = new Vector3(0,0 ,3 );
              Venus.position = new Vector3(0, 3, -3);
              Mars.position = new Vector3(6, 3, 6);
              Jupite.position = new Vector3(-6, 2, -4);
              Saturn.position = new Vector3(8, 6, 3);
              Neptune.position = new Vector3(-9, -3, 12);
              Uranus.position = new Vector3(-12 -3, 9);
              rand();
          }
          Vector3 p, p1, p2, p3, p4, p5, p6, p7, p8, p9;
      
          void rand() {
      
      
              int a1 = Random.Range(0, 5) % 5;
              int b1 = Random.Range(0, 5) % 5;
              int c1 = Random.Range(0, 5) % 5;
              p1 = new Vector3(a1, b1, c1);
              // Update is called once per frame
             
      
              int a3 = Random.Range(0, 5) % 5;
              int b3 = Random.Range(0, 5) % 5;
              int c3 = Random.Range(0, 5) % 5;
              p3 = new Vector3(a3, b3, c3);
      
              int a4 = Random.Range(0, 5) % 5;
              int b4 = Random.Range(0, 5) % 5;
              int c4 = Random.Range(0, 5) % 5;
              p4 = new Vector3(a4, b4, c4);
      
              int a5 = Random.Range(0, 5) % 5;
              int b5 = Random.Range(0, 5) % 5;
              int c5 = Random.Range(0, 5) % 5;
               p5 = new Vector3(a5, b5, c5);
      
              int a6 = Random.Range(0, 5) % 5;
              int b6 = Random.Range(0, 5) % 5;
              int c6 = Random.Range(0, 5) % 5;
              p6 = new Vector3(a6, b6, c6);
      
              int a7 = Random.Range(0, 5) % 5;int b7 = Random.Range(0, 5) % 5;
              int c7 = Random.Range(0, 5) % 5;
              p7 = new Vector3(a7, b7, c7);
      
               int a8 = Random.Range(0, 5) % 5;
               int b8 = Random.Range(0, 5) % 5;
               int c8 = Random.Range(0, 5) % 5;
               p8 = new Vector3(a8, b8, c8);
      
      
               int a9 = Random.Range(0, 5) % 5;
               int b9 = Random.Range(0, 5) % 5;
               int c9 = Random.Range(0, 5) % 5;
             p9 = new Vector3(a9, b9, c9); 
              }
          void Update () {
      
      		earth.RotateAround(sun.position, p1, 10 * Time.deltaTime);
      		earth.Rotate (Vector3.up * 30 * Time.deltaTime);
      		moon.transform.RotateAround(earth.position, Vector3.up, 359 * Time.deltaTime);
               
              Mercury.RotateAround(sun.position, p3, 20 * Time.deltaTime);
      
              Venus.RotateAround(sun.position, p4, 15 * Time.deltaTime);
      
              Mars.RotateAround(sun.position, p5, 25 * Time.deltaTime);
      
              Jupite.RotateAround(sun.position, p6, 18 * Time.deltaTime);
      
              Saturn.RotateAround(sun.position, p7, 12 * Time.deltaTime);
             
              Neptune.RotateAround(sun.position, p8, 40 * Time.deltaTime);
      
              Uranus.RotateAround(sun.position, p9, 30 * Time.deltaTime);
      
          }
      }
      

      在上述代码中,每个行星的旋转轴都是随机确定的。

  • 2、编程实践

  • 阅读以下游戏脚本
  • play the game ( http://www.flash-game.net/game/2535/priests-and-devils.html )
  • 列出游戏中提及的事物(Objects)
  • 用表格列出玩家动作表(规则表),注意,动作越少越好
  • 请将游戏中对象做成预制
  • 在 GenGameObjects 中创建 长方形、正方形、球 及其色彩代表游戏中的对象。
  • 使用 C# 集合类型 有效组织对象
  • 整个游戏仅 主摄像机 和 一个 Empty 对象, 其他对象必须代码动态生成!!! 。 整个游戏不许出现 Find 游戏对象, SendMessage 这类突破程序结构的 通讯耦合 语句。 违背本条准则,不给分
  • 请使用课件架构图编程,不接受非 MVC 结构程序
  • 注意细节,例如:船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件!
  • 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)
    • 两个岸边

    • 3个牧师

    • 3个魔鬼

  • 用表格列出玩家动作表(规则表),注意,动作越少越好
    • 动作规则
      船过河船上有人,从所在岸移动带另一岸
      开始岸牧师上船船上有空位,船在开始岸
      开始岸魔鬼上船船上有空位,船在开始岸
      结束岸牧师上船船上有空位,船在结束岸
      结束岸魔鬼上船船上有空位,船在结束岸
      船空位1下船船空位1有人
      船空位2下船船空位2有人

       

  • 请将游戏中对象做成预制
  • 在 GenGameObjects 中创建 长方形、正方形、球 及其色彩代表游戏中的对象。
  • 使用 C# 集合类型 有效组织对象
  • 整个游戏仅 主摄像机 和 一个 Empty 对象, 其他对象必须代码动态生成!!! 。 整个游戏不许出现 Find 游戏对象, SendMessage 这类突破程序结构的 通讯耦合 语句。 违背本条准则,不给分
  • 请使用课件架构图编程,不接受非 MVC 结构程序
  • 注意细节,例如:船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件!
    • 先编写导演类,负责联系场景的切换

      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;}
      
      	// https://blog.csdn.net/qiaoquan3/article/details/51339242
      	public bool Paused { get { return Time.timeScale == 0; } set {Time.timeScale = value?0:1;} } 
      
      	// get instance anytime anywhare!
      	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;
      	}
      
      	public void NextScene(){
      		Debug.Log ("Waiting next Scene now...");
      		#if UNITY_EDITOR  
      		UnityEditor.EditorApplication.isPlaying = false;
      		//UnityEditor.EditorApplication.Exit(0);
      		#else  
      		Application.Quit();  
      		#endif  
      	}
      }

      之后写出场景控制的接口,和用户行为的接口方便调用

    • using System;
      
      
      /// <summary>
      /// Abstact Scene Controller. Interface between scene controllers and the director
      /// </summary>
      public interface ISceneController
      {
      	void LoadResources();
      }
      
      using System;
      
      
      public interface IUserAction
      {
      	void GameOver();
      }
      

      之后开始编写第一个场景控制FirstScenceController

    • 首先要让导演类指示该场景应该实现了,所以代码如下:

    •   void Awake()
          {
              SSDirector director = SSDirector.getInstance();
              director.setFPS(60);
              director.currentSceneController = this;
              director.currentSceneController.LoadResources();
          }

      之后场景类负责该场景内所有对象的加载和交互,首先先实现资源的加载:

    • public void LoadResources()
          {
              Boat = Instantiate<GameObject>(
                                      Resources.Load<GameObject>("prefabs/Boat"),
                  new Vector3((float)0, (float)-1, (float)0), Quaternion.identity);
              Boat.name = "Boat";
              Devils1 = Instantiate<GameObject>(
                  Resources.Load<GameObject>("prefabs/Devils1"),
                  new Vector3((float)-0.3, (float)-0.5, (float)3.5), Quaternion.identity);
              Devils1.name = "Devils1";
              Devils2 = Instantiate<GameObject>(
                  Resources.Load<GameObject>("prefabs/Devils2"),
                  new Vector3((float)-0.3, (float)-0.5, (float)4), Quaternion.identity);
              Devils2.name = "Devils2";
              Devils3 = Instantiate<GameObject>(
                  Resources.Load<GameObject>("prefabs/Devils3"),
                  new Vector3((float)-0.3, (float)-0.5, (float)4.5), Quaternion.identity);
              Devils3.name = "Devils3";
              Priests1 = Instantiate<GameObject>(
                  Resources.Load<GameObject>("prefabs/Priests1"),
                  new Vector3((float)-0.3, (float)-0.5, (float)2), Quaternion.identity);
              Priests1.name = "Priests1";
              Priests2 = Instantiate<GameObject>(
                  Resources.Load<GameObject>("prefabs/Priests2"),
                  new Vector3((float)-0.3, (float)-0.5, (float)2.5), Quaternion.identity);
              Priests2.name = "Priests2";
              Priests3 = Instantiate<GameObject>(
                  Resources.Load<GameObject>("prefabs/Priests3"),
                  new Vector3((float)-0.3, (float)-0.5, (float)3), Quaternion.identity);
              Priests3.name = "Priests3";
              Left = Instantiate<GameObject>(
                  Resources.Load<GameObject>("prefabs/Left"),
                  new Vector3((float)-0.3, (float)-0.7, (float)3.5), Quaternion.identity);
              Left.name = "Left";
              Right = Instantiate<GameObject>(
                                      Resources.Load<GameObject>("prefabs/Right"),
                  new Vector3((float)-0.3, (float)-0.7, (float)-6), Quaternion.identity);
              Right.name = "Right";
          }

      加载好资源后,就要实现资源的行为,这里定义为,每点击一下按钮就从一个位置移动到另一个位置,所以定义好按钮和位置变化。

    • int Boatchair = 0;
          //0代表船上没人,1代表船上右边有人 ,2代表船上左边有人,3代表船上满人
      
          int BoatFlag = 1;
      
          //上述变量: 
          // 1代表船上没人,且在左岸
          // 2代表船上没人,且在右岸
          // 3代表船上有人,且在左岸,左
          // 4代表船上有人,且在右岸
          double Priests1flag = 1;
      
          double Priests2flag = 1;
      
          double Priests3flag = 1;
      
          double Devils1flag = 1;
          double Devils2flag = 1;
          double Devils3flag = 1;
          void OnGUI()
          {
              if (state == 0)
              {
                  //give advice firs
                  bool DriveFlag = GUI.Button(new Rect(350, 350, 100, 50), "Drive");
      
                  if (DriveFlag == true && BoatFlag == 1 && Boatchair != 0)
                  {
      
                      Boat.transform.position = new Vector3(0, -1, -3);
                      BoatFlag = 2;
                      if (Math.Abs(Priests1flag - 2.1) < 1E-6 || Math.Abs(Priests1flag - 2.2) < 1E-6)
                      {
                          Priests1.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Priests1flag = 3.1;
                      }
                      if (Math.Abs(Priests1flag - 2.3) < 1E-6 || Math.Abs(Priests1flag - 2.4) < 1E-6)
                      {
                          Priests1.transform.position = new Vector3((float)0, (float)-0.8, (float)-2);
                          Priests1flag = 3.3;
                      }
      
                      if (Math.Abs(Priests2flag - 2.1) < 1E-6 || Math.Abs(Priests2flag - 2.2) < 1E-6)
                      {
                          Priests2.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Priests2flag = 3.1;
                      }
                      if (Math.Abs(Priests2flag - 2.3) < 1E-6 || Math.Abs(Priests2flag - 2.4) < 1E-6)
                      {
                          Priests2.transform.position = new Vector3((float)0, (float)-0.8, (float)-2);
                          Priests2flag = 3.3;
                      }
      
                      if (Math.Abs(Priests3flag - 2.1) < 1E-6 || Math.Abs(Priests3flag - 2.2) < 1E-6)
                      {
                          Priests3.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Priests3flag = 3.1;
                      }
                      if (Math.Abs(Priests3flag - 2.3) < 1E-6 || Math.Abs(Priests3flag - 2.4) < 1E-6)
                      {
                          Priests3.transform.position = new Vector3((float)0, (float)-0.8, (float)-2);
                          Priests3flag = 3.3;
                      }
      
                      if (Math.Abs(Devils1flag - 2.1) < 1E-6 || Math.Abs(Devils1flag - 2.2) < 1E-6)
                      {
                          Devils1.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Devils1flag = 3.1;
                      }
                      if (Math.Abs(Devils1flag - 2.3) < 1E-6 || Math.Abs(Devils1flag - 2.4) < 1E-6)
                      {
                          Devils1.transform.position = new Vector3((float)0, (float)-0.8, (float)-2);
                          Devils1flag = 3.3;
                      }
      
                      if (Math.Abs(Devils2flag - 2.1) < 1E-6 || Math.Abs(Devils2flag - 2.2) < 1E-6)
                      {
                          Devils2.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Devils2flag = 3.1;
                      }
                      if (Math.Abs(Devils2flag - 2.3) < 1E-6 || Math.Abs(Devils2flag - 2.4) < 1E-6)
                      {
                          Devils2.transform.position = new Vector3((float)0, (float)-0.8, (float)-2);
                          Devils2flag = 3.3;
                      }
      
                      if (Math.Abs(Devils3flag - 2.1) < 1E-6 || Math.Abs(Devils3flag - 2.2) < 1E-6)
                      {
                          Devils3.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Devils3flag = 3.1;
                      }
                      if (Math.Abs(Devils3flag - 2.3) < 1E-6 || Math.Abs(Devils3flag - 2.4) < 1E-6)
                      {
                          Devils3.transform.position = new Vector3((float)0, (float)-0.8, (float)-2);
                          Devils3flag = 3.3;
                      }
                      checkGame();
                  }
                  else if (DriveFlag == true && BoatFlag == 2)
                  {
                      Boat.transform.position = new Vector3(0, -1, 0);
                      BoatFlag = 1;
                      if (Math.Abs(Priests1flag - 3.2) < 1E-6 || Math.Abs(Priests1flag - 3.1) < 1E-6)
                      {
                          Priests1.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Priests1flag = 2.2;
                      }
                      if (Math.Abs(Priests1flag - 3.3) < 1E-6 || Math.Abs(Priests1flag - 3.4) < 1E-6)
                      {
                          Priests1.transform.position = new Vector3((float)0, (float)-0.8, (float)1);
                          Priests1flag = 2.4;
                      }
      
                      if (Math.Abs(Priests2flag - 3.2) < 1E-6 || Math.Abs(Priests2flag - 3.1) < 1E-6)
                      {
                          Priests2.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Priests2flag = 2.2;
                      }
                      if (Math.Abs(Priests2flag - 3.3) < 1E-6 || Math.Abs(Priests2flag - 3.4) < 1E-6)
                      {
                          Priests2.transform.position = new Vector3((float)0, (float)-0.8, (float)1);
                          Priests2flag = 2.4;
                      }
      
                      if (Math.Abs(Priests3flag - 3.2) < 1E-6 || Math.Abs(Priests3flag - 3.1) < 1E-6)
                      {
                          Priests2.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Priests2flag = 2.2;
                      }
                      if (Math.Abs(Priests3flag - 3.3) < 1E-6 || Math.Abs(Priests3flag - 3.4) < 1E-6)
                      {
                          Priests3.transform.position = new Vector3((float)0, (float)-0.8, (float)1);
                          Priests3flag = 2.4;
                      }
      
      
                      if (Math.Abs(Devils1flag - 3.2) < 1E-6 || Math.Abs(Devils1flag - 3.1) < 1E-6)
                      {
                          Devils1.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Devils1flag = 2.2;
                      }
                      if (Math.Abs(Devils1flag - 3.3) < 1E-6 || Math.Abs(Devils1flag - 3.4) < 1E-6)
                      {
                          Devils1.transform.position = new Vector3((float)0, (float)-0.8, (float)1);
                          Devils1flag = 2.4;
                      }
      
                      if (Math.Abs(Devils2flag - 3.2) < 1E-6 || Math.Abs(Devils2flag - 3.1) < 1E-6)
                      {
                          Devils2.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Devils2flag = 2.2;
                      }
                      if (Math.Abs(Devils2flag - 3.3) < 1E-6 || Math.Abs(Devils2flag - 3.4) < 1E-6)
                      {
                          Devils2.transform.position = new Vector3((float)0, (float)-0.8, (float)1);
                          Devils2flag = 2.4;
                      }
      
                      if (Math.Abs(Devils3flag - 3.2) < 1E-6 || Math.Abs(Devils3flag - 3.1) < 1E-6)
                      {
                          Devils3.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Devils3flag = 2.2;
                      }
                      if (Math.Abs(Devils3flag - 3.3) < 1E-6 || Math.Abs(Devils3flag - 3.4) < 1E-6)
                      {
                          Devils3.transform.position = new Vector3((float)0, (float)-0.8, (float)1);
                          Devils3flag = 2.4;
                      }
                      checkGame();
                  }
      
      
      
      
      
                  bool P1flag = GUI.Button(new Rect(260, 250, 30, 30), "P1");
                  bool P2flag = GUI.Button(new Rect(225, 250, 30, 30), "P2");
                  bool P3flag = GUI.Button(new Rect(190, 250, 30, 30), "P3");
                  bool D1flag = GUI.Button(new Rect(155, 250, 30, 30), "D1");
                  bool D2flag = GUI.Button(new Rect(120, 250, 30, 30), "D2");
                  bool D3flag = GUI.Button(new Rect(85, 250, 30, 30), "D3");
                  if (P1flag == true)
                  {
                      if (Math.Abs(Priests1flag - 1) < 1E-6 && BoatFlag == 1 && Boatchair == 0)
                      {
                          Debug.Log(Boatchair);
                          Priests1.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Priests1flag = 2.1;
                          Boatchair = 1;
                          checkleft[1] = 0;
                      }
                      if (Math.Abs(Priests1flag - 1) < 1E-6 && BoatFlag == 1 && Boatchair == 2)
                      {
                          Debug.Log(Boatchair);
                          Priests1.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Priests1flag = 2.1;
                          Boatchair = 3;
                          checkleft[1] = 0;
                      }
                      if (Math.Abs(Priests1flag - 1) < 1E-6 && BoatFlag == 1 && Boatchair == 1)
                      {
                          Debug.Log(Boatchair);
                          Priests1.transform.position = new Vector3((float)0, (float)-0.8, (float)1);
                          Priests1flag = 2.3;
                          Boatchair = 3;
                          checkleft[1] = 0;
                      }
      
      
                      if (Math.Abs(Priests1flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 0)
                      {
                          Priests1.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Priests1flag = 3.2;
                          Boatchair = 1;
                          checkright[1] = 0;
                      }
                      if (Math.Abs(Priests1flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 1)
                      {
                          Priests1.transform.position = new Vector3((float)0, (float)-0.8, (float)-2);
                          Priests1flag = 3.4;
                          Boatchair = 3;
                          checkright[1] = 0;
                      }
                      if (Math.Abs(Priests1flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 2)
                      {
                          Priests1.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Priests1flag = 3.2;
                          Boatchair = 3;
                          checkright[1] = 0;
                      }
      
                      if (Math.Abs(Priests1flag - 2.2) < 1E-6)
                      {
                          Priests1.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)2);
                          Priests1flag = 1;
                          if (Boatchair == 3)
                              Boatchair = 2;
                          if (Boatchair == 1)
                              Boatchair = 0;
                          checkleft[1] = 1;
                      }
                      if (Math.Abs(Priests1flag - 2.4) < 1E-6)
                      {
                          Priests1.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)2);
                          Priests1flag = 1;
                          if (Boatchair == 3)
                              Boatchair = 1;
                          if (Boatchair == 2)
                              Boatchair = 0;
                          checkleft[1] = 1;
                      }
                      if (Math.Abs(Priests1flag - 3.1) < 1E-6)
                      {
                          Priests1.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)-4.5);
                          Priests1flag = 4;
                          if (Boatchair == 3)
                              Boatchair = 2;
                          if (Boatchair == 1)
                              Boatchair = 0;
                          checkright[1] = 1;
                      }
                      if (Math.Abs(Priests1flag - 3.3) < 1E-6)
                      {
                          Priests1.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)-4.5);
                          Priests1flag = 4;
                          if (Boatchair == 3)
                              Boatchair = 1;
                          if (Boatchair == 2)
                              Boatchair = 0;
                          checkright[1] = 1;
                      }
                  }
                  if (P2flag == true)
                  {
                      if (Math.Abs(Priests2flag - 1) < 1E-6 && BoatFlag == 1 && (Boatchair == 0))
                      {
                          Priests2.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Priests2flag = 2.1;
                          if (Boatchair == 0)
                              Boatchair = 1;
                          checkleft[2] = 0;
                      }
                      if (Math.Abs(Priests2flag - 1) < 1E-6 && BoatFlag == 1 && (Boatchair == 2))
                      {
                          Priests2.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Priests2flag = 2.1;
                          if (Boatchair == 2)
                              Boatchair = 3;
                          checkleft[2] = 0;
                      }
                      if (Math.Abs(Priests2flag - 1) < 1E-6 && BoatFlag == 1 && Boatchair == 1)
                      {
                          Priests2.transform.position = new Vector3((float)0, (float)-0.8, (float)1);
                          Priests2flag = 2.3;
                          Boatchair = 3;
                          checkleft[2] = 0;
                      }
      
      
                      if (Math.Abs(Priests2flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 0)
                      {
                          Priests2.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Priests2flag = 3.2;
                          Boatchair = 1;
                          checkright[2] = 0;
                      }
                      if (Math.Abs(Priests2flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 1)
                      {
                          Priests2.transform.position = new Vector3((float)0, (float)-0.8, (float)-2);
                          Priests2flag = 3.4;
                          Boatchair = 3;
                          checkright[2] = 0;
                      }
                      if (Math.Abs(Priests2flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 2)
                      {
                          Priests2.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Priests2flag = 3.2;
                          Boatchair = 3;
                          checkright[2] = 0;
                      }
                      if (Math.Abs(Priests2flag - 2.2) < 1E-6)
                      {
                          Priests2.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)2.5);//
                          Priests2flag = 1;
                          if (Boatchair == 3)
                              Boatchair = 2;
                          if (Boatchair == 1)
                              Boatchair = 0;
                          checkleft[2] = 1;
                      }
                      if (Math.Abs(Priests2flag - 2.4) < 1E-6)
                      {
                          Priests2.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)2.5);//
                          Priests2flag = 1;
                          if (Boatchair == 3)
                              Boatchair = 1;
                          if (Boatchair == 2)
                              Boatchair = 0;
                          checkleft[2] = 1;
                      }
                      if (Math.Abs(Priests2flag - 3.1) < 1E-6)
                      {
                          Priests2.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)-5.0);//
                          Priests2flag = 4;
                          if (Boatchair == 3)
                              Boatchair = 2;
                          if (Boatchair == 1)
                              Boatchair = 0;
                          checkright[2] = 1;
                      }
                      if (Math.Abs(Priests2flag - 3.3) < 1E-6)
                      {
                          Priests2.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)-5.0);//
                          Priests2flag = 4;
                          if (Boatchair == 3)
                              Boatchair = 1;
                          if (Boatchair == 2)
                              Boatchair = 0;
                          checkright[2] = 1;
                      }
                  }
                  if (P3flag == true)
                  {
                      if (Math.Abs(Priests3flag - 1) < 1E-6 && BoatFlag == 1 && (Boatchair == 0))
                      {
                          Priests3.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Priests3flag = 2.1;
                          if (Boatchair == 0)
                              Boatchair = 1;
                          checkleft[3] = 0;
                      }
                      if (Math.Abs(Priests3flag - 1) < 1E-6 && BoatFlag == 1 && (Boatchair == 2))
                      {
                          Priests3.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Priests3flag = 2.1;
                          if (Boatchair == 2)
                              Boatchair = 3;
                          checkleft[3] = 0;
                      }
                      if (Math.Abs(Priests3flag - 1) < 1E-6 && BoatFlag == 1 && Boatchair == 1)
                      {
                          Priests3.transform.position = new Vector3((float)0, (float)-0.8, (float)1);
                          Priests3flag = 2.3;
                          Boatchair = 3;
                          checkleft[3] = 0;
                      }
      
      
                      if (Math.Abs(Priests3flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 0)
                      {
                          Priests3.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Priests3flag = 3.2;
                          Boatchair = 1;
                          checkright[3] = 0;
                      }
                      if (Math.Abs(Priests3flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 1)
                      {
                          Priests3.transform.position = new Vector3((float)0, (float)-0.8, (float)-2);
                          Priests3flag = 3.4;
                          Boatchair = 3;
                          checkright[3] = 0;
                      }
                      if (Math.Abs(Priests3flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 2)
                      {
                          Priests3.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Priests3flag = 3.2;
                          Boatchair = 3;
                          checkright[3] = 0;
                      }
                      if (Math.Abs(Priests3flag - 2.2) < 1E-6)
                      {
                          Priests3.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)3);//+0.5
                          Priests3flag = 1;
                          if (Boatchair == 3)
                              Boatchair = 2;
                          if (Boatchair == 1)
                              Boatchair = 0;
                          checkleft[3] = 1;
                      }
                      if (Math.Abs(Priests3flag - 2.4) < 1E-6)
                      {
                          Priests3.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)3);//+0.5
                          Priests3flag = 1;
                          if (Boatchair == 3)
                              Boatchair = 1;
                          if (Boatchair == 2)
                              Boatchair = 0;
                          checkleft[3] = 1;
                      }
                      if (Math.Abs(Priests3flag - 3.1) < 1E-6)
                      {
                          Priests3.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)-5.5);//-0.5
                          Priests3flag = 4;
                          if (Boatchair == 3)
                              Boatchair = 2;
                          if (Boatchair == 1)
                              Boatchair = 0;
                          checkright[3] = 1;
                      }
                      if (Math.Abs(Priests3flag - 3.3) < 1E-6)
                      {
                          Priests3.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)-5.5);//-0.5
                          Priests3flag = 4;
                          if (Boatchair == 3)
                              Boatchair = 1;
                          if (Boatchair == 2)
                              Boatchair = 0;
                          checkright[3] = 1;
                      }
                  }
                  if (D1flag == true)
                  {
                      if (Math.Abs(Devils1flag - 1) < 1E-6 && BoatFlag == 1 && (Boatchair == 0))
                      {
                          Devils1.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Devils1flag = 2.1;
                          if (Boatchair == 0)
                              Boatchair = 1;
                          checkleft[4] = 0;
                      }
                      if (Math.Abs(Devils1flag - 1) < 1E-6 && BoatFlag == 1 && (Boatchair == 2))
                      {
                          Devils1.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Devils1flag = 2.1;
                          if (Boatchair == 2)
                              Boatchair = 3;
                          checkleft[4] = 0;
                      }
                      if (Math.Abs(Devils1flag - 1) < 1E-6 && BoatFlag == 1 && Boatchair == 1)
                      {
                          Devils1.transform.position = new Vector3((float)0, (float)-0.8, (float)1);
                          Devils1flag = 2.3;
                          Boatchair = 3;
                          checkleft[4] = 0;
                      }
      
      
                      if (Math.Abs(Devils1flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 0)
                      {
                          Devils1.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Devils1flag = 3.2;
                          Boatchair = 1;
                          checkright[4] = 0;
                      }
                      if (Math.Abs(Devils1flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 1)
                      {
                          Devils1.transform.position = new Vector3((float)0, (float)-0.8, (float)-2);
                          Devils1flag = 3.4;
                          Boatchair = 3;
                          checkright[4] = 0;
                      }
                      if (Math.Abs(Devils1flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 2)
                      {
                          Devils1.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Devils1flag = 3.2;
                          Boatchair = 3;
                          checkright[4] = 0;
                      }
                      if (Math.Abs(Devils1flag - 2.2) < 1E-6)
                      {
                          Devils1.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)3.5);//+0.5
                          Devils1flag = 1;
                          if (Boatchair == 3)
                              Boatchair = 2;
                          if (Boatchair == 1)
                              Boatchair = 0;
                          checkleft[4] = 2;
                      }
                      if (Math.Abs(Devils1flag - 2.4) < 1E-6)
                      {
                          Devils1.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)3.5);//+0.5
                          Devils1flag = 1;
                          if (Boatchair == 3)
                              Boatchair = 1;
                          if (Boatchair == 2)
                              Boatchair = 0;
                          checkleft[4] = 2;
                      }
                      if (Math.Abs(Devils1flag - 3.1) < 1E-6)
                      {
                          Devils1.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)-6.0);//-0.5
                          Devils1flag = 4;
                          if (Boatchair == 3)
                              Boatchair = 2;
                          if (Boatchair == 1)
                              Boatchair = 0;
                          checkright[4] = 2;
                      }
                      if (Math.Abs(Devils1flag - 3.3) < 1E-6)
                      {
                          Devils1.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)-6.0);//-0.5
                          Devils1flag = 4;
                          if (Boatchair == 3)
                              Boatchair = 1;
                          if (Boatchair == 2)
                              Boatchair = 0;
                          checkright[4] = 2;
                      }
                  }
      
                  if (D2flag == true)
                  {
                      if (Math.Abs(Devils2flag - 1) < 1E-6 && BoatFlag == 1 && (Boatchair == 0))
                      {
                          Devils2.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Devils2flag = 2.1;
                          if (Boatchair == 0)
                              Boatchair = 1;
                          checkleft[5] = 0;
                      }
                      if (Math.Abs(Devils2flag - 1) < 1E-6 && BoatFlag == 1 && (Boatchair == 2))
                      {
                          Devils2.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Devils2flag = 2.1;
                          if (Boatchair == 2)
                              Boatchair = 3;
                          checkleft[5] = 0;
                      }
                      if (Math.Abs(Devils2flag - 1) < 1E-6 && BoatFlag == 1 && Boatchair == 1)
                      {
                          Devils2.transform.position = new Vector3((float)0, (float)-0.8, (float)1);
                          Devils2flag = 2.3;
                          Boatchair = 3;
                          checkleft[5] = 0;
                      }
      
      
                      if (Math.Abs(Devils2flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 0)
                      {
                          Devils2.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Devils2flag = 3.2;
                          Boatchair = 1;
                          checkright[5] = 0;
                      }
                      if (Math.Abs(Devils2flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 1)
                      {
                          Devils2.transform.position = new Vector3((float)0, (float)-0.8, (float)-2);
                          Devils2flag = 3.4;
                          Boatchair = 3;
                          checkright[5] = 0;
                      }
                      if (Math.Abs(Devils2flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 2)
                      {
                          Devils2.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Devils2flag = 3.2;
                          Boatchair = 3;
                          checkright[5] = 0;
                      }
                      if (Math.Abs(Devils2flag - 2.2) < 1E-6)
                      {
                          Devils2.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)4);//+0.5
                          Devils2flag = 1;
                          if (Boatchair == 3)
                              Boatchair = 2;
                          if (Boatchair == 1)
                              Boatchair = 0;
                          checkleft[5] = 2;
                      }
                      if (Math.Abs(Devils2flag - 2.4) < 1E-6)
                      {
                          Devils2.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)4);//+0.5
                          Devils2flag = 1;
                          if (Boatchair == 3)
                              Boatchair = 1;
                          if (Boatchair == 2)
                              Boatchair = 0;
                          checkleft[5] = 2;
                      }
                      if (Math.Abs(Devils2flag - 3.1) < 1E-6)
                      {
                          Devils2.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)-6.5);//-0.5
                          Devils2flag = 4;
                          if (Boatchair == 3)
                              Boatchair = 2;
                          if (Boatchair == 1)
                              Boatchair = 0;
                          checkright[5] = 2;
                      }
                      if (Math.Abs(Devils2flag - 3.3) < 1E-6)
                      {
                          Devils2.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)-6.5);//-0.5
                          Devils2flag = 4;
                          if (Boatchair == 3)
                              Boatchair = 1;
                          if (Boatchair == 2)
                              Boatchair = 0;
                          checkleft[5] = 2;
                      }
                  }
                  if (D3flag == true)
                  {
                      if (Math.Abs(Devils3flag - 1) < 1E-6 && BoatFlag == 1 && (Boatchair == 0))
                      {
                          Devils3.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Devils3flag = 2.1;
                          if (Boatchair == 0)
                              Boatchair = 1;
                          checkleft[6] = 0;
                      }
                      if (Math.Abs(Devils3flag - 1) < 1E-6 && BoatFlag == 1 && (Boatchair == 2))
                      {
                          Devils3.transform.position = new Vector3((float)0, (float)-0.8, (float)0);
                          Devils3flag = 2.1;
                          if (Boatchair == 2)
                              Boatchair = 3;
                          checkleft[6] = 0;
                      }
                      if (Math.Abs(Devils3flag - 1) < 1E-6 && BoatFlag == 1 && Boatchair == 1)
                      {
                          Devils3.transform.position = new Vector3((float)0, (float)-0.8, (float)1);
                          Devils3flag = 2.3;
                          Boatchair = 3;
                          checkleft[6] = 0;
                      }
      
      
                      if (Math.Abs(Devils3flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 0)
                      {
                          Devils3.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Devils3flag = 3.2;
                          Boatchair = 1;
                          checkright[6] = 0;
                      }
                      if (Math.Abs(Devils3flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 1)
                      {
                          Devils3.transform.position = new Vector3((float)0, (float)-0.8, (float)-2);
                          Devils3flag = 3.4;
                          Boatchair = 3;
                          checkright[6] = 0;
                      }
                      if (Math.Abs(Devils3flag - 4) < 1E-6 && BoatFlag == 2 && Boatchair == 2)
                      {
                          Devils3.transform.position = new Vector3((float)0, (float)-0.8, (float)-3);
                          Devils3flag = 3.2;
                          Boatchair = 3;
                          checkright[6] = 0;
                      }
                      if (Math.Abs(Devils3flag - 2.2) < 1E-6)
                      {
                          Devils3.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)4.5);//+0.5
                          Devils3flag = 1;
                          if (Boatchair == 3)
                              Boatchair = 2;
                          if (Boatchair == 1)
                              Boatchair = 0;
                          checkleft[6] = 2;
                      }
                      if (Math.Abs(Devils3flag - 2.4) < 1E-6)
                      {
                          Devils3.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)4.5);//+0.5
                          Devils3flag = 1;
                          if (Boatchair == 3)
                              Boatchair = 1;
                          if (Boatchair == 2)
                              Boatchair = 0;
                          checkright[6] = 0;
                      }
                      if (Math.Abs(Devils3flag - 3.1) < 1E-6)
                      {
                          Devils3.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)-7.0);//-0.5
                          Devils3flag = 4;
                          if (Boatchair == 3)
                              Boatchair = 2;
                          if (Boatchair == 1)
                              Boatchair = 0;
                          checkright[6] = 2;
                      }
                      if (Math.Abs(Devils3flag - 3.3) < 1E-6)
                      {
                          Devils3.transform.position = new Vector3((float)-0.3, (float)-0.5, (float)-7.0);//-0.5
                          Devils3flag = 4;
                          if (Boatchair == 3)
                              Boatchair = 1;
                          if (Boatchair == 2)
                              Boatchair = 0;
                          checkright[6] = 2;
                      }
                  }
              }
              else{
                  if(state==1){
                      GUI.Label(new Rect(350, 50, 100, 50), "YOU FAILED!");
                  }
                  else if(state==2){
                      GUI.Label(new Rect(350, 50, 100, 50), "YOU SUCCEED!");
                  }
                  if(GUI.Button(new Rect(350,150,100,50),"ReStart")){
                      Restart();
                      state = 0;
                  }
      
      
              }
      }

      之后判断游戏的状态,在每点击一次过河按钮时,检查两岸的牧师和魔鬼的人数检验游戏的状态

    • int state = 0;//0:continue 1:fail 2:succeed
          void checkGame()
          {
              int count1 = 0;
              int count2 = 0;
              for (int i = 1; i <= 6; i++)
              {
                  if (checkleft[i] == 1)
                      count1++;
                  if (checkleft[i] == 2)
                      count2++;
              }
              if (count2 > count1&&count1!=0)
                  state = 1;
              count1 = 0;
              count2 = 0;
              for (int i = 1; i <= 6; i++)
              {
                  if (checkright[i] == 1)
                      count1++;
                  if (checkright[i] == 2)
                      count2++;
              }
              if (count2 > count1&&count1!=0)
                  state = 1;
      
              if (checkright[1] == 1 && checkright[2] == 1 && checkright[3] == 1)
                  state = 2;
          }

      然后编写重新开始函数,代码如下:

void Restart()
    {
        Priests1.transform.position = new Vector3(-0.3F, -0.5F, 2F);
        Priests2.transform.position = new Vector3(-0.3F, -0.5F, 2.5F);
        Priests3.transform.position = new Vector3(-0.3F, -0.5F, 3F);
        Devils1.transform.position = new Vector3(-0.3F, -0.5F, 3.5F);
        Devils2.transform.position = new Vector3(-0.3F, -0.5F, 4F);
        Devils3.transform.position = new Vector3(-0.3F, -0.5F, 4.5F);
        Boat.transform.position = new Vector3(0F, -1F, 0F);
        Priests1flag = 1;
        Priests2flag = 1;
        Priests3flag = 1;
        Devils1flag = 1;
        Devils2flag = 1;
        Devils3flag = 1;
        Boatchair = 0;
        BoatFlag = 1;
        checkleft[1] = checkleft[2] = checkleft[3] = 1;
        checkleft[4] = checkleft[5] = checkleft[6] = 2;
        for (int i = 1; i <= 6;i++){
            checkright[i] = 0;
        }

    }

以上便配置好了基本上所有的行为。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值