完整游戏过程可见以下视频:
/
完整代码可见以下仓库:
游戏对象 | 对象类型 |
---|---|
Priest 牧师 | Cube |
Devil 魔鬼 | Sphere |
Boat 船 | Cube |
River 河流 | Cube |
Coast 河岸 | Cube |
动作 | 触发事件 |
---|---|
点击 Character 人物 | 人物上船,下船 |
点击 Boat 船只 | 船只过河 |
事件 | 结果 |
---|---|
其中一边恶魔数量大于牧师(包括船上和岸上的) | 玩家失败 |
所有牧师和恶魔都到了另一岸 | 玩家成功 |
基于职责设计,使用面向对象技术设计游戏。
项目的程序代码采用MVC结构,根据功能将代码文件分别放入Controllers, Models, Views文件夹中。
// 得到 GameControllervoid Start () {status = 0;action = Director.GetInstance().CurrentSecnController as IUserAction;}
void OnGUI () {textStyle = new GUIStyle {fontSize = 40,alignment = TextAnchor.MiddleCenter};hintStyle = new GUIStyle {fontSize = 15,fontStyle = FontStyle.Normal};btnStyle = new GUIStyle("button") {fontSize = 30};// 打印标题和规则提示信息GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 195, 100, 50), "Priest And Devil", textStyle);GUI.Label(new Rect(Screen.width / 2 - 400, Screen.height / 2 - 125, 100, 50), "White Cube: Periestn Red Sphere: Devilnn" + rule, hintStyle);// 打印游戏结果和重开游戏的按钮if (status == 1) {// LoseGUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 85, 100, 50), "You Lose!", textStyle);if (GUI.Button(new Rect(Screen.width / 2 - 70, Screen.height / 2, 140, 70), "Restart", btnStyle)) {status = 0;action.Restart();}} else if (status == 2) {// WinGUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 85, 100, 50), "You Win!", textStyle);if (GUI.Button(new Rect(Screen.width / 2 - 70, Screen.height / 2, 140, 70), "Restart", btnStyle)) {status = 0;action.Restart();}}}
void OnMouseDown() {if (gameObject.name == "boat") {action.MoveBoat();} else {action.CharacterClicked(characterCtrl);}}
public class Director : System.Object{// Singlton instance.private static Director instance;public ISceneController CurrentSecnController { get; set; }// get instance anytime anywhere!public static Director GetInstance(){if (instance == null){instance = new Director();}return instance;}public int getFPS(){return Application.targetFrameRate;}public void setFPS(int fps){Application.targetFrameRate = fps;}}
public interface ISceneController{void LoadResources();}
public interface IUserAction {void MoveBoat();void CharacterClicked(CharacterController characterCtrl);void Restart();}
public void SetDestination(Vector3 pos) {destination = pos;middle = pos;if (pos.y == transform.position.y) { // 移动船status = 2;} else if (pos.y < transform.position.y) { // 将角色从岸上移到船上middle.y = transform.position.y;} else { // 将角色从船上移到岸上middle.x = transform.position.x;}status = 1;}
void Awake() {Director director = Director.GetInstance();director.CurrentSecnController = this;userGUI = gameObject.AddComponent<UserGUI>() as UserGUI;characters = new MyNamespace.CharacterController[6];LoadResources();}
private int CheckGameOver() {int rightPriest = 0;int rightDevil = 0;int leftPriest = 0;int leftDevil = 0;int status = 0;rightPriest += rightCoastCtrl.GetCharacterNum()[0];rightDevil += rightCoastCtrl.GetCharacterNum()[1];leftPriest += leftCoastCtrl.GetCharacterNum()[0];leftDevil += leftCoastCtrl.GetCharacterNum()[1];// Winif (leftPriest + leftDevil == 6) {status = 2; }if (boatCtrl.boat.Location == Location.right) {rightPriest += boatCtrl.GetCharacterNum()[0];rightDevil += boatCtrl.GetCharacterNum()[1];} else {leftPriest += boatCtrl.GetCharacterNum()[0];leftDevil += boatCtrl.GetCharacterNum()[1];}// Loseif ((rightPriest < rightDevil && rightPriest > 0) ||(leftPriest < leftDevil && leftPriest > 0)) {status = 1;}return status;}
完整游戏过程可见以下视频:
/
本文发布于:2024-01-27 18:54:58,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17063528972008.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |