C#初中级实战—3DFlipBird(一)

1.需要的C#知识点:

C#初中级教程全部完成了,下面根据这部分教程做个小Demo
对于初中级的C#而言,最重要的是C#面向对象的三大特征封装,继承,多态。(前两者是最为常用的)

2.需要的unity知识点

会打开,了解transform组件,unity的按键输入,rigBody的简单使用。

3.需求分析

做任何一个程序,功能,或者Demo,首先要做的事是需求分析,然后设计实现步骤,接下来才是实现方法,调试等等。
我们用思维导图来进行需求分析。
首先把想法列出来,我们确定我们的角色是一个鸟,我们需要实现的功能:
1.鸟可以上下跳跃躲避障碍(所以我们还需要障碍)
2.障碍可以移动,可以推动鸟,推到一定程度,游戏失败
3.障碍高低不同,会不断产出(有数量需求)
4.鸟不能无限飞高,有一个天花板
5.天花板也会产出倒立的障碍
6.需要一个分数器,根据鸟的存活时间,不断增加
7.游戏结束后,计时器显示在屏幕上
8.输入方法使用空格跳跃(所以demo是一个针对PC的小游戏)
根据我们的想法,画思维导图,分析哪些脚本需要是一个类,哪些和哪些进行继承(比如我们有好几种障碍,圆的,方的。好几种鸟,胖的,瘦的,跳的高的,跳的快的)

4.思维导图

我们需要写代码的部分,主要是鸟和障碍物,而且这两个也是有很多种类,所以我们首先需要两个基类,鸟的基类,和障碍的基类。
除此之外,我们还需要一个游戏管理器,GameMager,这个物体管理游戏的开始,结束,以及管理障碍物的创造和销毁,显示分数应该也是游戏管理的责任。
综上所述,我们一共有三个大块要写,鸟,障碍,游戏管理
画出思维导图的框架如下
在这里插入图片描述
养成规范的思考习惯,复杂的程序才不会手忙脚乱。

5.Demo初实现

第一步把场景搭建出来,我们用一个正方体代表我们的鸟,拉出两个长方体作为地板和天花板,用一个大一点的胶囊体作为障碍物。
在这里插入图片描述
BirdBase代码

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代码

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代码

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;

        }
    }

}

最后的效果
在这里插入图片描述
工程地址

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

猜你喜欢

转载自blog.csdn.net/euphorias/article/details/105894547
今日推荐