I’m writing a simple game – snake. I would like to have background and my snake on it. I think that the best way is use two pictureBox (one with background and second – transparent with snake on it).
This is a good way? And how can I put several small images (snake’s segments) on one picture box in different places (just copy pixel’s (one after one) from image to pictureBox or maybe there is fastest way – putting all image in correct place)? I have pictureBox with background (parent) and and another, transparent (child) on it now.
The result should be look something like that:

I’ve made something like that (thanks to @dotTutorials), but my snake’s segments are a little bit bigger than original images and cookie is smaller. Where can be a problem?
Drawing Code:
public Bitmap PrinPlayground()
{
char[,] tempPitch = play.getPitch();
Graphics g = pb2.CreateGraphics();
Bitmap bitmap = new Bitmap(512, 512);
Graphics BBG = Graphics.FromImage(bitmap);
Bitmap head = CookieSnake.Properties.Resources.head;
Bitmap body01 = CookieSnake.Properties.Resources.body01;
Bitmap tail = CookieSnake.Properties.Resources.tail;
Bitmap cookie = CookieSnake.Properties.Resources.cookie;
BBG.Clear(Color.Transparent);
for (int i = 0; i < 16; i++)
for (int j = 0; j < 16; j++)
{
if (tempPitch[i, j] == 'H')
{
BBG.DrawImage(head, new Point(32*j, 32*i));
}
else if (tempPitch[i, j] == 'B')
{
BBG.DrawImage(body01, new Point(32*j, 32*i));
}
else if (tempPitch[i, j] == 'T')
{
BBG.DrawImage(tail, new Point(32 * j, 32 * i));
}
else if (tempPitch[i, j] == 'C')
{
BBG.DrawImage(cookie, new Point(32 * j, 32 * i));
}
}
g.DrawImage(bitmap, new Point(0,0));
return bitmap;
}
Result:

The best way to achieve this is definitely to use the ‘Graphics’ class. Please look further into the GDI, and the namespace
System.Drawing.If you want to use a Picturebox representing the game space, you can easily implement graphics to a picturebox as well, by calling the member
CreateGraphics.I hope this helps you! 🙂
Please notice that when you get into serious game development, you’ll have to find a better alternative than GDI. I personally prefer the XNA library
Example usage of GDI [This written fast, and should not be copy – pasted. However; It is a good point of origin :)]
UPDATE: The DrawImage method also accepts a RectangleF input. A RectangleF consits of 4 datatypes, float X, float Y, float Width and float Height.
With a RectangleF you can easily specify the size of the drawn image. Take a look at the code below: