3D-太阳系、牧师与恶魔小游戏

1、简单并用程序验证

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

    本质上是游戏对象坐标的变换

  • 使用三种方法实现物体的抛物线运动

    1. Vector3.MoveTowards

      private float step = Time.deltaTime;
      
      // Update is called once per frame
      void Update () {
          float step = Time.deltaTime;
          if (flag) {
              if (origin - 100 * Time.deltaTime < 0.00001) {
                  Vector3 target = transform.position + Vector3.right * Time.deltaTime + Vector3.down * Time.deltaTime;
                  transform.position = Vector3.MoveTowards(transform.position, target, step);
              } else if (origin - 200 * Time.deltaTime < 0.00001) {
                  Vector3 target = transform.position + Vector3.right * Time.deltaTime + Vector3.up * Time.deltaTime;
                  transform.position = Vector3.MoveTowards(transform.position, target, step);
              } else {
                  flag = false;
              }
              origin += Time.deltaTime;
          } else { /*backward*/ }
      }
      
    2. Vector3.Lerp

      void Update {
          if (flag) {
            if (origin - 100 * Time.deltaTime < 0.00001) {
                Vector3 target = transform.position + Vector3.right * Time.deltaTime + Vector3.down * Time.deltaTime;
                transform.position = Vector3.Lerp(transform.position, target, 1);
            } else if (origin - 200 * Time.deltaTime < 0.00001) {
                Vector3 target = transform.position + Vector3.right * Time.deltaTime + Vector3.up * Time.deltaTime;
                transform.position = Vector3.Lerp(transform.position, target, 1);
            } else {
                flag = false;
            }
            origin += Time.deltaTime;
        } else { /*backward*/ }
      }
      
    3. 利用transform属性

      private float origin = Time.deltaTime;
      private bool flag = true;
      
      // Update is called once per frame
      void Update () {
          if (flag) {
              if (origin - 100 * Time.deltaTime < 0.00001) {
                  transform.position += Vector3.down * Time.deltaTime;
                  transform.position += Vector3.right * Time.deltaTime;
              } else if (origin - 200 * Time.deltaTime < 0.00001) {
                  transform.position += Vector3.up * Time.deltaTime;
                  transform.position += Vector3.right * Time.deltaTime;
              } else {
                  flag = false;
              }
              origin += Time.deltaTime;
          } else {
              if (origin - 100 * Time.deltaTime > 0.00001) { 
                  transform.position += Vector3.down * Time.deltaTime;
                  transform.position += Vector3.left * Time.deltaTime;
              } else if (origin > 0.00001) {
                  transform.position += Vector3.up * Time.deltaTime;
                  transform.position += Vector3.left * Time.deltaTime;
              } else {
                  flag = true;
              }
              origin -= Time.deltaTime;
          }
      }
      
  • 写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。

    实现太阳系,使用RotateRound函数即可,第一个参数是选择旋转的中心,第二个参数是选择旋转围绕的方向,为了使每个星球在不同法平面旋转,需要适当调整这个值。具体代码如下:

    move.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class move : MonoBehaviour
    {
        public Texture2D img;
    	public Transform sun;
        public Transform waterplanet;
        public Transform earth;
        //public Transform moon;
        public Transform goldplanet;
        public Transform mars;
        public Transform woodplanet;
        public Transform mudplanet;
        public Transform tianwang;
        public Transform haiwang;
        // Start is called before the first frame update
        private void OnGUI()
        {
            //GUIStyle bg = new GUIStyle();
            //bg.normal.background = img;
            //GUI.Label(new Rect(0, 0, 1024, 781), "", bg);
        }
        // Update is called once per frame
        void Update()
        {
            sun.Rotate(Vector3.up * 3 * Time.deltaTime);
            waterplanet.RotateAround(sun.position, new Vector3(0,1,1), 200 * Time.deltaTime);
            goldplanet.RotateAround(sun.position, new Vector3(0, 2, 1), 100 * Time.deltaTime);
            earth.RotateAround(sun.position, Vector3.up, 50 * Time.deltaTime);
            //moon.RotateAround(earth.position, new Vector3(0,0.1f,0.1f), 100 * Time.deltaTime);
            mars.RotateAround(sun.position, new Vector3(0, 2, 2), 40 * Time.deltaTime);
            woodplanet.RotateAround(sun.position, new Vector3(0, 1, 2), 30 * Time.deltaTime);
            mudplanet.RotateAround(sun.position, new Vector3(0, 3, 2), 20 * Time.deltaTime);
            tianwang.RotateAround(sun.position, new Vector3(0, 3, 1), 10 * Time.deltaTime);
            haiwang.RotateAround(sun.position, new Vector3(0, 2, 4), 5 * Time.deltaTime);
        }
    }
    
    

    earth_moon.cs

    地月是一个子系统,需要单独来写。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class earth_moon : MonoBehaviour
    {
        public Transform earth;
        public Transform moon;
        // Start is called before the first frame update
        void Start()
        {
            
        }
    
        // Update is called once per frame
        void Update()
        {
            moon.RotateAround(earth.position, Vector3.up, 100 * Time.deltaTime);
        }
    }
    
    

    最后运行截图如下:
    在这里插入图片描述

2、编程实践

  • 阅读以下游戏脚本

Priests and Devils

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

程序需要满足的要求:

  • play the game ( http://www.flash-game.net/game/2535/priests-and-devils.html )

  • 列出游戏中提及的事物(Objects)

    河流、小船、牧师、魔鬼、岸

  • 用表格列出玩家动作表(规则表),注意,动作越少越好

    牧师、魔鬼上船、下船
    载着人物驶向对岸
  • 请将游戏中对象做成预制

在这里插入图片描述

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

实现

使用MVC结构进行架构。

  • model

    • Boat
    • Character
    • Coast
    • River
  • View

    • UserGUI

      用于展示游戏界面,以及用户点击事件

  • Controller

    • BoatController

      控制船的移动,以及船上人的信息

    • Charactercontroller

      人物的动作:上下船

    • CoastController

      河岸承担了与船的交互,以及人员的增减

    • director

        public class Director : System.Object
        {
            private static Director _instance;
    
            public ISceneController CurrentSecnController { get; set; }
            public static Director GetInstance()
            {
                return _instance ?? (_instance = new Director());
            }
        }
    

    ​ 所有游戏只有一个实例,就是通过这个类来实例化的。

    • Interfaces

    • KernelController

      主要的控制器,控制了船的移动和游戏输赢的判定,它是一个Director类,控制着游戏的创建、加载等动作

    • Move

      所有对象移动的动作

项目地址
视频地址

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C#语言实现牧师与野人问题求解的代码和注释: ```csharp using System; using System.Collections.Generic; namespace MissionariesAndCannibals { class Program { static void Main(string[] args) { // 初始化状态 State initialState = new State(3, 3, true, 0, 0); // 初始化搜索树 SearchTree searchTree = new SearchTree(initialState); // 进行搜索 searchTree.BFS(); } } // 表示状态的类 class State { public int MissionariesLeft { get; set; } // 左岸传教士数量 public int CannibalsLeft { get; set; } // 左岸野人数量 public bool BoatPosition { get; set; } // 船的位置,true表示在左岸,false表示在右岸 public int MissionariesRight { get; set; } // 右岸传教士数量 public int CannibalsRight { get; set; } // 右岸野人数量 // 构造函数 public State(int missionariesLeft, int cannibalsLeft, bool boatPosition, int missionariesRight, int cannibalsRight) { MissionariesLeft = missionariesLeft; CannibalsLeft = cannibalsLeft; BoatPosition = boatPosition; MissionariesRight = missionariesRight; CannibalsRight = cannibalsRight; } // 判断状态是否合法 public bool IsValid() { if (MissionariesLeft < 0 || CannibalsLeft < 0 || MissionariesRight < 0 || CannibalsRight < 0) { return false; } if (MissionariesLeft > 3 || CannibalsLeft > 3 || MissionariesRight > 3 || CannibalsRight > 3) { return false; } if (CannibalsLeft > MissionariesLeft && MissionariesLeft > 0) { return false; } if (CannibalsRight > MissionariesRight && MissionariesRight > 0) { return false; } return true; } // 判断状态是否为目标状态 public bool IsGoal() { return MissionariesLeft == 0 && CannibalsLeft == 0; } // 判断两个状态是否相等 public override bool Equals(object obj) { State state = obj as State; if (state == null) { return false; } return MissionariesLeft == state.MissionariesLeft && CannibalsLeft == state.CannibalsLeft && BoatPosition == state.BoatPosition && MissionariesRight == state.MissionariesRight && CannibalsRight == state.CannibalsRight; } // 获取状态的哈希值 public override int GetHashCode() { return MissionariesLeft * 10000 + CannibalsLeft * 1000 + (BoatPosition ? 100 : 0) + MissionariesRight * 10 + CannibalsRight; } // 获取状态的字符串表示 public override string ToString() { return "MissionariesLeft: " + MissionariesLeft + ", CannibalsLeft: " + CannibalsLeft + ", BoatPosition: " + (BoatPosition ? "Left" : "Right") + ", MissionariesRight: " + MissionariesRight + ", CannibalsRight: " + CannibalsRight; } } // 表示搜索树的类 class SearchTree { private State _initialState; // 初始状态 private Queue<Node> _frontier; // 存放待扩展的节点的队列 private HashSet<State> _exploredSet; // 存放已扩展过的状态的集合 // 构造函数 public SearchTree(State initialState) { _initialState = initialState; _frontier = new Queue<Node>(); _frontier.Enqueue(new Node(_initialState, null)); _exploredSet = new HashSet<State>(); } // 广度优先搜索 public void BFS() { while (_frontier.Count > 0) { Node node = _frontier.Dequeue(); State state = node.State; if (state.IsGoal()) { // 找到了目标状态,输出路径 List<State> path = new List<State>(); while (node != null) { path.Insert(0, node.State); node = node.Parent; } foreach (State s in path) { Console.WriteLine(s); } return; } _exploredSet.Add(state); List<State> successors = GetSuccessors(state); foreach (State successor in successors) { if (!_exploredSet.Contains(successor)) { _frontier.Enqueue(new Node(successor, node)); } } } Console.WriteLine("No solution found."); } // 获取一个状态的所有合法后继状态 private List<State> GetSuccessors(State state) { List<State> successors = new List<State>(); if (state.BoatPosition) { // 船在左岸 for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { if (i + j >= 1 && i + j <= 2) { State successor = new State(state.MissionariesLeft - i, state.CannibalsLeft - j, false, state.MissionariesRight + i, state.CannibalsRight + j); if (successor.IsValid()) { successors.Add(successor); } } } } } else { // 船在右岸 for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { if (i + j >= 1 && i + j <= 2) { State successor = new State(state.MissionariesLeft + i, state.CannibalsLeft + j, true, state.MissionariesRight - i, state.CannibalsRight - j); if (successor.IsValid()) { successors.Add(successor); } } } } } return successors; } } // 表示搜索树中的节点的类 class Node { public State State { get; set; } // 节点对应的状态 public Node Parent { get; set; } // 父节点 // 构造函数 public Node(State state, Node parent) { State = state; Parent = parent; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值