Flappy Bird 相关笔记

这个忘了干嘛的

    public float speed;
       private void Start()
       {
              Destroy(gameObject, 5);
              //p=FindObjectOfType<Player>();
              
       }
       void Update () {
              if (Player.isAlive )
              {
                     MoveToLeft();
              }
      
       }
    void MoveToLeft()
    {

        transform.Translate(Vector2 .left *Time .deltaTime *speed, Space.Self);

创建水管

       public GameObject waterPipePrefabs;
       float nowtime = 0;
       float creattime = 2;
       float offY=0f;
    //创建水管
    void CreatWaterPipes()
    {
              offY = Random.Range(0,1.6f);
              Instantiate(waterPipePrefabs, this.transform.position + new Vector3(0, offY,0), Quaternion.identity);
    }
       private void Update()
       {
              if (Player.isAlive == false)
                     return;
              //间隔x秒生产障碍物
              nowtime += Time.deltaTime;
              if (nowtime >= creattime)
              {
                     CreatWaterPipes();
                     nowtime = 0;
              }
              
       }

}

流程图
在这里插入图片描述
小鸟上下跳跃

 Rigidbody2D rd2d;
    public float upSpeed;
       public  int playerScore = 0;
       public int BestScore = 0;
       public  Text currentScoreText;
       public Text BestScoreText;
 
    void Start () {
        Init();
    }
    void Update()

    {

        if (Input.GetKeyDown(KeyCode.Space)  && isAlive       )
        {           
            Jump();
        }
              //当鸟GG的时候,跳转到EndScene
              if (isAlive == false)
              {
                     SceneManager.LoadScene("FlappyBird_EndScene");
              }
    }
    void Init()
    {
              //保证鸟是活的
              isAlive = true;
              //读取玩家最高得分
               BestScore = PlayerPrefs.GetInt("bestScore");
              BestScoreText.text = BestScore.ToString();
              rd2d = GetComponent<Rigidbody2D>();
              playerScore = 0;
              //鸟定位到初始位置
              transform.position = new Vector3(-1.44f, 0.51f, -0.01f);
       }
    void Jump()
    {
        //由于有惯性存在,会导致跳跃力度不稳定
       // rd2d.AddForce(new Vector2(0, upPower));
        rd2d.velocity = new Vector2(0, upSpeed);
    }
       //是否活着
       public static bool isAlive = true;
       void PlayerDead()
       {
              isAlive = false;
              
       }
       private void OnCollisionEnter2D(Collision2D collision)
       {
                     //     collision.gameObject.CompareTag("WaterPipe");
              if (collision.gameObject .tag .Equals ("WaterPipe" )  )
              {
                     PlayerDead();
              }      
       }
       private void OnTriggerEnter2D(Collider2D collision)
       {
              if (collision.gameObject.tag.Equals("CheckPoint"))
              {
                     //更新得分
                     playerScore += 1;
                     //更新最高得分
                     if(playerScore>BestScore)
                     {
                           BestScore = playerScore;
                           //把得分写入持久数据
                           PlayerPrefs.SetInt("bestScore", BestScore);
                     }
                     //更新UI
                     currentScoreText.text = playerScore.ToString();
                     BestScoreText.text = BestScore.ToString();
                     Debug.Log(playerScore);
              }
              
       }

}

背景图移动
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveBg : MonoBehaviour {
   // public float scrollSpeed = 0.01f;
    //public Renderer rend;
    Material mat;
       
       void Start () {
        mat = GetComponent<SpriteRenderer>().material;
        //rend = GetComponent<Renderer>();
       }
       
       // Update is called once per frame
       void Update () {
        mat.mainTextureOffset = new Vector2( Time.time*0.1f,0);
       // float offset = Time.time * scrollSpeed;
        //rend.material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
       }

}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值