我一直试图从一组具有自己位置的指定图像生成随机图像。我还没有尝试过任何代码,因为我无法找到合适的代码。
更具体地说,我的图像命名为" orb1,orb2,orb3 ... orb18"。 orb2-18设置为不可见。当按下orb1时,我希望其中一个在指定的位置随机出现。我已经整理出图像的印刷功能,这样就没有问题了。
这是我的代码:
public Beginner1()
{
this.InitializeComponent();
orb2.Visibility = Visibility.Collapsed;
orb3.Visibility = Visibility.Collapsed;
orb4.Visibility = Visibility.Collapsed;
orb5.Visibility = Visibility.Collapsed;
orb6.Visibility = Visibility.Collapsed;
orb7.Visibility = Visibility.Collapsed;
orb8.Visibility = Visibility.Collapsed;
orb9.Visibility = Visibility.Collapsed;
orb10.Visibility = Visibility.Collapsed;
orb11.Visibility = Visibility.Collapsed;
orb12.Visibility = Visibility.Collapsed;
orb13.Visibility = Visibility.Collapsed;
orb14.Visibility = Visibility.Collapsed;
orb15.Visibility = Visibility.Collapsed;
orb16.Visibility = Visibility.Collapsed;
orb17.Visibility = Visibility.Collapsed;
orb18.Visibility = Visibility.Collapsed;
}
private void o1(object sender, PointerRoutedEventArgs e)
{
}
private void o12(object sender, PointerRoutedEventArgs e)
{
}
private void o3(object sender, PointerRoutedEventArgs e)
{
}
private void o11(object sender, PointerRoutedEventArgs e)
{
}
private void o9(object sender, PointerRoutedEventArgs e)
{
}
private void o16(object sender, PointerRoutedEventArgs e)
{
}
private void o10(object sender, PointerRoutedEventArgs e)
{
}
private void o18(object sender, PointerRoutedEventArgs e)
{
}
private void o17(object sender, PointerRoutedEventArgs e)
{
}
private void o15(object sender, PointerRoutedEventArgs e)
{
}
private void o14(object sender, PointerRoutedEventArgs e)
{
}
private void o13(object sender, PointerRoutedEventArgs e)
{
}
private void o8(object sender, PointerRoutedEventArgs e)
{
}
private void o7(object sender, PointerRoutedEventArgs e)
{
}
private void o6(object sender, PointerRoutedEventArgs e)
{
}
private void o5(object sender, PointerRoutedEventArgs e)
{
}
private void o4(object sender, PointerRoutedEventArgs e)
{
}
private void o2(object sender, PointerRoutedEventArgs e)
{
}
private void scores_TextChanged(object sender, TextChangedEventArgs e)
{
}
}
我在这段代码中没有任何错误,一切都很好。
所以基本上,我想要一些帮助,当按下orb1时,从那些18个球体中可以看到随机的球体。
谢谢:)
答案 0 :(得分:0)
您必须定义一个随机数提供程序并创建一个使随机数字出现的方法:
private Random rnd = new Random();
private void showRandomOrb()
{
switch(rnd.Next(2,19)) // selects random integer from 2 to 18
{
case 2:
orb2.Visibility = Visibility.Visible;
break;
case 3:
orb3.Visibility = Visibility.Visible;
break;
case 4:
orb4.Visibility = Visibility.Visible;
break;
// etc..
case 18:
orb18.Visibility = Visibility.Visible;
break;
}
}
private void o1(object sender, PointerRoutedEventArgs e)
{
showRandomOrb();
}