【问题标题】:Seven Segment Display XNA七段显示器 XNA
【发布时间】:2013-08-08 04:57:20
【问题描述】:

我正在尝试使用以下方法绘制七段显示,即使我通过调试器运行,我也看不出原因,但由于某种原因它不显示数字。这里有什么问题?您可以忽略大数组,这只是为了展示我如何存储值。

    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);
                }
            }
        }



    }

【问题讨论】:

  • 我假设两个级别的循环 (i, j) 通过一个大数组,是什么导致缓慢?我没有看到其他任何东西。为什么你的“数字”数组的“大不透明浓汤”,没有清晰/有意义的结构,如“数字”或“段”?
  • 二维数组的每个部分都是从0开始的不同数字,我不评论是不好的。
  • 将数字存储为整个纹理而不是构建它们可能更容易和更快。除此之外,我真的看不到您的数组的结构。为什么每个内部数组有三个条目,中间数组有五个数组,每个数字产生 15 个条目。对我来说没有多大意义。
  • 正是@NicoSchertler。带有段列表的类“DigitFigure”会很清楚。段可以是枚举或常量。但这段代码不是。没有任何常见的子表达式或有意义的变量.. 只是一个 glob。
  • 每个数字需要 15 个“段”来显示,8 个段无论如何都已经是黑色的,因为它绘制了一个矩形,我将 7 段显示放在 15 段显示中。如果您查看每个二维数组的形状,它是从 0 开始到 9 的 '1' 形状。

标签: c# .net xna xna-4.0


【解决方案1】:

在 Java 中:

public class Digit {
    protected int value;
    protected List<Segment> segmentList;

    public Digit (int value, Segment... segments) {
        this.value = value;
        this.segmentList = Arrays.asList( segments);
    }

    public void draw (int x, int y) {
        for (Segment seg : segmentList) {
            seg.draw( x, y);
        }
    }
}

public enum Segment {
    TOP (0, 0, 1, 0),    // x0,y0, x1,y1
    LT  (0, 0, 0, 1),
    RT  (1, 0, 1, 1),
    MID (0, 1, 1, 1),
    LB  (0, 1, 0, 2),
    RB  (1, 1, 1, 2),
    BOT (0, 2, 1, 2);
    private Segment (int x0, int y0, int x1, int y1) {
         // assign x0,y0 & x1,y1 to fields.
    }
    public draw (int xofs, int yofs) {
         // draw..
    }
}



// setup the Digits somewhere..  then:

public void drawScore (int number, int xofs, int yofs) {
    int remain = number;
    int digitI = 0;
    while (remain > 0 || digitI == 0) {
        int digit = (remain % 10);
        remain /= 10;

        // draw the digit.
        //
        int xpos = digit * DIGIT_WIDTH;
        digits[digit].draw( xpos, SCORE_YPOS);
    }
}

【讨论】:

    【解决方案2】:

    我决定让整个班级来处理这个问题。

    下图是在 (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));
    

    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);
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      • 2021-07-12
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多