I coding a very simple program.
A pictureBox drops from above and another pictureBox (located at button) have to grab the pictureBox (collision between those two picturesBoxes) and then you get one point.
My issue is when the collision happens then Point
are counting up as long the collision between the two pictureBoxes happens.
How to stop this and only count ++ when a collision happens? - I'm total rookie coding c# so please don't answer a very advanced answer :)
namespace Animation
{
public partial class Form1 : Form
{
Point p1 = new Point();
Point p2 = new Point();
int bredde;
int højde;
int score;
int count = 0;
public Form1()
{
InitializeComponent();
p1.X = pictureBox1.Location.X;
p1.Y = pictureBox1.Location.Y;
p2.X = pictureBox2.Location.X;
p2.Y = pictureBox2.Location.Y;
højde = ClientSize.Height;
bredde = ClientSize.Width;
}
Boolean max = true;
void collision()
{
if (pictureBox1.Bounds.IntersectsWith(pictureBox2.Bounds))
{
score++;
label1.Text = Convert.ToString(score);
max = true;
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.W && p1.Y > 0)
p1.Y -= 3;
if (e.KeyData == Keys.D && p1.X + pictureBox1.Width < bredde)
p1.X +=3;
if (e.KeyData == Keys.A && p1.X > 0)
p1.X -=3;
if (e.KeyData == Keys.S && p1.Y + pictureBox1.Height < højde)
p1.Y +=3;
pictureBox1.Location = p1;
if (e.KeyData == Keys.W && p2.Y > 0)
if (e.KeyData == Keys.D && p2.X + pictureBox2.Width < bredde)
if (e.KeyData == Keys.A && p2.X > 0)
if (e.KeyData == Keys.S && p2.Y + pictureBox2.Height < højde)
pictureBox2.Location = p2;
}
private void timer1_Tick(object sender, EventArgs e)
{
collision();
p2.Y +=5;
pictureBox2.Location = p2;
}
private void buttonStart_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void buttonStop_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
}
}