C++游戏飞翔的小鸟软件二次开发

引言:
在快节奏的现代生活中,人们总是在寻找一种方式来放松自己,释放内心的压力。游戏作为一种娱乐方式,早已深入人心。今天,我要向大家介绍的是一款简单而又充满挑战的小游戏——飞翔的小鸟。
这款游戏的核心玩法是控制一只小鸟在无尽的天空中飞翔,通过点击屏幕使小鸟上升,避开各种障碍物,如树枝、岩石等。游戏的画面简洁明了,色彩鲜艳,给人一种轻松愉快的感觉。同时,游戏的音效也十分悦耳,为玩家营造了一个舒适的游戏环境。
在此本篇笔者对该小游戏进行二次开发

游戏主界面:

游戏主要代码

点击查看代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;


public class StarButLis : MonoBehaviour
{
      // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    //用来监听鼠标按下物体
    private void OnMouseDown(){
        transform.localScale = transform.localScale * 0.8f;//鼠标缩小
    }
    //用于监听鼠标松开了物体
    private void OnMouseUp(){
        transform.localScale = transform.localScale / 0.8f;//鼠标放大
        //SceneManager.LoadScene("game");//转换到game场景   3删了
        PlayerCol.life = true;//死亡重新开始
        ScoreCol.score=0;//死亡重新开始算分
        Danrudanchu.qiehuannchangjing("game");//淡入淡出.切换场景
    }
}
该模块主要是完成游戏开始以及重新开始和开始按钮的缩放。
代码中的(life、score、qiehuannchangjing("game"))
PlayerCol.life = true;//死亡重新开始
        ScoreCol.score=0;//死亡重新开始算分
        Danrudanchu.qiehuannchangjing("game");//淡入淡出.切换场景
分别为三个脚本(PlayerCol、  ScoreCol、Danrudanchu)中的元素和函数。


2.PlayerCol(脚本)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCol : MonoBehaviour
{
    // Start is called before the first frame update
    public float speed;
    public float jumpForce;//跳跃的力度
    private Rigidbody2D rb;//刚体
    public  static bool life=true;//小鸟是否还活着
    private Animator anim;//动画控制器

    private GameObject  gameover;//gameover的图片,小鸟死亡的图片

    private float time;


    void Start()
    {
        rb=GetComponent<Rigidbody2D>();//获得刚体
        anim = GetComponent<Animator>();

        gameover = GameObject.Find("Canvas/Image");//获得死亡图片
    }

    // Update is called once per frame
    void Update()
    {
        if(life)
        { 
           rb.velocity=new Vector2(speed, rb.velocity.y);//赋予玩家水平速度
           if(Input.GetMouseButtonDown(0))
           {
           rb.velocity=new Vector2(rb.velocity.x, jumpForce);//赋予玩家垂直速度的向量
           }
        }

        else{
            time +=Time.deltaTime;
           if(time>=3){
                //life = true;
               // ScoreCol.score=0;
                 Danrudanchu.qiehuannchangjing("title");
            }
        }

        gameover.SetActive(!life);

    }
    //如果碰撞器碰撞到了某个物体,判断小鸟死亡
    private  void OnCollisionEnter2D(Collision2D collision){
        if(life){
           huamianshanshuo.shanshuo();
        }
        life = false;
        anim.SetBool("life",false);
    }
}

//}

该代码主要是完成小鸟跳跃、速度、以及动画控制器、判断小鸟是否存活的代码。
rb.velocity=new Vector2(speed, rb.velocity.y);//赋予玩家水平速度
这个代码是使玩家获得一个水平的速度。
代价中的life就是小鸟的存活状态,如果为true则小鸟没有死亡。
如果小鸟为false,则说明小鸟死亡,然后游戏将重新开始。
else{
            time +=Time.deltaTime;
           if(time>=3){
                //life = true;
               // ScoreCol.score=0;
                 Danrudanchu.qiehuannchangjing("title");
            }
        }

切换到游戏开始的页面, 
 private  void OnCollisionEnter2D(Collision2D collision){
        if(life){
           huamianshanshuo.shanshuo();
        }
        life = false;
        anim.SetBool("life",false);
    }
}
并且小鸟将停止挥动。


3.YanXuWuTi(脚本)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    public GameObject yaoshengchengdewuti;//要生成的物体
    public float shengchengzaiqianfangdejuli;//生成在前方的距离

    public bool yijingchufa = false;//已经触发
    private float time =0f;

    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(yijingchufa && PlayerCol.life){
          time += Time.deltaTime;
           if(time > 3)
           {
               Destroy(transform.gameObject);//自动销毁
           }

        }
    }
    //当某个刚体碰撞到触发器,开始延续物体
private void OnTriggerEnter2D(Collider2D collision){
        Instantiate(yaoshengchengdewuti,new Vector3(transform.position.x+shengchengzaiqianfangdejuli,transform.position.y,transform.position.z),transform.rotation );
       // Destroy(transform.gameObject,3);//3秒后自动销毁
        yijingchufa=true;
    }
}
该代码模块主要是完成背景和地面以及柱子无限延续的作用。
  if(yijingchufa && PlayerCol.life){
          time += Time.deltaTime;
           if(time > 3)
           {
               Destroy(transform.gameObject);//自动销毁
           }

        }
此处代码模块是完成延续物体产生之后,小鸟运动过程中将后方已经产生的延续物体自动删除。* 

改进项目

1.丰富用户体验游戏的体验感

点击查看代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Danrudanchu : MonoBehaviour
{// Start is called before the first frame update
    public Texture img;//黑色的图片系统传过来
    private static string changjing;//场景
    private static float alpha=0f;
    private static  bool danchu=false;//淡出
    public float speed;
    public static bool danru=false; //淡入

    void Start()
    {
        
    }

    // Update is called once per frame
    void OnGUI()
    {
        if(danchu){
            alpha +=speed * Time.deltaTime;
            if(alpha>=1){
                danru=true;
                danchu=false;
                SceneManager.LoadScene(changjing);//切换场景
            }
        }

        if(danru){
            alpha -=speed * Time.deltaTime;
            if(alpha<=0){
                danru =false;
            }
        }
        GUI.color = new Color (GUI.color.r,GUI.color.g,GUI.color.b, alpha);//透明度
        GUI.DrawTexture(new Rect(0,0, Screen.width,Screen.height), img);//绘制黑色的图片
    }

    public static void qiehuannchangjing(string scene)
    {//切换场景
        if(danru) {danru=false;}
        danchu=true;
        changjing=scene;
    }
}
此处代码主要是完成切换场景时淡入淡出的作用,这样子显得更加生动。

2.小鸟发生消失问题

GameObject.Find("player");引入角色player(小鸟)。

void Update()
{

transform.position = new Vector3(player.transform.position.x, transform.position.y, transform.position.z);
}
此处代码主要是完成小鸟运动时水平跟随游戏界面,这样子就不会发生小鸟消失的问题了。

3.优化小鸟死亡时的动态效果

点击查看代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class huamianshanshuo : MonoBehaviour
{
    ///死亡画面
    public Texture img;//白色图片
    public float speed;
    private static bool zhrngzaibianbai = false;//正在变白
    private float alpha = 0f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

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

    void OnGUI(){
        if(zhrngzaibianbai){
            alpha += speed * Time.deltaTime;
            if(alpha >= 1){
                zhrngzaibianbai = false;
            }
        }
        else{
            if(alpha>0){
                alpha -= speed * Time.deltaTime;
            }
        }
        GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b,alpha);
        GUI.DrawTexture(new Rect(0,0,Screen.width, Screen.height),img);

    }

    public static void shanshuo(){//闪烁
        zhrngzaibianbai = true;
    }
}
该模块主要是完成小鸟死亡时产生白色闪烁。

结语:

通过对飞翔的小鸟游戏的二次开发,我获得了许多宝贵的经验和教训。虽然经过数次的努力和改进,游戏仍然存在一些缺陷和不足,但这个过程让我更加熟悉了游戏开发的流程和细节,也让我更有信心面对编程中的挑战和困难。在不断地优化和迭代中,我逐渐提高了自己的问题分析能力、解决问题的技巧以及编程实践能力。

展望未来,我将持续努力,不断提升自己的编程技能,追求更高的技术成就。虽然编程之路充满了挑战,但正是这种挑战推动着我不断前行,实现自我超越。感谢飞翔的小鸟游戏二次开发带给我的成长和启示,让我更加坚定地走在编程的道路上!

posted @ 2024-03-04 21:28  XuiMri  阅读(256)  评论(0)    收藏  举报