日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

my射飞碟小游戏

發(fā)布時(shí)間:2023/12/3 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 my射飞碟小游戏 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

# 射飛碟游戲 #

### 具體要求如下: ###

假設(shè)有一支槍在攝像機(jī)位置(0,1,-10),在(0,0,0-10-20)放置三個(gè)小球作為距離標(biāo)記,調(diào)整視角直到小球在下中部
將鼠標(biāo)所在平面坐標(biāo),轉(zhuǎn)換為子彈(球體)射出的角度方向。子彈使用物理引擎,初速度恒定。(U3d 坐標(biāo)變換: http://www.cnblogs.com/tekkaman/p/3809409.html )

`Vector3 mp = Input.mousePosition; //get Screen Position
print (mp.ToString());
Vector3 mp1 = cam.camera.ScreenToViewportPoint (mp);
mp1.z = 10; //距攝像頭 10 位置立面
mp1 = cam.camera.ViewportToWorldPoint (mp1);
print (mp1.ToString());`

游戲要分多個(gè) round , 飛碟數(shù)量每個(gè) round 都是 n 個(gè),但色彩,大小;發(fā)射位置,速度,角度,每次發(fā)射數(shù)量按預(yù)定規(guī)則變化。
用戶按空格后,321倒數(shù)3秒,飛碟飛出(物理引擎控制),點(diǎn)擊鼠標(biāo),子彈飛出。飛碟落地,或被擊中,則準(zhǔn)備下一次射擊。

以下是一些技術(shù)要求:
子彈僅需要一個(gè),不用時(shí)處于 deactive 狀態(tài)
飛碟用一個(gè)帶緩存的工廠生產(chǎn),template 中放置預(yù)制的飛碟對(duì)象
程序類圖設(shè)計(jì)大致如下:![](http://ss.sysu.edu.cn/~pml/se347/_images/HitDisk_2.png)

下面是游戲的組件代碼:
###1、GameModel.cs###
????using UnityEngine;
????using System.Collections;
????using System.Collections.Generic;
????using Com.Mygame;

????public class GameModel : MonoBehaviour {
????public float countDown = 3f;??
????public float timeToEmit;??????
????private bool counting;??????
????private bool shooting;?????
?????
????public bool isCounting() {
????????return counting;
????}

????public bool isShooting() {
????????return shooting;
????}?

????private List<GameObject> disks = new List<GameObject>();????// 飛碟對(duì)象列表??
????private List<int> diskIds = new List<int>();?????????????
????private int diskScale;????????
????private Color diskColor;???????????
????private Vector3 emitPosition;??????
????private Vector3 emitDirection;???????
????private float emitSpeed;????????????
????private int emitNumber;??????????????
????private bool emitEnable;???????????

????private SceneController scene;

????void Awake() {
????????scene = SceneController.getInstance();
????????scene.setGameModel(this);
????}

????public void setting(int scale, Color color, Vector3 emitPos, Vector3 emitDir, float speed, int num) {??// 場(chǎng)景設(shè)置
????????diskScale = scale;??// 沒(méi)什么好說(shuō)的,賦值
????????diskColor = color;
????????emitPosition = emitPos;
????????emitDirection = emitDir;
????????emitSpeed = speed;
????????emitNumber = num;
????}

????// 準(zhǔn)備下一次發(fā)射??
????public void prepareToEmitDisk() {
????????if (counting == false && shooting == false) {
????????????timeToEmit = countDown;
????????????emitEnable = true;??// 可發(fā)射變?yōu)檎?/span>
????????}
????}

????// 發(fā)射飛碟??
????void emitDisks() {
????????for (int i = 0; i < emitNumber; ++i) {
????????????diskIds.Add(Factory.getInstance().getDisk());
????????????disks.Add(Factory.getInstance().getDiskObject(diskIds[i]));
????????????disks[i].transform.localScale *= diskScale;
????????????disks[i].transform.position = new Vector3(emitPosition.x, emitPosition.y + i, emitPosition.z);
????????????disks[i].SetActive(true);
????????????disks[i].rigidbody.AddForce(emitDirection * Random.Range(emitSpeed * 5, emitSpeed * 10) / 10, ForceMode.Impulse);
????????}
????}

????// 回收??
????void freeADisk(int i) {
????????Factory.getInstance().free(diskIds[i]);
????????disks.RemoveAt(i);
????????diskIds.RemoveAt(i);
????}

????void FixedUpdate() {
????????if (timeToEmit > 0) {
????????????counting = true;
????????????timeToEmit -= Time.deltaTime;
????????} else {
????????????counting = false;
????????????if (emitEnable == true) {
????????????????emitDisks();
????????????????emitEnable = false;??// 改變之后可發(fā)射變?yōu)榧?/span>
????????????????shooting = true;
????????????}
????????}
????}

????void Update() {
????????for (int i = 0; i < disks.Count; i++) {
????????????if (!disks[i].activeInHierarchy) {
????????????????scene.getJudge().scoreADisk();
????????????????freeADisk(i);
????????????} else if (disks[i].transform.position.y < 0) {
????????????????scene.getJudge().failADisk();
????????????????freeADisk(i);
????????????}
????????}
????????if (disks.Count == 0) {??// 如果不存在飛碟了,停止射擊
????????????shooting = false;
????????}
????}
????}

這個(gè)文件控制的是游戲場(chǎng)景,通過(guò)調(diào)用Factory.getInstance()功能來(lái)獲取飛碟的實(shí)例,當(dāng)飛碟被擊中或者自己落地的時(shí)候,將被freeDisk()函數(shù)回收,并且通過(guò)調(diào)用Judge類中的函數(shù),來(lái)決定分?jǐn)?shù)的獲得和扣除,并以此來(lái)決定是否結(jié)束游戲或者進(jìn)入下一關(guān)卡。FixedUpdate()和Update()函數(shù)通過(guò)bool條件判斷,來(lái)決定是否可以繼續(xù)射擊。

###2、UserInterface###
????using UnityEngine;
????using UnityEngine.UI;
????using System.Collections;
????using Com.Mygame;

????public class UserInterface : MonoBehaviour {
????public Text mainText;
????public Text scoreText;??
????public Text roundText;?
????private int round;??// 判定當(dāng)前回合?
????public GameObject bullet;????????
????public ParticleSystem explosion;???
????public float fireRate = .25f;??????
????public float speed = 500f;?????
????private float nextFireTime;????????
????private IUserInterface userInt;
????private IQueryStatus queryInt;??
????void Start() {??// 游戲開(kāi)始,加載
????????bullet = GameObject.Instantiate(bullet) as GameObject;
????????explosion = GameObject.Instantiate(explosion) as ParticleSystem;
????????userInt = SceneController.getInstance() as IUserInterface;
????????queryInt = SceneController.getInstance() as IQueryStatus;
????}

????void Update() {
????????if (queryInt.isCounting()) {
????????????mainText.text = ((int)queryInt.getEmitTime()).ToString();
????????} else {
????????????if (Input.GetKeyDown("space")) {??// 輸入空格開(kāi)始
????????????????yield return new WaitforSeconds(3f);??// 倒數(shù)3秒后開(kāi)始
????????????????userInt.emitDisk();
????????????}
????????????if (queryInt.isShooting()) {
????????????????mainText.text = "";?????// 隱藏maintext
????????????}
????????????// 發(fā)射子彈??
????????????if (queryInt.isShooting() && Input.GetMouseButtonDown(0) && Time.time > nextFireTime) {
????????????????nextFireTime = Time.time + fireRate;??// 開(kāi)火冷卻時(shí)間
????????????????Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
????????????????bullet.rigidbody.velocity = Vector3.zero;???????????????????????// 重置子彈速度?
????????????????bullet.transform.position = transform.position;??????????????????// 子彈射出??
????????????????bullet.rigidbody.AddForce(ray.direction * speed, ForceMode.Impulse);
????????????????RaycastHit hit;
????????????????if (Physics.Raycast(ray, out hit) && hit.collider.gameObject.tag == "Disk") {
????????????????????explosion.transform.position = hit.collider.gameObject.transform.position;
????????????????????explosion.Play();
????????????????????// 擊中飛碟自動(dòng)回收??
????????????????????hit.collider.gameObject.SetActive(false);
????????????????}
????????????}
????????}
????????roundText.text = "Round: " + queryInt.getRound().ToString();
????????scoreText.text = "Score: " + queryInt.getPoint().ToString();?
????????if (round != queryInt.getRound()) {
????????????round = queryInt.getRound();
????????????mainText.text = "Round " + round.ToString() + " Start";
????????}
????}
????}

該文件創(chuàng)建用戶界面,其中包含著加載、提示、游戲主體等部分。作業(yè)要求中,用戶按空格后,321倒數(shù)3秒,飛碟飛出(物理引擎控制),點(diǎn)擊鼠標(biāo)左鍵,子彈飛出。飛碟落地,或被擊中,則準(zhǔn)備下一次射擊。我用了waitforSeconds函數(shù)來(lái)實(shí)現(xiàn),在等待3秒之后,開(kāi)始飛出飛碟,飛碟落地或被擊中的函數(shù)已經(jīng)在GameModel.cs文件中寫(xiě)出,這里就不再贅述。值得一提的是,剛開(kāi)始我測(cè)試游戲的時(shí)候,子彈的速度會(huì)越來(lái)越快,這是因?yàn)槲覜](méi)有將其清零的緣故,后來(lái)我令其velocity賦值為0來(lái)進(jìn)行清零。上一次的作業(yè)中我的子彈使用的是射線判定,這次則用的是有碰撞體積的物理引擎,這點(diǎn)可以在裝載之后發(fā)現(xiàn)。

###3、SceneController.cs###
????using UnityEngine;
????using System.Collections;
????using Com.Mygame;

????namespace Com.Mygame {
????public interface IUserInterface {??// 場(chǎng)景控制器,是游戲中十分重要的一個(gè)環(huán)節(jié)
????????void emitDisk();
????}

????public interface IQueryStatus {??// 記錄回合和得分
????????bool isCounting();
????????bool isShooting();
????????int getRound();
????????int getPoint();
????????int getEmitTime();
????}

????public interface IJudgeEvent {
????????void nextRound();
????????void setPoint(int point);
????}

????public class SceneController : System.Object, IQueryStatus, IUserInterface, IJudgeEvent {??// 繼承各類
????????private static SceneController _instance;
????????private SceneControllerBC _baseCode;??// 分別為SceneControllerBC、GameModel、Judege
????????private GameModel _gameModel;
????????private Judge _judge;

????????private int _round;
????????private int _point;

????????public static SceneController getInstance() {
????????????if (_instance == null) {
????????????????_instance = new SceneController();??// 返回SceneController的實(shí)例
????????????}
????????????return _instance;
????????}

????????public void setGameModel(GameModel obj) {
????????????_gameModel = obj;
????????}

????????internal GameModel getGameModel() {
????????????return _gameModel;
????????}

????????public void setJudge(Judge obj) {
????????????_judge = obj;
????????}

????????internal Judge getJudge() {
????????????return _judge;
????????}

????????public void setSceneControllerBC(SceneControllerBC obj) {
????????????_baseCode = obj;
????????}
????????internal SceneControllerBC getSceneControllerBC() {
????????????return _baseCode;
????????}

????????public void emitDisk() {
????????????_gameModel.prepareToEmitDisk();
????????}

????????public bool isCounting() {
????????????return _gameModel.isCounting();
????????}

????????public bool isShooting() {
????????????return _gameModel.isShooting();
????????}

????????public int getRound() {
????????????return _round;
????????}

????????public int getPoint() {
????????????return _point;
????????}

????????public int getEmitTime() {
????????????int result = (int)_gameModel.timeToEmit + 1;
????????????return result;
????????}

????????public void setPoint(int point) {
????????????_point = point;
????????}

????????public void nextRound() {
????????????_point = 0; _baseCode.loadRoundData(++_round);
????????}
????}
????}
場(chǎng)景控制器,實(shí)現(xiàn)各種成員變量的返回實(shí)現(xiàn),以及接口定義和保存注入對(duì)象。其中它有兩個(gè)私有變量round和point,分別記錄游戲正在進(jìn)行的回合,以及玩家目前的得分,應(yīng)用在GameModel中實(shí)現(xiàn)。

###4、SceneControllerBC.cs###
????UnityEngine;
????using System.Collections;
????using Com.Mygame;

????public class SceneControllerBC : MonoBehaviour {
????private Color color;
????private Vector3 emitPos;
????private Vector3 emitDir;
????private float speed;

????void Awake() {
????????SceneController.getInstance().setSceneControllerBC(this);
????}

????public void loadRoundData(int round) {??// 關(guān)卡的設(shè)計(jì)
????????switch (round) {
????????????case 1:?????// 第一關(guān)??
????????????????color = Color.green;
????????????????emitPos = new Vector3(-2.5f, 0.2f, -5f);??// 初始條件
????????????????emitDir = new Vector3(24.5f, 40.0f, 67f);
????????????????speed = 4;
????????????????SceneController.getInstance().getGameModel().setting(1, color, emitPos, emitDir.normalized, speed, 1);
????????????????break;
????????????case 2:?????// 第二關(guān)??
????????????????color = Color.red;
????????????????emitPos = new Vector3(2.5f, 0.2f, -5f);
????????????????emitDir = new Vector3(-24.5f, 35.0f, 67f);
????????????????speed = 4;
????????????????SceneController.getInstance().getGameModel().setting(1, color, emitPos, emitDir.normalized, speed, 2);??// 設(shè)定條件
????????????????break;
????????????case 3:?????// 第三關(guān)??
????????????????color = Color.red;
????????????????emitPos = new Vector3(2.5f, 0.2f, -5f);
????????????????emitDir = new Vector3(-24.5f, 35.0f, 67f);??// 飛碟初始位置不變,僅改變速度
????????????????speed = 5;
????????????????SceneController.getInstance().getGameModel().setting(1, color, emitPos, emitDir.normalized, speed, 4);??// 其實(shí)改一下飛碟的速度就可以了
????????????????break;
????????}
????}
????}
此文件為關(guān)卡類的定義,我只做了3個(gè)關(guān)卡,題目要求我:游戲要分多個(gè) round , 飛碟數(shù)量每個(gè) round 都是 n 個(gè),但色彩,大小;發(fā)射位置,速度,角度,每次發(fā)射數(shù)量按預(yù)定規(guī)則變化。因此我沒(méi)有改變飛碟的數(shù)量,位置和角度,從速度入手,越到后面的關(guān)卡越快。函數(shù)Awake()用來(lái)獲取場(chǎng)景控制器實(shí)例,loadRoundData()用來(lái)初始化游戲場(chǎng)景,即定義飛碟的屬性。

###5、Factory.cs###
????using UnityEngine;
????using System.Collections;
????using System.Collections.Generic;
????using Com.Mygame;

????namespace Com.Mygame {
????public class Factory : System.Object {
????????private static Factory _instance;??// 獲取實(shí)例
????????private static List<GameObject> diskList;
????????public GameObject diskTemplate;????????

????????public static Factory getInstance() {??// 工廠實(shí)例
????????????if (_instance == null) {
????????????????_instance = new Factory();
????????????????diskList = new List<GameObject>();
????????????}
????????????return _instance;
????????}

????????public int getDisk() {
????????????for (int i = 0; i < diskList.Count; i++) {
????????????????if (!diskList[i].activeInHierarchy) {??//??判斷是否飛碟空閑
????????????????????return i;?
????????????????}
????????????}
????????????diskList.Add(GameObject.Instantiate(diskTemplate) as GameObject);??// 設(shè)置新的飛碟
????????????return diskList.Count-1;
????????}

????????public void free(int id) {
????????????if (id >= 0 && id < diskList.Count) {??// 判斷是否有效
????????????????diskList[id].rigidbody.velocity = Vector3.zero;??// 速度清零
????????????????diskList[id].transform.localScale = diskTemplate.transform.localScale;
????????????????diskList[id].SetActive(false);??// 活動(dòng)能力重置
????????????}
????????}

????????public GameObject getDiskObject(int id) {
????????????if (id > -1 && id < diskList.Count) {
????????????????return diskList[id];
????????????}
????????????return null;
????????}

????????
????}
????}

????public class DiskFactoryBC : MonoBehaviour {??// 另一個(gè)類BC
????public GameObject disk;
????void Awake() {
????????Factory.getInstance().diskTemplate = disk;??// 用于獲取飛碟實(shí)例
????}
????}
題目要求:飛碟用一個(gè)帶緩存的工廠生產(chǎn),template 中放置預(yù)制的飛碟對(duì)象。這個(gè)就是飛碟工廠,管理飛碟實(shí)例,同時(shí)對(duì)外屏蔽飛碟實(shí)例的的提取和回收細(xì)節(jié)。它定義了獲取、回收飛碟的功能,并且由于是public函數(shù),可以被其他函數(shù)直接調(diào)用,來(lái)管理、應(yīng)用飛碟。

###6、Judge.cs###
????using UnityEngine;
????using System.Collections;
????using Com.Mygame;

????public class Judge : MonoBehaviour {??// 判定得分失分,能否進(jìn)入下一回合
????public int oneDiskScore = 10;
????public int oneDiskFail = 10;
????public int disksToWin = 4;
????private SceneController scene;

????void Awake() {
????????scene = SceneController.getInstance();
????????scene.setJudge(this);??// 設(shè)定規(guī)則
????}

????void Start() {
????????scene.nextRound();??// 第一關(guān)??
????}
?
????public void scoreADisk()??// 得分
????{
????????scene.setPoint(scene.getPoint() + oneDiskScore);
????????if (scene.getPoint() == disksToWin * oneDiskScore) {
????????????scene.nextRound();
????????}
????}

????public void failADisk() {??// 失分
????????scene.setPoint(scene.getPoint() - oneDiskFail);??// 判定條件,飛碟掉落
????}
????}
用以判斷得失分的類,當(dāng)符合得分條件時(shí),調(diào)用得分函數(shù);當(dāng)符合失分條件時(shí)(即飛碟未擊中而掉落時(shí)),調(diào)用失分函數(shù)。當(dāng)?shù)梅址蠗l件時(shí),進(jìn)入下一回合。


*題目條件:有一支槍在攝像機(jī)位置(0,1,-10),在(0,0,0-10-20)放置三個(gè)小球作為距離標(biāo)記,調(diào)整視角直到小球在下中部。*
部件設(shè)定:
攝像機(jī):# 射飛碟游戲 #

### 具體要求如下: ###

假設(shè)有一支槍在攝像機(jī)位置(0,1,-10),在(0,0,0-10-20)放置三個(gè)小球作為距離標(biāo)記,調(diào)整視角直到小球在下中部
將鼠標(biāo)所在平面坐標(biāo),轉(zhuǎn)換為子彈(球體)射出的角度方向。子彈使用物理引擎,初速度恒定。(U3d 坐標(biāo)變換: http://www.cnblogs.com/tekkaman/p/3809409.html )

`Vector3 mp = Input.mousePosition; //get Screen Position
print (mp.ToString());
Vector3 mp1 = cam.camera.ScreenToViewportPoint (mp);
mp1.z = 10; //距攝像頭 10 位置立面
mp1 = cam.camera.ViewportToWorldPoint (mp1);
print (mp1.ToString());`

游戲要分多個(gè) round , 飛碟數(shù)量每個(gè) round 都是 n 個(gè),但色彩,大小;發(fā)射位置,速度,角度,每次發(fā)射數(shù)量按預(yù)定規(guī)則變化。
用戶按空格后,321倒數(shù)3秒,飛碟飛出(物理引擎控制),點(diǎn)擊鼠標(biāo),子彈飛出。飛碟落地,或被擊中,則準(zhǔn)備下一次射擊。

以下是一些技術(shù)要求:
子彈僅需要一個(gè),不用時(shí)處于 deactive 狀態(tài)
飛碟用一個(gè)帶緩存的工廠生產(chǎn),template 中放置預(yù)制的飛碟對(duì)象
程序類圖設(shè)計(jì)大致如下:![](http://ss.sysu.edu.cn/~pml/se347/_images/HitDisk_2.png)

下面是游戲的組件代碼:
###1、GameModel.cs###
????using UnityEngine;
????using System.Collections;
????using System.Collections.Generic;
????using Com.Mygame;

????public class GameModel : MonoBehaviour {
????public float countDown = 3f;??
????public float timeToEmit;??????
????private bool counting;??????
????private bool shooting;?????
?????
????public bool isCounting() {
????????return counting;
????}

????public bool isShooting() {
????????return shooting;
????}?

????private List<GameObject> disks = new List<GameObject>();????// 飛碟對(duì)象列表??
????private List<int> diskIds = new List<int>();?????????????
????private int diskScale;????????
????private Color diskColor;???????????
????private Vector3 emitPosition;??????
????private Vector3 emitDirection;???????
????private float emitSpeed;????????????
????private int emitNumber;??????????????
????private bool emitEnable;???????????

????private SceneController scene;

????void Awake() {
????????scene = SceneController.getInstance();
????????scene.setGameModel(this);
????}

????public void setting(int scale, Color color, Vector3 emitPos, Vector3 emitDir, float speed, int num) {??// 場(chǎng)景設(shè)置
????????diskScale = scale;??// 沒(méi)什么好說(shuō)的,賦值
????????diskColor = color;
????????emitPosition = emitPos;
????????emitDirection = emitDir;
????????emitSpeed = speed;
????????emitNumber = num;
????}

????// 準(zhǔn)備下一次發(fā)射??
????public void prepareToEmitDisk() {
????????if (counting == false && shooting == false) {
????????????timeToEmit = countDown;
????????????emitEnable = true;??// 可發(fā)射變?yōu)檎?/span>
????????}
????}

????// 發(fā)射飛碟??
????void emitDisks() {
????????for (int i = 0; i < emitNumber; ++i) {
????????????diskIds.Add(Factory.getInstance().getDisk());
????????????disks.Add(Factory.getInstance().getDiskObject(diskIds[i]));
????????????disks[i].transform.localScale *= diskScale;
????????????disks[i].transform.position = new Vector3(emitPosition.x, emitPosition.y + i, emitPosition.z);
????????????disks[i].SetActive(true);
????????????disks[i].rigidbody.AddForce(emitDirection * Random.Range(emitSpeed * 5, emitSpeed * 10) / 10, ForceMode.Impulse);
????????}
????}

????// 回收??
????void freeADisk(int i) {
????????Factory.getInstance().free(diskIds[i]);
????????disks.RemoveAt(i);
????????diskIds.RemoveAt(i);
????}

????void FixedUpdate() {
????????if (timeToEmit > 0) {
????????????counting = true;
????????????timeToEmit -= Time.deltaTime;
????????} else {
????????????counting = false;
????????????if (emitEnable == true) {
????????????????emitDisks();
????????????????emitEnable = false;??// 改變之后可發(fā)射變?yōu)榧?/span>
????????????????shooting = true;
????????????}
????????}
????}

????void Update() {
????????for (int i = 0; i < disks.Count; i++) {
????????????if (!disks[i].activeInHierarchy) {
????????????????scene.getJudge().scoreADisk();
????????????????freeADisk(i);
????????????} else if (disks[i].transform.position.y < 0) {
????????????????scene.getJudge().failADisk();
????????????????freeADisk(i);
????????????}
????????}
????????if (disks.Count == 0) {??// 如果不存在飛碟了,停止射擊
????????????shooting = false;
????????}
????}
????}

這個(gè)文件控制的是游戲場(chǎng)景,通過(guò)調(diào)用Factory.getInstance()功能來(lái)獲取飛碟的實(shí)例,當(dāng)飛碟被擊中或者自己落地的時(shí)候,將被freeDisk()函數(shù)回收,并且通過(guò)調(diào)用Judge類中的函數(shù),來(lái)決定分?jǐn)?shù)的獲得和扣除,并以此來(lái)決定是否結(jié)束游戲或者進(jìn)入下一關(guān)卡。FixedUpdate()和Update()函數(shù)通過(guò)bool條件判斷,來(lái)決定是否可以繼續(xù)射擊。

###2、UserInterface###
????using UnityEngine;
????using UnityEngine.UI;
????using System.Collections;
????using Com.Mygame;

????public class UserInterface : MonoBehaviour {
????public Text mainText;
????public Text scoreText;??
????public Text roundText;?
????private int round;??// 判定當(dāng)前回合?
????public GameObject bullet;????????
????public ParticleSystem explosion;???
????public float fireRate = .25f;??????
????public float speed = 500f;?????
????private float nextFireTime;????????
????private IUserInterface userInt;
????private IQueryStatus queryInt;??
????void Start() {??// 游戲開(kāi)始,加載
????????bullet = GameObject.Instantiate(bullet) as GameObject;
????????explosion = GameObject.Instantiate(explosion) as ParticleSystem;
????????userInt = SceneController.getInstance() as IUserInterface;
????????queryInt = SceneController.getInstance() as IQueryStatus;
????}

????void Update() {
????????if (queryInt.isCounting()) {
????????????mainText.text = ((int)queryInt.getEmitTime()).ToString();
????????} else {
????????????if (Input.GetKeyDown("space")) {??// 輸入空格開(kāi)始
????????????????yield return new WaitforSeconds(3f);??// 倒數(shù)3秒后開(kāi)始
????????????????userInt.emitDisk();
????????????}
????????????if (queryInt.isShooting()) {
????????????????mainText.text = "";?????// 隱藏maintext
????????????}
????????????// 發(fā)射子彈??
????????????if (queryInt.isShooting() && Input.GetMouseButtonDown(0) && Time.time > nextFireTime) {
????????????????nextFireTime = Time.time + fireRate;??// 開(kāi)火冷卻時(shí)間
????????????????Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
????????????????bullet.rigidbody.velocity = Vector3.zero;???????????????????????// 重置子彈速度?
????????????????bullet.transform.position = transform.position;??????????????????// 子彈射出??
????????????????bullet.rigidbody.AddForce(ray.direction * speed, ForceMode.Impulse);
????????????????RaycastHit hit;
????????????????if (Physics.Raycast(ray, out hit) && hit.collider.gameObject.tag == "Disk") {
????????????????????explosion.transform.position = hit.collider.gameObject.transform.position;
????????????????????explosion.Play();
????????????????????// 擊中飛碟自動(dòng)回收??
????????????????????hit.collider.gameObject.SetActive(false);
????????????????}
????????????}
????????}
????????roundText.text = "Round: " + queryInt.getRound().ToString();
????????scoreText.text = "Score: " + queryInt.getPoint().ToString();?
????????if (round != queryInt.getRound()) {
????????????round = queryInt.getRound();
????????????mainText.text = "Round " + round.ToString() + " Start";
????????}
????}
????}

該文件創(chuàng)建用戶界面,其中包含著加載、提示、游戲主體等部分。作業(yè)要求中,用戶按空格后,321倒數(shù)3秒,飛碟飛出(物理引擎控制),點(diǎn)擊鼠標(biāo)左鍵,子彈飛出。飛碟落地,或被擊中,則準(zhǔn)備下一次射擊。我用了waitforSeconds函數(shù)來(lái)實(shí)現(xiàn),在等待3秒之后,開(kāi)始飛出飛碟,飛碟落地或被擊中的函數(shù)已經(jīng)在GameModel.cs文件中寫(xiě)出,這里就不再贅述。值得一提的是,剛開(kāi)始我測(cè)試游戲的時(shí)候,子彈的速度會(huì)越來(lái)越快,這是因?yàn)槲覜](méi)有將其清零的緣故,后來(lái)我令其velocity賦值為0來(lái)進(jìn)行清零。上一次的作業(yè)中我的子彈使用的是射線判定,這次則用的是有碰撞體積的物理引擎,這點(diǎn)可以在裝載之后發(fā)現(xiàn)。

###3、SceneController.cs###
????using UnityEngine;
????using System.Collections;
????using Com.Mygame;

????namespace Com.Mygame {
????public interface IUserInterface {??// 場(chǎng)景控制器,是游戲中十分重要的一個(gè)環(huán)節(jié)
????????void emitDisk();
????}

????public interface IQueryStatus {??// 記錄回合和得分
????????bool isCounting();
????????bool isShooting();
????????int getRound();
????????int getPoint();
????????int getEmitTime();
????}

????public interface IJudgeEvent {
????????void nextRound();
????????void setPoint(int point);
????}

????public class SceneController : System.Object, IQueryStatus, IUserInterface, IJudgeEvent {??// 繼承各類
????????private static SceneController _instance;
????????private SceneControllerBC _baseCode;??// 分別為SceneControllerBC、GameModel、Judege
????????private GameModel _gameModel;
????????private Judge _judge;

????????private int _round;
????????private int _point;

????????public static SceneController getInstance() {
????????????if (_instance == null) {
????????????????_instance = new SceneController();??// 返回SceneController的實(shí)例
????????????}
????????????return _instance;
????????}

????????public void setGameModel(GameModel obj) {
????????????_gameModel = obj;
????????}

????????internal GameModel getGameModel() {
????????????return _gameModel;
????????}

????????public void setJudge(Judge obj) {
????????????_judge = obj;
????????}

????????internal Judge getJudge() {
????????????return _judge;
????????}

????????public void setSceneControllerBC(SceneControllerBC obj) {
????????????_baseCode = obj;
????????}
????????internal SceneControllerBC getSceneControllerBC() {
????????????return _baseCode;
????????}

????????public void emitDisk() {
????????????_gameModel.prepareToEmitDisk();
????????}

????????public bool isCounting() {
????????????return _gameModel.isCounting();
????????}

????????public bool isShooting() {
????????????return _gameModel.isShooting();
????????}

????????public int getRound() {
????????????return _round;
????????}

????????public int getPoint() {
????????????return _point;
????????}

????????public int getEmitTime() {
????????????int result = (int)_gameModel.timeToEmit + 1;
????????????return result;
????????}

????????public void setPoint(int point) {
????????????_point = point;
????????}

????????public void nextRound() {
????????????_point = 0; _baseCode.loadRoundData(++_round);
????????}
????}
????}
場(chǎng)景控制器,實(shí)現(xiàn)各種成員變量的返回實(shí)現(xiàn),以及接口定義和保存注入對(duì)象。其中它有兩個(gè)私有變量round和point,分別記錄游戲正在進(jìn)行的回合,以及玩家目前的得分,應(yīng)用在GameModel中實(shí)現(xiàn)。

###4、SceneControllerBC.cs###
????UnityEngine;
????using System.Collections;
????using Com.Mygame;

????public class SceneControllerBC : MonoBehaviour {
????private Color color;
????private Vector3 emitPos;
????private Vector3 emitDir;
????private float speed;

????void Awake() {
????????SceneController.getInstance().setSceneControllerBC(this);
????}

????public void loadRoundData(int round) {??// 關(guān)卡的設(shè)計(jì)
????????switch (round) {
????????????case 1:?????// 第一關(guān)??
????????????????color = Color.green;
????????????????emitPos = new Vector3(-2.5f, 0.2f, -5f);??// 初始條件
????????????????emitDir = new Vector3(24.5f, 40.0f, 67f);
????????????????speed = 4;
????????????????SceneController.getInstance().getGameModel().setting(1, color, emitPos, emitDir.normalized, speed, 1);
????????????????break;
????????????case 2:?????// 第二關(guān)??
????????????????color = Color.red;
????????????????emitPos = new Vector3(2.5f, 0.2f, -5f);
????????????????emitDir = new Vector3(-24.5f, 35.0f, 67f);
????????????????speed = 4;
????????????????SceneController.getInstance().getGameModel().setting(1, color, emitPos, emitDir.normalized, speed, 2);??// 設(shè)定條件
????????????????break;
????????????case 3:?????// 第三關(guān)??
????????????????color = Color.red;
????????????????emitPos = new Vector3(2.5f, 0.2f, -5f);
????????????????emitDir = new Vector3(-24.5f, 35.0f, 67f);??// 飛碟初始位置不變,僅改變速度
????????????????speed = 5;
????????????????SceneController.getInstance().getGameModel().setting(1, color, emitPos, emitDir.normalized, speed, 4);??// 其實(shí)改一下飛碟的速度就可以了
????????????????break;
????????}
????}
????}
此文件為關(guān)卡類的定義,我只做了3個(gè)關(guān)卡,題目要求我:游戲要分多個(gè) round , 飛碟數(shù)量每個(gè) round 都是 n 個(gè),但色彩,大小;發(fā)射位置,速度,角度,每次發(fā)射數(shù)量按預(yù)定規(guī)則變化。因此我沒(méi)有改變飛碟的數(shù)量,位置和角度,從速度入手,越到后面的關(guān)卡越快。函數(shù)Awake()用來(lái)獲取場(chǎng)景控制器實(shí)例,loadRoundData()用來(lái)初始化游戲場(chǎng)景,即定義飛碟的屬性。

###5、Factory.cs###
????using UnityEngine;
????using System.Collections;
????using System.Collections.Generic;
????using Com.Mygame;

????namespace Com.Mygame {
????public class Factory : System.Object {
????????private static Factory _instance;??// 獲取實(shí)例
????????private static List<GameObject> diskList;
????????public GameObject diskTemplate;????????

????????public static Factory getInstance() {??// 工廠實(shí)例
????????????if (_instance == null) {
????????????????_instance = new Factory();
????????????????diskList = new List<GameObject>();
????????????}
????????????return _instance;
????????}

????????public int getDisk() {
????????????for (int i = 0; i < diskList.Count; i++) {
????????????????if (!diskList[i].activeInHierarchy) {??//??判斷是否飛碟空閑
????????????????????return i;?
????????????????}
????????????}
????????????diskList.Add(GameObject.Instantiate(diskTemplate) as GameObject);??// 設(shè)置新的飛碟
????????????return diskList.Count-1;
????????}

????????public void free(int id) {
????????????if (id >= 0 && id < diskList.Count) {??// 判斷是否有效
????????????????diskList[id].rigidbody.velocity = Vector3.zero;??// 速度清零
????????????????diskList[id].transform.localScale = diskTemplate.transform.localScale;
????????????????diskList[id].SetActive(false);??// 活動(dòng)能力重置
????????????}
????????}

????????public GameObject getDiskObject(int id) {
????????????if (id > -1 && id < diskList.Count) {
????????????????return diskList[id];
????????????}
????????????return null;
????????}

????????
????}
????}

????public class DiskFactoryBC : MonoBehaviour {??// 另一個(gè)類BC
????public GameObject disk;
????void Awake() {
????????Factory.getInstance().diskTemplate = disk;??// 用于獲取飛碟實(shí)例
????}
????}
題目要求:飛碟用一個(gè)帶緩存的工廠生產(chǎn),template 中放置預(yù)制的飛碟對(duì)象。這個(gè)就是飛碟工廠,管理飛碟實(shí)例,同時(shí)對(duì)外屏蔽飛碟實(shí)例的的提取和回收細(xì)節(jié)。它定義了獲取、回收飛碟的功能,并且由于是public函數(shù),可以被其他函數(shù)直接調(diào)用,來(lái)管理、應(yīng)用飛碟。

###6、Judge.cs###
????using UnityEngine;
????using System.Collections;
????using Com.Mygame;

????public class Judge : MonoBehaviour {??// 判定得分失分,能否進(jìn)入下一回合
????public int oneDiskScore = 10;
????public int oneDiskFail = 10;
????public int disksToWin = 4;
????private SceneController scene;

????void Awake() {
????????scene = SceneController.getInstance();
????????scene.setJudge(this);??// 設(shè)定規(guī)則
????}

????void Start() {
????????scene.nextRound();??// 第一關(guān)??
????}
?
????public void scoreADisk()??// 得分
????{
????????scene.setPoint(scene.getPoint() + oneDiskScore);
????????if (scene.getPoint() == disksToWin * oneDiskScore) {
????????????scene.nextRound();
????????}
????}

????public void failADisk() {??// 失分
????????scene.setPoint(scene.getPoint() - oneDiskFail);??// 判定條件,飛碟掉落
????}
????}
用以判斷得失分的類,當(dāng)符合得分條件時(shí),調(diào)用得分函數(shù);當(dāng)符合失分條件時(shí)(即飛碟未擊中而掉落時(shí)),調(diào)用失分函數(shù)。當(dāng)?shù)梅址蠗l件時(shí),進(jìn)入下一回合。


*題目條件:有一支槍在攝像機(jī)位置(0,1,-10),在(0,0,0-10-20)放置三個(gè)小球作為距離標(biāo)記,調(diào)整視角直到小球在下中部。*
部件設(shè)定:
攝像機(jī):
飛碟:

子彈:
標(biāo)記球:

1、

2、

3、

最后在攝像機(jī)上掛載文件就可以了。一個(gè)射飛碟的小游戲就做好了。

總結(jié)

以上是生活随笔為你收集整理的my射飞碟小游戏的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。