Skip to content

Instantly share code, notes, and snippets.

@chobby
Last active August 29, 2015 14:07

Revisions

  1. chobby revised this gist Oct 4, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Main.cpp
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@

    //May2014
    # include <Siv3D.hpp>

    void Main()
  2. chobby created this gist Oct 4, 2014.
    139 changes: 139 additions & 0 deletions Main.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,139 @@

    # include <Siv3D.hpp>

    void Main()
    {
    Window::Resize(854, 480);

    Rect window(Window::Size());

    //任意の画像を用意してください
    Texture openingTexture(L"data/opening.png");
    Texture mainTexture(L"data/main.png");
    Texture gameOverTexture(L"data/gameOver.png");
    Texture gameClearTexture(L"data/gameClear.png");

    Font font(30);
    String highScoresString;

    int nowScore = 0;
    std::array<int, 5> highScores;
    highScores.fill(0);

    auto setScore = [&](int score)
    {
    auto it = std::min_element(highScores.begin(), highScores.end());

    if (score > *it)
    {
    *it = score;
    }

    std::sort(highScores.begin(), highScores.end(), std::greater<int>());

    highScoresString.clear();

    for (const auto& score : highScores)
    {
    highScoresString += Format(score, L"\n");
    }
    };

    auto init = [&]()
    {
    nowScore = 0;
    };


    enum class GameState
    {
    Opening,
    Main,
    GameOver,
    GameClear,
    Result
    };

    GameState gameState = GameState::Opening;

    auto opening = [&]()
    {
    if (Input::AnyKeyClicked())
    {
    init();

    gameState = GameState::Main;
    }

    window(openingTexture).draw();
    };

    auto gameMain = [&]()
    {
    if (Input::KeyZ.clicked)
    {
    gameState = GameState::GameClear;
    }
    if (Input::KeyX.clicked)
    {
    gameState = GameState::GameOver;
    }

    if (Input::KeySpace.clicked)
    {
    ++nowScore;
    }

    window(mainTexture).draw();

    font(nowScore).draw();
    };

    auto gameOver = [&]()
    {
    if (Input::AnyKeyClicked())
    {
    gameState = GameState::Opening;
    }

    window(gameOverTexture).draw();
    };

    auto gameClear = [&]()
    {
    if (Input::AnyKeyClicked())
    {
    setScore(nowScore);

    gameState = GameState::Result;
    }

    window(gameClearTexture).draw();
    };

    auto result = [&]()
    {
    if (Input::AnyKeyClicked())
    {
    gameState = GameState::Opening;
    }

    font(highScoresString).drawCenter(Window::Center());
    };


    std::vector<std::function<void()>> sequences =
    {
    opening,
    gameMain,
    gameOver,
    gameClear,
    result
    };


    while (System::Update())
    {
    sequences[static_cast<int>(gameState)]();
    }
    }