Vb.net 如何生成多个图片框并使用计时器控制它们?

Vb.net 如何生成多个图片框并使用计时器控制它们?,vb.net,Vb.net,我的大学让我们用VB做一个游戏,我对这门语言真的不太了解。 我正在尝试做一个游戏,气球会飞到屏幕的顶端,在到达之前必须先打开气球 Public Class Form1 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick If PictureBox1.Top = 0 Then PictureBox1.Visible = False Timer1.Ena

我的大学让我们用VB做一个游戏,我对这门语言真的不太了解。 我正在尝试做一个游戏,气球会飞到屏幕的顶端,在到达之前必须先打开气球

Public Class Form1


Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    If PictureBox1.Top = 0 Then
        PictureBox1.Visible = False
        Timer1.Enabled = False
    End If
    PictureBox1.Top = PictureBox1.Top - 1
End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Timer1.Interval = 1
    Timer1.Enabled = True
End Sub


Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
    PictureBox1.Visible = False
End Sub

End Class
这是我到目前为止的代码,当我单击按钮时,气球开始上升,如果我单击气球,它会消失,如果它到达顶部并且计时器停止,它也会消失


如何生成更多气球并使用该计时器控制它们?

现在,您所要做的就是添加更多
图片框的功能,可能是第二个计时器,当您创建它们时,使用
Addhandler
语句向它们指出我创建的
pbs\u Click
事件,并将它们添加到我创建的
列表中

Public Class Form1
 Private PBs As New List(Of PictureBox)
 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  For Each pb As PictureBox In PBs
    If pb.Top = 0 Then
     pb.Visible = False
     Timer1.Enabled = False
    Else
     pb.Top = pb.Top - 1
  End If
 End Sub
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Timer1.Interval = 1000'ms
  Timer1.Enabled = True
 End Sub
 Private Sub pbs_Click(sender As Object, e As EventArgs)
  Dim pb As PictureBox = DirectCast(sender, PictureBox)
  PBs.Remove(pb)
 End Sub
 Private Sub makeNewPB()
  Dim pb As New PictureBox
  Addhandler pb.Click, AddressOf pbs_Click
  'don't forget to make them the size you need
  PBs.Add(pb)
 End Sub
End Class

您可以创建picturebox列表,并使用事件处理程序动态创建picturebox(如您所述单击它)。