Public Group

# viewing part of a map

This topic is 4833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Here's my problem I am making an rpg type game using C# I want to only let the user view a part of my displayed map but I don;t know what code to use to do that. Any help please.

##### Share on other sites
How are you storing your maps?

##### Share on other sites
I am storing my maps in a separate folder using a 2d array to display my tiles.

I am making a 2d prg game.

##### Share on other sites
So say you have a map of size 10x10, and you want the viewspace to be 5x5 tiles. You could draw tiles [x][y] to [x+4][y+4] where x,y is the upper left hand corner tile, updating x and y as the player moves. So if the upper lefthand tile is 0,0 it would draw tiles:

[0,0] [1,0] [2,0] [3,0] [4,0]
[0,1] [1,1] [2,1] [3,1] [4,1]
[0,2] [1,2] [2,2] [3,2] [4,2]
[0,3] [1,3] [2,3] [3,3] [4,3]
[0,4] [1,4] [2,4] [3,4] [4,4]

Then if the player moves right you update the upper left tile to be 1,0 and it would draw the tiles:

[1,0] [2,0] [3,0] [4,0] [5,0]
[1,1] [2,1] [3,1] [4,1] [5,1]
[1,2] [2,2] [3,2] [4,2] [5,2]
[1,3] [2,3] [3,3] [4,3] [5,3]
[1,4] [2,4] [3,4] [4,4] [5,4]

To determine where to draw the tiles on screen you can do something like multiply their index by their width/height. So if they were 40x40 tile 0,0 would be drawn at screen coordinates 0,0. Tile 0,1 would be drawn at screen coordinates 0,40. Tile 0,2 would be drawn at screen coordinates 0,80. etc.

edited: had my x's and y's all mixed up. fixed

##### Share on other sites
So there is no simple code to view a part of the form from a certain coordinate point to a another corrdinate point when I run my form using Visual C#.Net?

##### Share on other sites
Well, the problem with what (it seems) you're saying is that to have a map of any size, you'll need a HUGE form with tons of individual images... not a good idea. Omnibus's suggestion is good, however. Keep your 2d array to keep track of the whole map, and have yourself a viewing area on your form that contains X number of tiles in each direction (like the 10 that Omnibus refered to). Then, all you need to do is change the images on those 100 image controls (10x10, as per the example) to reflect the tiles that are surrounding the player. Thus, your player never actually moves (being always located in the middle), the map just moves around him.

##### Share on other sites
How simple are you looking for?

##### Share on other sites
Finally have my view space working, thanks for the info

My next question deals woth an image list

This is my working array
I have set my array to 40, 40 and am using an image list to hold my
images for my tiles that I am going to place at each number

My problem is that last line of code, the blue rectangele is for testing purposes

First off, I am trying to draw a blue colored rectangle but the last line does not work

Any pointers?

int[,] testArray = new int[firstNumber, secondNumber];

for (int i = 0; i < secondNumber; i++)
{

for (int j = 0; j < firstNumber; j++)
{

testArray[j, i] = i + j + 1;
Graphics dc = this.CreateGraphics();
this.Show();
Pen bluepen = new Pen(Color.Blue, 3);
testArray[0,0] == dc.DrawRectangle(bluepen, 50, 50);
}

1. 1
2. 2
Rutin
18
3. 3
khawk
15
4. 4
A4L
14
5. 5

• 10
• 13
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633745
• Total Posts
3013667
×