遍历C#XNA中的2D数组 [英] traversing a 2D array in C# XNA

查看:85
本文介绍了遍历C#XNA中的2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个数组基本上勾勒出瓷砖的位置。瓷砖是25X25,代表墙壁。我们在数组中使用1和0。 ''1'表示瓷砖位于那里而''0'表示没有。角色不能移动到有'1'的地方。

这是数组的代码:

 < span class =code-keyword> int  [,] Tiles =  new   int  [  31  23 ] 
{
{ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 ,< span class =代码位> 0 , 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 ,<跨班=code-digit> 0 , 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ,< span class =code-digit> 0 , 0 0 0 0 0 1 },
{ 1 0 ,< span class =code-digit> 0 , 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ,< span class =code-digit> 0 , 1 },
{ 1 0 0 0 0 0 0 ,< span class =code-digit> 0 , 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ,< span class =c ode-digit> 0 , 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ,< span class = code-digit> 0 , 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ,< span class =code-digit> 1 },
{ 1 0 0 0 0 0 0 0 ,< span class =code-digit> 0 , 0 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 ,< span class =code-digit> 0 , 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 , 1 },
{ 1 ,< span class =code-digit> 0 , 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 ,< span class =code-digit> 0 , 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ,< span class =code-digit> 0 , 0 1 },
{ 1 0 0 0 0 0 ,< span class =code-digit> 0 , 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 ,< span class =code-digit> 0 , 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ,< span class =code-digit> 0 , 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 ,< span class =code-digit> 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 } ,
{ 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 },
{ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 }
};





我们想做的是每当有'1'时,我们想放置一个瓷砖。所以,我们需要遍历数组,看看哪里有''1'......对吗?

我写这段代码来绘制矩形但它不起作用:

  foreach  int 元素 in  Tiles)
{
if (element == 1
{
wallX + = 25 ;
wallY + = 0 ;
recwall2 = new 矩形(wallX,wallY, 25 25 );
spriteBatch.Draw(wall,recwall2,Color.White);
}
}





我以为我们必须为每个小瓷砖使用矩形但是我确信这比坐着写出50多个矩形更简单。我正在研究如何制作一个列表来保存那些矩形但我不知道如何合并它。

谁能帮助我们,我们将如何遍历数组放置瓷砖在屏幕上的地方有一个'1'?

解决方案

请查看我对该问题的评论并查看墙数据的演示。它总是长方形,墙壁只有侧面吗?是这样,你需要的只是矩形的大小,而对象有两个整数分量。可以有很多墙吗?介绍墙体对象的集合,每个表示为墙的位置,长度及其方向。简单,不是吗?



换句话说,您未能在代码中选择足够的数据表示。删除它并从头开始。



此外,您提问的方法适得其反。我不能告诉你任何与代码有关的事情,因为你从错误的一方面解决问题。这就成了进一步讨论的终结者。但如果您解释了整个项目的最终目标,我们可以从一开始就帮助您。如果你有误解,那就不会阻止我们修复它。



总是解释整个项目的最终目标。



-SA


We have an array to basically outline where there are ''tiles''. The tiles are 25X25 and represent walls. We use 1s and 0s in the array. ''1'' represents that a tile is located there and a ''0'' means there isn''t. The character can''t move where there is a ''1''.
This is the code for the array:

int[,] Tiles = new int[31, 23] 
        { 
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } 
        };



What we want to do is whenever there is a ''1'', we want a tile placed. So, we would need to traverse the array and see where there are ''1''s... right?
I wrote this code to draw the rectangles but it doesn''t work:

foreach (int element in Tiles)
            {
                if (element == 1)
                {
                    wallX += 25;
                    wallY += 0;
                    recwall2 = new Rectangle(wallX, wallY, 25, 25);
                    spriteBatch.Draw(wall, recwall2, Color.White);
                }
            }



I was thinking we''d have to use Rectangles for each little tile that we place but I''m sure there''s an easier way than sitting and writing out 50+ rectangles. I was looking at how to make a list to hold those rectangles but I have no idea how to incorporate that.
Can anyone help us on how we would traverse the array to place the tiles on the screen wherever there is a ''1''?

解决方案

Please see my comment to the question and review the presentation of the wall data. Is it always rectangular, with walls only on the side? Is so, all you need it just the size of the rectangle, and object with two integer component. Can be many walls? Introduce the collection of "wall object", each is represented as location, length of a wall and its direction. Simple, isn''t it?

In other words, you failed to choose adequate data presentation in your code. Delete it and start from scratch.

Besides, your approach to asking questions is counter-productive. I cannot advice you anything related to the code, because you approached the problem from the wrong side. And that put a stopper of further discussions. But if you explained the ultimate goals of the whole project, we could help you with the very beginning. If you had a misconception, it would not prevent us from fixing it.

Always explain the ultimate goals of the whole project.

—SA


这篇关于遍历C#XNA中的2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆