Unity实例1-----SPACE WAR

游戏设定:

这个应用程序实现了人类宇宙飞船和怪物宇宙飞船之间的太空战争,它们想要攻击地球。

所以游戏由一艘“人类”飞船组成,它与“怪物”太空船作战,必须尽可能多地摧毁怪物太空梭。

此游戏必须按以下说明工作:

1.载人飞船:可以左右移动,前后移动,发射火箭摧毁怪物的穿梭机。

2.怪物宇宙飞船:可以自上而下移动,当被某些人类火箭摧毁时,它必须出现在某个顶部位置。此外,还有两种类型的巨型宇宙飞船:

a、 怪物1号飞船不能发射火箭,被摧毁时你可以得到2分

b、 怪物2号飞船可以向人类飞船发射火箭,摧毁后你可以获得5分

3.收集点数:当你消灭一个敌人时,收集并显示总分。

4.人和怪物都有生命周期:例如人可以有3个生命周期,怪物可以有5个生命周期,当它与火箭相撞时会减少1个生命周期,当生命周期为零时,它将被摧毁。

任务需求:

1太空船向左/向右/向后移动

2.如何发射火箭

工程
在这里插入图片描述
场景在这里插入图片描述
失败在这里插入图片描述
攻击在这里插入图片描述


using UnityEngine;
using System.Collections;
//[AddComponentMenu("MyGame/Player")]
public class Player : MonoBehaviour {
    public float m_speed = 1;
    public float m_life = 3;
    // prefab
    public Transform m_rocket;
    protected Transform m_transform;
    float m_rocketRate = 0;
    public AudioClip m_shootClip;
    protected AudioSource m_audio;
    public Transform m_explosionFX;
    protected Vector3 m_targetPos;
    public LayerMask m_inputMask;
    //float movev=0;
    //float moveh=0;
    // Use this for initialization
    void Start () {
        m_transform = this.transform;
        m_audio = this.GetComponent<AudioSource>();
        m_targetPos = this.m_transform.position;
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetKey(KeyCode.UpArrow)) {
            //movev -= m_speed * Time.deltaTime;
            transform.Translate(Vector3.back * Time.deltaTime*m_speed);
        }
        if(Input.GetKey(KeyCode.DownArrow)){
           // movev += m_speed * Time.deltaTime;
            transform.Translate(Vector3.forward * Time.deltaTime*m_speed);
        }
        if(Input.GetKey(KeyCode.LeftArrow)){
            transform.Translate(Vector3.right * Time.deltaTime*m_speed);
           // moveh += m_speed * Time.deltaTime;
        }
        if ( Input.GetKey( KeyCode.RightArrow ) )
        {
            //moveh -= m_speed * Time.deltaTime;
            transform.Translate(Vector3.left * Time.deltaTime*m_speed);
        }
      //  this.m_transform.Translate( new Vector3( moveh, 0, movev ) );
        m_rocketRate -= Time.deltaTime;
        if ( m_rocketRate <= 0 )
        {
            m_rocketRate = 0.1f;
            if ( Input.GetKey( KeyCode.Space ) || Input.GetMouseButton(0) )
            {
                Instantiate( m_rocket, m_transform.position, m_transform.rotation );
                m_audio.PlayOneShot(m_shootClip);
            }
        }
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.tag.CompareTo("PlayerRocket") != 0)
        {
            m_life -= 1;
            if (m_life <= 0) 
            {
                Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);
                Destroy(this.gameObject);
            }
        }
    }
}

using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
    public float m_speed = 1;
    public float m_life = 10;
    protected float m_rotSpeed = 30;
    protected Transform m_transform;
    public Transform m_explosionFX;
    public int m_point = 10;
    // Use this for initialization
    void Start () {
        m_transform = this.transform;
    }
    // Update is called once per frame
    void Update () {
        UpdateMove();
    }
    protected virtual void UpdateMove()
    {
        // moving left and right
        float rx = Mathf.Sin(Time.time) * Time.deltaTime;
        // move forward
        m_transform.Translate(new Vector3(rx, 0, -m_speed * Time.deltaTime));
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.tag.CompareTo("PlayerRocket") == 0)
        {
            Rocket rocket = other.GetComponent<Rocket>();
            if (rocket != null)
            {
                m_life -= rocket.m_power;
                if (m_life <= 0)
                {
                    GameManager.Instance.AddScore(m_point);
                  Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);
                    Destroy(this.gameObject);
                }
            }
        }
        else if (other.tag.CompareTo("Player") == 0)
        {
            m_life = 0;
            Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);
            Destroy(this.gameObject);
        }
        if (other.tag.CompareTo("bound") == 0)
        {
            m_life = 0;
            Destroy(this.gameObject);
        }
    }
}

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
    public static GameManager Instance;
    public int m_score = 0;
    public static int m_hiscore = 0;
    protected Player m_player; 
    public AudioClip m_musicClip;
    protected AudioSource m_Audio;
    void Awake()
    {
        Instance = this;
    }
    // Use this for initialization
    void Start () {
        m_Audio = this.GetComponent<AudioSource>();
        // find player
        GameObject obj = GameObject.FindGameObjectWithTag("Player");
        if (obj != null)
        {
            m_player = obj.GetComponent<Player>();
        }
    }
    // Update is called once per frame
    void Update () {
        // paly background music in loop 
        if (!m_Audio.isPlaying)
        {
            m_Audio.clip = m_musicClip;
            m_Audio.Play();
        }
        // pause the game
        if (Time.timeScale > 0 && Input.GetKeyDown(KeyCode.Escape))
        {
            Time.timeScale = 0;
        }
    }
    void OnGUI()
    {
        // pause the game
        if (Time.timeScale == 0)
        {
            // continue the game
            if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.4f, 100, 30), "Continue the game"))
            {
                Time.timeScale = 1;
            }
            // exit the game
            if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.6f, 100, 30), "game exit"))
            {
                // exit the game
                Application.Quit();
            }
        }
        int life = 0;
        if (m_player != null)
        {
            // get the life value of player
            life = (int)m_player.m_life;
        }
        else // game over
        {
            // enlarger the font
            GUI.skin.label.fontSize = 50;
            // display the game failure
            GUI.skin.label.alignment = TextAnchor.LowerCenter;
            GUI.Label(new Rect(0, Screen.height * 0.2f, Screen.width, 60), "game failue");
            GUI.skin.label.fontSize = 20;
            // display button to try again
            if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.5f, 100, 30), "Try again"))
            {
                // read the current level
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
               // Application.LoadLevel(Application.loadedLevelName);
            }
       }
        GUI.skin.label.fontSize = 15;
        // display the player life value
        GUI.Label(new Rect(5, 5, 100, 30), "player" + life);
        // display the recorded highest score
        GUI.skin.label.alignment = TextAnchor.LowerCenter;
        GUI.Label(new Rect(0, 5, Screen.width, 30), "Records" + m_hiscore);
        // display the current score
        GUI.Label(new Rect(0, 25, Screen.width, 30), "Score " + m_score);
    }
    // 
    public void AddScore( int point )
    {
        m_score += point;
        // update the highest recorded score
        if (m_hiscore < m_score)
            m_hiscore = m_score;
    }
}

using UnityEngine;
using System.Collections;

[AddComponentMenu("MyGame/EnemyRocket")]
public class EnemyRocket : Rocket
{
    void OnTriggerEnter(Collider other)
    {
        if (other.tag.CompareTo("Player") != 0)
            return;
        Destroy(this.gameObject);
    }
}

using UnityEngine;
using System.Collections;

[AddComponentMenu("MyGame/EnemySpawn")]
public class EnemySpawn : MonoBehaviour
{
    //  Enemy Prefab
    public Transform m_enemy;
    // the time interval to generate enemy 
    protected float m_timer = 5;
    protected Transform m_transform;
    // Use this for initialization
    void Start () {
        m_transform = this.transform;
    }
    // Update is called once per frame
    void Update () {
        m_timer -= Time.deltaTime;
        if (m_timer <= 0)
        {
            m_timer = Random.value * 15.0f;
            if (m_timer < 5)
                m_timer = 5;
            Instantiate(m_enemy, m_transform.position, Quaternion.identity);
        }
    }
    void  OnDrawGizmos () 
    {
        Gizmos.DrawIcon (transform.position, "item.png", true);
    }
}


using UnityEngine;
using System.Collections;

[AddComponentMenu("MyGame/SuperEnemy")] 

public class SuperEnemy : Enemy {

    public Transform m_rocket;
    protected float m_fireTimer = 2;
    protected Transform m_player;
    void Awake()
    {
        GameObject obj=GameObject.FindGameObjectWithTag("Player");
        if ( obj!=null )
        {
            m_player=obj.transform;
        }
    }
    protected override void UpdateMove()
    {
        m_fireTimer -= Time.deltaTime;
        if (m_fireTimer <= 0)
        {
            m_fireTimer = 2;
            if ( m_player!=null )
            {
                Vector3 relativePos = m_transform.position-m_player.position;
                Instantiate( m_rocket, m_transform.position, Quaternion.LookRotation(relativePos) );
            }
        }
        // move forward
        m_transform.Translate(new Vector3(0, 0, -m_speed * Time.deltaTime));
    }
}


using UnityEngine;
using System.Collections;
[AddComponentMenu("MyGame/Rocket")]
public class Rocket : MonoBehaviour {

    // bullet fly speed
    public float m_speed = 10;
    // life time
    public float m_liveTime = 1;
    // power
    public float m_power = 1.0f;
    protected Transform m_trasform;
    // Use this for initialization
    void Start () {
        m_trasform = this.transform;
        Destroy(this.gameObject, m_liveTime);
    }
    
    // Update is called once per frame
    void Update () {
        m_trasform.Translate( new Vector3( 0, 0, -m_speed * Time.deltaTime ) );
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.tag.CompareTo("Enemy")!=0)
            return;
        Destroy(this.gameObject);
    }
}


using UnityEngine;
using System.Collections;

[AddComponentMenu("MyGame/TitleScreen")]
public class TitleScreen : MonoBehaviour
{

    void OnGUI()
    {
        // word fontsize
        GUI.skin.label.fontSize = 48;

        // UIcenter align
        GUI.skin.label.alignment = TextAnchor.LowerCenter;

        // display title
        GUI.Label(new Rect(0, 30, Screen.width, 100), "space war");


        // start game button 
        if (GUI.Button(new Rect(Screen.width * 0.5f - 100, Screen.height * 0.7f, 200, 30), "Start game"))
        {
            // start to load next level 
            Application.LoadLevel("level1");
        }
    }
} 
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值