S老师 打飞机 学习

时间:2023-11-09 20:28:26

S老师 打飞机 学习

 using UnityEngine;
 using System.Collections;

 /// <summary>
 /// 奖励
 /// </summary>
 public class Award : MonoBehaviour {

     ; //0 gun   1 explode

     public float speed = 1.5f;

     void Update(){
         this.transform.Translate( Vector3.down*Time.deltaTime*speed );
         if(this.transform.position.y<=-4.5f){
             Destroy(this.gameObject);
         }
     }
 }

Award

 using UnityEngine;
 using System.Collections;

 /// <summary>
 /// 背景移动
 /// </summary>
 public class BackgroundTransform : MonoBehaviour {

     public static float moveSpeed = 2f;

     void Update () {
         this.transform.Translate( Vector3.down * moveSpeed * Time.deltaTime );
         Vector3 postion = this.transform.position;
         if(postion.y<=-8.52f){
             ,postion.z );
         }
     }
 }

BackgroundTransform

 using UnityEngine;
 using System.Collections;

 /// <summary>
 /// 子弹
 /// </summary>
 public class Bullet : MonoBehaviour {

     ;

     // Update is called once per frame
     void Update () {
         transform.Translate( Vector3.up * speed *Time.deltaTime );
         if(transform.position.y>4.3f){
             Destroy(this.gameObject);
         }
     }

     void OnTriggerEnter2D(Collider2D other) {
         if(other.tag=="Enemy"){
             if(!other.GetComponent<Enemy>().isDeath){
                 other.gameObject.SendMessage("BeHit");
                 GameObject.Destroy(this.gameObject);
             }
         }
     }

 }

Bullet

 using UnityEngine;
 using System.Collections;

 public enum EnemyType{
     smallEnemy,
     middleEnemy,
     bigEnemy
 }

 /// <summary>
 /// 敌人
 /// </summary>
 public class Enemy : MonoBehaviour {

     ;

     ;

     ;

     public EnemyType type= EnemyType.smallEnemy;

     public bool isDeath = false;

     public Sprite[] explosionSprites;

     ;

     ;

     private SpriteRenderer render;

     public float hitTimer = 0.2f;
     private float resetHitTime ;

     public Sprite[] hitSprites;

     // Use this for initialization
     void Start () {
         render = this.GetComponent<SpriteRenderer>();

         resetHitTime=hitTimer;
         hitTimer=;
     }

     // Update is called once per frame
     void Update () {
         this.transform.Translate( Vector3.down*speed*Time.deltaTime );
         if(this.transform.position.y<=-5.6f){
             Destroy(this.gameObject);
         }

         if(isDeath){
                 timer+=Time.deltaTime;
                 int frameIndex = (int)(timer/(1f/explosionAnimationFrame));
                 if(frameIndex>=explosionSprites.Length){
                     //destroy
                     Destroy(this.gameObject);
                 }else{
                     render.sprite= explosionSprites[frameIndex];
                 }
         }else{
             if(type==EnemyType.middleEnemy||type==EnemyType.bigEnemy){
                 ){
                     hitTimer-=Time.deltaTime;

                     int frameIndex = (int)((resetHitTime-hitTimer)/(1f/explosionAnimationFrame));
                     frameIndex%=;
                     render.sprite= hitSprites[frameIndex];
                 }
             }
         }
     }

     public void BeHit(){
         hp-=;
         // explosion
         ){
             toDie();
         }else{
             hitTimer=resetHitTime;
         }
     }

     private void toDie(){
         if(!isDeath){
             isDeath=true;
             GameManager._instance.score+=score;
         }
     }
 }

Enemy

 using UnityEngine;
 using System.Collections;

 public enum GameState{
     Runing,
     Pause
 }

 /// <summary>
 /// 游戏管理
 /// </summary>
 public class GameManager : MonoBehaviour {

     public static GameManager _instance;

     ;

     private GUIText guiText;
     public GameState gameState = GameState.Runing;

     void Awake(){
         _instance=this;
         guiText=GameObject.FindGameObjectWithTag("ScoreGUI").GetComponent<GUIText>();
     }

     // Update is called once per frame
     void Update () {
         guiText.text="Score:"+score;
     }
     public void transfromGameState(){
         if(gameState==GameState.Runing){
             pauseGame();
         }else if(gameState==GameState.Pause){
             continueGame();
         }
     }

     public void pauseGame(){
         Time.timeScale=;// time.delatTime = 0
         gameState=GameState.Pause;
     }
     public void continueGame(){
         Time.timeScale=;
         gameState=GameState.Runing;
     }
 }

GameManager

 using UnityEngine;
 using System.Collections;

 /// <summary>
 /// 游戏暂停
 /// </summary>
 public class GamePause : MonoBehaviour {

     void OnMouseUpAsButton() {
         print ("click me!");
         GameManager._instance.transfromGameState();

         GetComponent<AudioSource>().Play();
     }

 }

GamePause

 using UnityEngine;
 using System.Collections;

 /// <summary>
 /// 飞机的枪
 /// </summary>
 public class Gun : MonoBehaviour {

     public float rate =0.2f;

     public GameObject bullet;

     public void fire(){
         GameObject.Instantiate(bullet,transform.position,Quaternion.identity );
     }

     public void openFire(){
         InvokeRepeating(,rate);
     }

     public void stopFire(){
         CancelInvoke("fire");
     }

 }

Gun

 using UnityEngine;
 using System.Collections;

 /// <summary>
 /// 飞机
 /// </summary>
 public class Hero : MonoBehaviour {

     public bool animation = true;

     ;

     ;

     public Sprite[] sprites;

     public float superGunTime = 10f;

     public Gun gunTop;
     public Gun gunLeft;
     public Gun gunRight;

     private float resetSuperGunTime ;

     private SpriteRenderer spriteRender;

     private bool isMouseDown = false;

     private Vector3 lastMousePosition = Vector3.zero;

     private Transform hero;

     ;

     void Start(){
         spriteRender = this.GetComponent<SpriteRenderer>();
         hero = GameObject.FindGameObjectWithTag("Player").transform;

         resetSuperGunTime = superGunTime;
         superGunTime=;

         gunTop.openFire();
     }

     // Update is called once per frame
     void Update () {
         if(animation){
             timer+=Time.deltaTime;// 1f/frameCountPersconds
             int frameIndex = (int)(timer/(1f/frameCountPersconds));
             ;
             spriteRender.sprite = sprites[frame];
         }
         )){
             isMouseDown=true;
         }
         )){
             isMouseDown=false;
             lastMousePosition = Vector3.zero;
         }
         if(isMouseDown && GameManager._instance.gameState==GameState.Runing ){
             if(lastMousePosition!=Vector3.zero){
                 //Camera.main.ScreenToWorldPoint(Input.mousePosition)
                 //print (Camera.main.ScreenToWorldPoint(Input.mousePosition));
                 Vector3 offset = Camera.main.ScreenToWorldPoint(Input.mousePosition) -lastMousePosition;

                 transform.position = transform.position+offset;
                 checkPosition();
             }
             lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         }
         superGunTime-=Time.deltaTime;
         ){
             ){
                 transformToSuperGun();
             }
         }else{
             ){
                 tranformToNormalGun();
             }
         }
     }

     private void transformToSuperGun(){
         gunCount=;
         gunLeft.openFire();
         gunRight.openFire();
         gunTop.stopFire();
     }
     private void tranformToNormalGun(){
         gunCount=;

         gunLeft.stopFire();
         gunRight.stopFire();
         gunTop.openFire();
     }

     private void checkPosition(){
         //check x -2.22f +2.22f
         // check y -3.9 3.4
         Vector3 pos = transform.position;
         float x = pos.x;
         float y = pos.y;
         if(x<-2.22f){
             x=-2.22f;
         }
         if(x>2.22f){
             x=2.22f;
         }
         if(y<-3.9f){
             y=-3.9f;
         }
         if(y>3.4f){
             y=3.4f;
         }
         transform.position= );
     }

     public void OnTriggerEnter2D(Collider2D collider){
         if(collider.tag=="Award"){
             GetComponent<AudioSource>().Play();
             Award award = collider.GetComponent<Award>();
             ){
                 //transform gun
                 superGunTime=resetSuperGunTime;
                 Destroy(collider.gameObject);
             }
         }
     }
 }

Hero

 using UnityEngine;
 using System.Collections;

 /// <summary>
 /// 敌人孵化
 /// </summary>
 public class Spawn : MonoBehaviour {

     public GameObject enemy0Prefab;
     public GameObject enemy1Prefab;
     public GameObject enemy2Prefab;

     public GameObject awardType0Prefab;
     public GameObject awardType1Prefab;

     public float enemy0Rate = 0.5f;
     public float enemy1Rate = 5f;
     public float enemy2Rate = 8f;

     ;

     ;

     // Use this for initialization
     void Start () {
         InvokeRepeating(,enemy0Rate);
         InvokeRepeating(,enemy1Rate);
         InvokeRepeating(,enemy2Rate);

         InvokeRepeating(,awardType0Rate);
         InvokeRepeating(,awardType1Rate);

     }

     // Update is called once per frame
     void Update () {

     }

     public void createEnemy0(){
         float x = Random.Range(-2.15f,2.15f);
         GameObject.Instantiate(enemy0Prefab,),Quaternion.identity);

     }
     public void createEnemy1(){
         float x = Random.Range(-2.04f,2.04f);
         GameObject.Instantiate(enemy1Prefab,),Quaternion.identity);

     }
     public void createEnemy2(){
         float x = Random.Range(-2.04f,2.04f);
         GameObject.Instantiate(enemy2Prefab,),Quaternion.identity);

     }

     public void createAwardType0(){
         GetComponent<AudioSource>().Play();
         float x = Random.Range(-2.1f,2.1f);
         GameObject.Instantiate(awardType0Prefab,),Quaternion.identity);
     }
     public void createAwardType1(){
         GetComponent<AudioSource>().Play();
         float x = Random.Range(-2.1f,2.1f);
         GameObject.Instantiate(awardType1Prefab,),Quaternion.identity);
     }
 }

Spawn

视频:https://pan.baidu.com/s/1jHTh3oy

项目:https://pan.baidu.com/s/1sljipdF