gpt4 book ai didi

c# - 如何创建磁贴系统

转载 作者:太空宇宙 更新时间:2023-11-03 18:37:57 27 4
gpt4 key购买 nike

与其为每个图 block 创建单独的案例,不如这样做更好的方法是什么?

public void Draw(SpriteBatch spriteBatch, Level level)
{
for (int row = 0; row < level._intMap.GetLength(1); row++) {
for (int col = 0; col < level._intMap.GetLength(0); col++) {
switch (level._intMap[col, row]) {
case 0:
spriteBatch.Draw(_texture, new Rectangle(row * _tileWidth, col * _tileHeight, _tileWidth, _tileHeight), new Rectangle(0 * _tileWidth, 0 * _tileHeight, _tileWidth, _tileHeight), Color.White);
break;
case 1:
spriteBatch.Draw(_texture, new Rectangle(row * _tileWidth, col * _tileHeight, _tileWidth, _tileHeight), new Rectangle(1 * _tileWidth, 0 * _tileHeight, _tileWidth, _tileHeight), Color.White);
break;
case 2:
spriteBatch.Draw(_texture, new Rectangle(row * _tileWidth, col * _tileHeight, _tileWidth, _tileHeight), new Rectangle(2 * _tileWidth, 0 * _tileHeight, _tileWidth, _tileHeight), Color.White);
break;
case 3:
spriteBatch.Draw(_texture, new Rectangle(row * _tileWidth, col * _tileHeight, _tileWidth, _tileHeight), new Rectangle(3 * _tileWidth, 0 * _tileHeight, _tileWidth, _tileHeight), Color.White);
break;

}
}
}
}

最佳答案

不需要case语句,只需要使用一个变量。

var n = level._intMap[col, row];
spriteBatch.Draw(_texture,
new Rectangle(row * _tileWidth, col * _tileHeight, _tileWidth, _tileHeight),
new Rectangle(n * _tileWidth, 0 * _tileHeight, _tileWidth, _tileHeight), Color.White
);

如果您需要将输出限制为值 0-3(如 case 语句的效果),那么最好使用条件 if (n >= 0 && n <= 3) { } .

关于c# - 如何创建磁贴系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12613170/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com