=========cike例题===========
name = 弹动的小圆
窗体=1个圆 1个时间
设置:把Shape设置成圆 时间间隔=20
代码:
==================================
Dim 下右 As Boolean
Dim 下左 As Boolean
Dim 上右 As Boolean
Dim 上左 As Boolean '定义方向
Private Sub Form_Load()
Randomize ' 这里让圆随机方向移动
a = Int(Rnd * 4)
If a = 0 Then 上左 = False: 上右 = False: 下左 = False: 下右 = True
If a = 1 Then 上左 = False: 上右 = False: 下左 = True: 下右 = False
If a = 2 Then 上左 = False: 上右 = True: 下左 = False: 下右 = False
If a = 3 Then 上左 = True: 上右 = False: 下左 = False: 下右 = False

End Sub
Private Sub Timer1_Timer() '移动过程
x = Shape1.Left '提取当前x
y = Shape1.Top '提取当前y
If 下右 = True And x + Shape1.Width > Me.Width - 140 Then 下左 = True: 下右 = False '若方向是 下右 并且碰到 右边 则向 下左
If 下右 = True And y + Shape1.Width > Me.Height - 500 Then 上右 = True: 下右 = False '若方向是 下右 并且碰到 下边 则向 上右
If 上右 = True And x + Shape1.Width > Me.Width - 140 Then 上右 = False: 上左 = True ' 类推....
If 上右 = True And y < 0 Then 上右 = False: 下右 = True
If 上左 = True And x < 0 Then 上左 = False: 上右 = True
If 上左 = True And y < 0 Then 上左 = False: 下左 = True
If 下左 = True And y + Shape1.Width > Me.Height - 500 Then 下左 = False: 上左 = True
If 下左 = True And x < 0 Then 下左 = False: 下右 = True
If 上左 = True Then x = x - 30: y = y - 30 '判断是什么方向 就怎么移动
If 上右 = True Then x = x + 30: y = y - 30
If 下左 = True Then x = x - 30: y = y + 30
If 下右 = True Then x = x + 30: y = y + 30
Shape1.Move x, y '使用move 函数 移动
End Sub


被折叠的 条评论
为什么被折叠?



