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

时间:2014-04-13 14:50:57

标签: 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.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

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

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

1 个答案:

答案 0 :(得分:0)

现在你要做的就是添加添加更多PictureBoxes的功能,可能是第二个计时器,当你创建它们时,使用Addhandler语句指向它们pbs_Click我制作的活动,并将它们添加到我制作的List

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