简体   繁体   English

死后如何重置值

[英]How to reset values after dying

I am making a google chrome dinosaur game using an SDL Template for graphics and I am almost finished but I have run into the issue of needing to be able to reset to the starting values of the game just like how you reset when you hit spacebar after dying in the google chrome dinosaur game.我正在使用 SDL 图形模板制作 google chrome 恐龙游戏,我快完成了,但我遇到了需要能够重置为游戏的起始值的问题,就像你在按下空格键后的重置方式一样在 google chrome 恐龙游戏中死去。 I have made an isGameOver function but I don't know how to reset the values to start a new game which is my main problem.我做了一个 isGameOver function 但我不知道如何重置值以开始新游戏这是我的主要问题。

this is my GameScene class where the game over logic is located这是我的 GameScene class 游戏结束逻辑所在的位置

GameScene.h游戏场景.h

#pragma once
#include "Scene.h"
#include "GameObject.h"
#include "Player.h"
#include "Floor.h"
#include "Obstacle.h"
#include "FlyingObstacle.h"
#include <vector>
#include "util.h"
#include "text.h"

class GameScene : public Scene
{
public:
    GameScene();
    ~GameScene();
    void start();
    void draw();
    void update();
    std::vector<Obstacle*> spawnedObstacle;
    std::vector<FlyingObstacle*> spawnedBird;

private:
    Player* player;
    Floor* floor;

    float spawnTime;
    float currentSpawnTimer;
    void floorCollision();
    void obstacleCollision();
    void birdCollision();
    void obstacleSpawn();
    void birdSpawn();
    void despawnObstacle(Obstacle* obstacle);
    void despawnBird(FlyingObstacle* bird);
    int points;
    int highscore;
    bool isGameOver;
};

GameScene.cpp游戏场景.cpp

#include "GameScene.h"

GameScene::GameScene()
{
    // Register and add game objects on constructor
    player = new Player();
    this->addGameObject(player);

    floor = new Floor();
    this->addGameObject(floor);

    points = 0;
    highscore = 0;

}

GameScene::~GameScene()
{
    delete player;
}

void GameScene::start()
{
    Scene::start();
    // Initialize any scene logic here

    initFonts();
    isGameOver = true;
    currentSpawnTimer = 300;
    spawnTime = rand() % 300; //spawn time of 5 seconds

    for (int i = 0; i < 1; i++)
    {

        obstacleSpawn();

    }

    for (int i = 0; i < 3; i++)
    {
        birdSpawn();
    }


}

void GameScene::draw()
{
    Scene::draw();

        drawText(110, 20, 255, 255, 255, TEXT_CENTER, "POINTS: %03d", points);


        if (player->getIsAlive() == true)
        {
            drawText(900, 20, 255, 255, 255, TEXT_CENTER, "PRESS SPACE TO START MOVING");
        }

    if (player->getIsAlive() == false)
    {
        if (isGameOver == false)

        drawText(SCREEN_WIDTH / 2, 200, 255, 255, 255, TEXT_CENTER, "YOU LOSE! PRESS SPACE TO SHOW POINTS");

        if (isGameOver == true)
        {
            drawText(SCREEN_WIDTH / 2, 200, 255, 255, 255, TEXT_CENTER, "HIGHSCORE: %03d", highscore); 

                if (points > highscore)
                {
                    drawText(SCREEN_WIDTH / 2, 200, 255, 255, 255, TEXT_CENTER, "NEW HIGHSCORE: %03d", points, highscore);
                }
        }
    }
}

void GameScene::update()
{

    if (isGameOver == true)
    {
        if (app.keyboard[SDL_SCANCODE_SPACE])
        {
            isGameOver = false;
            
        }

    }
    if (isGameOver == false)
    {
        Scene::update();

        floorCollision();
        obstacleCollision();
        birdCollision();


        if (currentSpawnTimer > 0)
            currentSpawnTimer--;

        if (currentSpawnTimer <= 0)
        {
            for (int i = 0; i < 1; i++)
            {
                obstacleSpawn();
            }

            for (int i = 0; i < 3; i++)
            {
                birdSpawn();
            }

            currentSpawnTimer = spawnTime;
        }
        //This is where Gravity strength is located
        if (player->getOnFloor() == false) {
            player->setY(player->getY() + 7);
        }

        else {
            player->getY() + 0;
        }
    }
}



void GameScene::floorCollision()
{
        //Checks for collisions
        for (int i = 0; i < objects.size(); i++)
        {
            //Cast to floor
            Floor* floor = dynamic_cast<Floor*>(objects[i]);

            //Check if the floor was casted
            if (floor != NULL)
            {

                int collision = checkCollision(
                    player->getX(), player->getY(), player->getWidth(), player->getHeight(),
                    floor->getX(), floor->getY(), floor->getWidth(), floor->getHeight()
                );

                if (collision == 1)
                {

                    player->setOnFloor(true);
                    
                    if (player->getIsAlive() == true)
                    {
                        points++;
                        highscore++;
                    }

                    break;
                }

            }
        }
}


void GameScene::obstacleCollision()
{
    for (int i = 0; i < objects.size(); i++)
    {

        Obstacle* obstacle = dynamic_cast<Obstacle*>(objects[i]);

        if (obstacle != NULL)
        {
            if (obstacle != NULL)
            {
                int collision = checkCollision(
                    player->getX(), player->getY(), player->getWidth(), player->getHeight(),
                    obstacle->getX(), obstacle->getY(), obstacle->getWidth(), obstacle->getHeight()
                );

                if (collision == 1)
                {
                    player->doDeath();
                    isGameOver = true;
                    break;
                }
            }
        }

    }

}

void GameScene::birdCollision()
{
    for (int i = 0; i < objects.size(); i++)
    {

        FlyingObstacle* bird = dynamic_cast<FlyingObstacle*>(objects[i]);


        if (bird != NULL)
        {
            if (bird != NULL)
            {
                int collision = checkCollision(
                    player->getX(), player->getY(), player->getWidth(), player->getHeight(),
                    bird->getX(), bird->getY(), bird->getWidth(), bird->getHeight()
                );

                if (collision == 1)
                {
                    player->doDeath();
                    isGameOver = true;
                    break;
                }
            }
        }

    }
}


void GameScene::obstacleSpawn()
{
    Obstacle* obstacle = new Obstacle();
    this->addGameObject(obstacle);

    obstacle->setPosition(1200, 300 + (rand() % 300));
    spawnedObstacle.push_back(obstacle);
}

void GameScene::birdSpawn()
{
    FlyingObstacle* bird = new FlyingObstacle();
    this->addGameObject(bird);

    bird->setPos(1200, 300 + (rand() % 300));
    spawnedBird.push_back(bird);
}

void GameScene::despawnObstacle(Obstacle* obstacle)
{
    int index = -1;
    for (int i = 0; i < spawnedObstacle.size(); i++)
    {
        //If pointer matches
        if (obstacle == spawnedObstacle[i])
        {
            index = i;
            break;
        }
    }

    //If any match is found
    if (index != -1)
    {
        spawnedObstacle.erase(spawnedObstacle.begin() + index);
        delete obstacle;
    }
}

void GameScene::despawnBird(FlyingObstacle* bird)
{
    int index = -1;
    for (int i = 0; i < spawnedBird.size(); i++)
    {
        //If pointer matches
        if (bird == spawnedBird[i])
        {
            index = i;
            break;
        }
    }

    //If any match is found
    if (index != -1)
    {
        spawnedBird.erase(spawnedBird.begin() + index);
        delete bird;
    }
}

I tried making an isGameOver bool inside my GameScene.h and I made it so that pressing spacebar would reset the game but in reality when the player dies the screen pauses every movement instead of resetting and if I press space again the game continues to move even though the player is dead.我尝试在我的 GameScene.h 中制作一个 isGameOver bool 并且我这样做是为了按下空格键会重置游戏但实际上当玩家死亡时屏幕会暂停每一个动作而不是重置并且如果我再次按下空格键游戏会继续移动甚至虽然玩家已经死了。

Your entry point for the game(probably main function will probably have some form like this)您的游戏入口点(可能main function 可能有这样的形式)

#include ...
#include ...
...

int main()
{
   bool game_running = true;
   while(game_running)
   {
       GameInstance* game = new Game(); //Allocation and initialization of all resources needed to play the game.
       game.play(); // This function will finish when you lose the game.
       delete game; // Deallocation.
       if(!run_again())
       {
           game_running = false;
       }
   }

   return 0;
}

After deallocation, you check if user wants to play again, you enter the next iteration of while loop, and new instance of Game is allocated, where all the resource handling takes place.解除分配后,检查用户是否想再次玩游戏,进入while循环的下一次迭代,分配新的Game实例,所有资源处理都在此处进行。 The only problem is that allocating Game instance for every iteration might be a little costly, but you can ignore that if you don't worry about performance much.唯一的问题是为每次迭代分配Game实例可能有点昂贵,但如果您不太担心性能,则可以忽略它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM