使用unity实现《鼠标打飞碟(Hit UFO)》小游戏

本文介绍了中山大学软件工程学院2023年的3D游戏编程作业,详细讲述了游戏规则、实现原理的UML图、文件结构以及关键的裁判类、飞碟工厂和飞行动作类代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

这是中山大学软件工程学院2023年3D游戏编程与设计的第六次作业,也欢迎大家学习参考交流
github个人主页: innitinnit (github.com)

目录

前言

目录

游戏要求/规则

游戏实现原理UML图

游戏文件结构

游戏关键代码

游戏画面


游戏要求/规则

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

游戏实现原理UML图

游戏文件结构

游戏关键代码

裁判类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Judger : MonoBehaviour
{
    private int score;
    private int miss;
    void Start()
    {
        score = 0;
        miss = 0;
    }
    public int getScore()
    {
        return score;
    }
    public bool checkGame()
    {
        if (miss >= 10)
        {
            return false;
        }
        return true;
    }

    public void hit(GameObject disk)
    {
        if (disk.GetComponent<DiskInfo>().color == Color.white)
        {
            score += 1;
        }
        else if (disk.GetComponent<DiskInfo>().color == Color.red)
        {
            score += 2;
        }
        else
        {
            score += 3;
        }
    }
    public void Miss()
    {
        Debug.Log("Miss one!");
        miss += 1;
    }
    public void restart()
    {
        miss = 0;
        score = 0;

    }
}

飞碟工厂

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DiskFactory : MonoBehaviour
{
    public GameObject disk = null;
    private List<DiskInfo> activeList = new List<DiskInfo>();
    private List<DiskInfo> freeList = new List<DiskInfo>();
    int rand1 = 6, rand2 = 9, rand3 = 13;
    public int difficulty = 0;
    public GameObject GetDisk(int round)
    {
        GameObject newDisk = null;
        if (freeList.Count > 0)
        {
            newDisk = freeList[0].gameObject;
            freeList.Remove(freeList[0]);
        }
        else
        {
            newDisk = Instantiate(Resources.Load<GameObject>("Prefabs/Disk"), Vector3.zero, Quaternion.identity);

            newDisk.AddComponent<DiskInfo>();
        }

        switch (round)
        {
            case 1:
                {
                    difficulty = Random.Range(0, rand1);
                    break;
                }
            case 2:
                {
                    difficulty = Random.Range(0, rand2);
                    break;
                }
            case 3:
                {
                    difficulty = Random.Range(0, rand3);
                    break;
                }
        }

        if (difficulty < rand1 - 1)
        {
            newDisk.GetComponent<DiskInfo>().color = Color.white;
            newDisk.GetComponent<DiskInfo>().speed = 5.0f;
            float RanX = Random.Range(-1f, 1f) < 0 ? -2 : 2;
            newDisk.GetComponent<DiskInfo>().target = new Vector3(RanX, 1, 0);
            newDisk.GetComponent<Renderer>().material.color = Color.white;
        }
        else if (difficulty < rand2 - 1)
        {
            newDisk.GetComponent<DiskInfo>().color = Color.red;
            newDisk.GetComponent<DiskInfo>().speed = 7.0f;
            float RanX = Random.Range(-1f, 1f) < 0 ? -2 : 2;
            newDisk.GetComponent<DiskInfo>().target = new Vector3(RanX, 1, 0);
            newDisk.GetComponent<Renderer>().material.color = Color.red;
        }
        else
        {
            newDisk.GetComponent<DiskInfo>().color = Color.black;
            newDisk.GetComponent<DiskInfo>().speed = 9.0f;
            float RanX = Random.Range(-1f, 1f) < 0 ? -2 : 2;
            newDisk.GetComponent<DiskInfo>().target = new Vector3(RanX, 1, 0);
            newDisk.GetComponent<Renderer>().material.color = Color.black;
        }

        activeList.Add(newDisk.GetComponent<DiskInfo>());
        return newDisk;
    }
    public void freeDisk(GameObject disk)
    {
        DiskInfo tmp = null;
        foreach (DiskInfo i in activeList)
        {
            if (disk.GetInstanceID() == i.gameObject.GetInstanceID())
            {
                tmp = i;
                break;
            }
        }
        if (tmp != null)
        {
            tmp.gameObject.SetActive(false);
            tmp.hit = 0;
            freeList.Add(tmp);
            activeList.Remove(tmp);
        }
    }
}

public class DiskInfo : MonoBehaviour
{
    public Vector3 pos;
    public Color color;
    public float speed;
    public Vector3 target;
    public int hit = 0;
}

飞行动作类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlyAction : SSAction
{
    public float g = 9.8f;
    public Vector3 to;//初速度方向
    public float v; //初速度
    public float v_down = 0;
    public float time;
    private FlyAction() { }

    public static FlyAction GetSSAction(Vector3 target, float speed)
    {
        FlyAction action = ScriptableObject.CreateInstance<FlyAction>();
        action.to = target;
        action.v = speed;
        return action;
    }

    public override void Update()
    {
        time += Time.fixedDeltaTime;
        this.transform.position += Vector3.down * (float)(v_down * Time.fixedDeltaTime + 0.5 * g * (Time.fixedDeltaTime) * (Time.fixedDeltaTime));
        this.transform.position += to * v * Time.fixedDeltaTime;
        v_down = time * g;

        if (this.transform.position.y <= -5)
        {
            this.destroy = true;
            if (this.transform.position.y > -15)
            {
                Singleton<Judger>.Instance.Miss();
            }
            this.callBack.SSActionEvent(this);

        }
    }
    public override void Start()
    {

    }
}
游戏画面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值