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

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

基于Python开发一个有趣的工作时长计算器

《基于Python开发一个有趣的工作时长计算器》随着远程办公和弹性工作制的兴起,个人及团队对于工作时长的准确统计需求日益增长,本文将使用Python和PyQt5打造一个工作时长计算器,感兴趣的小伙伴可... 目录概述功能介绍界面展示php软件使用步骤说明代码详解1.窗口初始化与布局2.工作时长计算核心逻辑3

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示

JavaScript实战:智能密码生成器开发指南

本文通过JavaScript实战开发智能密码生成器,详解如何运用crypto.getRandomValues实现加密级随机密码生成,包含多字符组合、安全强度可视化、易混淆字符排除等企业级功能。学习密码强度检测算法与信息熵计算原理,获取可直接嵌入项目的完整代码,提升Web应用的安全开发能力 目录

一文教你如何解决Python开发总是import出错的问题

《一文教你如何解决Python开发总是import出错的问题》经常朋友碰到Python开发的过程中import包报错的问题,所以本文将和大家介绍一下可编辑安装(EditableInstall)模式,可... 目录摘要1. 可编辑安装(Editable Install)模式到底在解决什么问题?2. 原理3.

Python+PyQt5开发一个Windows电脑启动项管理神器

《Python+PyQt5开发一个Windows电脑启动项管理神器》:本文主要介绍如何使用PyQt5开发一款颜值与功能并存的Windows启动项管理工具,不仅能查看/删除现有启动项,还能智能添加新... 目录开篇:为什么我们需要启动项管理工具功能全景图核心技术解析1. Windows注册表操作2. 启动文件

使用Python开发Markdown兼容公式格式转换工具

《使用Python开发Markdown兼容公式格式转换工具》在技术写作中我们经常遇到公式格式问题,例如MathML无法显示,LaTeX格式错乱等,所以本文我们将使用Python开发Markdown兼容... 目录一、工具背景二、环境配置(Windows 10/11)1. 创建conda环境2. 获取XSLT