创建一个VB程序,利用图片框实现随机弹球(各个方向45度角运动),要求具有暂停功能,并且球初始方向随机产生。
拜托了!
求大神用VB编写一个随机弹球。。急急急。。。。
答案:2 悬赏:0 手机版
解决时间 2021-02-09 00:08
- 提问者网友:两耳就是菩提
- 2021-02-08 03:47
最佳答案
- 五星知识达人网友:摆渡翁
- 2021-02-08 04:08
'一个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)
Print
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
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
全部回答
- 1楼网友:胯下狙击手
- 2021-02-08 05:44
这个简单,就是
1:无偿懒得做;2:即使写了也可能是无视不采纳,早没积极性了。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯