FlappyBird开发总结(七)—— GameOver计分板

这里写图片描述
这是一个新的Scene,就是小鸟死亡后跳转的界面,小子不才,只会用NGUI做出这样子,对应关系我画出来了,这是三个拖黑的物体我是放在Again按钮的下面,也就是作为它的子物体,这个只是一个布局,你放哪都一样。
好了,在这之前我们好歹得让小鸟死亡吧,怎么死亡呢?
当小鸟在发生以下事件就触发死亡:
1、碰撞地面
2、碰撞水管
3、貌似没了。。。。。
好了,怎么做呢?
首先,请创建一个脚本 ColiderFloor.cs,也就是当掉到地面上时,我们让小鸟死亡,当然,你得把脚本放到每个背景的地面上啊,地面也得有碰撞器哦,代码如下:

using UnityEngine;
using System.Collections;

public class ColliderFloor : MonoBehaviour {

    void OnCollisionEnter(Collision gameObject)
    {

        if (gameObject.gameObject.tag == "Player")
        {
            audio.Play();//播放碰撞的声源
            GameManager.gameInstance.currentGameState = (int)GameManager.GameState.GAMEOVER;//将游戏状态设为GAMEOVER
        }
    }


}

好了,地面的设置完成,接下来就是每个背景的上下两个水管了,新建脚本ColiderPipe.cs,然后放到各个上下两个水管物体下(确定你的水管有碰撞器哦),
脚本代码:

using UnityEngine;
using System.Collections;

public class ColliderPipe : MonoBehaviour {
    public AudioSource hit;//撞击时播放的声源
    public AudioSource die;//小鸟哦死亡的声源
    private float  delayTime = 0.5f;
    void OnCollisionEnter(Collision gameObject)
    {

        if(gameObject.gameObject.tag =="Player")
        {

            StartCoroutine(RetuenTheState());//喜闻乐见的协程函数

        }
    }

    IEnumerator RetuenTheState()
    {
        hit.Play();//先播放撞击的声源
        GameManager.gameInstance.currentGameState = (int)GameManager.GameState.GAMEOVER;//设置当前游戏状态为GAMEOVER状态
        yield return new WaitForSeconds(delayTime);//等待0.5秒
        die.Play();//等待0.5秒后播放小鸟死亡声源
    }
}

喔了,各种死亡情况的代码都写好了,接下来回到GameManager.cs脚本,还记得我们之前几行在Update()中没解释的代码么?就是它了:

    void Update
    {
        if(currentGameState==(int)GameState.GAMEOVER)//当小鸟死亡,也就是当前状态是GAMEOVER时执行以下代码
        {

           RemenberScores.currentScore = currentScores;
           StartCoroutine("ChangeScene");

        }
    }
    IEnumerator ChangeScene()
    {
        yield return new WaitForSeconds(delayTime);//延迟delayTime秒
        Application.LoadLevel("MenuScene");//延迟后跳转到计分面板的场景

    }

好吧,问题来了

RemenberScores.currentScore = currentScores;

这是干嘛的?
试想一下,我们当前计分的是GameManager.cs中的

public int currentScores=0;

然而我们怎样把这个值传到下个场景中的Labl中去?这就是我们需要上面代码的意思了,当然,不仅仅是它,嘿嘿,只有它当然会报错。
这里就是需要一个单例类RemenberScores.cs了,下面贴代码

using UnityEngine;
using System.Collections;

public class RemenberScores {

    public static RemenberScores scoresInstance;

    public static float currentScore;

    private GameObject scoreTitle;
    void Awake()
    {
        scoresInstance = this;
    }
    void Update()
    {
        //当按返回时结束游戏
        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }
    }
}

这个脚本不要放到任何物体下哦,这样我们就能在静态存储池用currentScore这个变量存储当前的分数咯。so,现在我们就来到计分板的场景吧,我的场景名是MenuScene。
这里我创建了一个脚本MenuManager.cs,直接拖到了这个场景的根物体上,代码如下:

using UnityEngine;
using System.Collections;

public class MenuManager : MonoBehaviour {
    public UILabel currentScores;
    public UILabel bestScores;
    public static MenuManager menuInstance;
    private float currentScore;
    void Awake(){
        menuInstance = this;
        currentScore =RemenberScores.currentScore;
        UpdateTheScores(currentScore);
    }
    public void UpdateTheScores(float currentScore)
    {

        float bestScore = PlayerPrefs.GetFloat("score", 0);
        if (currentScore > bestScore)
        {
            bestScore = currentScore;
            GameObject.Find("Title").GetComponent<UILabel>().text = "Good Score!";
        }
        else
        {
            GameObject.Find("Title").GetComponent<UILabel>().text = "No Good!";
        }
        PlayerPrefs.SetFloat("score",bestScore);

        currentScores.text = currentScore + "";
        bestScores.text = bestScore + "";



    }
}

左看右看没什么难点,关于PlyerPrefs.SetFloat(),以及 PlayerPrefs.SetFloat(),给大家一个大神的链接吧 http://blog.csdn.net/qinyuanpei/article/details/24195977

好吧,现在分数也记录了,场景也能跳转了。。。
你们会说我瞎了吧,那么大三个按钮还没写。。。好吧。。。我贴上来。。这里提一下,MainMenu的场景在下一篇来写,主要是用来控制游戏难度的,说白了就是控制小鸟的速度。。。
按钮MANINMENU的脚本:

using UnityEngine;
using System.Collections;

public class LoadMainMenuScene : MonoBehaviour {

    public void LoadMainMenu()
    {
        Application.LoadLevel("MainScene");
    }

}

按钮AGAIN的脚本

using UnityEngine;
using System.Collections;

public class ChangeToStartGameScene : MonoBehaviour {

    public void GoGame()
    {
        Application.LoadLevel("StartGame");
    }

}

按钮EXIT的脚本

using UnityEngine;
using System.Collections;

public class EXIT : MonoBehaviour {

    public void GoOut()
    {
        Application.Quit();
    }
}

好吧,别打我。。。我是用NGUI写的,菜鸟表示只会这样。。。好了。。。这章OVER了,计算板实现完毕。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的 Flappy Bird 游戏的 C++ 代码示例: ```c++ #include<iostream> #include<conio.h> #include<cstdlib> #include<ctime> #include<windows.h> using namespace std; bool gameOver; // 游戏结束标志 const int width = 20; // 游戏宽度 const int height = 20; // 游戏高度 int x, y; // 玩家坐标 int fruitX, fruitY; // 水果坐标 int score; // 分数 int tailX[100], tailY[100]; // 尾巴坐标 int nTail; // 尾巴长度 enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN}; // 方向枚举 eDirection dir; void Setup() // 初始设置 { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() // 绘制游戏画面 { system("cls"); // 清空屏幕 // 绘制上边框 for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; // 绘制游戏区域 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) cout << "#"; if (i == y && j == x) // 绘制玩家 cout << "O"; else if (i == fruitY && j == fruitX) // 绘制水果 cout << "F"; else { bool print = false; for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { cout << "o"; // 绘制尾巴 print = true; } } if (!print) cout << " "; } if (j == width - 1) cout << "#"; } cout << endl; } // 绘制下边框 for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; // 显示分数 cout << "Score:" << score << endl; } void Input() // 处理输入 { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } } void Logic() // 逻辑处理 { // 处理尾巴 int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } // 处理方向 switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } // 处理撞墙 if (x >= width || x < 0 || y >= height || y < 0) gameOver = true; // 处理撞尾巴 for (int i = 0; i < nTail; i++) { if (tailX[i] == x && tailY[i] == y) gameOver = true; } // 处理吃水果 if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; nTail++; } } int main() { srand(time(0)); // 随机数种子 Setup(); // 初始设置 while (!gameOver) { Draw(); // 绘制游戏画面 Input(); // 处理输入 Logic(); // 处理逻辑 Sleep(100); // 暂停100毫秒 } return 0; } ``` 注意:该代码仅供学习和参考,不保证完全正确和可运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值