Skip to content

Instantly share code, notes, and snippets.

@chobby
Last active August 29, 2015 14:07
Show Gist options
  • Save chobby/2d5cb208bf51d0245cf0 to your computer and use it in GitHub Desktop.
Save chobby/2d5cb208bf51d0245cf0 to your computer and use it in GitHub Desktop.
//May2014
# 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)]();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment