C# Junior and Intermediate Actual Combat—3DFlipBird (1)

1. Required C# knowledge points:

C# elementary and intermediate level tutorials are all completed. Let's make a small demo based on this part of the tutorial.
For elementary and intermediate level C#, the most important thing is the three major features of C# object-oriented encapsulation, inheritance, and polymorphism. (The first two are the most commonly used)

2. Required unity knowledge points

Will open, understand the transform component, the key input of unity, and the simple use of rigBody.

3. Demand analysis

To make any program, function, or Demo, the first thing to do is to analyze the requirements, then design the implementation steps, and then the implementation method, debugging, etc.
We use mind maps for demand analysis.
First list the ideas. We make sure that our character is a bird. The functions we need to achieve:
1. Birds can jump up and down to avoid obstacles (so we also need obstacles)
2. Obstacles can move and can push the bird to a certain extent , The game fails
. 3. Obstacles are different in height and will be produced continuously (there is a quantity requirement)
4. Birds cannot fly infinitely high, there is a ceiling
5. The ceiling will also produce inverted obstacles
6. A score device is required, according to the survival of the bird The time keeps increasing.
7. After the game is over, the timer is displayed on the screen.
8. The input method uses space jump (so the demo is a small game for PC).
According to our ideas, draw a mind map and analyze which scripts need to be a Class, which and which to inherit (for example, we have several kinds of obstacles, round, square. Many kinds of birds, fat, thin, high jump, fast jump)

4. Mind map

The part that we need to write code is mainly birds and obstacles, and there are many types of these two, so we first need two base classes, the base class of birds, and the base class of obstacles.
In addition, we also need a game manager, GameMager. This object manages the start and end of the game, and manages the creation and destruction of obstacles. The display of scores should also be the responsibility of game management.
In summary, we have a total of three big blocks to write, birds, obstacles, and game management
. The framework for drawing a mind map is as follows.
Insert picture description here
Develop a standardized thinking habit so that complicated programs will not be rushed.

5. Demo first realization

The first step is to build the scene. We use a cube to represent our bird, pull out two cubes as the floor and ceiling, and use a larger capsule as an obstacle.
Insert picture description here
BirdBase code

using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;

public class BirdBase : MonoBehaviour
{
    
    
    public Rigidbody rigBird;
    public float jumpForce;
    public string jumpKey;

    // Start is called before the first frame update
    void Start()
    {
    
    

    }

    // Update is called once per frame
    void Update()
    {
    
    //检测键盘输入,在外界输入一个值
        if (Input.GetKeyDown(jumpKey))
        {
    
    
            BirdJump();

        }
    }

    public void BirdJump()
    {
    
    
        rigBird.AddForce(new Vector3(0, jumpForce, 0));//利用unity的RigBody,添加一个跳跃的力,实现鸟的起跳,外界可调整跳跃力的大小
    }


}

ObstacleBase code

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

public class ObstacleBase : MonoBehaviour
{
    
    
    public  Transform obstacleMoveT;
    public float moveSpeed;

    // Start is called before the first frame update
    void Start()
    {
    
     
         
    }

    // Update is called once per frame
    void Update()
    {
    
    
        //obstacleMoveT.position = new Vector3(obstacleMoveT.position.x, obstacleMoveT.position.y,
        //    obstacleMoveT.position.z * Time.deltaTime * moveSpeed);
        obstacleMoveT.position = new Vector3(obstacleMoveT.position.x, obstacleMoveT.position.y,
            obstacleMoveT.position.z + moveSpeed);
            //外界传入障碍物的transform,利用transform进行移动,加入一个速度控制字段。这里有两种写法,乘和加


    }
}

GameMager code

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


public class GameMager : MonoBehaviour
{
    
    
    public Transform onePosition;
    public Transform twoPosition;
    public Transform threePosition;
    //三个创造的位置
    public GameObject creatGameObject;
    //需要创造的物体


    public float creatTime; //创造的时间间隔
    private float timeTemp; //每次创造时间结束后,重新赋予creatTime值

    // Start is called before the first frame update
    void Start()
    {
    
    
        timeTemp = creatTime;

    }

    // Update is called once per frame
    void Update()
    {
    
    
        RangeCreat();
        creatTime -= Time.deltaTime;
        

    }
//封装unity的创造物体方法
    public void Creat(Transform position)
    {
    
    


        Instantiate((creatGameObject), position);


    }

    public void RangeCreat()
    {
    
    
        if (creatTime <= 0)//计时结束,创造物体
        {
    
    
            float cTemp = Random.Range(1, 4);//达不到最大值,所以随机1到3,根据随机的不同值,在3个位置进行创造

            Debug.Log(cTemp);
            if (cTemp == 1)
            {
    
    
                Creat(onePosition);


            }
            else if (cTemp == 2)
            {
    
    
                Creat(twoPosition);


            }
            else if (cTemp == 3)
            {
    
    
                Creat(threePosition);


            }
            creatTime = timeTemp;

        }
    }

}

The final effect
Insert picture description here
project address

https://github.com/euphoriaer/3DBird

Guess you like

Origin blog.csdn.net/euphorias/article/details/105894547