求大神用VB编写一个随机弹球。。急急急。。。。 10
创建一个VB程序,利用图片框实现随机弹球(各个方向45度角运动),要求具有暂停功能,并且球初始方向随机产生。拜托了!...
创建一个VB程序,利用图片框实现随机弹球(各个方向45度角运动),要求具有暂停功能,并且球初始方向随机产生。
拜托了! 展开
拜托了! 展开
2个回答
展开全部
'一个timer1控件,一个picture1控件,在picture1控件中再加入一个shape1控件。单击picture1控件可以暂停或继续
Option Explicit
Const offset = 200
Dim flag As Boolean, u, r
Private Sub Form_Load()
With Timer1
.Enabled = False
.Interval = 30
End With
Randomize
u = Int(Rnd * 2): r = Int(Rnd * 2)
End Sub
Private Sub Form_Resize()
With Picture1
.Top = 10
.Left = 10
.Height = Me.ScaleHeight - 2 * .Top
.Width = Me.ScaleWidth - 2 * .Left
End With
With Shape1
.Width = Picture1.Width / 20
.Left = (Picture1.Width - .Width) / 2
.Top = (Picture1.Height - .Height) / 2
.Shape = 3
.FillStyle = 0
.FillColor = vbRed
End With
End Sub
Private Sub Picture1_Click()
flag = Not flag
If flag Then Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
If Not flag Then
Timer1.Enabled = False
Exit Sub
End If
With Shape1
If r = 1 And u = 1 Then '右上
.Left = .Left + offset
If .Left + .Width >= Picture1.Width Then
.Left = Picture1.Width - .Width
r = 0
End If
.Top = .Top - offset
If .Top <= 0 Then
.Top = 0
u = 0
End If
End If
If r = 1 And u = 0 Then '右下
.Left = .Left + offset
If .Left + .Width >= Picture1.Width Then
.Left = Picture1.Width - .Width
r = 0
End If
.Top = .Top + offset
If .Top + .Height >= Picture1.Height Then
.Top = Picture1.Height - .Height
u = 1
End If
End If
If r = 0 And u = 1 Then '左上
.Left = .Left - offset
If .Left <= 0 Then
.Left = 0
r = 1
End If
.Top = .Top - offset
If .Top <= 0 Then
.Top = 0
u = 0
End If
End If
If r = 0 And u = 0 Then '左下
.Left = .Left - offset
If .Left <= 0 Then
.Left = 0
r = 1
End If
.Top = .Top + offset
If .Top + .Height >= Picture1.Height Then
.Top = Picture1.Height - .Height
u = 1
End If
End If
End With
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询