3D游戏编程与设计_HW3

2023-10-25 04:50
文章标签 设计 编程 3d 游戏 hw3

本文主要是介绍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 initializationvoid Start () {}// Update is called once per framevoid 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 initializationvoid Start () {}// Update is called once per framevoid 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 initializationvoid Start () {}// Update is called once per framevoid 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 initializationprivate 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 initializationvoid 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 frameint 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 instanceprivate static SSDirector _instance;public ISceneController currentSceneController { get; set;}// https://blog.csdn.net/qiaoquan3/article/details/51339242public 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 firsbool 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.5Priests3flag = 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.5Priests3flag = 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.5Priests3flag = 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.5Priests3flag = 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.5Devils1flag = 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.5Devils1flag = 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.5Devils1flag = 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.5Devils1flag = 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.5Devils2flag = 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.5Devils2flag = 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.5Devils2flag = 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.5Devils2flag = 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.5Devils3flag = 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.5Devils3flag = 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.5Devils3flag = 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.5Devils3flag = 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:succeedvoid 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;}}

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

 

这篇关于3D游戏编程与设计_HW3的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/280136

相关文章

Springboot3统一返回类设计全过程(从问题到实现)

《Springboot3统一返回类设计全过程(从问题到实现)》文章介绍了如何在SpringBoot3中设计一个统一返回类,以实现前后端接口返回格式的一致性,该类包含状态码、描述信息、业务数据和时间戳,... 目录Spring Boot 3 统一返回类设计:从问题到实现一、核心需求:统一返回类要解决什么问题?

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

AOP编程的基本概念与idea编辑器的配合体验过程

《AOP编程的基本概念与idea编辑器的配合体验过程》文章简要介绍了AOP基础概念,包括Before/Around通知、PointCut切入点、Advice通知体、JoinPoint连接点等,说明它们... 目录BeforeAroundAdvise — 通知PointCut — 切入点Acpect — 切面

C#异步编程ConfigureAwait的使用小结

《C#异步编程ConfigureAwait的使用小结》本文介绍了异步编程在GUI和服务器端应用的优势,详细的介绍了async和await的关键作用,通过实例解析了在UI线程正确使用await.Conf... 异步编程是并发的一种形式,它有两大好处:对于面向终端用户的GUI程序,提高了响应能力对于服务器端应

C# async await 异步编程实现机制详解

《C#asyncawait异步编程实现机制详解》async/await是C#5.0引入的语法糖,它基于**状态机(StateMachine)**模式实现,将异步方法转换为编译器生成的状态机类,本... 目录一、async/await 异步编程实现机制1.1 核心概念1.2 编译器转换过程1.3 关键组件解析

Python38个游戏开发库整理汇总

《Python38个游戏开发库整理汇总》文章介绍了多种Python游戏开发库,涵盖2D/3D游戏开发、多人游戏框架及视觉小说引擎,适合不同需求的开发者入门,强调跨平台支持与易用性,并鼓励读者交流反馈以... 目录PyGameCocos2dPySoyPyOgrepygletPanda3DBlenderFife