求VB小球碰壁反弹的代码,谢谢

 我来答
环业软件
2012-03-26 · TA获得超过1276个赞
知道小有建树答主
回答量:865
采纳率:100%
帮助的人:590万
展开全部
加一个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
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式