12 ディスプレイに猫を動かしてみよう

猫画像の設定

下の画像を右クリックして「名前を付けて画像を保存」してください。保存時には下部のファイル名を用いてください。画像サイズは32×37となっています。

cat1_a.bmpcat1_b.bmp ソリューションのあるフォルダ内に入れておくと便利です。

リソースを登録する

プロジェクトのプロパティをクリックします
リソースを選びます
リソースの追加の右側の三角をクリックします
既存のファイルの追加をクリックします
保存したcat1_a.bmpファイルを選択するとリソースに追加されます。このアイコンをダブルクリックすると編集モードになります。
右下のプロパティのところの形式を16bpp BGR565にします。これを行わないと、これからこのリソースを読み込んで、プログラムで表示できる形式に変換できません。同様にcat1_b.bmpも読み込んでください。



猫を歩かせる
public partial class Program
{
    //                                                              x   y
    static BrainPad.Display.Image cat1 = new BrainPad.Display.Image(32, 37);
    static BrainPad.Display.Image cat2 = new BrainPad.Display.Image(32, 37);
    private void main()
    {
        BrainPad.Display.Clear();
        BrainPad.LightBulb.SetColor(BrainPad.Color.Black);
        getResourceBmp(cat1, Resources.BinaryResources.cat1_a);
        getResourceBmp(cat2, Resources.BinaryResources.cat1_b);
        while (true)
        {
            BrainPad.Display.DrawImage(64, 80, cat1);
            Thread.Sleep(500);
            BrainPad.Display.DrawImage(64, 80, cat2);
            Thread.Sleep(500);
        }
    }

    static void getResourceBmp(BrainPad.Display.Image image, Resources.BinaryResources id)
    {           // 16bpp BGR565から変換する
        byte[] z = (byte[])
                ResourceUtility.GetObject(Resources.ResourceManager, id); //32*37=2368
        BrainPad.Color color = new BrainPad.Color();
        int dt = 0x42;      // BMPのデータ開始アドレス
        for (int Y = image.Height - 1; Y >= 0; Y--)
        {
            for (int X = 0; X < image.Width; X++)
            {    // ピクセルデータの取得
                byte d = z[dt++];
                byte e = z[dt++];
                color.B = (byte)(d & 0x1f);
                color.G = (byte)(((d & 0xe0) >> 5) + ((e & 0x07) << 3));
                color.R = (byte)((e & 0xf8) >> 3);
                image.SetPixel(X, Y, color);
            }
        }
    }
}
 1 
 2 
 3 
 4 表示用オブジェクトを作る
 5 画像の入れ物を作る
 6 
 7 
 8 ディスプレイ消去
 9 
10 リソースからオブジェクトに変換
11 
12 
13 
14 x=64,y=80に表示させる
15 
16 
17 
18 
19 
20 
21 
22 
23 リソースオブジェクトデータを
24 一次元配列として得る
25 カラーオブジェクト
26 ヘッダーデータを飛ばす
27 
28 
29 
30 
31 
32 
33 BGR565からLCD用bitmapに変換する
34 
35 
36 
37 
38 
39 
40 



少し高度なプログラミング

12A ボールを跳ね返させる

 前項では、2つのグラフィックデータを交互に表示させました。今度は、ボールを動かしてみます。始めは簡単に横方向にだけ動かします。

これを右クリックしてダウンロードして利用してください。


ボールを動かす
    public partial class Program
    {
        static int Size = 16;
        static BrainPad.Display.Image ball = new BrainPad.Display.Image(Size, Size);
        static BrainPad.Display.Image black = new BrainPad.Display.Image(Size, Size);

        private void main()
        {
            BrainPad.Display.Clear();
            BrainPad.LightBulb.SetColor(BrainPad.Color.Black);
            getResourceBmp(ball, Resources.BinaryResources.tennis);
            for (int x = 0; x < Size; x++)
                for (int y = 0; y < Size; y++)
                {
                    black.SetPixel(x, y, BrainPad.Color.Black);
                }
            while (true)
            {
                for (int i = 0; i < 160- Size; i += 10)
                {
                    BrainPad.Display.DrawImage(i, 80, ball);
                    Thread.Sleep(500);
                    BrainPad.Display.DrawImage(i, 80, black);
                }
            }
        }
        static void getResourceBmp(BrainPad.Display.Image image, Resources.BinaryResources id)
        {           // 16bpp BGR565から変換する
            byte[] z = (byte[])
                    ResourceUtility.GetObject(Resources.ResourceManager, id); //16*16=256
            BrainPad.Color color = new BrainPad.Color();
            int dt = 0x42;      // BMPのデータ開始アドレス
            for (int Y = image.Height - 1; Y >= 0; Y--)
            {
                for (int X = 0; X < image.Width; X++)
                {    // ピクセルデータの取得
                    byte d = z[dt++];
                    byte e = z[dt++];
                    color.B = (byte)(d & 0x1f);
                    color.G = (byte)(((d & 0xe0) >> 5) + ((e & 0x07) << 3));
                    color.R = (byte)((e & 0xf8) >> 3);
                    image.SetPixel(X, Y, color);
                }
            }
        }
    }
 1 
 2 
 3 
 4 画像の入れ物を作る
 5 画像の入れ物を作る
 6 
 7 
 8 
 9 ディスプレイ消去
10 電球LEDも消しておく
11 リソースから入れ物に入れる
12 
13 
14 
15 黒の画像を作る
16 
17 
18 
19 
20 
21 ボールの表示
22 500ms待つ
23 ボールを消す
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 


複数のボールを跳ね返させる

 ボールを複数つくり、斜めに動かし、壁にぶつかった場合に反射させます。

複数のボールを跳ね返させる
    public partial class Program
    {
        private void main()
        {
            BrainPad.Display.Clear();
            BrainPad.LightBulb.SetColor(BrainPad.Color.Black);

            Ball ball = new Ball(22, 14);
            Ball ball2 = new Ball(102, 54);
            while (true)
            {
                ball.move();
                ball2.move();
                Thread.Sleep(300);
            }
        }
    }
    class Ball
    {
        const int Size = 16;
        static BrainPad.Display.Image ball = new BrainPad.Display.Image(Size, Size);
        static BrainPad.Display.Image black = new BrainPad.Display.Image(Size, Size);
        static int rightLimit = 160, bottomLimit = 128;
        int x = 0, y = 0, step=8;
        bool xDir = false, yDir = false;
        public Ball(int x, int y)
        {
            this.x = x;
            this.y = y;
            getResourceBmp(ball, Resources.BinaryResources.tennis);
            for (int X = 0; X < Size; X++)
                for (int Y = 0; Y < Size; Y++)
                {
                    black.SetPixel(X, Y, BrainPad.Color.Black);
                }
        }
        public void move()
        {
            BrainPad.Display.DrawImage(x,y, black);
            if (!xDir) x += step;
            else x -= step;
            if (!yDir) y+=step;
            else y-=step;
            if (x < 0) { x += step; xDir = !xDir; }
            if (rightLimit - Size <= x) {
                x -= step; xDir = !xDir; }
            if (y < 0) { y =0; yDir = !yDir; }
            if (bottomLimit - Size <= y)
            {
                y = bottomLimit- Size; yDir = !yDir;
            }
            BrainPad.Display.DrawImage(x, y, ball);
        }
        static void getResourceBmp(BrainPad.Display.Image image, Resources.BinaryResources id)
        {           // 16bpp BGR565から変換する
            byte[] z = (byte[])
                    ResourceUtility.GetObject(Resources.ResourceManager, id); //16*16=256
            BrainPad.Color color = new BrainPad.Color();
            int dt = 0x42;      // BMPのデータ開始アドレス
            for (int Y = image.Height - 1; Y >= 0; Y--)
            {
                for (int X = 0; X < image.Width; X++)
                {    // ピクセルデータの取得
                    byte d = z[dt++];
                    byte e = z[dt++];
                    color.B = (byte)(d & 0x1f);
                    color.G = (byte)(((d & 0xe0) >> 5) + ((e & 0x07) << 3));
                    color.R = (byte)((e & 0xf8) >> 3);
                    image.SetPixel(X, Y, color);
                }
            }
        }
    }
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 ボールを設定座標に置く
 9 2つ目のボールを置く
10 
11 
12 ボールを動かす
13 2つ目のボールを動かす
14 300ms待つ
15 
16 
17 
18 
19 
20 
21 ボールの入れ物
22 黒の入れ物
23 
24 
25 
26 
27 
28 
29 
30 リソースから画像に変換
31 
32 
33 
34 黒画像の作成
35 
36 
37 
38 
39 以前描画した画像を消す
40 動かす
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 ボールを描画する
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 


ボールごとに動くスピードを設定する

 スレッドを用いて、各々のスレッド上でボールを動かします。この場合、完全に独立に動作させることが可能です。
Ballクラスは前項と同じなので、省略します。

スレッドでボールを動かす
    public partial class Program
    {
        private void main()
        {
            BrainPad.Display.Clear();
            BrainPad.LightBulb.SetColor(BrainPad.Color.Black);
            new Thread(new ThreadStart(new PingPong(12, 67,100).ping)).Start();
            new Thread(new ThreadStart(new PingPong(69, 6,200).ping)).Start();
            while (true)
            {
                Thread.Sleep(1000);
            }
        }
    }

    class PingPong
    {
        Ball ball;
        int sleep;
        public PingPong(int x, int y,int sleep)
        {
            this.sleep = sleep;
            ball = new Ball(x, y);
        }
        public void ping()
        {
            while (true)
            {
                ball.move();
                Thread.Sleep(sleep);
            }
        }
    }

 1 
 2 
 3 
 4 
 5 
 6 
 7 100msで動くボールを表示させる
 8 200msで動くボールを表示させる
 9 
10 
11 取りあえず1秒ごと待つ
12 実際は他の仕事をさせる
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 ボールを描画する
24 
25 
26 
27 
28 
29 動かす
30 指定時間待つ
31 
32 
33