unity3D-learnin:Priests and Devils

这是一个关于牧师与魔鬼过河的游戏编程项目,玩家需要帮助3位牧师和3位魔鬼在限定时间内安全渡河。游戏使用C#语言编程,遵循MVC架构,并通过Unity引擎创建了游戏中的各种对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 阅读以下游戏脚本

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!

程序需要满足的要求:

  • play the game ( http://www.flash-game.net/game/2535/priests-and-devils.html )
  • 列出游戏中提及的事物(Objects)
  • 用表格列出玩家动作表(规则表),注意,动作越少越好
  • 请将游戏中对象做成预制
  • 在 GenGameObjects 中创建 长方形、正方形、球 及其色彩代表游戏中的对象。
  • 使用 C# 集合类型 有效组织对象
  • 整个游戏仅 主摄像机 和 一个 Empty 对象, 其他对象必须代码动态生成!!! 。 整个游戏不许出现 Find 游戏对象, SendMessage 这类突破程序结构的 通讯耦合 语句。 违背本条准则,不给分
  • 请使用课件架构图编程,不接受非 MVC 结构程序
  • 注意细节,例如:船未靠岸,牧师与魔鬼上下船运动中,均不能接受用户事件!
  答:游戏中提及的事物(object)有: 牧师Priest),(boat),魔鬼devil),河流(river),河岸River bank)。
      玩家动作表:
        
玩家动作表
                   动作及实现方式                                      条件                                            结果          
左岸上船(点击上船按钮)左岸有人,船在左岸,船员未满船上增加人员,左岸减少人员
右岸上船(点击上船按钮右岸有人,船在右岸,船员未满船上增加人员,右岸减少人员
行船(点击行船按钮)船上有人,船往对岸移动
左岸上岸(点击上岸按钮)船上有人,船在右岸左岸增加人员,船上减少人员
右岸上岸(点击上岸按钮)船上有人,船在左岸右岸增加人员,船上减少人员

  • 请将游戏中对象做成预制

                

                                                            (河流以及行驶在上面的小船)

                

                                                    (绿油油的河岸以及站在上面的牧师妹子和黑暗魔鬼)

  • 在 GenGameObjects 中创建 长方形、正方形、球 及其色彩代表游戏中的对象。
         //设置两岸
        startside = GameObject.CreatePrimitive(PrimitiveType.Cube);
        startside.transform.localScale = startsidescale;
        startside.transform.position = startsidepos;
        startside.GetComponent<Renderer>().material.color = Color.green;
        endside = GameObject.CreatePrimitive(PrimitiveType.Cube);
        endside.transform.localScale = endsidescale;
        endside.transform.position = endsidepos;
        endside.GetComponent<Renderer>().material.color = Color.green;
        //设置船只    
        Boat = GameObject.CreatePrimitive(PrimitiveType.Cube);
        Boat.transform.position = boatStartpos;
        Boat.transform.localScale = boatscale;
        Boat.GetComponent<Renderer>().material.color = Color.grey;
        //设置魔鬼与牧师
        for (int i = 0; i < 3; i++)
        {
            GameObject devil = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            devil.GetComponent<Renderer>().material.color = Color.black;
            devil.transform.position = devilStartpos[i];
            devils.Add(devil);
            GameObject priest = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            priest.transform.position = priestStartpos[i];
            priests.Add(priest);

        }
  • 整个游戏仅 主摄像机 和 一个 Empty 对象, 其他对象必须代码动态生成!!! 。 整个游戏不许出现 Find 游戏对象, SendMessage 这类突破程序结构的 通讯耦合 语句。 违背本条准则,不给分
  • 请使用课件架构图编程,不接受非 MVC 结构程序.

       完整代码在github给出,这里介绍部分代码。

       为了满足MVC结构,写了View.cs,Controller.cs,Models.cs  将两个脚本文件挂在摄像头上和一个挂在空对象上就实现了我们的程序,其中所有的对象都是动态生成,但是没有采用预制。

   void Update()
    {    //前提是船上有人
        if ( flag == true)
        {   //船在左岸开往右岸
            Boat.transform.position = Vector3.MoveTowards(Boat.transform.position, End, boatspeed);
            if (Boat.transform.position == End) flag = false;
        }
        else if (flag == false)
        {  //船在右岸开往左岸
            Boat.transform.position = Vector3.MoveTowards(Boat.transform.position, Start, boatspeed);
            if (Boat.transform.position == Start) falg = true;
        }
    }
    //判断游戏是否可以继续
    public void check()
    {
        //lose check
        print(devilStartside);
        print(devilEndside);
        print(priestStartside);
        print(priestEndside);
        if (state == 0)
        {
            if ((devilStartside > priestStartside && priestStartside != 0) || ((3 - devilStartside) > (3 - priestStartside) && (3 - priestStartside) != 0))
            {
                mygame.wl = WinOrLose.Lose;
                return;
            }
        }
        else if (state == 1)
        {
            if ((devilEndside > priestEndside && priestEndside != 0) || ((3 - devilEndside) > (3 - priestEndside) && (3 - priestEndside) != 0))
            {
                mygame.wl = WinOrLose.Lose;
                return;
            }
        }
        //win check
        if (devilStartside == 0 && priestStartside == 0) mygame.wl = WinOrLose.Win;

    }
 public interface ImyAction
    {
        void DevilGoOnBoat();
        void DevilGoOffBoat();
        void PriestGoOnBoat();
        void PriestGoOffBoat();
        void BoatGo();
        //魔鬼和牧师上下船还有行船五个操作,实现五个函数能够实现动作操作。
    }
完整代码在github上,游戏运行视频上传到百度云,链接在github上,代码参考师兄博客。思路和实现融入了自己的做法。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值