クリックしたブロックの色を背景色に混ぜる

クリックしたブロックの色を背景色に混ぜる

このゲームで最も重要な、背景色をクリックした色ブロックの色を混ぜて反映させます(図3)。

背景色はrgb配列の要素で決まります。rgb配列が(1,2,3)の場合、最大の3を分母にして(r,g,b)=(255*1/3,255*2/3,255*3/3)となります。つまりrgb配列の要素がすべて同じ数値なら(r,g,b)=(255,255,255)で真っ白な背景色になり、ステージクリアです。

図3:背景色を反映

・サンプルコード「ColorBlock.cpp」ファイル

(前略)
extern int g_color;

ColorBlock::ColorBlock(Canvas imageCanvas,int c) {
	x = rand() % 1700;
	y = rand() % 800;
	vx = (rand() % 7) - 3;
	vy = (rand() % 7) - 3;
	color = c;
    BitmapImage bitmap;
    bitmap.UriSource(Uri(
        L"ms-appx:///Assets/" + std::to_wstring(c) + L".png"
    ));
    image.Source(bitmap);
    // 表示サイズ
    image.Width(100);
    image.Height(100);
    // Canvas上の位置
    Canvas::SetLeft(image, x);
    Canvas::SetTop(image, y);
    // 動的にイベントを登録
    image.PointerPressed(
        [this](IInspectable const& sender,
            Windows::UI::Xaml::Input::PointerRoutedEventArgs const& e)
        {
            x = 100000;
            g_color = color;
        });
    imageCanvas.Children().Append(image);
}

int ColorBlock::Update() {
(中略)
}

【サンプルコードの解説】
グローバル変数「g_color」をextern(外部定義の参照)として宣言します。
「ColorBlock」クラスで、「image」プロパティの「PointerPressed」でクリックされたらx座標を画面外に移動し、g_colorに色ブロックの色を代入します。

・サンプルコード「MainPage.h」ファイル

(前略)
namespace winrt::colorWinRTUWP::implementation
{
    struct MainPage : MainPageT<MainPage>
    {
        int stage = 2;
        float rgb[3];
        std::vector<std::unique_ptr<ColorBlock>> colors;
        Windows::UI::Xaml::DispatcherTimer timer{ nullptr };
 
        MainPage();
        int32_t MyProperty();
        void MyProperty(int32_t value);

        void TitleImage_PointerPressed(
            Windows::Foundation::IInspectable const& sender,
            Windows::UI::Xaml::Input::PointerRoutedEventArgs const& e);
        void Init();
        void StartTimer();
        void StopTimer();
        void UpdateFrame();
        bool SetBgColor();
    };
}

namespace winrt::colorWinRTUWP::factory_implementation
{
(中略)
}

【サンプルコードの解説】
背景色rgbプロパティを宣言します。

・サンプルコード「MainPage.cpp」ファイル

(前略)
int g_color = -1;

namespace winrt::colorWinRTUWP::implementation
{
    MainPage::MainPage()
    {
(中略)
    }

    int32_t MainPage::MyProperty()
    {
        throw hresult_not_implemented();
    }

    void MainPage::MyProperty(int32_t /* value */)
    {
        throw hresult_not_implemented();
    }

    void MainPage::TitleImage_PointerPressed(
        Windows::Foundation::IInspectable const& sender,
        Windows::UI::Xaml::Input::PointerRoutedEventArgs const& e)
    {
(中略)
    }

    void MainPage::Init()
    {
        rgb[0] = 0;
        rgb[1] = 0;
        rgb[2] = 0;
        while (rgb[0] == rgb[1]) {
            rgb[0] = rand() % stage;
            rgb[1] = rand() % stage;
            rgb[2] = rand() % stage;
        }
        ImageCanvas().Children().Clear();
        colors.clear();
        for (int i = 0; i < 3*stage; ++i)
        {
            colors.push_back(std::make_unique<ColorBlock>(ImageCanvas(), i % 3));
        }
    }

    void MainPage::StartTimer()
    {
(中略)
    }

    void MainPage::StopTimer()
    {
(中略)
    }

    void MainPage::UpdateFrame()
    {
        int flg = 0;
        for (auto& col : colors)
        {
            flg += col->Update();
        }
        if (flg == 0)
        {
            TitleGrid().Visibility(Windows::UI::Xaml::Visibility::Visible);
            ImageCanvas().Visibility(Windows::UI::Xaml::Visibility::Collapsed);
            StopTimer();
        }
        if (SetBgColor())
        {
            stage++;
            Init();
        }
    }

    bool MainPage::SetBgColor()
    {
        if (g_color >= 0)
        {
            rgb[g_color]++;
            g_color = -1;
        }
        int max = (rgb[0] < rgb[1]) ? (rgb[1] < rgb[2] ? 2 : 1) : (rgb[0] < rgb[2] ? 2 : 0);
        float ratio = 255.0 / rgb[max];
        unsigned char r = rgb[0] * ratio;
        unsigned char g = rgb[1] * ratio;
        unsigned char b = rgb[2] * ratio;
        auto color = ColorHelper::FromArgb(255, r, g, b);
        ImageCanvas().Background(SolidColorBrush(color));
        if (r == g && g == b) return true;
        return false;
    }
}

【サンプルコードの解説】
g_colorはグローバル変数として宣言します。これにクリックした色ブロックの色を代入するのですが、グローバルに変数を扱えるところが他の言語と違ってC++がパワフルな点です。
「MainPage」構造体の「Init」メソッドで、rgb配列の各要素にランダムな値を代入します。
「UpdateFrame」メソッドで「SetBgColor」メソッドを呼び出し、背景色が真っ白になったらstageプロパティをインクリメントしてステージクリア処理を行います。
SetBgColorメソッドで、g_colorが-1ではなく色ブロックの0~2のいずれかであれば、rgb配列の該当するインデックスの要素をインクリメントし、g_colorにクリックなしを示す-1を代入します。
rgb配列の最大の要素のインデックスをmax変数に代入し、そこから背景色(r,g,b)を計算してImageCanvasの背景色にセットします。r・g・bが同値なら戻り値としてtrueを返し、そうでなければfalseを返します。

【コラム】3Dのクオリティ

PlayStation5が発売されて6年も経って、今さらクオリティの凄さを見ました。映画並なので映画並の予算で作る3Dデザイナーが賞金3,000円の似顔絵塾に投稿する訳がありませんでした。だから僕の3DCGでも似顔絵塾に掲載されたんでしょう。

おわりに

今回は、一般に「C#」「Visual Basic.NET」向けの「.NET」技術を「C++」から扱える「C++/WinRT」で「UWP」をプログラミングしてColorゲームを開発しました。これまでの連載で、Visual Studioの.NETデスクトップアプリ開発(WinUI、UWP、WPF、Forms)に関しては、だいたいできるようになったのではないでしょうか。

人気記事トップ10

人気記事ランキングをもっと見る

企画広告も役立つ情報バッチリ! Sponsored