【发布时间】: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' 形状。