0

この次の方法を使用して 7 セグメント表示を描画しようとしていますが、デバッガーを実行しても理由が​​わかりませんが、何らかの理由で数字が表示されません。ここで何が問題なのですか?大きな配列は無視してかまいません。これは、値を格納する方法を示すためのものです。

    private void DrawScore(SpriteBatch spriteBatch, int score, int playerNumber)
    {
        int[,,] numbers =
            {
                // Zero 
                // Output: 
                // [ ][.][ ]  [.] = white square [ ] = black square
                // [.][ ][.]
                // [ ][.][ ]
                // [.][ ][.]
                // [ ][.][ ]   
                {
                    {0, 1, 0},
                    {1, 0, 1},
                    {0, 0, 0},
                    {1, 0, 1},
                    {0, 1, 0}
                },
                {
                    {0, 0, 0},
                    {0, 0, 1},
                    {0, 0, 0},
                    {0, 0, 1},
                    {0, 0, 0}
                },
                {
                    {0, 1, 0},
                    {0, 0, 1},
                    {0, 1, 0},
                    {1, 0, 0},
                    {0, 1, 0}
                },

                {
                    {0, 1, 0},
                    {0, 0, 1},
                    {0, 1, 0},
                    {0, 0, 1},
                    {0, 1, 0}
                },
                {
                    {0, 0, 0},
                    {1, 0, 1},
                    {0, 1, 0},
                    {0, 0, 1},
                    {0, 0, 0}
                },
                {
                    {0, 1, 0},
                    {1, 0, 0},
                    {0, 1, 0},
                    {0, 0, 1},
                    {0, 1, 0}
                },
                {
                    {0, 1, 0},
                    {1, 0, 0},
                    {0, 1, 0},
                    {1, 0, 1},
                    {0, 1, 0}
                },
                {
                    {0, 1, 0},
                    {0, 0, 1},
                    {0, 0, 0},
                    {0, 0, 1},
                    {0, 0, 0}
                },
                {
                    {0, 1, 0},
                    {1, 0, 1},
                    {0, 1, 0},
                    {1, 0, 1},
                    {0, 1, 0}
                },
                {
                    {0, 1, 0},
                    {1, 0, 1},
                    {0, 1, 0},
                    {0, 0, 1},
                    {0, 0, 0}
                }
            };

        for (int i = 0; i < numbers.GetLength(1); i++)
        {
            for (int j = 0; j < numbers.GetLength(2); j++)
            {
                Debug.WriteLine("Score: {0}", score);
                Debug.WriteLine("\ti, j: {0}", numbers[score, i, j]);
                if (playerNumber == 1)
                {
                    spriteBatch.Draw(numbers[score, i, j] == 0 ? _scoreSegmentTexBlack : _scoreSegmentTexWhite,
                                     new Vector2(
                                         (Graphics.PreferredBackBufferWidth/2) - _scoreSegmentTex.Width*(3 + i),
                                         _scoreSegmentTex.Height*j + 1),
                                     Color.White);
                }
                if (playerNumber == 2)
                {
                    spriteBatch.Draw(numbers[score, i, j] == 0 ? _scoreSegmentTexBlack : _scoreSegmentTexWhite,
                                     new Vector2(
                                         (Graphics.PreferredBackBufferWidth / 2) + _scoreSegmentTex.Width*(1 + i),
                                         _scoreSegmentTex.Height*j + 1),
                                     Color.White);
                }
            }
        }



    }
4

2 に答える 2

0

代わりに、これを処理するクラス全体を作成することにしました。

下の図は、(0, 0) に描画された '0' です。(x = 0, y = 0) 以下のコードを使用:

SevenSegmentDisplay myDisplay = new SevenSegmentDisplay(0, 0);
// Inside the game loop somewhere.
if (playerScoreCondition) {
    // player.Score++;
    // In this case score is still 0.
    myDisplay.Update(player.Score);    
}
// Draw
myDisplay.Draw(spriteBatch, segmentTexture, scoreDisplayX, scoreDisplayY, Color.White, new Color(30, 30, 30, 255));

0時に描いた7セグ表示の絵

internal class SevenSegmentDisplay
{
    private int a, b, c, d, e, f, g;

    private readonly int[,] numbers;

    public SevenSegmentDisplay()
    {
        numbers = new[,] {
              /* Format is A - G, see: 
               * https://en.wikipedia.org/wiki/Seven-segment_display
               */
              // 0
              {1, 1, 1, 1, 1, 1, 0},
              // 1
              {0, 1, 1, 0, 0, 0, 0},
              // 2
              {1, 1, 0, 1, 1, 0, 1},
              // 3
              {1, 1, 1, 1, 0, 0, 1},
              // 4
              {0, 1, 1, 0, 0, 1, 1},
              // 5
              {1, 0, 1, 1, 0, 1, 1},
              // 6
              {1, 0, 1, 1, 1, 1, 1},
              // 7
              {1, 1, 1, 0, 0, 0, 0},
              // 8
              {1, 1, 1, 1, 1, 1, 1},
              // 9
              {1, 1, 1, 1, 0, 1, 1}
        };
        // Initialize each segment to 0 (black)
        a = 0;
        b = 0;
        c = 0;
        d = 0;
        e = 0;
        f = 0;
        g = 0;
    }

    private void Update(IList<int> i)
    {
        // Update each segment
        a = i[0];
        b = i[1];
        c = i[2];
        d = i[3];
        e = i[4];
        f = i[5];
        g = i[6];
    }

    public void Update(int i)
    {
        Update(IntToSevenSegment(i));
    }

    private int[] IntToSevenSegment(int i)
    {
        int[] temp = new int[7];

        for (int counter = 0; counter < 7; counter++)
            temp[counter] = numbers[i, counter];

        return temp;
    }

    public void Draw(SpriteBatch spriteBatch, Texture2D texture, int x, int y, Color on, Color off)
    {
        // Texture should be a white square, to handle the drawing of each segment.
        // Handle each segment A - G and draw them according to their positions depending on the texture size.

        Rectangle a = new Rectangle(x + texture.Width, y, texture.Width*2, texture.Height);
        Rectangle b = new Rectangle(x + texture.Width*3, y + texture.Height, texture.Width, texture.Height*2);
        Rectangle c = new Rectangle(x + texture.Width*3, y + texture.Height*4, texture.Width, texture.Height*2);
        Rectangle d = new Rectangle(x + texture.Width, y + texture.Height*6, texture.Width*2, texture.Height);
        Rectangle e = new Rectangle(x, y + texture.Height*4, texture.Width, texture.Height*2);
        Rectangle f = new Rectangle(x, y + texture.Height, texture.Width, texture.Height*2);
        Rectangle g = new Rectangle(x + texture.Width, y + texture.Height*3, texture.Width*2, texture.Height);

        spriteBatch.Draw(texture, a, this.a == 1 ? on : off);
        spriteBatch.Draw(texture, b, this.b == 1 ? on : off);
        spriteBatch.Draw(texture, c, this.c == 1 ? on : off);
        spriteBatch.Draw(texture, d, this.d == 1 ? on : off);
        spriteBatch.Draw(texture, e, this.e == 1 ? on : off);
        spriteBatch.Draw(texture, f, this.f == 1 ? on : off);
        spriteBatch.Draw(texture, g, this.g == 1 ? on : off);

    }
}
于 2013-08-07T20:57:20.693 に答える