0
\$\begingroup\$

I have almost solved my problem. I am able to shoot a bullet and hit a bug and it then draws a collision bitmap. but then it redraws a bug, I want to clear the bug bitmap after the collision happens.

public Form1()
{
    InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
    timer1.Interval = 20;
    timer1.Start();
    timer2.Interval = 100;
    timer2.Start();
    this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
}
int x = 0, y = 0;
Bitmap bug_one = new Bitmap("bug_one.bmp", true);
Bitmap ship = new Bitmap("ship.bmp", true);
Bitmap bullet = new Bitmap("bullet.bmp", true);
Bitmap coll = new Bitmap("coll.bmp", true);
Bitmap black = new Bitmap("black.bmp", true);
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Left)
    {
        x-=5;
        if(x<=-350)
        {
            x = -350;
        }
    }
    if(e.KeyCode == Keys.Right)
    {
        x+=5;
        if(x>=375)
        {
            x = 375;
        }
    }
    if (e.KeyCode == Keys.Space)
    {
        if (y <= -530)
        {
            y = 0;
        }
    }
}
int count = 102;
public void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.Clear(Color.Black);
    e.Graphics.DrawImage(ship, 350 + x, 530);
    e.Graphics.DrawImage(bullet, 375 + x, 520 + y);
    e.Graphics.DrawImage(bug_one, 350, 0);
    if (y <= -510 && x >= -15 && x <= 15 && count > 0)
    {
        e.Graphics.DrawImage(coll, 350, 0);
        count = 0;
    }
}
public void timer1_Tick(object sender, EventArgs e)
{
    y -= 5;
    Invalidate();
}
public void timer2_Tick(object sender, EventArgs e)
{
    count--;
    Invalidate();
}

This attempt almost works: when a bullet hits a bug it draws a collision image, but then all the bugs are cleared. I only want one bug to be cleared at a time.

    public void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImage(ship, x-25, 530);
        e.Graphics.DrawImage(bullet, x, y);
        if (collision)
        {
            return;
        }
        foreach (Bug bug in bugs)
        {
            e.Graphics.DrawImage(bug_one, bug.px, bug.py);
        }
        foreach (Bug bug in bugs)
        {
            if (x >= bug.px && y >= bug.py && x <= bug.px + bug.width && y <= bug.py + bug.height) 
            {
                e.Graphics.DrawImage(coll, bug.px, bug.py);
                collision = true;
            }
        }
    }
\$\endgroup\$
  • \$\begingroup\$ is this where I should post my comments? \$\endgroup\$ – gamer67 May 20 '19 at 21:55
  • \$\begingroup\$ can I get some input on my problem? \$\endgroup\$ – gamer67 Jun 1 '19 at 0:24
1
\$\begingroup\$

I assume that Form1_Paint is called repeatedly. Why not add some kind of flag indicating that a collision happened, and just avoid drawing the image?

public void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.Clear(Color.Black);
    e.Graphics.DrawImage(ship, 350 + x, 530);
    if (collisionHapened) {
        return;
    }
    e.Graphics.DrawImage(bullet, 375 + x, 520 + y);
    e.Graphics.DrawImage(bug_one, 350, 0);
    if (y <= -510 && x >= -15 && x <= 15 && count > 0)
    {
        e.Graphics.DrawImage(coll, 350, 0);
        count = 0;
        collisionHappened = true;
    }
}

If you want to really clear the bitmap, which is a relatively slow operation, use MakeTransparent and then set the each of the bitmap's pixels' alpha to zero (see example).

On a side note, consider not relying on global state, and instead pass to each method its required parameters. If the behavior of a method fully relies on its arguments instead of on global state, it's significantly easier to understand why erroneous behavior happens.

\$\endgroup\$
  • \$\begingroup\$ is there a easier way to set the bitmaps pixels alpha to zero? \$\endgroup\$ – gamer67 Apr 2 '19 at 17:48
  • \$\begingroup\$ You can just change the bitmap - load instead a single clear pixel. \$\endgroup\$ – nihohit Apr 2 '19 at 19:43
  • \$\begingroup\$ how do I set all of the pixels in my bitmap to zero alpha? \$\endgroup\$ – gamer67 Apr 3 '19 at 0:49
  • \$\begingroup\$ I attached a link in my answer. \$\endgroup\$ – nihohit Apr 3 '19 at 6:54
  • \$\begingroup\$ I looked at the link but I am still a little confused about how to implement it \$\endgroup\$ – gamer67 Apr 4 '19 at 20:38
0
\$\begingroup\$
        public void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImage(ship, x-25, 530);
        e.Graphics.DrawImage(bullet, x, y);
        foreach (Bug bug in bugs)
        {
            if (bug.collision==false)
            {
                e.Graphics.DrawImage(bug_one, bug.px, bug.py);
            }
        }
            foreach (Bug bug in bugs)
            {
                if (x >= bug.px && y >= bug.py && x <= bug.px + bug.width && y <= bug.py + bug.height)
                {
                    e.Graphics.DrawImage(coll, bug.px, bug.py);
                    bug.collision = true;
                }
        }
    }

I finally got my code to work properly it draws a bug then it gets hit by a bullet and then it draws a collision image and then disappears, just like I wanted it to do.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.