2013-08-02 23 views
0

我试图用下面的方法绘制一个七段显示器,我看不出为什么,即使当我运行调试器时,但是因为somereason它不显示数字。这里有什么问题?你可以忽略大数组,这只是为了展示我如何存储这些值。七段显示器XNA

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



    } 
+0

我假定循环(I,J)通大阵列的两个级别,是什么原因造成的慢度?我什么都看不到。为什么你的“数字”数组中的“大不透明的浓汤”,以及没有明确/有意义的结构,如“数字”或“段”? –

+0

2d数组的每一部分都是从0开始的不同数字,我的评论不好。 –

+1

将数字存储为整个纹理而非构建它们可能更简单快捷。除此之外,我真的不知道你的数组是如何构造的。为什么每个内部数组有三个条目,中间数组有五个数组,每个数字产生15个条目。对我来说没有多大意义。 –

回答

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

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