3D游戏编程——牧师和恶魔

阅读: 评论:0

3D游戏编程——牧师和恶魔

3D游戏编程——牧师和恶魔

简答题

游戏对象运动的本质

游戏对象的运动过程实际就是游戏对象随着时间进行空间位置、旋转角度、大小的变化。
游戏对象的运动过程本质上是使用矩阵变换(平移、旋转、缩放)改变游戏对象的空间属性。

抛物线运动

请用三种方法以上方法,实现物体的抛物线运动。

方法一:使用物体的重力属性

public class usegravity : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){gameObject.AddComponent<Rigidbody>();// 给一个初始速度gameObject.GetComponent<Rigidbody>().velocity = new Vector3(1, 0, 0);}
}

方法二:使用transform.position

主要使用的是公式
y = x 2 y=x^2 y=x2

public class usegravity : MonoBehaviour
{public Vector3 initial;// Start is called before the first frame updatevoid Start(){initial = transform.position;}// Update is called once per framevoid Update(){transform.position -= new Vector3(Time.deltaTime, Time.deltaTime * Time.deltaTime + 2 * initial.y * Time.deltaTime, 0);}
}

方法三:使用anslate

与方法二类似。

public class usegravity : MonoBehaviour
{public Vector3 initial;// Start is called before the first frame updatevoid Start(){initial = transform.position;}// Update is called once per framevoid Update(){initial.x -= 0.1f;transform.Translate(Time.deltaTime * new Vector3(initial.x, initial.x*initial.x, 0), Space.World);}
}

太阳系模拟

写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。

public class sunround : MonoBehaviour {public Transform sun;public int x, y, z, speed;// Use this for initializationvoid Start () {speed = Random.Range(150, 200);x = Random.Range(-50, 50);y = Random.Range(-50, 50);z = Random.Range(-50, 50);}// Update is called once per framevoid Update () {var axis = new Vector3(x, y, z);transform.RotateAround(sun.position, axis, speed * Time.deltaTime);}
}

Priests and Devils

完整工程文件在github()。

游戏视频

游戏中的事物

牧师,恶魔,船,河流,两岸

其中牧师是红色方块,恶魔是蓝色球体,船是棕色长方体。


但是这样的游戏界面实在太过于单调,所以经过我的修改,最终游戏界面如下图所示,其中,水面还利用正弦函数增加了波浪效果。

规则表

当前状态玩家操作结果
牧师或恶魔在岸边,船上有空位玩家点击靠近船一侧岸上的恶魔或牧师恶魔或牧师上船
牧师或恶魔在船上玩家点击船上的恶魔或牧师恶魔或牧师上靠近船的岸
其中一侧的恶魔的数量大于牧师的数量——显示玩家输
恶魔和牧师全部到河边另一侧——显示玩家赢

游戏对象

灰色长方体——堤岸

棕色长方体——船

蓝色长方体——河

红色正方体——牧师

蓝色球体——恶魔

设计过程

游戏框架如下图所示

Director职责如下所示:

  • 获取当前游戏的场景
  • 控制场景运行、切换、入栈与出栈
  • 暂停、恢复、退出
  • 管理游戏全局状态
  • 设定游戏的配置
  • 设定游戏全局视图
public class Director : System.Object {private static Director _instance;             //导演类的实例public SceneController scene { get; set; }public static Director GetInstance(){if (_instance == null){_instance = new Director();}return _instance;}
}

Director类是单实例的,具有全局属性,可以在任何地方访问它,它继承至 C# 根对象,所以不会受 Unity 引擎管理,也不要加载。它通过一个抽象的场景接口访问不同场的场记(控制器)。例如:它不知道每个场景需要加载哪些资源,它可以与实现 SceneController 的不同场记对话。

controller

虽然在本次项目中,我们只创建了一个场景控制器FirstController,但是为了设计的完整性,我们还是需要定义一个场记控制器接口SceneController

public interface SceneController
{void LoadResources();           //加载场景void print();                   // 打印 测试用int getPosition(string name);   // 获取位置void moveto(string name);		// 移动void Reset();	                // reset
}

我们实现SceneController接口,创建了FirstController,并挂载到一个空物体上,便于初始化加载。该场景控制器首先需要加载游戏中的资源,并进行各种初始化。在此之前,我们先要将游戏对象做成预制,便于我们的直接使用。

然后加载到场景中。

   public void LoadResources(){Debug.Log("load resource");state = -1;// 创建新的GUImygui = new UserGUI();// 加载数量fromDevilNum = fromPriestNum = 3;toDevilNum = toPriestNum = 0;// 加载地面GroundFrom = Object.Instantiate(Resources.Load("ground", typeof(GameObject)), new Vector3(0, -5, 15), Quaternion.identity, null) as GameObject;GroundFrom.name = "groundfrom";GroundTo = Object.Instantiate(Resources.Load("ground", typeof(GameObject)), new Vector3(0, -5, -15), Quaternion.identity, null) as GameObject;GroundTo.name = "groundto";// 加载水Water = Object.Instantiate(Resources.Load("Water", typeof(GameObject)), new Vector3(0, -9, 0), Quaternion.identity, null) as GameObject;Water.name = "water";// 加载船boat = new Boat();// 加载牧师对象for (int i = 0;i < 3;i ++){Role temp = new Role("Cube");temp.setName("Priest"+ i);Priests[i] = temp;float p_z = (float)(8 + i * 1.5);Priests[i].setOriginalPos(new Vector3(0, 0, p_z));Priests[i].setDestPos(new Vector3(0, 0, -p_z));}// 加载恶魔对象for (int i = 0; i < 3; i++){Role temp = new Role("Sphere");temp.setName("Devil" + i);Devils[i] = temp;float p_z = (float)(13 + i * 1.5);Devils[i].setOriginalPos(new Vector3(0, 0, p_z));Devils[i].setDestPos(new Vector3(0, 0, -p_z));}}

作为场记类,FirstController还需要响应各种事件。在本游戏中需要响应的事件为:鼠标点击、游戏重置、检测游戏输赢。游戏点击的事件触发是通过Move类来通知的。Move类实际上是一个检测类,挂载到所有可以移动的游戏对象上,用来检测是否有鼠标点击事件。如果鼠标点击了某个可移动的游戏对象,该类将告知场记,然后场记再交给相应的处理程序。

public class Move : MonoBehaviour {void OnMouseDown(){//Debug.Log("onmousedown");Director.GetInstance().veto(transform.name);}// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}
}

model

而本游戏中的事物如船、牧师、恶魔等,便是MVC模式中的model了。我们创建一个Role类和Boat类来作为相应的模型类,用来记录每个事物的状态等。

public class Role {public GameObject character;public Move moveto;public int position;    // 0:from 1:to 2:boatpublic int boatPos;     // 0 1public Vector3 origin;public Vector3 dest;public Role(string name){position = 0;character = Object.Instantiate(Resources.Load(name, typeof(GameObject)), , Quaternion.identity, null) as GameObject;moveto = character.AddComponent(typeof(Move)) as Move;}public void setName(string name){character.name = name;}public void getBoat(int i){position = 2;if (i == 1){boatPos = ansform.position = new Vector3(0, -3, 4);}else if(i == 2){boatPos = ansform.position = new Vector3(0, -3, 6);}else if(i == 3){boatPos = ansform.position = new Vector3(0, -3, -4);}else{boatPos = ansform.position = new Vector3(0, -3, -6);}}public void setOriginalPos(Vector3 a){ansform.position = a;origin = a;}public void setDestPos(Vector3 a){dest = a;}public void MoveToOrigin(){position = ansform.position = origin;}public void MoveToDest(){position = ansform.position = dest;}}public class Boat{public int[] BoatState;  // 记录船上的载荷位置public int num;         // 记录载荷public int position;        // 记录船的位置:0在from的位置,1在to的位置public GameObject thisboat;public Move moveto;public Boat(){num = 0;position = 0;BoatState = new int[2];BoatState[0] = BoatState[1] = 0;thisboat = Object.Instantiate(Resources.Load("Boat", typeof(GameObject)), new Vector3(0, -4, 5), Quaternion.identity, null) as GameObject;thisboat.name = "boat";moveto = thisboat.AddComponent(typeof(Move)) as Move;}
}

view

场景类的接口是IUserAction

public interface IUserAction 
{void display();void setLose();void setWin();
}

UserGUI实现了该场景类的接口,用来作为GUI显示button、label等部件。同时通过跟controller的交互传递信息,改变状态。

public class UserGUI : MonoBehaviour, IUserAction {private int state;public UserGUI(){state = -1;}public void setWin(){state = 1;}public void setLose(){state = 0;}public void display(){OnGUI();}void OnGUI(){GUIStyle style = new GUIStyle{border = new RectOffset(10, 10, 10, 10),fontSize = 50,fontStyle = FontStyle.BoldAndItalic,};// normal:Rendering settings for when the component is displayed al.textColor = new Color(200 / 255f, 180 / 255f, 150 / 255f);    // 需要除以255,因为范围是0-1GUI.Label(new Rect(240, 10, 200, 80), "Priests and Devils", style);if (GUI.Button(new Rect(50, 10, 100, 50), "Reset")){Director.GetInstance().scene.Reset();}if (state == 0){Color = new Color(255 / 255f, 0 / 255f, 0 / 255f);GUI.Label(new Rect(330, 350, 200, 80), "You Lose!", style);}if (state == 1){Color = new Color(255 / 255f, 0 / 255f, 0 / 255f);GUI.Label(new Rect(340, 350, 200, 80), "You Win!", style);}}
}

基本的框架搭建完毕后,再在此基础上进行游戏逻辑的设计(游戏的动作、输赢逻辑等)就简单多了,在此不做赘述,详细见github()。

实现效果如下所示:

本文发布于:2024-01-29 17:15:53,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170651975516967.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:牧师   恶魔   游戏
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23