Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
C#
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:
C#
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'?
Posted
Comments
Sergey Alexandrovich Kryukov 26-Apr-13 22:37pm    
Excuse me, does it means that you cannot address problems in abstracted and formal manner, without showing that huge initializer for an array. Who cares what's in it? Besides, the mere fact it has only 1s and 0s suggests the presentation of the object in memory is totally wrong. Rather, you need a bitmap. Or something else.
—SA

1 solution

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
 
Share this answer
 
Comments
ukcharlie 28-Apr-13 12:37pm    
@Sergey Alexandrovich kryukov. In response to your comment, I do not know how to make bitmaps. It will not always have just walls around the outside. That is just what we're doing for right now. The whole concept of the game has nothing to do with what I am attempting to do with the walls right now so there's no use in wasting time and writing it all down. Anyway, I've got some ideas from other people on how to write this code. They suggested to change the forloop into a nested for loop. So, im going to work on that. Thanks for your input.
Sergey Alexandrovich Kryukov 28-Apr-13 13:26pm    
Everything boils down to a simple idea: you should describe the ultimate goals of the project. It's a matter of holistic approach to the project; it defines the algorithms and data structures to be used. After looking at your question, I cannot trust your "has nothing to do...", I strongly suspect it does. And you did not even clearly explain what do you want to do with the walls.

As to the bitmap: it could be just the array of bytes (private), but interface of the class allows you to work with individual bits addressed by X and Y. I think this idea should be enough to implement.

What I described, about the collection, is another approach. You should pickup one which is better for your calculations over this matrix.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900