Unity3D-魔鬼与牧师游戏开发

2023-12-16 11:10

本文主要是介绍Unity3D-魔鬼与牧师游戏开发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一、魔鬼与牧师的游戏介绍

     二、游戏的初步分析与设计

(一)游戏中提及的事物(Objects)

(二)玩家动作表(规则表)

(三)各个对象的预制

三、基于MVC框架确定脚本结构:实现模型——视图——控制器的分离

(一)Models

(二)Contollers

(三) View

(四)代码MVC框架结构UML图

四、游戏运行效果


一、魔鬼与牧师的游戏介绍

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!

游戏内容如下:

牧师和魔鬼是一款益智游戏,您将在其中帮助牧师和魔鬼过河。河的一侧有3个祭司和3个魔鬼。他们都想去这条河的另一边,但只有一条船,这条船每次只能载两个人。而且必须有一个人将船从一侧驾驶到另一侧。您可以单击按钮来移动它们,然后单击移动按钮将船移动到另一个方向。如果靠岸的船上和同一侧岸上的牧师被岸上的魔鬼人数所淹没,他们就会被杀死,游戏就结束了。您可以通过多种方式尝试它。让所有的祭司活着!最后所有牧师和魔鬼都成功过河,则表示游戏胜利。

二、游戏的初步分析与设计

(一)游戏中提及的事物(Objects)

牧师(Priest)、魔鬼(Devil)、船(boat)、两岸陆地(land)

(二)玩家动作表(规则表)

动作(玩家事件)响应条件角色效果(结果)
点击牧师/魔鬼游戏未结束;角色在船上角色上岸
点击牧师/魔鬼游戏未结束;角色在岸上角色上船
点击船游戏未结束;船没有在移动;船上至少存在一个角色,至多有两个角色船开到对岸
点击“Restart”重新开始

(三)各个对象的预制

通过创建GameObject并使用Metariel设置好对应的颜色和形状作为预制,最后完成的预制对象如下图:

 

三、基于MVC框架确定脚本结构:实现模型——视图——控制器的分离

(一)Models

包含两个部分:

  • 包含各个对象的模块,每个模块中定义了对象的类
  • 包含自定义的部件,用于处理事件

具体的模型有:船、岸、角色 ,抽象的模型有:点击、移动、位置 

1、船

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Boat
{public GameObject boat;//船对象public Boat(Vector3 initPos){boat = GameObject.Instantiate(Resources.Load("Prefabs/boat", typeof(GameObject))) as GameObject;boat.transform.position = initPos;boat.AddComponent<Click>();}}

 2、角色

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Role
{public GameObject role;public Role(int roleType, Vector3 initPos){string path = "Prefabs/" + ((roleType == FirstController.PRIEST) ? "priest" : "devil");role = GameObject.Instantiate(Resources.Load(path, typeof(GameObject))) as GameObject;role.transform.position = initPos;role.AddComponent<Click>();}
}

 3、陆地

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Land
{public GameObject land;public Land(Vector3 initPos){land = GameObject.Instantiate(Resources.Load("Prefabs/land", typeof(GameObject))) as GameObject;land.transform.position = initPos;}
}

4、点击

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Click : MonoBehaviour
{IObjectController clickAction;public void setClickAction(IObjectController clickAction) {this.clickAction = clickAction;}void OnMouseDown() {clickAction.DealClick();}
}

 5、移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Move : MonoBehaviour
{public bool isMoving;public float speed = 5;public Vector3 destination;void Update(){if (transform.localPosition == destination) {isMoving = false;return;}isMoving = true;transform.localPosition = Vector3.MoveTowards(transform.localPosition, destination, speed * Time.deltaTime);}
}

6、位置

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Move : MonoBehaviour
{public bool isMoving;public float speed = 5;public Vector3 destination;void Update(){if (transform.localPosition == destination) {isMoving = false;return;}isMoving = true;transform.localPosition = Vector3.MoveTowards(transform.localPosition, destination, speed * Time.deltaTime);}
}

(二)Contollers

每一个模型的副本都会有一个对应的控制器(位置模型除外),控制模型的行为。

另外还有一类很重要的控制器就是场景控制器和导演。导演贯穿了所有场景,导演控制器必须被写成单例模式,这就确保了各控制器属于同一场“话剧”。

场景控制器(FirstController)管理所有该场景内的模型控制器,并且实现他们的综合行为。FirstController会生成并且管理:一个BoatController,两个LandController,六个RoleController,一个MoveController。所有该场景内的综合行为都会在这个场景控制器内实现。

最后还有用于规范同一类型的控制器应当具有的行为的接口设计:IObjectContoller应当被所有模型的控制器继承、IScenceController应当被所有场景的控制器继承、IUserAction负责与视图方面沟通的接口。

代码示例:

1、BoatController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class BoatController : IObjectController
{public bool onLeftside;IUserAction userAction;public int[] seat;public Boat boatModel;public BoatController(){userAction = SSDirector.GetInstance().CurrentSceneController as IUserAction;seat = new int[3];Reset();}public void Reset(){onLeftside = true;for(int i = 0; i < 3; i++){seat[i] = -1;}}public int embark(int roleID){for(int i = 0; i < 3; i++){if(seat[i] == -1){seat[i] = roleID;return i;}}return -1;}public int getEmptySeat(){for(int i = 0; i < 3; i++){if(seat[i] == -1){return i;}}return -1;}public void disembark(int roleID){for(int i = 0; i < 3; i++){if(seat[i] == roleID){seat[i] = -1;return;}}}public void CreateModel(){boatModel = new Boat(Position.boatLeftPos);boatModel.boat.GetComponent<Click>().setClickAction(this);}public void DealClick(){userAction.MoveBoat();}public GameObject GetModelGameObject(){return boatModel.boat;}}

2、 MoveController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class MoveController
{GameObject moveObject;public bool IsMoving(){return(this.moveObject != null && this.moveObject.GetComponent<Move>().isMoving == true);}public void SetMove(GameObject moveObject, Vector3 destination) {// 设置一个新的移动Move test;this.moveObject = moveObject;if (!moveObject.TryGetComponent<Move>(out test)) {moveObject.AddComponent<Move>();}this.moveObject.GetComponent<Move>().destination = destination;}
}

3、 SSDirector

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SSDirector : System.Object
{static SSDirector _instance;public ISceneController CurrentSceneController {get; set;}public static SSDirector GetInstance() {if (_instance == null) {_instance = new SSDirector();}return _instance;}
}

 4、FirstController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class FirstController : MonoBehaviour, ISceneController, IUserAction
{public static int LEFTLAND = 0;public static int RIGHTLAND = 1;public static int BOAT = 2;public static int PRIEST = 0;public static int DEVIL = 1;public static int PLAYING = 0;public static int WIN = 1;public static int FAILED = 2;BoatController BoatCtrl;RoleController[] RoleCtrl = new RoleController[6];LandController[] LandCtrl = new LandController[2];MoveController MoveCtrl;int[] rolesID = new int[6]{0,1,2,3,4,5};int gameState;void Awake(){SSDirector director = SSDirector.GetInstance();director.CurrentSceneController = this;director.CurrentSceneController.Initialize();}public void Initialize(){//如果有,则释放原有的GameObjectfor(int i = 0; i < 6; i++){if(RoleCtrl[i] != null){Destroy(RoleCtrl[i].GetModelGameObject());}}for(int i = 0; i < 2; i++){if(LandCtrl[i] != null){Destroy(LandCtrl[i].GetModelGameObject());}}if(BoatCtrl != null){Destroy(BoatCtrl.GetModelGameObject());}// 加载控制器和模型BoatCtrl = new BoatController();BoatCtrl.CreateModel();for(int i = 0; i < 6; i++){int roleType = (i < 3) ? PRIEST : DEVIL;RoleCtrl[i] = new RoleController(roleType, rolesID[i]);RoleCtrl[i].CreateModel();}LandCtrl[0] = new LandController(LEFTLAND, rolesID);LandCtrl[1] = new LandController(RIGHTLAND, rolesID);LandCtrl[0].CreateModel();LandCtrl[1].CreateModel();MoveCtrl = new MoveController();//开始游戏gameState = PLAYING;}//将角色的ID转换成数组的下标int IDToNumber(int ID){for(int i = 0; i < 6; i++){if(rolesID[i] == ID){return i;}}return -1;}//点击船时执行public void MoveBoat(){if(gameState != PLAYING || MoveCtrl.IsMoving()) return;CheckAndSetGameState();if(BoatCtrl.onLeftside){MoveCtrl.SetMove(BoatCtrl.GetModelGameObject(), Position.boatRightPos);for(int i = 0; i < 3; i++){if(BoatCtrl.seat[i] != -1){RoleController r = RoleCtrl[IDToNumber(BoatCtrl.seat[i])];MoveCtrl.SetMove(r.GetModelGameObject(), Position.seatRightPos[i]);}}}else{MoveCtrl.SetMove(BoatCtrl.GetModelGameObject(), Position.boatLeftPos);for(int i = 0; i < 3; i++){if(BoatCtrl.seat[i] != -1){RoleController r = RoleCtrl[IDToNumber(BoatCtrl.seat[i])];MoveCtrl.SetMove(r.GetModelGameObject(), Position.seatLeftPos[i]);}} }BoatCtrl.onLeftside = !BoatCtrl.onLeftside;}//点击角色时执行public void MoveRole(int id){int num = IDToNumber(id);if(gameState != PLAYING || MoveCtrl.IsMoving()) return;int seat;switch(RoleCtrl[num].roleState){case 0: // LEFTLANDif(!BoatCtrl.onLeftside) return;LandCtrl[0].LeaveLand(id);seat = BoatCtrl.embark(id);RoleCtrl[num].GoTo(BOAT);if(seat == -1) return;MoveCtrl.SetMove(RoleCtrl[num].GetModelGameObject(), Position.seatLeftPos[seat]);break;case 1: // RIGHTLANDif(BoatCtrl.onLeftside) return;LandCtrl[1].LeaveLand(id);seat = BoatCtrl.embark(id);RoleCtrl[num].GoTo(BOAT);if(seat == -1) return;MoveCtrl.SetMove(RoleCtrl[num].GetModelGameObject(), Position.seatRightPos[seat]);break;case 2: //BOATif(BoatCtrl.onLeftside){seat = LandCtrl[0].getEmptySeat();BoatCtrl.disembark(id);LandCtrl[0].GoOnLand(id);RoleCtrl[num].GoTo(LEFTLAND);MoveCtrl.SetMove(RoleCtrl[num].GetModelGameObject(), Position.roleLeftPos[seat]);}else{seat = LandCtrl[1].getEmptySeat();BoatCtrl.disembark(id);LandCtrl[1].GoOnLand(id);RoleCtrl[num].GoTo(RIGHTLAND);MoveCtrl.SetMove(RoleCtrl[num].GetModelGameObject(), Position.roleRightPos[seat]);}break;default: break;}}//判断游戏状态public void CheckAndSetGameState(){if(gameState != PLAYING) return;//判断是否失败int[,] rolePos = new int[2, 3]{{0, 0, 0}, {0, 0, 0}};foreach(RoleController r in RoleCtrl){rolePos[r.roleType, r.roleState]++;}if((rolePos[0,0]>0 && rolePos[0,0]<rolePos[1,0]) || (rolePos[0,1]>0 && rolePos[0,1]<rolePos[1,1]) || (rolePos[0,2]>0 && rolePos[0,2] < rolePos[1,2])){gameState = FAILED;return;}  //判断是否成功foreach(RoleController r in RoleCtrl){if(r.roleType == 0 && r.roleState != RIGHTLAND){return;}}gameState = WIN;return;}//Reset按钮执行的功能public void Restart(){Initialize();gameState = PLAYING;}//获取游戏当前状态public int GetGameState(){return gameState;}
}

5、IUserAction

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public interface IUserAction {void MoveBoat();void MoveRole(int id);int GetGameState();void Restart();
}

 

(三) View

视图:UserGUI,视图控制了所有与3D游戏实体不相关的,GUI上的组件。比如说:标题、重新开始的按钮、游戏成功或者失败时的提示。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class UserGUI : MonoBehaviour
{IUserAction userAction;GUIStyle msgStyle, titleStyle;void Start(){userAction = SSDirector.GetInstance().CurrentSceneController as IUserAction;msgStyle = new GUIStyle();msgStyle.normal.textColor = Color.white;msgStyle.alignment = TextAnchor.MiddleCenter;msgStyle.fontSize = 30;titleStyle = new GUIStyle();titleStyle.normal.textColor = Color.white;titleStyle.alignment = TextAnchor.MiddleCenter;titleStyle.fontSize = 60;}// Update is called once per framevoid OnGUI() {// 重新开始的按钮if(GUI.Button(new Rect(Screen.width*0.4f, Screen.height*0.65f, Screen.width*0.2f, Screen.height*0.1f), "Restart")){userAction.Restart();}// 检查是否正确GUI.Label(new Rect(0, 0, Screen.width, Screen.height*0.2f), "Preists and Devils", titleStyle);if(userAction.GetGameState() == FirstController.WIN){GUI.Label(new Rect(0, Screen.height*0.8f, Screen.width, Screen.height*0.2f), "You Win.", msgStyle);}     else if(userAction.GetGameState() == FirstController.FAILED){GUI.Label(new Rect(0, Screen.height*0.8f, Screen.width, Screen.height*0.2f), "You failed.", msgStyle);}}
}

(四)代码MVC框架结构UML图

 

四、游戏运行效果展示

将Assert/Scripts/UserGUI拖动到Main Camera上,创建一个新的空GameObject,将FirstController拖动到GameObject上,点击运行即可。

视频展示:Devil and Priest_哔哩哔哩_bilibili

这篇关于Unity3D-魔鬼与牧师游戏开发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

Linux_kernel驱动开发11

一、改回nfs方式挂载根文件系统         在产品将要上线之前,需要制作不同类型格式的根文件系统         在产品研发阶段,我们还是需要使用nfs的方式挂载根文件系统         优点:可以直接在上位机中修改文件系统内容,延长EMMC的寿命         【1】重启上位机nfs服务         sudo service nfs-kernel-server resta

【区块链 + 人才服务】区块链集成开发平台 | FISCO BCOS应用案例

随着区块链技术的快速发展,越来越多的企业开始将其应用于实际业务中。然而,区块链技术的专业性使得其集成开发成为一项挑战。针对此,广东中创智慧科技有限公司基于国产开源联盟链 FISCO BCOS 推出了区块链集成开发平台。该平台基于区块链技术,提供一套全面的区块链开发工具和开发环境,支持开发者快速开发和部署区块链应用。此外,该平台还可以提供一套全面的区块链开发教程和文档,帮助开发者快速上手区块链开发。

Vue3项目开发——新闻发布管理系统(六)

文章目录 八、首页设计开发1、页面设计2、登录访问拦截实现3、用户基本信息显示①封装用户基本信息获取接口②用户基本信息存储③用户基本信息调用④用户基本信息动态渲染 4、退出功能实现①注册点击事件②添加退出功能③数据清理 5、代码下载 八、首页设计开发 登录成功后,系统就进入了首页。接下来,也就进行首页的开发了。 1、页面设计 系统页面主要分为三部分,左侧为系统的菜单栏,右侧

v0.dev快速开发

探索v0.dev:次世代开发者之利器 今之技艺日新月异,开发者之工具亦随之进步不辍。v0.dev者,新兴之开发者利器也,迅速引起众多开发者之瞩目。本文将引汝探究v0.dev之基本功能与优势,助汝速速上手,提升开发之效率。 何谓v0.dev? v0.dev者,现代化之开发者工具也,旨在简化并加速软件开发之过程。其集多种功能于一体,助开发者高效编写、测试及部署代码。无论汝为前端开发者、后端开发者

国产游戏崛起:技术革新与文化自信的双重推动

近年来,国产游戏行业发展迅猛,技术水平和作品质量均得到了显著提升。特别是以《黑神话:悟空》为代表的一系列优秀作品,成功打破了过去中国游戏市场以手游和网游为主的局限,向全球玩家展示了中国在单机游戏领域的实力与潜力。随着中国开发者在画面渲染、物理引擎、AI 技术和服务器架构等方面取得了显著进展,国产游戏正逐步赢得国际市场的认可。然而,面对全球游戏行业的激烈竞争,国产游戏技术依然面临诸多挑战,未来的