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;
}
}
}