本文主要是介绍牧师与魔鬼——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!
稍微总结一下:
目标:成功将3个牧师和3个魔鬼运到河对岸
限制条件:
- 船上最多载2人,且必须至少一人才能开船
- 河的一岸魔鬼人数多于牧师,且牧师数不为0,则游戏失败。
用户操作:点击相应的人物使其上下船,点击船即可移动到对岸。
游戏成果展示
视频展示:
牧师与魔鬼小游戏unity实录_哔哩哔哩bilibili
项目获取:
魔鬼与牧师 https://www.aliyundrive.com/s/bYTRunqvV43
提取码: 3z2c
游戏制作准备
1.分析游戏对象
1.牧师
2.魔鬼
3.船
4.河岸(两边都有)
2.玩家动作规则表
玩家动作 | 结果 | 条件 |
---|---|---|
点击人物角色(牧师/魔鬼) | 人物上船/下船 | 游戏进行中且人物在岸上/船上 |
点击船 | 船开动 | 船上至少有一人且游戏未结束 |
点击Restart | 重新开始游戏 | 游戏结束且弹出Restart的按钮 |
点击return menu | 返回菜单 | 无 |
点击pause | 游戏暂停 | 无 |
点击game rules | 查看游戏规则 | 无 |
点击start game | 开始游戏 | 处于Start Menu |
3.对象预制
用方块建立河流、河岸等对象,调整其合适大小,并且记录其在摄像机范围内显示的坐标。用暗红色球体表示魔鬼,用白色立方体表示牧师,用长方块表示船。
大致完成好的场景如下图:
核心代码分析(MVC)
UML图
1.MVC介绍
1. 模型(Model):
游戏中的模型代表游戏的数据和业务逻辑。它可以包括玩家、敌人、物品、关卡等游戏中的实体或对象。模型负责处理这些实体的状态、行为和规则,并提供一种访问和操作数据的接口。模型可以包括数据结构、算法、碰撞检测、游戏规则等。
2. 视图(View):
游戏中的视图是玩家所看到的游戏界面。它可以包括游戏场景、用户界面、HUD(头上显示)等。视图负责将游戏中的数据和状态以可视化的方式呈现给玩家。视图可以根据模型的状态进行更新,并处理用户输入以进行交互。
3. 控制器(Controller):
游戏中的控制器是处理用户输入和响应的组件。它接收来自玩家的输入,例如键盘、鼠标或手柄输入,并将其转化为游戏中的操作。控制器根据输入更新模型的状态,并根据模型的变化更新视图。它还可以处理游戏中的逻辑和规则,例如游戏流程、胜利条件、失败条件等。
2.Model
角色模型
/*角色模型*/public class RoleModel{GameObject role; //角色对象int role_sign; //角色类别标记,0为牧师,1为恶魔Click click;Move move;bool on_boat; //是否在船上 LandModel land_model = (SSDirector.GetInstance().CurrentScenceController as Controllor).start_land;/*构造函数初始化*/public RoleModel(string role_name){if (role_name == "priest"){role = Object.Instantiate(Resources.Load("Perfabs/Priest", typeof(GameObject)), Vector3.zero, Quaternion.Euler(0, -8, 0)) as GameObject;role_sign = 0;}else{role = Object.Instantiate(Resources.Load("Perfabs/Devil", typeof(GameObject)), Vector3.zero, Quaternion.Euler(0, -8, 0)) as GameObject;role_sign = 1;}move = role.AddComponent(typeof(Move)) as Move;click = role.AddComponent(typeof(Click)) as Click;click.SetRole(this);}/*get,set函数*/public int GetSign() { return role_sign;}public LandModel GetLandModel(){return land_model;}public string GetName() { return role.name; }public bool IsOnBoat() { return on_boat; }public void SetName(string name) { role.name = name; }public void SetPosition(Vector3 pos) { role.transform.position = pos; }/*角色移动*/public void Move(Vector3 vec){move.MovePosition(vec);}public void MoveToLand(LandModel land){ role.transform.parent = null;land_model = land;on_boat = false;}public void MoveToBoat(BoatModel boat){role.transform.parent = boat.GetBoat().transform;land_model = null; on_boat = true;}/*重置*/public void Reset(){land_model = (SSDirector.GetInstance().CurrentScenceController as Controllor).start_land;MoveToLand(land_model);SetPosition(land_model.GetEmpty());land_model.AddRoleOnLand(this);}}
船只模型
public class BoatModel{GameObject boat; Vector3[] start_vacancy; //船在起点的空位Vector3[] end_vacancy; //船在终点的空位Move move; Click click;int boat_sign = 1; //船在起点还是终点,1为起点,-1为终点RoleModel[] roles = new RoleModel[2]; //在船上的角色/*构造函数,完成一些简单的初始化*/public BoatModel(){boat = Object.Instantiate(Resources.Load("Perfabs/Boat", typeof(GameObject)), new Vector3(4, -1.5F, 0), Quaternion.identity) as GameObject;//复制实例化对象,指定其位置并返回boat.name = "boat";move = boat.AddComponent(typeof(Move)) as Move;click = boat.AddComponent(typeof(Click)) as Click;click.SetBoat(this);start_vacancy = new Vector3[] { new Vector3(3.5F, -1, 0), new Vector3(4.5F, -1, 0) };end_vacancy = new Vector3[] { new Vector3(-4.5F, -1, 0), new Vector3(-3.5F, -1, 0) };}/*判断船是否是空的,空船不能移动*/public bool IsEmpty(){for (int i = 0; i < roles.Length; ++i){if (roles[i] != null)return false;}return true;}/*根据所在位置,确定移动方向*/public void BoatMove(){if (boat_sign == -1){move.MovePosition(new Vector3(4, -1.5F, 0));boat_sign = 1;}else{move.MovePosition(new Vector3(-4, -1.5F, 0));boat_sign = -1;}}/*放回当前位置的标志*/public int GetBoatSign(){ return boat_sign;}public RoleModel DeleteRoleByName(string role_name){for (int i = 0; i < roles.Length; ++i){if (roles[i] != null && roles[i].GetName() == role_name){RoleModel role = roles[i];roles[i] = null;return role;}}return null;}/*返回空位的角标*/public int GetEmptyIndex(){for (int i = 0; i < roles.Length; ++i){if (roles[i] == null){return i;}}return -1;}/*返回空位*/public Vector3 GetEmpty(){Vector3 pos;if(boat_sign == 1)pos = start_vacancy[GetEmptyIndex()];elsepos = end_vacancy[GetEmptyIndex()];return pos;}/*在船上添加角色*/public void AddRoleOnBoat(RoleModel role){roles[GetEmptyIndex()] = role;}/*返回船*/public GameObject GetBoat(){ return boat; }/*重置*/public void Reset(){if (boat_sign == -1)BoatMove();roles = new RoleModel[2];}/*得到船上角色的数量*/public int[] GetRoleNumber(){int[] count = { 0, 0 };for (int i = 0; i < roles.Length; ++i){if (roles[i] == null)continue;if (roles[i].GetSign() == 0)count[0]++;elsecount[1]++;}return count;}}
河岸模型
public class LandModel{GameObject land; //陆地对象Vector3[] positions; //角色放在陆地上的位置int land_type; //陆地类型,-1为目的陆地标志,1为起点陆地标志RoleModel[] roles = new RoleModel[6]; //在陆地上的角色/*构造函数*/public LandModel(string land_type_string){//设置角色放在陆地上的位置positions = new Vector3[] {new Vector3(5.3F,-0.3F,0), new Vector3(6.1F,-0.3F,0), new Vector3(6.9F,-0.3F,0),new Vector3(7.7F,-0.3F,0), new Vector3(8.5F,-0.3F,0), new Vector3(9.3F,-0.3F,0)};if (land_type_string == "start"){//设置起点land = Object.Instantiate(Resources.Load("Perfabs/Stone", typeof(GameObject)), new Vector3(8, -1.5F, 0), Quaternion.identity) as GameObject;land_type = 1;}else if (land_type_string == "end"){//设置终点land = Object.Instantiate(Resources.Load("Perfabs/Stone", typeof(GameObject)), new Vector3(-8, -1.5F, 0), Quaternion.identity) as GameObject;land_type = -1;}}/*返回空位角标*/public int GetEmptyIndex(){for (int i = 0; i < roles.Length; ++i){if (roles[i] == null)return i;}return -1; //没有则返回-1}/*成员变量LandType的get函数*/public int GetLandType() { return land_type; }/*返回空位*/public Vector3 GetEmpty(){Vector3 pos = positions[GetEmptyIndex()];pos.x = land_type * pos.x; //利用两岸的x坐标对称return pos;}/*添加角色在该陆地上*/public void AddRoleOnLand(RoleModel role){roles[GetEmptyIndex()] = role;}/*删除该陆地上的指定角色*/public RoleModel DeleteRoleByName(string role_name) { for (int i = 0; i < roles.Length; ++i){if (roles[i] != null && roles[i].GetName() == role_name){RoleModel role = roles[i];roles[i] = null;return role;}}return null;}/*得到角色数量*/public int[] GetRoleNum(){int[] count = { 0, 0 }; //count[0]是牧师数,count[1]是魔鬼数for (int i = 0; i < roles.Length; ++i){if (roles[i] != null){if (roles[i].GetSign() == 0)count[0]++;elsecount[1]++;}}return count;}/*重置*/public void Reset(){roles = new RoleModel[6];}}
3.View
游戏界面
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Mygame;
public class UserGUI : MonoBehaviour {private IUserAction action;public int sign = 0;//=0表示游戏在进行,=1表示游戏失败,=2表示游戏胜利,=3表示游戏停止bool isShowRules = false;public string timeStr = string.Empty;void Start(){action = SSDirector.GetInstance().CurrentScenceController as IUserAction;}void OnGUI(){/*设置计时器*/timeStr = string.Format("用时:{0:D2}:{1:D2}:{2:D2}", action.getTimer().hour, action.getTimer().minute, action.getTimer().second);GUI.Label(new Rect(500, 10, 100, 200), timeStr);/*定义字体风格*/GUIStyle text_style;GUIStyle button_style;text_style = new GUIStyle(){fontSize = 30};button_style = new GUIStyle("button"){fontSize = 15};/*游戏规则按钮*/if (GUI.Button(new Rect(10, 10, 100, 30), "game rules", button_style)){/*点一下打开游戏规则提示,再点一下关闭游戏规则提示*/isShowRules = !isShowRules;}/*展示游戏规则*/if(isShowRules){GUI.Label(new Rect(Screen.width / 2 - 150, 50, 300, 50), "Win: all priests and demons cross the river");GUI.Label(new Rect(Screen.width / 2 - 150, 70, 400, 50), "Lose: there are more demons than priests on either side");GUI.Label(new Rect(Screen.width / 2 - 150, 90, 300, 50), "Tap priest, demon, ship to move");}/*返回菜单按钮*/if (GUI.Button(new Rect(120, 10, 100, 30), "return menu", button_style)) {SceneManager.LoadScene("startMenu", LoadSceneMode.Single);;//切换到"startMenu"界面并销毁本界面}/*重新开始按钮*/if (GUI.Button(new Rect(230, 10, 100, 30), "restart", button_style)) {sign = 0;action.Restart();}/*游戏暂停按钮*/if(GUI.Button(new Rect(340, 10, 100, 30), "pause", button_style)){if(sign==0){//游戏正在进行sign = 3;//游戏的状态发生改变action.getTimer().StopTiming();//事件暂停}}/*游戏暂停展示*/if(sign == 3){//游戏暂停,打印暂停并显示“Return Game”按钮提示玩家可以点击回到游戏中GUI.Label(new Rect(Screen.width / 2-100, Screen.height / 2-120, 100, 50), "Game pause!", text_style);if (GUI.Button(new Rect(Screen.width / 2 - 70, Screen.height / 2-40, 120, 50), "Return Game", button_style)){//回到游戏,游戏状态发生改变,时间开始计时sign = 0;action.getTimer().beginTiming();}}/*游戏结局展示*/if (sign == 1){//游戏失败,打印失败并显示“Try again”按钮提示玩家再进行尝试GUI.Label(new Rect(Screen.width / 2-90, Screen.height / 2-120, 100, 50), "Gameover!", text_style);if (GUI.Button(new Rect(Screen.width / 2 - 70, Screen.height / 2-40, 100, 50), "Try again", button_style)){sign = 0;action.Restart(); }}else if (sign == 2){//游戏成功,打印成狗并显示“Play again”按钮提示玩家再玩一局GUI.Label(new Rect(Screen.width / 2 - 80, Screen.height / 2 - 120, 100, 50), "You win!", text_style);if (GUI.Button(new Rect(Screen.width / 2 - 70, Screen.height / 2-40, 100, 50), "Play again", button_style)){sign = 0;action.Restart();}}}
}
主菜单界面
using UnityEngine;
using UnityEngine.SceneManagement;public class StartMenu : MonoBehaviour {public Texture2D img;//背景图片private void OnGUI() {/*定义数值参数*/float height = Screen.height * 0.5f;float width = Screen.width * 0.5f;int ButtonHeight = 50;int ButtonWidth = 150;int TitleHeight = 80;int TitleWidth = 200;/*定义字体风格*/GUIStyle tStyle1 = new GUIStyle {fontSize = 40,fontStyle = FontStyle.Bold,};GUIStyle tStyle2 = new GUIStyle {fontSize = 30,fontStyle = FontStyle.Bold,};/*设置背景图片*/GUIStyle BackgroundStyle = new GUIStyle();BackgroundStyle.normal.background = img;/*设置按钮字体风格*/GUIStyle ButtonStyle = new GUIStyle("button");ButtonStyle.fontSize = 20;/*放置标题*/GUI.Label(new Rect(0, 0, Screen.width, Screen.height), "", BackgroundStyle);GUI.Label(new Rect(width - TitleWidth / 2 - TitleWidth / 4, height - TitleHeight * 2, TitleWidth, TitleHeight), "Priests And Devils", tStyle1);GUI.Label(new Rect(width - ButtonWidth / 2, height - TitleHeight , TitleWidth, TitleHeight), "Start Menu", tStyle2);/*开始游戏按钮*/if (GUI.Button(new Rect(width - ButtonWidth / 2 , height + ButtonHeight / 4, ButtonWidth, ButtonHeight), "Start Game", ButtonStyle)) {SceneManager.LoadScene("game", LoadSceneMode.Single);//切换到"game"界面并销毁开始菜单界面}}
}
4.Controller
Director
首先是Director,它是游戏设计中最高层的管理者,采用了单例模式,也就是说游戏中只能有一个Director实例存在。这个比喻非常贴切,无论场景如何变化,都只有一个指挥者来指挥全局,否则就会变得混乱不堪。Director在游戏中扮演了这样的角色,名义上负责管理所有的事务,但实际上它主要负责管理底下的Controller,指挥它们完成各种不同的功能。
/*导演类:掌控着场景的加载、切换等*/public class SSDirector : System.Object{private static SSDirector _instance; //导演类的实例public ISceneController CurrentScenceController { get; set; }public static SSDirector GetInstance(){if (_instance == null){_instance = new SSDirector();}return _instance;}}
Controller
游戏中所有事物的总控制器,其中包含所有游戏所需要的逻辑代码,与控制代码。如创建对象,移动船,移动角色,判定游戏输赢等。
public class Controllor : MonoBehaviour, ISceneController, IUserAction{public LandModel start_land; //开始陆地public LandModel destination; //目标陆地public BoatModel boat; //船private RoleModel[] roles; //角色UserGUI game_GUI; //用户界面public Timer timer; //计时器void Start (){SSDirector.GetInstance().CurrentScenceController = this;//设置导演类的控制器game_GUI = gameObject.AddComponent<UserGUI>() as UserGUI;//挂载用户界面组件timer = gameObject.AddComponent(typeof(Timer)) as Timer;//挂载计时器组件LoadResources();//加载界面资源}/*定义该函数,便于用户界面获得计时器以及其中的时钟变量*/public Timer getTimer(){return timer;}/*创建资源,并将其定义在特定的位置*/public void LoadResources(){//创建水,陆地,角色,船GameObject water = Instantiate(Resources.Load("Perfabs/Water", typeof(GameObject)), new Vector3(0 , -2 , 0), Quaternion.identity) as GameObject;water.name = "water"; //创建陆地:两岸start_land = new LandModel("start");destination = new LandModel("end");//创建船boat = new BoatModel();//创建6个角色roles = new RoleModel[6];//创建3个牧师for (int i = 0; i < 3; ++i){RoleModel role = new RoleModel("priest");role.SetName("priest" + i);role.SetPosition(start_land.GetEmpty());role.MoveToLand(start_land);start_land.AddRoleOnLand(role);roles[i] = role;}//创建3个恶魔for (int i = 0; i < 3; ++i){RoleModel role = new RoleModel("devil");role.SetName("devil" + i);role.SetPosition(start_land.GetEmpty());role.MoveToLand(start_land);start_land.AddRoleOnLand(role);roles[i + 3] = role;}}/*移动船*/public void MoveBoat(){if (boat.IsEmpty() || game_GUI.sign != 0) return;//船为空或者游戏没有在进行的时候,船不能移动boat.BoatMove();game_GUI.sign=Check();//每一次移动触发一次检查}/*移动角色*/public void MoveRole(RoleModel role){if (game_GUI.sign != 0) return;//如果游戏没有在进行则角色不能移动if (role.IsOnBoat()){//当角色在船上时,点击回到船所靠的陆地LandModel land;if (boat.GetBoatSign() == -1)land = destination;elseland = start_land;boat.DeleteRoleByName(role.GetName());//删掉船上的该角色role.Move(land.GetEmpty());role.MoveToLand(land);land.AddRoleOnLand(role);//添加该角色到上岸陆地}else{ LandModel land = role.GetLandModel();if (boat.GetEmptyIndex() == -1 || land.GetLandType() != boat.GetBoatSign()) return; //船没有空位,也不是船停靠的陆地,就不上船land.DeleteRoleByName(role.GetName());role.Move(boat.GetEmpty());role.MoveToBoat(boat);boat.AddRoleOnBoat(role);}game_GUI.sign=Check();//每一次移动触发一次检查}/*重新开始游戏*/public void Restart(){timer.Reset();start_land.Reset();destination.Reset();boat.Reset();for (int i = 0; i < roles.Length; ++i){roles[i].Reset();}}/*检查游戏是否结束*/public int Check(){int start_priest = (start_land.GetRoleNum())[0];int start_devil = (start_land.GetRoleNum())[1];int end_priest = (destination.GetRoleNum())[0];int end_devil = (destination.GetRoleNum())[1];//终点有全部对象,游戏胜利if (end_priest + end_devil == 6){timer.StopTiming();return 2;} //统计岸的一边(包括船和陆地上)魔鬼和牧师各自的数量int[] boat_role_num = boat.GetRoleNumber();if (boat.GetBoatSign() == 1) {start_priest += boat_role_num[0];start_devil += boat_role_num[1];}else{end_priest += boat_role_num[0];end_devil += boat_role_num[1];}//起点存在牧师且魔鬼数量大于牧师,牧师被吃,游戏失败if (start_priest > 0 && start_priest < start_devil){ timer.StopTiming();return 1;}//终点存在牧师且魔鬼数量大于牧师,牧师被吃,游戏失败if (end_priest > 0 && end_priest < end_devil){timer.StopTiming();return 1;}return 0;}
}
脚本
计时器脚本
/*计时器脚本*/public class Timer: MonoBehaviour{public int hour = 0; //小时public int minute = 0; //分钟public int second = 0; //秒public float time = 0f; //总时间public bool timeStop=false; //控制时钟是否暂停void Start(){hour = 0; //小时minute = 0; //分钟second = 0; //秒time = 0f; //总时间timeStop = false; //是否暂停计时}void Update(){//计时if(!timeStop){time += Time.deltaTime;if (time >= 1f){second++;time = 0f;}if (second >= 60){minute++;second = 0;}if (minute >= 60){hour++;minute = 0;}if (hour >= 99){hour = 0;}}}/*暂停计时*/public void StopTiming(){timeStop = true;}/*开始计时*/public void beginTiming(){timeStop = false;}/*重置时间*/public void Reset(){hour = 0; //小时minute = 0; //分钟second = 0; //秒time = 0f;timeStop = false;}}
移动脚本
/*移动脚本*/public class Move : MonoBehaviour{float move_speed = 30; //移动速度int move_sign = 0; //0是不动,1移动Vector3 end_pos; //存储最终位置Vector3 middle_pos; //存储角色从船移动到陆地或者从陆地移动到船上的轨迹转折点的位置。 void Update(){if(move_sign!=0 ){//物体可以移动transform.position = Vector3.MoveTowards(transform.position, middle_pos, move_speed * Time.deltaTime);if( transform.position == end_pos) move_sign=0;//只有当物体到达终点时,物体才停止移动else if (transform.position == middle_pos && middle_pos != end_pos){//转折点不是终点,需要再次移动middle_pos=end_pos;} }}public void MovePosition(Vector3 position){end_pos = position;middle_pos = position;if (position.y < transform.position.y){ //角色从陆地移动到船middle_pos = new Vector3(position.x, transform.position.y, position.z);}else{ //角色从船移动到陆地middle_pos = new Vector3(transform.position.x, position.y, position.z);}move_sign = 1;//设置物体移动}}
鼠标点击脚本
/*鼠标点击脚本*/public class Click : MonoBehaviour{IUserAction action;RoleModel role = null;BoatModel boat = null;public void SetRole(RoleModel role){this.role = role;}public void SetBoat(BoatModel boat){this.boat = boat;}void Start(){action = SSDirector.GetInstance().CurrentScenceController as IUserAction;}void OnMouseDown(){if (boat == null && role == null) return;//被点击的对象为空时返回if (boat != null){//点击的对象为船,触发船进行移动action.MoveBoat();}else if(role != null){//点击的角色为船,触发角色进行移动action.MoveRole(role);}}}}
这篇关于牧师与魔鬼——MVC程序设计游戏实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!