I'm currently working with C#/Xaml in VisualStudio (WPF application) to create a game for my first project. To simplify my problem, I currently have a button display a random image in a position in a window (I laid out 4 images, all collapsed, then this code will make a random one appear per second and then disappear).
Instead of a button click, I want a for loop to cycle through the code and display a random image, wait a second, then have that disappear and have another one pop up. I want this to happen automatically for a certain amount of cycles.
A section of my current code is:
private void Button_Click(object sender, RoutedEventArgs e)
{
for (int j = 0; j < 5; j++)
{
Food1.Visibility = Visibility.Collapsed;
Food2.Visibility = Visibility.Collapsed;
Food3.Visibility = Visibility.Collapsed;
Food4.Visibility = Visibility.Collapsed;
int ImageToDisplay = RandomFood.Next(0, 4);
if (ImageToDisplay == 0)
{
Food1.Visibility = Visibility.Visible;
}
else if (ImageToDisplay == 1)
{
Food2.Visibility = Visibility.Visible;
}
else if (ImageToDisplay == 2)
{
Food3.Visibility = Visibility.Visible;
}
else if (ImageToDisplay == 3)
{
Food4.Visibility = Visibility.Visible;
}
Thread.Sleep(1000);
}
}
When I execute my code, it freezes for 5 seconds, the window is blank, then only displays the last image after 5 seconds. How do I get it to display a different image and then disappear every second for 5 seconds?