简体   繁体   English

图片框的随机位置-VB.NET

[英]Random Picturebox Location - VB.NET

I am making a reaction game that involves the user clicking a picturebox which randomizes its location. 我正在制作一个反应游戏,其中涉及用户单击图片框以随机化其位置。

The tricky part is to randomize the picturebox's location so that it appears within the bounds of a panel which i have created. 棘手的部分是随机化图片框的位置,以使其出现在我创建的面板的范围内。 The reason for this is that i have other controls on the form. 这样做的原因是我在表单上还有其他控件。 Not sure if a panel would be the best way of going about this but i am not sure where to start. 不知道专家小组是否是解决此问题的最佳方法,但我不确定从哪里开始。

The picturebox's size is (70,55) and the panel's size is (640,400) in location (40,69) if it makes any difference. 图片盒的大小为(70,55),而面板的大小为(640,400),则位置(40,69)有所不同。

I have the following code so far: 到目前为止,我有以下代码:

 Dim rnd1 As Integer = panel1.Width * Rnd()
Dim rnd2 As Integer = panel1.Height * Rnd()

pb1.Top = rnd2
pb1.left = rnd1

while it works it randomizes the picturebox right on the edges of the panel so that some randomizations result in the picturebox not being visible at all. 当它起作用时,它将在面板边缘的右边随机化图片框,以便进行一些随机化操作,使图片框根本不可见。 What code would i have to use so that the picturebox is completely visible and does not cross the bounds of the panel? 我必须使用什么代码才能使图片框完全可见并且不会越过面板的边界?

Thanks 谢谢

You are not taking into account the height or width of the PictureBox object. 您没有考虑到PictureBox对象的高度或宽度。

if you set the left property of the picturebox to be the width of the panel then it will be out of view. 如果将picturebox的left属性设置为面板的宽度,则它将不可见。

You need to do something like this: 您需要执行以下操作:

Dim rnd1 As Integer = (panel1.Width - pb1.Width) * Rnd()
Dim rnd2 As Integer = (panel1.Height - pb1.Height) * Rnd()

pb1.Top = rnd2
pb1.left = rnd1

Note: If you use Rnd you need to call Randmonize() first otherwise your random numbers will be the same sequence each time you run your application: Using Randomize() before Rnd() in VB.NET 注意:如果使用Rnd ,则需要先调用Randmonize()否则每次运行应用程序时,随机数都将是相同的序列: 在VB.NET中,在Rnd()之前使用Randomize()

I would suggest using the Random class instead of Rnd as this does not require you to initialise a seed before you start using it. 我建议使用Random类而不是Rnd因为这不需要您在开始使用之前初始化种子。

Use Random class to generate your random number. 使用随机类生成您的随机数。 The argument is the max number you want to generate. 参数是要生成的最大数量。 For your top location, you generate a number between 0 and the panel's height minus picture box's height. 对于顶部位置,您将生成一个介于0到面板高度减去图片框高度之间的数字。 For your left location, a number between 0 and the panel's width minus picture box's width. 对于您的左侧位置,请在0到面板宽度减去图片框宽度之间的数字。

Put this in the form declaration. 将其放在表单声明中。

Dim random As New Random

Put this in your event (I use a timer event). 将其放在您的事件中(我使用计时器事件)。

Dim newTop As Integer = random.Next(panel1.Height - pb1.Height)
Dim newLeft As Integer = random.Next(panel1.Width - pb1.Width)

pb1.Top = newTop
pb1.Left = newLeft

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM