我的代码哪里写错了?

来源:百度知道 编辑:UC知道 时间:2024/05/18 07:24:20
请看截图和我写的代码.

先别管鲨鱼海豚螃蟹那三张图.

因为Fishy是那张小黄鱼.

此程序的目的是让小黄鱼斜着走, 并且碰到GUI的边缘会反弹游回去.

但是当我运行的时候, 小黄鱼就超出屏幕了. 其他都没任何错误.

可我明明已经弄好If条件的呀...-O- 本人新手..

先谢谢了.

Private Sub Timer1_Timer()

Dim MoveFishx As Integer
Dim step As Integer

MoveFishx = 10

For step = 1 To 1000
If (Fishy.Left) = (Form1.Width - Fishy.Width) And (Fishy.Top) = (Form1.Height - Fishy.Height) Then
MoveFishx = -10
ElseIf (Fishy.Left <= 0) And (Fishy.Top <= 0) Then
MoveFishx = 10
Timer1.Enabled = False

End If
Fishy.Left = Fishy.Left + MoveFishx
Fishy.Top = Fishy.Top + MoveFishx
Sleep (10)

Next step
End Sub

呵呵,是判断有问题,把两条IF语句中的,AND改成OR,第一条IF的=改成>=
If (Fishy.Left) >= (Form1.Width - Fishy.Width) Or (Fishy.Top) >= (Form1.Height - Fishy.Height) Then

ElseIf (Fishy.Left <= 0) Or (Fishy.Top <= 0) Then

End If
就行啦
另外你的这个程序可以强化一下方向,就是产生一个0到7的随机数,表示八个方向,用一个数组保存改变方向的增量,那样小鱼会更灵活啦,下面给出初始量,加在哪怎么加你自己看啦
'初始化移动方向参数
移动方向(1, 0) = -10 '1左下
移动方向(1, 1) = 10
移动方向(2, 0) = 0 '2下
移动方向(2, 1) = 10
移动方向(3, 0) = 10 '3右下
移动方向(3, 1) = 10
移动方向(4, 0) = 10 '4右
移动方向(4, 1) = 0
移动方向(5, 0) = 10 '5右上
移动方向(5, 1) = -10
移动方向(6, 0) = 0 '6上
移动方向(6, 1) = -10
移动方向(7, 0) = -10 '7左上
移动方向(7, 1) = -10
移动方向(0, 0) = -10 '8左
移动方向(0, 1) = 0

'产生随机方向 也可像你上面程序一样 用位置判断后改变方向
Randomize
n = Int(Rnd * 7) + 1

if判断有问题吧。

If (Fishy.Left) >(Form1.Width - Fishy.Width) then
MoveFishx = -10
Elseif (Fishy.Left <= 0)