牧师与魔鬼

游戏介绍

三个牧师与三个魔鬼想要过河,但是一条船只能同时坐两个人,并且如果一边岸上的魔鬼数量大于牧师(船上的牧师或者魔鬼不算在岸上),那么牧师就会被魔鬼杀死,游戏结束。要想所有人都安全过河,该怎么办呢?

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!

游戏实现框架

玩家行为     效果条件

点击角色

(牧师/魔鬼)

上船/下船上船时船上须有空位,船靠近相应的岸边
点击船船移动到对岸船上至少有一个角色(船不能为空)

依然采用MVC框架

model模型包括船boat,角色role(牧师和魔鬼),河岸bank,移动move(用于控制角色和船的运动)

view视图则有GUI,显示游戏失败或者胜利的字样。

controller控制器有boatCtrl,roleCtrl,bankCtrl,moveCtrl,FirstController,SSDirector等。

其中,最关键的是FirstController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirstController : MonoBehaviour, ISceneController, IUserAction {
    ShoreCtrl leftShoreController, rightShoreController;
    River river;
    BoatCtrl boatController;
    RoleCtrl[] roleControllers;
    MoveCtrl moveController;
    bool isRunning;
    float time;

    public void LoadResources() {
        //role
        roleControllers = new RoleCtrl[6];
        for (int i = 0; i < 6; ++i) {
            roleControllers[i] = new RoleCtrl();
            roleControllers[i].CreateRole(Position.role_shore[i], i < 3 ? true : false, i);
        }

        //shore
        leftShoreController = new ShoreCtrl();
        leftShoreController.CreateShore(Position.left_shore);
        leftShoreController.GetShore().shore.name = "left_shore";
        rightShoreController = new ShoreCtrl();
        rightShoreController.CreateShore(Position.right_shore);
        rightShoreController.GetShore().shore.name = "right_shore";

        //将人物添加并定位至左岸  
        foreach (RoleCtrl roleController in roleControllers)
        {
            roleController.GetRoleModel().role.transform.localPosition = leftShoreController.AddRole(roleController.GetRoleModel());
        }
        //boat
        boatController = new BoatCtrl();
        boatController.CreateBoat(Position.left_boat);

        //river
        river = new River(Position.river);

        //move
        moveController = new MoveCtrl();

        isRunning = true;
        time = 60;


    }

    public void MoveBoat() {
        if (isRunning == false || moveController.GetIsMoving()) return;
        if (boatController.GetBoatModel().isRight) {
            moveController.SetMove(Position.left_boat, boatController.GetBoatModel().boat);
        }
        else {
            moveController.SetMove(Position.right_boat, boatController.GetBoatModel().boat);
        }
        boatController.GetBoatModel().isRight = !boatController.GetBoatModel().isRight;
    }

    public void MoveRole(Role roleModel) {
        if (isRunning == false || moveController.GetIsMoving()) return;
        if (roleModel.inBoat) {
            if (boatController.GetBoatModel().isRight) {
                moveController.SetMove(rightShoreController.AddRole(roleModel), roleModel.role);
            }
            else {
                moveController.SetMove(leftShoreController.AddRole(roleModel), roleModel.role);
            }
            roleModel.onRight = boatController.GetBoatModel().isRight;
            boatController.RemoveRole(roleModel);
        }
        else {
            if (boatController.GetBoatModel().isRight == roleModel.onRight) {
                if (roleModel.onRight) {
                    rightShoreController.RemoveRole(roleModel);
                }
                else {
                    leftShoreController.RemoveRole(roleModel);
                }
                moveController.SetMove(boatController.AddRole(roleModel), roleModel.role);
            }
        }
    }

    public void Check() {
        if (isRunning == false) return;
        this.gameObject.GetComponent<UserGUI>().gameMessage = "";
        if (rightShoreController.GetShore().priestCount == 3) {
            this.gameObject.GetComponent<UserGUI>().gameMessage = "You Win!";
            isRunning = false;
        }
        else {
            int leftPriestCount, rightPriestCount, leftDevilCount, rightDevilCount;
            leftPriestCount = leftShoreController.GetShore().priestCount ;
            rightPriestCount = rightShoreController.GetShore().priestCount;
            leftDevilCount = leftShoreController.GetShore().devilCount ;
            rightDevilCount = rightShoreController.GetShore().devilCount;
            if (leftPriestCount != 0 && leftPriestCount < leftDevilCount || rightPriestCount != 0 && rightPriestCount < rightDevilCount) {
                this.gameObject.GetComponent<UserGUI>().gameMessage = "Game Over!";
                isRunning = false;
            }
        }
    }

    void Awake() {
        SSDirector.GetInstance().CurrentSceneController = this;
        LoadResources();
        this.gameObject.AddComponent<UserGUI>();
    }

    void Update() {
        if (isRunning) {
            time -= Time.deltaTime;
            this.gameObject.GetComponent<UserGUI>().time = (int)time;
            if (time <= 0) {
                this.gameObject.GetComponent<UserGUI>().time = 0;
                this.gameObject.GetComponent<UserGUI>().gameMessage = "Game Over!";
                isRunning = false;
            }
        }
    }
}

这段代码实现了ISceneControllerIUserAction两个接口,其中ISceneController是游戏场景控制器需要实现的接口,IUserAction是用户交互操作需要实现的接口。

该控制器包含了以下功能:

  • LoadResources():加载游戏资源,包括创建角色、岸边、船只、河流等,并初始化游戏状态。
  • MoveBoat():移动船只的方法,根据船只的当前位置进行移动,并更新船只的位置状态。
  • MoveRole():移动角色的方法,根据角色的当前位置和状态进行移动操作,包括将角色从岸边移动到船上或从船上移动到岸边。
  • Check():检查游戏胜负条件,判断游戏是否胜利或失败,并更新游戏消息和状态。
  • Awake():在对象被创建时调用的方法,用于设置当前场景控制器、加载资源和添加用户界面。
  • Update():在每一帧更新时调用的方法,用于计时和判断游戏是否结束。

这段代码主要用于控制游戏逻辑和状态的更新,具体的实际操作和显示则通过其他组件和对象来完成,例如角色控制器、岸边控制器、船只控制器等,这里略过。

游戏演示视频

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值