求VB小球碰壁反弹的代码,谢谢
1个回答
展开全部
加一个picture1,timer1
Option Explicit
Private Const BallR = 200
Private Const BallD = 2 * BallR + 1
Private CurX As Single
Private CurY As Single
Private OldX As Single
Private OldY As Single
Private VelX As Single
Private VelY As Single
Private Xmax As Single
Private Ymax As Single
Private Sub DrawBall()
OldX = CurX
OldY = CurY
Me.picture1.FillColor = &HEE00CC
Me.picture1.FillStyle = 6
Me.picture1.Cls
picture1.Circle (CurX, CurY), BallR, vbRed
End Sub
Private Sub Form_Load()
Width = (Width - ScaleWidth) + picture1.Width
Height = (Height - ScaleHeight) + picture1.Height
Xmax = picture1.ScaleWidth - BallR
Ymax = picture1.ScaleHeight - BallR
Randomize
CurX = Int((Xmax - BallR + 1) * Rnd + BallR)
CurY = Int((Ymax - BallR + 1) * Rnd + BallR)
OldX = CurX
OldY = CurY
VelX = Int((40 - 20 + 1) * Rnd + 20)
VelY = Int((40 - 20 + 1) * Rnd + 20)
DrawBall
Timer1.Interval = 10
Timer1.Enabled = True
End Sub
Private Sub timer1_Timer()
CurX = CurX + VelX
If (CurX > Xmax) Then
CurX = Xmax
VelX = -VelX
' Beep
ElseIf (CurX < BallR) Then
CurX = BallR
VelX = -VelX
' Beep
End If
CurY = CurY + VelY
If (CurY > Ymax) Then
CurY = Ymax
VelY = -VelY
' Beep
ElseIf (CurY < BallR) Then
CurY = BallR
VelY = -VelY
' Beep
End If
DrawBall
End Sub
Option Explicit
Private Const BallR = 200
Private Const BallD = 2 * BallR + 1
Private CurX As Single
Private CurY As Single
Private OldX As Single
Private OldY As Single
Private VelX As Single
Private VelY As Single
Private Xmax As Single
Private Ymax As Single
Private Sub DrawBall()
OldX = CurX
OldY = CurY
Me.picture1.FillColor = &HEE00CC
Me.picture1.FillStyle = 6
Me.picture1.Cls
picture1.Circle (CurX, CurY), BallR, vbRed
End Sub
Private Sub Form_Load()
Width = (Width - ScaleWidth) + picture1.Width
Height = (Height - ScaleHeight) + picture1.Height
Xmax = picture1.ScaleWidth - BallR
Ymax = picture1.ScaleHeight - BallR
Randomize
CurX = Int((Xmax - BallR + 1) * Rnd + BallR)
CurY = Int((Ymax - BallR + 1) * Rnd + BallR)
OldX = CurX
OldY = CurY
VelX = Int((40 - 20 + 1) * Rnd + 20)
VelY = Int((40 - 20 + 1) * Rnd + 20)
DrawBall
Timer1.Interval = 10
Timer1.Enabled = True
End Sub
Private Sub timer1_Timer()
CurX = CurX + VelX
If (CurX > Xmax) Then
CurX = Xmax
VelX = -VelX
' Beep
ElseIf (CurX < BallR) Then
CurX = BallR
VelX = -VelX
' Beep
End If
CurY = CurY + VelY
If (CurY > Ymax) Then
CurY = Ymax
VelY = -VelY
' Beep
ElseIf (CurY < BallR) Then
CurY = BallR
VelY = -VelY
' Beep
End If
DrawBall
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询