《太空射击》扩展

来源:互联网 发布:无损鉴定软件 编辑:程序博客网 时间:2024/04/27 21:00

游戏在Unity官方教程的基础上进行了一些扩展,例如倒计时、最终Boss、核弹、血量补给包和火力升级。

一、游戏简介

这是一款Unity 3D游戏引擎制作的一个太空射击类游戏,主要是升级武器,尽量多的打击敌人以获得高分。

二、游戏规则

通过手机触屏或者鼠标来控制飞船的移动,躲避陨石和敌机的同时尽可能的击毁它们,以得到高分。左上角有记录目前分数(及倒计时),右上角则是玩家的血条(以及BOSS的血槽)。游戏玩家通过射击敌人,获得分数,每打死一个获得100分,记录在score里,如果游戏玩家被敌人攻击3次,则死亡,结束游戏(游戏中有补血包以及核弹和武器升级系统),或者坚持120秒,迎击BOSS,打败BOSS可以获得高分加成。游戏结束后游戏玩家可以选择按选择退出游戏或者重新开始游戏。

三、游戏操作

通过手机触屏或者鼠标来控制飞船的移动以及射击

四、制作过程

1、    游戏开始界面制作

 

设置一个循环滚动的背景,再插入游戏名称以及“开始”和“退出”按钮,在按钮下插入场景跳转的脚本。

using UnityEngine;

using System.Collections;

 

public class BtnRestart :MonoBehaviour {

 

        voidOnMouseUpAsButton()

        {

               GetComponent<AudioSource>().Play();

               Invoke("Jump",0.5f);

       

        }

       

       

        voidJump()

        {

               Application.LoadLevel("Start");

       

        }

}

 

 

2、    游戏场景制作

 


上角有记录目前分数及倒计时,分数计入需要创建一个UI并辅以代码,倒计时完全是代码控制显示。右上角则是玩家的血条(以及BOSS的血槽),通过创建Canvas实现。

 

 Boss的移动

using UnityEngine;

using System.Collections;

 

public class BoosFly :MonoBehaviour {

 

        publicfloat speed = 1f;//速度

        floatk = 1;            //改变移动方向的标记

        Transformm_transform;

 

        voidStart ()

        {

               //获取Ttansform组件

               m_transform= this.transform;

       

        }

       

        //Update is called once per frame

        voidUpdate ()

        {

               //当物体移动到屏幕边界的时候,改变移动方向

               if(m_transform.position.x> 5 || m_transform.position.x < -5)

                       k= -k;

               //通过改变物体的X坐标值来使物体左右移动,这里使用了正弦函数来赋值

               m_transform.position+= new Vector3 (Mathf.Sin (m_transform.position .y * speed * k *Time.deltaTime), 0f, 0f);

               //if(m_transform.position.y> 5 || m_transform.position.y < -5)

 

               //通过改变物体的X坐标值来使物体左右移动,这里使用了正弦函数来赋值

               //m_transform.position+= new Vector3 (Mathf.Sin (m_transform.position .y * speed * j * Time.deltaTime),0f, 0f);

       

        }

}

以及Boss超多的生命值

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

 

public class boss :MonoBehaviour {

              public GameObject explosion;

              public float blood = 1000f;

              public Slider ui_blood;

              public int score= 10000;

              void OnTriggerEnter(Collider other)

              {

                            if(other.CompareTag("PlayerBullet"))

                            {

                                          Destroy(other.gameObject);

                                          blood -=4;

                                          ui_blood.value = blood *0.001f;

                                          if(blood <= 0)

                                          {

                                                        Dead();

                                                        GameManager._instance.AddScore (score);

                                          }

                            }

              }

 

       void Dead()

       {

                    Instantiate(explosion,transform.position,Quaternion.identity);

                     Destroy(gameObject);

                     GameManager._instance.GameOver();

 

       }

}

 

各种补给包,配合在Unity里制作的预制资源,就是改了下原先子弹的数量和发射位置、偏移方向。

using UnityEngine;

using System.Collections;

 

public class Playergun :MonoBehaviour {

 

       public int lever = 1;

       public Transform bullet1;

       public Transform bullet2;

       public Transform bullet3;

       public float fireRate = 1f;

       Transform m_transform;

 

       void Start ()

       {

              m_transform = this.transform;

              InvokeRepeating ("OpenFire", 0.3f, fireRate);

       }

      

       // Update is called once per frame

       void OpenFire ()

       {

              switch (lever)

              {

                     case 2:

                            Instantiate (bullet2,m_transform.position, Quaternion.identity);

                            break;

              case 3:

                     Instantiate (bullet3, m_transform.position,Quaternion.identity);

                     break;

                     default :

      

                            Instantiate (bullet1,m_transform.position, Quaternion.identity);

                            break;

              }

       }

}


这是飞机身上挂的代码,包含了血量补给包(Blood)

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

 

public class Player :MonoBehaviour {

 

              public GameObject explosion,explosion1;

              public float blood = 10;

              public Slider ui_blood;

              void OnTriggerEnter(Collider other)

              {

                            if(other.CompareTag("Blood"))

                            {

                                          //blood = 10;

                                          blood += 3;

                                          if(blood > 10)

                                                        blood = 10;

                                          ui_blood.value = blood *0.1f;

                                          Destroy(other.gameObject);

                            }

                            if(other.CompareTag("Enemy") ||other.CompareTag("EnemyBullet"))

                            {

                                          Destroy(other.gameObject);

                                         Instantiate(explosion,transform.position,Quaternion.identity);

                                          blood -=4;

                                          ui_blood.value = blood *0.1f;

                                          if(blood <= 0)

                                          {

                                                        Dead();

                                          }

                            }

              }

 

              void Dead()

              {

                          Instantiate(explosion1,transform.position,Quaternion.identity);

                            Destroy(gameObject);

                            GameManager._instance.GameOver();

 

              }

}

 

3、    游戏结束界面制作

 

游戏结束是会显示是最高分和本次得分。玩家可以选择重新开始或者退出,和开始界面的制作一样,不过要键入EndScore脚本

以便显示历史最高分。

 


原创粉丝点击