做一个简单的打飞碟游戏
简介
就是一个简单的打飞碟游戏。。游戏难度会随着你获得的分数提升,胜利条件是获得1000分。
对象处理
就简单的一个飞碟预制就行了(十分简单,就一个圆柱体。。完毕)
还有一个粒子系统用来模拟飞碟被击中时的爆炸效果,粒子的参数如下:
就这么简单。。没了
UML
代码部分
其实这个飞碟的代码跟之前的那个牧师与魔鬼的代码十分的相似,主要是由于老师提供的框架可塑性实在是太强大了(膜拜一下老师)。只需要增加一个记分员,一个飞碟工厂,再加上之前的框架,就搞定了。
Singleton.cs(单例模型的模板)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Singleton<T> where T : MonoBehaviour
{private static T instance;public static T Instance {get {if (instance == null) {instance = (T)Object.FindObjectOfType(typeof(T));if (instance == null) {Debug.LogError("Can't find instance of " + typeof(T));}}return instance;}}
}
Director.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Director : System.Object {public static Director _instance;public ISceneController currentSceneController { get; set; }public bool running { get; set; }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;}
}
DiskData.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class DiskData : MonoBehaviour {public float size;public Color color;public float speed;
}
ScoreRecorder.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ScoreRecorder : MonoBehaviour {private float score;public float getScore() {return score;}public void Record(GameObject disk) {score += (100 - disk.GetComponent<DiskData>().size *(20 - disk.GetComponent<DiskData>().speed));Color c = disk.GetComponent<DiskData>().color;switch (c.ToString()) {case "red":score += 50;break;case "green":score += 40;break;case "blue":score += 30;break;case "yellow":score += 10;break;}}public void Reset() {score = 0;}
}
DiskFactory.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class DiskFactory : MonoBehaviour {private List<GameObject> used = new List<GameObject>();private List<GameObject> free = new List<GameObject>();private Color[] color = { Color.red, Color.green, Color.blue, Color.yellow };public GameObject GetDisk(int ruler) {GameObject a_disk;if (free.Count > 0) {a_disk = free[0];free.Remove(free[0]);} else {a_disk = GameObject.Instantiate(Resources.Load("prefabs/Disk")) as GameObject;}switch (ruler) {case 1:a_disk.GetComponent<DiskData>().size = UnityEngine.Random.Range(0, 6);a_disk.GetComponent<DiskData>().color = color[UnityEngine.Random.Range(0, 4)];a_disk.GetComponent<DiskData>().speed = UnityEngine.Random.Range(10, 15);a_disk.transform.localScale = new Vector3(a_disk.GetComponent<DiskData>().size * 2, a_disk.GetComponent<DiskData>().size * 0.1f, a_disk.GetComponent<DiskData>().size * 2);a_disk.GetComponent<Renderer>().material.color = a_disk.GetComponent<DiskData>().color;break;case 2:a_disk.GetComponent<DiskData>().size = UnityEngine.Random.Range(0, 4);a_disk.GetComponent<DiskData>().color = color[UnityEngine.Random.Range(0, 4)];a_disk.GetComponent<DiskData>().speed = UnityEngine.Random.Range(15, 20);a_disk.transform.localScale = new Vector3(a_disk.GetComponent<DiskData>().size * 2, a_disk.GetComponent<DiskData>().size * 0.1f, a_disk.GetComponent<DiskData>().size * 2);a_disk.GetComponent<Renderer>().material.color = a_disk.GetComponent<DiskData>().color;break;}a_disk.SetActive(true);used.Add(a_disk);return a_disk;}public void FreeDisk(GameObject disk) {for(int i = 0; i < used.Count; i++) {if(used[i] == disk) {disk.SetActive(false);used.Remove(used[i]);free.Add(disk);}}}
}
RoundController.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public enum State { WIN, LOSE, PAUSE, CONTINUE, START };public class RoundController : MonoBehaviour, IUserAction, ISceneController {public DiskFactory diskFactory;public RoundActionManager actionManager;public ScoreRecorder scoreRecorder;private List<GameObject> disks;private int round;private GameObject shootAtSth;GameObject explosion;public State state { get; set; }public int leaveSeconds;public int count;IEnumerator DoCountDown() {while (leaveSeconds >= 0) {yield return new WaitForSeconds(1);leaveSeconds--;}}void Awake() {Director director = Director.getInstance();director.setFPS(60);director.currentSceneController = this;LoadResources();diskFactory = Singleton<DiskFactory>.Instance;scoreRecorder = Singleton<ScoreRecorder>.Instance;actionManager = Singleton<RoundActionManager>.Instance;leaveSeconds = 60;count = leaveSeconds;state = State.PAUSE;disks = new List<GameObject>();}void Start () {round = 1;LoadResources();}void Update() {LaunchDisk();Judge();RecycleDisk();}public void LoadResources() {Camera.main.transform.position = new Vector3(0, 0, -15);explosion = Instantiate(Resources.Load("Prefabs/ParticleSys"), new Vector3(-40, 0, 0), Quaternion.identity) as GameObject;}public void shoot() {if (Input.GetMouseButtonDown(0) && (state == State.START || state == State.CONTINUE)) {Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;if (Physics.Raycast(ray, out hit)) {if ((Director.getInstance().currentSceneController.state == State.START || Director.getInstance().currentSceneController.state == State.CONTINUE)) {shootAtSth = hit.transform.gameObject;explosion.transform.position = hit.collider.gameObject.transform.position;explosion.GetComponent<Renderer>().material = hit.collider.gameObject.GetComponent<Renderer>().material;explosion.GetComponent<ParticleSystem>().Play();}}}}public void LaunchDisk() {if(count - leaveSeconds == 1) {count = leaveSeconds;GameObject disk = diskFactory.GetDisk(round);Debug.Log(disk);disks.Add(disk);actionManager.addRandomAction(disk);}}public void RecycleDisk() {for(int i = 0; i < disks.Count; i++) {if( disks[i].transform.position.z < -18) {diskFactory.FreeDisk(disks[i]);disks.Remove(disks[i]);}}}public void Judge() {if(shootAtSth != null && shootAtSth.transform.tag == "Disk" && shootAtSth.activeInHierarchy) {scoreRecorder.Record(shootAtSth);diskFactory.FreeDisk(shootAtSth);shootAtSth = null;}if(scoreRecorder.getScore() > 500 * round) {round++;leaveSeconds = count = 60;}if (round == 3) {StopAllCoroutines ();state = State.WIN;} else if (leaveSeconds == 0 && scoreRecorder.getScore () < 500 * round) {StopAllCoroutines ();state = State.LOSE;} else state = State.CONTINUE;}public void Pause() {state = State.PAUSE;StopAllCoroutines();for (int i = 0; i < disks.Count; i++) disks[i].SetActive(false);}public void Resume() {StartCoroutine(DoCountDown());state = State.CONTINUE;for (int i = 0; i < disks.Count; i++) disks[i].SetActive(true);}public void Restart() {scoreRecorder.Reset();Application.LoadLevel(Application.loadedLevelName);Director.getInstance().currentSceneController.state = State.START;}
}
RoundActionManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class RoundActionManager : SSActionManager, ISSActionCallback {public RoundController scene;public MoveToAction action1, action2;float speed;public void addRandomAction(GameObject gameObj) {int[] X = { -20, 20 };int[] Y = { -5, 5 };int[] Z = { -20, -20 };Vector3 startPos = new Vector3(UnityEngine.Random.Range(-20, 20),UnityEngine.Random.Range(-5, 5),UnityEngine.Random.Range(50, 10));gameObj.transform.position = startPos;Vector3 randomTarget = new Vector3(X[UnityEngine.Random.Range(0, 2)],Y[UnityEngine.Random.Range(0, 2)],Z[UnityEngine.Random.Range(0, 2)]);MoveToAction action = MoveToAction.GetSSAction(randomTarget, gameObj.GetComponent<DiskData>().speed);RunAction(gameObj, action, this);}protected void Start() {scene = (RoundController)Director.getInstance().currentSceneController;scene.actionManager = this;}protected new void Update() {base.Update();}public void SSActionEvent(SSAction source,SSActionEventType events = SSActionEventType.Completed,int intParam = 0,string strParam = null,Object objectParam = null) {}
}
UserGUI.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public interface IUserAction {void shoot();
}public class UserGUI : MonoBehaviour {private IUserAction action;private float width, height;private string countDownTitle;void Start() {countDownTitle = "Start";action = Director.getInstance().currentSceneController as IUserAction;}float castw(float scale) {return (Screen.width - width) / scale;}float casth(float scale) {return (Screen.height - height) / scale;}void OnGUI() {width = Screen.width / 12;height = Screen.height / 12;GUI.Label(new Rect(castw(2f)+20, casth(6f) - 20, 50, 50), ((RoundController)Director.getInstance().currentSceneController).leaveSeconds.ToString());GUI.Button(new Rect(580, 10, 80, 30), ((RoundController)Director.getInstance().currentSceneController).scoreRecorder.getScore().ToString());if (Director.getInstance().currentSceneController.state != State.WIN && Director.getInstance().currentSceneController.state != State.LOSE&& GUI.Button(new Rect(10, 10, 80, 30), countDownTitle)) {if (countDownTitle == "Start") {countDownTitle = "Pause";Director.getInstance().currentSceneController.Resume();} else {countDownTitle = "Start";Director.getInstance().currentSceneController.Pause();}}if (Director.getInstance().currentSceneController.state == State.WIN) {if (GUI.Button(new Rect(castw(2f), casth(6f), width, height), "Win!")) Director.getInstance().currentSceneController.Restart();} else if (Director.getInstance().currentSceneController.state == State.LOSE) {if (GUI.Button(new Rect(castw(2f), casth(6f), width, height), "Lose!")) Director.getInstance().currentSceneController.Restart();}}void Update() {action.shoot();}
}
剩下的就跟之前的牧师与魔鬼的代码一样啦。
这里最最最重要的是(红笔五角星),要调整脚本的调用顺序。否则会出错的。
没啦,就这么多了。希望玩得愉快
做一个简单的打飞碟游戏相关推荐
- 用pygame做一个简单的python小游戏---贪吃蛇
用pygame做一个简单的python小游戏-贪吃蛇 贪吃蛇游戏博客链接:(方法一样,语言不一样) c++贪吃蛇:https://blog.csdn.net/weixin_46791942/artic ...
- 用pygame做一个简单的python小游戏---七彩同心圆
用pygame做一个简单的python小游戏-七彩同心圆 这个小游戏原是我同学python课的课后作业,并不是很难,就简单实现了一下,顺便加强一下pygame库的学习. 玩法:每次点击鼠标时,会以鼠标 ...
- 用pygame做一个简单的python小游戏---生命游戏
用pygame做一个简单的python小游戏-生命游戏 生命游戏(Game of Life) 生命游戏(Game of Life)是剑桥大学约翰·何顿·康威(John Horton Conway)教授 ...
- python七彩同心圆_用pygame做一个简单的python小游戏---七彩同心圆
用pygame做一个简单的python小游戏---七彩同心圆 用pygame做一个简单的python小游戏-七彩同心圆 这个小游戏原是我同学python课的课后作业,并不是很难,就简单实现了一下,顺便 ...
- 做一个简单的java小游戏--单机版五子棋
做一个简单的java小游戏–单机版五子棋 学了java有一段时间了,今天就来搞一个简单的单机版五子棋游戏. 实现功能:那必须能进行基础的输赢判断.还有重新开始的功能,悔棋的功能,先手设置的功能和退出的 ...
- 做一个简单的java小游戏--贪吃蛇
做一个简单的java小游戏–贪吃蛇 贪吃蛇游戏博客链接:(方法一样,语言不一样) c++贪吃蛇:https://blog.csdn.net/weixin_46791942/article/detail ...
- python做一个简单的对战游戏
今天没什么事情,就自己做了一个简单的对战游戏. 这个小游戏流程大概是这样的 开始游戏 ↓ 选择人物出场顺序及技能 ↓ ← ← ← ← ← ← ← ← ← ← ↖ 开始战斗→胜利方→+ ...
- 基于C++如何使用EGE做一个简单的坦克大战游戏
作为一个C语言刚入门的小萌新,学完C语言基础语法后就迫不及待想要自己做一个游戏出来了,然后想到了小时候插卡玩的坦克大战,于是做了一个黑框框版的简单坦克大战,后面学了一点EGE图形库就在黑框框版的坦克大 ...
- 做一个简单的塔防游戏
1.设置一个Create Empty 挂上三个脚本取名GameControlScript.CreatTowerScript.CreatEnemyScripts (1).GameControlScrip ...
最新文章
- Solaris10 for x86网卡替换配置
- 通信系统之信道(二)
- 20年老码农分享20条编程经验,你pick哪些?
- jSearch(聚搜) v0.5.0 发布,多项更新和体验优化
- nginx中的rewrite用法及实例
- session already invalidate
- java文件读写操作大全
- Java线程阻塞原语-LockSupport
- 中国移动研究院人力群面
- 计算机应用基础文字处理测试题,国家开放大学《计算机应用基础》考试与答案形考任务模块2Word2010文字处理系统—客观题测验答案.docx...
- 不知不觉,到51cto一年了!
- 文件传输工具rzsz
- MPC5744p时钟模块
- python随机森林回归_从零实现回归随机森林
- 2018华东师范软件复试机试
- 宝物志分享:那些具有潜力的彩宝收藏品种
- 爬取百大弹幕,大家还是喜欢上罗老师的课!
- 利用计算机实施盗窃罪300万,盗窃网络虚拟财产的新定性及刑法规制.pdf
- [职场全攻略] 【职场攻略】看透“潜”职场规则
- 电路习题解答 第五章 5-5、5-6