Last active
September 4, 2016 08:29
-
-
Save shield-9/475128d26844d96585ed688bf074cf93 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <Siv3D.hpp> | |
# include <HamFramework.hpp> | |
struct CommonData | |
{ | |
int counter = 0; | |
int score = 0; | |
// 大きさ 30 のフォントを用意 | |
Font font{30}; | |
}; | |
using MyApp = SceneManager<String, CommonData>; | |
class Title : public MyApp::Scene | |
{ | |
public: | |
void update() override | |
{ | |
if (Input::MouseL.clicked) | |
{ | |
++m_data->counter; | |
changeScene(L"Game"); | |
} | |
} | |
void draw() const override | |
{ | |
Window::ClientRect().draw(Palette::Blue); | |
m_data->font(L"TITLE").drawCenter(Window::Center()); | |
} | |
}; | |
class Game : public MyApp::Scene | |
{ | |
public: | |
const Sound m_sound{ Wave(0.1s, [](double t) { return Fraction(t * 1760)*0.5 - 0.25; }) }; // ラムダ式 | |
const Point m_blockSize{ 40, 20 }; | |
const double m_speed = 8.0; | |
Rect m_wall{ 60, 10 }; | |
Circle m_ball{ 320, 400, 8 }; | |
Vec2 m_ballSpeed{ 0, -m_speed }; | |
Array<Rect> m_blocks; | |
void init() override | |
{ | |
for (auto p : step({ Window::Width() / m_blockSize.x , 5 })) | |
{ | |
m_blocks.emplace_back((p*m_blockSize).moveBy(0, 60), m_blockSize); // 参照で追加 | |
} | |
} | |
void update() override | |
{ | |
m_ball.moveBy(m_ballSpeed); | |
m_wall.setCenter(Mouse::Pos().x, 420); | |
for (auto it = m_blocks.begin(); it != m_blocks.end(); ++it) // イテレータ(iterator) | |
{ | |
if (it->intersects(m_ball)) // 当たり判定 | |
{ | |
(it->bottom.intersects(m_ball) || it->top.intersects(m_ball) // 移動方向を逆に(反発) | |
? m_ballSpeed.y : m_ballSpeed.x) *= -1; | |
m_blocks.erase(it); // ブロックを消す | |
m_data->score++; | |
m_sound.playMulti(); // | |
break; | |
} | |
} | |
for (auto const& block : m_blocks) | |
{ | |
block.stretched(-1).draw(HSV(block.y - 40)); // x,y両軸方向に1pxずつ縮めて表示 | |
} | |
if (m_ball.y < 0 && m_ballSpeed.y < 0) // 上の壁 | |
{ | |
m_ballSpeed.y *= -1; | |
} | |
if ((m_ball.x < 0 && m_ballSpeed.x < 0) || (Window::Width() < m_ball.x && m_ballSpeed.x > 0)) // 左右 | |
{ | |
m_ballSpeed.x *= -1; | |
} | |
if (m_ballSpeed.y > 0 && m_wall.intersects(m_ball)) // バーとの衝突 | |
{ | |
m_ballSpeed = Vec2((m_ball.x - m_wall.center.x) / 8, -m_ballSpeed.y).setLength(m_speed); | |
} | |
if (m_ball.y > Window::Height() && m_ballSpeed.y > 0) // 下の壁 | |
{ | |
++m_data->counter; | |
changeScene(L"Result"); | |
} | |
} | |
void draw() const override | |
{ | |
m_ball.draw(); | |
m_wall.draw(); | |
m_data->font(L"スコア: ", m_data->score).drawCenter(Window::Center()); | |
} | |
}; | |
class Result : public MyApp::Scene | |
{ | |
public: | |
void update() override | |
{ | |
if (Input::MouseL.clicked) | |
{ | |
++m_data->counter; | |
changeScene(L"Title"); | |
} | |
} | |
void draw() const override | |
{ | |
Window::ClientRect().draw(Palette::Green); | |
m_data->font(L"RESULT:", m_data->score).drawCenter(Window::Center()); | |
} | |
}; | |
void Main() | |
{ | |
MyApp manager; | |
manager.add<Title>(L"Title"); | |
manager.add<Game>(L"Game"); | |
manager.add<Result>(L"Result"); | |
while (System::Update()) | |
{ | |
manager.updateAndDraw(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment