1、编写一个简单的鼠标打飞碟(Hit UFO)游戏

  • 游戏内容要求:
  1. 游戏有 n 个 round,每个 round 都包括10次 trial;
  2. 每个 trial 的飞碟的色彩、大小、发射位置、速度、角度、同时出现的个数都可能不同。它们由该 round 的 ruler 控制;
  3. 每个 trial 的飞碟有随机性,总体难度随 round 上升;
  4. 鼠标点中得分,得分规则按色彩、大小、速度不同计算,规则可自由设定。
  • 游戏的要求:

    • 使用带缓存的工厂模式管理不同飞碟的生产与回收,该工厂必须是场景单例的!具体实现见参考资源 Singleton 模板类
    • 尽可能使用前面 MVC 结构实现人机交互与游戏模型分离
代码 UML 图:

代码说明:
  • 飞碟回收工厂类 DiskFactory.cs:
    这个类主要是用来实现飞碟的资源管理,如生成飞碟、自动回收飞碟等,使用 List 数组存储飞碟对象,方便管理。具体实现如下:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class DiskFactory : System.Object {private List<GameObject> used = new List<GameObject>();private List<GameObject> free = new List<GameObject>();private List<Color> c = new List<Color>();private int count = 0;private int flag;private static DiskFactory _instance;public void GetDisk(int ruler){GameObject disk;if (free.Count == 0){disk = GameObject.Instantiate(Resources.Load<GameObject>("prefabs/disk"));disk.name = "disk" + count;count++;used.Add(disk);}else{disk = free[0];disk.SetActive(true);used.Add(disk);free.RemoveAt(0);}flag = UnityEngine.Random.Range(0, 5);disk.GetComponent<Renderer>().material.color = c[(ruler + flag) % 9];disk.transform.localScale = getSize(ruler);disk.transform.position = getPosition(ruler + flag);DiskData data = disk.GetComponent(typeof(DiskData)) as DiskData;data.speed = ruler + 1;data.target = getTarget(ruler + flag);     }public int GetCount(){return used.Count;}private Vector3 getTarget(int ruler){if (ruler % 2 == 0){return new Vector3(100, 10 + ruler, 60);}return new Vector3(-100, 10+ruler, 60);}private Vector3 getPosition(int ruler){       if(ruler % 2 == 0){return new Vector3(-10, 0, -6);}else{return new Vector3(10, 0, -6);}        }private Vector3 getSize(int r){if(r == 0){return new Vector3(5, 0.1f, 5);}else if(r == 1){return new Vector3(4, 0.1f, 4);}else if (r == 2){return new Vector3(3, 0.1f, 3);}else if (r == 3){return new Vector3(2, 0.1f, 2);}else{return new Vector3(1, 0.1f, 1);}}public static DiskFactory GetInstance(){if (_instance == null){_instance = new DiskFactory();_instance.c.Add(Color.black);_instance.c.Add(Color.blue);_instance.c.Add(Color.cyan);_instance.c.Add(Color.gray);_instance.c.Add(Color.green);_instance.c.Add(Color.magenta);_instance.c.Add(Color.red);_instance.c.Add(Color.white);_instance.c.Add(Color.yellow);}return _instance;}public void FreeDisk(GameObject disk){disk.SetActive(false);free.Add(disk);used.Remove(disk);}// Use this for initializationvoid Start () {}
}
  • 飞碟数据信息 DiskData.cs:
    这个类主要用于记录和设置飞碟的相关属性,挂在飞碟的预制体上即可。具体实现如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class DiskData : MonoBehaviour {public Vector3 target;public float speed;public diskAction action;// Use this for initializationvoid Start () {action = SSDirector.getInstance().currentSceneController as diskAction;}// Update is called once per framevoid Update () {if (Input.GetButtonDown("Fire1")){Debug.Log("Fired Pressed");Debug.Log(Input.mousePosition);Vector3 mp = Input.mousePosition; //get Screen Position//create ray, origin is camera, and direction to mousepointCamera ca = Camera.main; //cam.GetComponent<Camera> ();Ray ray = ca.ScreenPointToRay(Input.mousePosition);//Return the ray's hitRaycastHit hit;if (Physics.Raycast(ray, out hit)){print(hit.transform.gameObject.name);if (hit.collider.gameObject.tag.Contains("Finish")){ //plane tagDebug.Log("hit " + hit.collider.gameObject.name + "!");}//消失action.Boomshakalaka(this.gameObject);}}this.transform.position = Vector3.MoveTowards(this.transform.position, target, speed * Time.deltaTime * 2);if(this.transform.position == target){            action.Drop(this.gameObject);}}
}
  • 回合控制类 RoundController.cs:
    这个类主要负责飞碟回合的控制。具体实现如下:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class RoundController : MonoBehaviour, IsceneController,diskAction {public ScoreRecorder scoreRecorder = new ScoreRecorder();private int trial = 1;private int round = 0;bool game_over = false;bool next_round = false;private DiskFactory df = DiskFactory.GetInstance();private int score;void Awake(){SSDirector director = SSDirector.getInstance();director.setFPS(60);director.currentSceneController = this;director.currentSceneController.LoadResources();}public void LoadResources(){df.GetDisk(round);}public void Boomshakalaka(GameObject disk){      df.FreeDisk(disk);if (df.GetCount() == 0){next_round = true;scoreRecorder.AddScore(round + 1);trial = 1;}       }public void Drop(GameObject disk){trial++;if(trial > 10){game_over = true;df.FreeDisk(disk);}else{df.FreeDisk(disk);df.GetDisk(round);           }}// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {score = scoreRecorder.getScore();}void OnGUI(){GUI.Box(new Rect(Screen.width / 2 - 75, 10, 150, 55), "Round " + (round + 1) + "\nYour score:  " + score + "\nYour trial left:  " + (11-trial));if(next_round){GUI.Window(0, new Rect(Screen.width / 2 - Screen.width / 12, Screen.height / 2 - Screen.height / 12, Screen.width / 6, Screen.height / 6), next_round_window, "Success !");}if (game_over){GUI.Window(0, new Rect(Screen.width / 2 - Screen.width / 12, Screen.height / 2 - Screen.height / 12, Screen.width / 6, Screen.height / 6), game_over_window, "Game Orver!");}}void next_round_window(int id){if (GUI.Button(new Rect(Screen.width / 24, Screen.height / 24 + 5, Screen.width / 12, Screen.height / 12), "Next")){NextRound();}}private void NextRound(){round++;next_round = false;df.GetDisk(round);       }void game_over_window(int id){if (GUI.Button(new Rect(Screen.width / 24, Screen.height / 24 + 5, Screen.width / 12, Screen.height / 12), "Restart")){Debug.Log("Restart");Restart();}}private void Restart(){      trial = 1;round = 0;game_over = false;next_round = false;scoreRecorder.Reset();df.GetDisk(round);}
}
  • 记分员类 ScoreRecorder.cs:
    这个类主要用于飞碟计分工作,包括加分和重置分数,根据飞碟的不同种类进行计分。具体实现如下:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ScoreRecorder : System.Object
{private int score = 0;public void AddScore(int s){score = score + s;}public int getScore(){return score;}public void Reset(){score = 0;}
}
  • 导演类 SSDirector.cs:
    具体实现如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SSDirector : System.Object
{// singlton instanceprivate static SSDirector _instance;public IsceneController currentSceneController { get; set; }public bool running { get; set; }//get instance anytime anywhere!public static SSDirector getInstance(){if (_instance == null){_instance = new SSDirector();}return _instance;}public int getFPS(){return Application.targetFrameRate;}public void setFPS(int fps){Application.targetFrameRate = fps;}
}
  • 用户交互类 Interface.cs:
    这个类主要用于用户交互。具体实现如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public enum Direction { Left, Right, Unfixed};public interface IsceneController
{void LoadResources();
}public interface diskAction
{void Boomshakalaka(GameObject disk);void Drop(GameObject disk);
}

3D游戏编程与设计5——与游戏世界交互相关推荐

  1. 【3D游戏编程与设计】一 游戏的分类与热点

    [3D游戏编程与设计]一 游戏的分类与热点 游戏分类与热点探索 使用思维导图描述游戏的分类.(游戏分类方法特别多) 按游戏设备划分 按游戏参与者类型划分 按网络使用情况划分 按视角维度划分 按人称视角 ...

  2. 【3D游戏编程与设计】四 游戏对象与图形基础 : 构建游戏场景+牧师与魔鬼 动作分离版

    [3D游戏编程与设计]四 游戏对象与图形基础 : 构建游戏场景+牧师与魔鬼 动作分离版 基本操作演练 下载 Fantasy Skybox FREE, 构建自己的游戏场景 下载 Fantasy Skyb ...

  3. 3D游戏编程与设计1——三国杀游戏分析

    文章目录 一.简介 二.玩家 1. 人数 2. 阵营 3. 交互模式 三.规则和目标 1. 通用部分 2. 身份模式 3.国战模式 4.对抗模式(3vs3/欢乐成双/1vs1) 四.资源 五.冲突 六 ...

  4. 【3D游戏编程与设计-HW1】游戏分类与当前热游分析

    游戏分类与当前热游分析 游戏分类与当前热门手游分析 导言 游戏的分类 热点分析 总结 游戏分类与当前热门手游分析 导言 游戏,作为第九艺术,已经逐渐与当今人类生活密不可分.而依靠智能手机为载体的游戏, ...

  5. 3D游戏编程与设计-井字棋

    3D游戏编程与设计-井字棋 目录 3D游戏编程与设计-井字棋 A. 简答题 1. 解释游戏对象(GameObjects)和资源(Assets)的区别与联系 ① 游戏对象 ② 资源 2. 下载几个游戏案 ...

  6. 3D游戏编程与设计作业10

    3D游戏编程与设计作业10 环境说明 Unity3D 导航与寻路 Agent 和 Navmesh 练习 Obstacle和Off-Mesh-Link练习 P&D 过河游戏智能帮助实现 状态图 ...

  7. 3D游戏编程与设计——游戏的本质章节作业与练习

    3D游戏编程与设计--游戏的本质章节作业与练习 18342138 郑卓民 3D游戏编程与设计--游戏的本质章节作业与练习 作业与练习: 游戏名称及简介: 游戏的随机性 游戏的玩法与目标 游戏的冲突 游 ...

  8. 3D 游戏编程与设计:第3次作业

    3D 游戏编程与设计:第3次作业 姓名:韩宝欣 学号:20331013 代码仓库:https://gitee.com/sse_20331013/3d-game.git 文章目录 3D 游戏编程与设计: ...

  9. 3D游戏编程与设计 PD(牧师与恶魔)过河游戏智能帮助实现

    3D游戏编程与设计 P&D(牧师与恶魔) 过河游戏智能帮助实现 文章目录 3D游戏编程与设计 P&D(牧师与恶魔) 过河游戏智能帮助实现 一.作业与练习 二.设计简述 1. 状态图基础 ...

  10. 3D游戏编程与设计作业09

    3D游戏编程与设计作业09 UGUI基础 画布 基础概念 测试渲染模式 UI布局基础 基本概念 锚点练习 UI组件与元素 基本概念 Mask练习 动画练习 富文本测试 简单血条 血条(Health B ...

最新文章

  1. 拥抱高效、拥抱 Bugtags 之来自用户的声音(三)
  2. 22岁复旦大学生拿下深度学习挑战赛冠军:明明可以靠脸吃饭,却偏偏要靠才华
  3. 连接池--在密码修改的影响
  4. python 函数部分
  5. ORACLE 10G DATAGUARD实战步骤(转载)
  6. python对大量数据去重_Python对多属性的重复数据去重实例
  7. 【牛客 - 2B】树(思维,dp,有坑)
  8. Java的接口与继承
  9. 利用request库请求api
  10. Java基础学习总结(55)——java8新特性:stream
  11. “U盘杀手”出现新变种 提醒用户小心谨防
  12. python实现不同图像数据的叠加处理、实现多张图像数据以子图形式组合为新的图像数据【图像叠加、图像组合】
  13. openCV无法打开源文件opencv2\opencv.hpp
  14. python 爬虫系列之极验滑块打码
  15. 通过阿里云容器镜像服务下载谷歌gcr.io镜像
  16. BOM 定时器+回调函数
  17. word删除括号里内容
  18. 南理工计算机专业好吗,吉大计算机or南理工计算机?(江苏考生)
  19. c语言汇编混合编译不了,IAR汇编与C语言混合编程的问题(内附源程序)
  20. 关于TopoJSON以及制作方法

热门文章

  1. dubbo源码分析-dubbo-serialization
  2. Cadence仿真笔记(二):传统noise仿真—共源极的噪声
  3. c语言输出成绩与排名,C语言算成绩 要求输完两个分数后 同时输出两个分数换算出来的成绩...
  4. pycharm 配置虚拟环境 安装虚拟环境
  5. java字段太多会栈溢出_Java内存溢出与栈溢出
  6. 基于微信视频点播小程序系统设计与实现 开题报告
  7. 百度分享在新闻列表页分享多篇文章
  8. Java学习者论坛【申明:来源于网络】
  9. VTK:图形基本操作进阶——连通区域分析
  10. GitGithub 备忘录