Benjamin B. answered 12/08/20
Designing Better Grades
foreach (Control x in this.Controls)
{
if (x is PictureBox)
{
if (Convert.ToString(x.Tag) == "coins")
{
if (pacman.Bounds.IntersectsWith(x.Bounds) && x.Enabled == true)
{
score++;
x.Enabled = false;
x.Visible = false;
}
}
if (Convert.ToString(x.Tag) == "barrier" || Convert.ToString(x.Tag == "ghosts")
{
if (pacman.Bounds.IntersectsWith(x.Bounds))
{
gameisover();
}
}
}
This is the most condensed way to be able to write this code in a shorter and simpler way. The last two if statements can be combined into an OR(||) statement because the rest of the code below both of the If statements are the same. When your code repeats that is a great way to be able to reduce it down or even turn it into a function and use that function in essence making performance much better.
Lily P.
Cool, thanks!!12/10/20