Unity学习第三周

游戏简介

在《FlappyBird》这款游戏中,玩家鼠标点击屏幕,小鸟就会往上飞,不断的点击就会不断的往高处飞。不点击的话则会快速下降。所以玩家要控制小鸟一直向前飞行,然后注意躲避途中高低不平的管子。
游戏效果
在这里插入图片描述

游戏设计思路
使用场景相对小鸟移动的过程间接实现小鸟在水平方向的位移,小鸟实际上只在垂直方向上进行了位置的改变,增加小鸟的重力,玩家点击鼠标按键或按空格时小鸟获得一个向上的力。当小鸟位于某根水管中间时,判断小鸟是否与该水管的上侧或者下侧,或地面发生了碰撞,如果发生碰撞则判定游戏结束,重新跳到开始界面。

游戏具体实现

1.场景的搭建
使用素材按游戏中的图样进搭建。
(1)将上下两个水管作为一个整体放入一个新建的模型(GameObject)中将该类和地面场景一起放入背景场景下。
(2)将背景场景放入预制体,ctrl+D复制。
(3)将小鸟素材制成动画(将小鸟所有图片选中拖入到Hierarchy)放入场景中。
2.游戏物体添加组件
为小鸟添加刚体和碰撞体,将地面,水管添加碰撞体。

编写代码

小鸟的代码
写小鸟的代码需要注意的是与地面,水管碰撞时用碰撞检测,而与金币时用触发检测。这里的金币也可以改成在两个水管之间加触发器,当小鸟经过水管后得一分。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Bird : MonoBehaviour
{
    
    
    public int score = 0;//分数
    public Rigidbody2D rigidbodybird;
    public GameObject isDefentUI;
    public Text text; 
    public SpriteRenderer sr;
    public Sprite birds;
    // Start is called before the first frame update
    private void Awake()
    {
    
    
        sr = GetComponent<SpriteRenderer>();
    }
    void Start()
    {
    
    
        rigidbodybird = GetComponent<Rigidbody2D>();

    }

    // Update is called once per frame
    void Update()
    {
    
    
       
        Up();
    }
     void Up()
    {
    
    
        if (Input.GetKeyDown(KeyCode.Space)||Input.GetMouseButton(0))
        {
    
    
            rigidbodybird.AddForce(new Vector2(0, 250f));
        }
    }
    public void OnTriggerEnter2D(Collider2D other)
    {
    
    
       //触发检测食物
        if (other.tag == "Food")
        {
    
    

            Destroy(other.gameObject);//销毁食物
            score++;
            text.text = "分数:" + score;
        }  
    }
    public void OnCollisionEnter2D(Collision2D collision)
    {
    
    
    //检测是否碰撞水管和地面
        if (collision.gameObject.tag == "Pipe" || collision.gameObject.tag == "Grass")
        {
    
    
            sr.sprite = birds;
            isDefentUI.SetActive(true);
            Invoke("ReturnMenu", 3);//如果游戏结束返回主界面

        }
    }
    private void ReturnMenu()
    {
    
    
        SceneManager.LoadScene(1);
    }
   
}

背景的代码
背景随时间的进行向左移动
这里注意是整个背景得移动。

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

public class backgrad : MonoBehaviour
{
    
    
    public float speed = 2f;//移动速度
    // Start is called before the first frame update
    void Start()
    {
    
    
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
        transform.Translate(Vector3.left * speed * Time.deltaTime);
    }
}

背景的循环
当相机视野移出背景触发了第二个背景中的触发器第一个背景移到第三个背景后。

//背景的循环
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pipe : MonoBehaviour
{
    
    
   
     void Update()
    {
    
    
     
    }
    public void PipeMove()
    {
    
    
        float move_y = Random.Range(1,8);
        this.transform.localPosition = new Vector2(this.transform.localPosition.x, move_y);
    }
}
//触发检测
using System.Collections.Generic;
using UnityEngine;

public class trige : MonoBehaviour
{
    
    
    public Transform GetTransform;
    public void OnTriggerEnter2D(Collider2D other)
    {
    
    
        if(other.tag=="Bird")
        {
    
    
            Transform tf = Newcamer.newcamera.tf;
            GetTransform.position = new Vector2(GetTransform.position.x+71, GetTransform.position.y);
            Newcamer.newcamera.tf = GetTransform;
        }
    }
}

主菜单的实现

主菜单界面(简化版)
在这里插入图片描述
在这里插入图片描述
Image的代码
用来选择和确认

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

public class Image : MonoBehaviour
{
    
    
    public Transform poseone;
    public Transform posetwo;
    private float h = 1;
    // Start is called before the first frame update
    void Start()
    {
    
    

    }

    // Update is called once per frame
    void Update()
    {
    
    
        if (Input.GetKeyDown(KeyCode.W))
        {
    
    
            h = 1;
            transform.position = poseone.position;
        }
        else if (Input.GetKeyDown(KeyCode.S))
        {
    
    
            h = 2;
            transform.position = posetwo.position;
        }
        if (Input.GetKeyDown(KeyCode.Space) && h == 1)
        {
    
    
            SceneManager.LoadScene(0);//跳到游戏界面
        }
    }
}

猜你喜欢

转载自blog.csdn.net/AD_GOD/article/details/122750676