1.编写一个简单的鼠标打飞碟(Hit UFO)游戏
游戏内容要求:
1.游戏有n个round,每个round都包括10次trial;
2.每个trial的飞碟的色彩、大小、发射位置、速度、角度、同时出现的个数都可能不同。它们由
3.该round的ruler控制;
4.每个trial的飞碟有随机性,总体难度随round上升;
5.鼠标点中得分,得分规则按色彩、大小、速度不同计算,规则可自由设定。
游戏的要求:
1.使用带缓存的工厂模式管理不同飞碟的生产与回收,该工厂必须是场景单实例的!具体实现见参考资源Singleton模板类
2.尽可能使用前面MVC结构实现人机交互与游戏模型分离
规则如下,共有两种飞盘,一种快一种慢,击中较快的飞盘可以得更高的分数。没有时间限制,当遗漏超过5个飞盘时游戏结束。
脚本文件如下:
其中多数为MVC架构的基本代码,以及Singleton单实例模板代码,我们不做展示,展示其中几个。
1.Disk Disk类就三个数据,类型分数与颜色
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Disk : MonoBehaviour {
public int type = 1;
public int score = 1;
public Color color = Color.green;
}
2.Disk Factory类:飞盘工厂,判断时新建飞盘还是从空闲列表中选取。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DiskFactory : MonoBehaviour{
public static DiskFactory DiskFact;
private List<Disk> BusyDisks = new List<Disk>();
private List<Disk> FreeDisks = new List<Disk>();
private DiskFactory(){}
public GameObject GetDisk(int type) {
GameObject disk_prefab = null;
if (FreeDisks.Count > 0) {//如果空闲飞盘不为空则选取一个加入
for(int i = 0; i < FreeDisks.Count; i++) {
if (FreeDisks[i].type == type) {
disk_prefab = FreeDisks[i].gameObject;
FreeDisks.Remove(FreeDisks[i]);
break;
}
}
}
if(disk_prefab == null) {//如果没有空闲飞盘则创建一个
if(type == 1) {
disk_prefab = GameObject.Instantiate(
Resources.Load<GameObject>("Prefabs/disk1"),
new Vector3(0, -10f, 0), Quaternion.identity);
}
else if (type == 2) {
disk_prefab = GameObject.Instantiate(
Resources.Load<GameObject>("Prefabs/disk2"),
new Vector3(0, -10f, 0), Quaternion.identity);
}
else {
disk_prefab = GameObject.Instantiate(
Resources.Load<GameObject>("Prefabs/disk3"),
new Vector3(0, -10f, 0), Quaternion.identity);
}
disk_prefab.GetComponent<Renderer>().material.color = disk_prefab.GetComponent<Disk>().color;
}
BusyDisks.Add(disk_prefab.GetComponent<Disk>());
disk_prefab.SetActive(true);
return disk_prefab;
}
public bool FreeUsedDisks() {
bool ret = true;
for(int i = 0; i < BusyDisks.Count; i++) {
if (BusyDisks[i].gameObject.transform.position.y + 10f > -0.2f && BusyDisks[i].gameObject.transform.position.y + 10f < 0.2f){
Debug.Log("1");
BusyDisks[i].gameObject.transform.position = new Vector3(0, -20f, 0);
ret =false;
}
else if (BusyDisks[i].gameObject.transform.position.y <= -15f) {
FreeDisks.Add(BusyDisks[i]);
BusyDisks.Remove(BusyDisks[i]);
}
}
return ret;
}
public void Reset() {
FreeUsedDisks();
}
}
3.First Controller类:空白物体上附加的脚本,初始启动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstController : MonoBehaviour, ISceneController, IUserAction {
public DiskFactory disk_factory;
public FlyActionManager action_manager;
public ScoreRecorder score_recorder;
public UserGUI user_gui;
private int round = 1;
private int trial = 0;
int count = 0;
private bool running = false;
private int HP=5;
void Start () {
SSDirector director = SSDirector.GetInstance();
director.CurrentScenceController = this;
disk_factory = Singleton<DiskFactory>.Instance;
score_recorder = Singleton<ScoreRecorder>.Instance;
action_manager = gameObject.AddComponent<FlyActionManager>() as FlyActionManager;
user_gui = gameObject.AddComponent<UserGUI>() as UserGUI;
}
void Update () {
if(running) {
count++;
if (Input.GetButtonDown("Fire1")) {
Vector3 pos = Input.mousePosition;
Hit(pos);
}
switch (round) {//根据round随机生成飞盘
case 1: {
if (count >= 600) {
count = 0;
SendDisk(trial % 2 + 1);
if(Random.Range(1,3) == 1)
SendDisk(1);
if (trial >= 10) {
round += 1;
trial = 0;
}
trial += 1;
}
break;
}
case 2: {
if (count >= 600) {
if (trial == 11) {
running = false;
}
count = 0;
if(trial <= 9){
SendDisk(Random.Range(1,3));
}
if(Random.Range(1,2) == 1){
SendDisk(Random.Range(1,3));
}
if(Random.Range(1,3) == 1){
SendDisk(Random.Range(1,3));
}
trial += 1;
}
break;
}
default:
break;
}
bool ret = disk_factory.FreeUsedDisks();
if(!ret){
HP--;
if(HP == 0){
GameOver();
}
}
}
}
public void LoadResources() {}
private void SendDisk(int type) {//发射飞盘
GameObject disk = disk_factory.GetDisk(type);
float disk_y = Random.Range(0f, 3f);
float disk_x = Random.Range(-1f, 1f) < 0 ? -1 : 1;
float speed = 0;
float angle = Random.Range(15f, 25f);
if (type == 1) {
speed = Random.Range(0.5f, 1f);
}
else if (type == 2) {
speed = Random.Range(1.5f, 2f);
}
disk.transform.position = new Vector3(disk_x * 14f, disk_y, 0);
action_manager.DiskFly(disk, angle, speed);
}
public void Hit(Vector3 pos) {//检测击中
// Debug.Log("2");
Ray ray = Camera.main.ScreenPointToRay(pos);
RaycastHit[] hits;
hits = Physics.RaycastAll(ray);
for (int i = 0; i < hits.Length; i++) {
RaycastHit hit = hits[i];
if (hit.collider.gameObject.GetComponent<Disk>() != null) {
score_recorder.RecordScore(hit.collider.gameObject);
hit.collider.gameObject.transform.position = new Vector3(0, -15, 0);
}
}
}
public void Restart() {
running = true;
score_recorder.Reset();
disk_factory.Reset();
round = 1;
trial = 0;
HP = 5;
}
public void GameOver() {
running = false;
}
public int GetHP(){
return HP;
}
public float GetScore() {
return score_recorder.GetScore();
}
public int GetRound() {
return round;
}
public int GetTrial() {
return trial;
}
public bool isEnd(){
return ((round == 2 && trial == 11) || HP <= 0);
}
}
4.Score Recorder类:计分
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScoreRecorder : MonoBehaviour {
public static ScoreRecorder scoreR;
private float score;
private ScoreRecorder(){}
void Start () {
score = 0;
}
public void RecordScore(GameObject disk) {
score += disk.GetComponent<Disk>().score;
}
public float GetScore() {
return score;
}
public void Reset() {
score = 0;
}
}
5.DiskFlyingAction类:设计飞行路线
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DiskFlyAction : SSAction {
public float gravity = -0.05f;
private Vector3 start_velocity;
private Vector3 delta_velocity = Vector3.zero;
private Vector3 current_angle = Vector3.zero;
private float time;
private DiskFlyAction() { }
public static DiskFlyAction GetSSAction(int lor, float angle, float speed) {
DiskFlyAction action = CreateInstance<DiskFlyAction>();
if (lor == -1) {
action.start_velocity = Quaternion.Euler(new Vector3(0, 0, -angle)) * Vector3.left * speed;
}
else {
action.start_velocity = Quaternion.Euler(new Vector3(0, 0, angle)) * Vector3.right * speed;
}
return action;
}
public override void Update() {
time += Time.fixedDeltaTime;
delta_velocity.y = gravity * time;
transform.position += (start_velocity + delta_velocity) * Time.fixedDeltaTime;
current_angle.z = Mathf.Atan((start_velocity.y + delta_velocity.y) / start_velocity.x) * Mathf.Rad2Deg;
transform.eulerAngles = current_angle;
if (this.transform.position.y < -10) {
this.enable = false;
}
}
public override void Start() { }
}
源代码链接:3D: 用于存放3D游戏编程课程作业的源代码 - Gitee.com
演示gif如下:
游戏截图: