七段显示XNA

时间:2013-08-02 05:12:38

标签: c# .net xna xna-4.0

我正在尝试使用以下方法绘制七段显示,我无法理解为什么,即使我通过调试器,但是因为某些原因它不显示数字。这有什么不对?您可以忽略大型数组,它只是为了显示我如何存储值。

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



    }

2 个答案:

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

答案 1 :(得分: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));

A picture of the seven segment display drawn at 0

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

    }
}