0

C# Beginner here.

I'm making a 2D Tanks game, and everything's working out nicely so far. Both of my tanks are Picture Boxes, and so is my Missile. The image of the missile and tanks in the PictureBoxes have transparent BackColour properties. The problem is, the background of the missile & tanks are not transparent while on top of the other picturebox (pbBackground). It looks like this.

I'm aware that using different PB's is an inefficient way of going about it, but I've come pretty far and I don't really know any better. Anyways, as you can see, the Missile and Tank PB backgrounds show the form colour. When I downloaded the images, the backgrounds were transparent, I'm sure of it. How do I go about making the background of my PB's truly transparent? (Matching the background of the Overlapped PB?)

I saw this but it doesn't really match my scenario and I don't understand the solution.

UPDATE: Okay, I followed Tommy's advice, is this the right way to go about moving it along pbBackground in a timer constantly changing MissileX and MissileY? Currently this does nothing.

 using (Graphics drawmissile = Graphics.FromImage(pbBackground.Image))
        {
            drawmissile.DrawImage(pbMissile.Image, new Point(MissileX,Convert.ToInt32(MissileY)));
        }
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Zachary Norman
  • 53
  • 1
  • 1
  • 5

3 Answers3

2

PictureBox is opaque. And PictureBox is not efficient.

For making games, you should study Paint event which directly draws on your form.

Bitmap backgroundBitmap = new Bitmap("background");
Bitmap tankBitmap = new Bitmap("tank");

private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImage(backgroundBitmap, 0, 0);
    e.Graphics.DrawImage(tankBitmap, 30, 30);
}

private void timer1_Tick(object sender, EventArgs e)
{
    this.Invalidate(); //trigger Form1_Paint to draw next frame
}
Tommy
  • 3,044
  • 1
  • 14
  • 13
1

Don't layer multiple PictureBox instances on top of each other. It will get very confusing, very quickly.

Instead, use one single PictureBox and use Paint to draw your images to it. In this way, you can have much more control over the graphics operations happening.

Have a look at this

private void DrawIt()
{
    System.Drawing.Graphics graphics = this.CreateGraphics();
    System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(
       50, 50, 150, 150);
    graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
    graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);
}

In this example they demonstrate how to draw shapes directly onto a Form. You would use your PictureBox there instead. You can also draw images.

There's lots of ways to draw shapes onto a form using a System.Drawing.Graphics object. Try taking a look at this question for a comparison.

Community
  • 1
  • 1
Athena
  • 3,200
  • 3
  • 27
  • 35
  • Am I able to draw an actual image? As in like an image downloaded from online? – Zachary Norman Jun 14 '16 at 17:15
  • Yes, you can with the `Graphics.drawImage` method. https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage(v=vs.110).aspx – Athena Jun 14 '16 at 17:17
0

Tommy's answer is right, however, if you're dead set on using pictureboxes (a bad idea), set the overlapping picturebox backgroundcolour to Transparent and the Form's Background to whatever image. TIL the Transparent BackColour just uses the form colour / image. Tommy actually has the right answer here, but this is what I did to fix my problem (the lazy way).

Zachary Norman
  • 53
  • 1
  • 1
  • 5