Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image Array? 1

Status
Not open for further replies.

tesseract36

Programmer
Feb 13, 2007
2
Im trying to make a 2d game and i was just going to use a fair amount of sprites and tiles for it, but I have just changed to C# in visual studio 2005 and am not sure how this can best be done.

In sort im trying to make a 16 tile by 16 tile board of images that i have imported into resources, there all bmps if it makes a differance.. The ideas i have tryed are making a multi dimentional array pictureBox's and an ArrayList, but neither have worked some error about how PictureBox's cant be an array, pls any tips would help, and im semi new to C# and not vary good with the complex stuff yet
 
Your best bet is to use GDI+,

its very simple, does a lot of 2d stuff for you. Put all the possible tiles into a single image, and put that image into a picturebox that is invisible.

Then you have your game picturebox. Your code will use System.Drawing.Graphics.DrawImage(...) to copy squares from your hidden image into various spots on your visible picturebox. Peice of cake. All you have to keep track of is the location of each of your pieces. Since I'm assuming all the tiles will be the same size, all you need is the x and y coordinates of the upper left corner of each tile in your hidden image.

You could even make your hidden image a single line (either vertical or horizontal doesnt matter) that way you know either the y(vertical) or x(horiz) is always zero, and you can just use (TileIndex * TileWidth) to grab the coordinates of whatever tile you want.

When the user clicks on the image box, you can use the mouseclick event of the picture box, which will give you a MouseClickEventArgs object, with the coordinates of the mouse click, some simple math will allow you to figure out which tile they clicked on.

Hope I got you going in the right direction...
 
Alright, i have tryed to follow what you said and got to here

public void DrawImagePoint(PaintEventArgs e)
{
// Create image.
Image newImage = Image.FromFile("groundSprite.bmp");
// Create Point for upper-left corner of image.
Point ulCorner = new Point(20, 20);
// Draw image to screen.
e.Graphics.DrawImage(newImage, ulCorner);
}

But i dont know how to call or activate this method in my form.. This is vary trubleing. what the app will do is draw a 16x16 image gride from this tile then change some at random to this second image 'treeSprite.bmp' but i havent finished with this part yet, and i was thinking after i draw all my tiles a can use an array to keep track of all the open and closed spaces for movment... but to do all that i need to first make a grid, just some info in case is helps.
 
Windows is an event driven environment, you should fire your method in whatever event you want to, wether in the form load event, when the user clicks a "Start" button, the image, or whatever, its up to you.

As for storing what tile is in which space, I would give each tile an integer index, with zero being a blank tile. Then store them in a two dimensional array. Then your DrawImage method can just loop through the array and draw each tile based on what index value is at each spot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top