Vb2010 - 蛇游戏问题

时间:2010-08-04 19:24:53

标签: vb.net

我目前正在VB2010中制作“Snake”游戏。我正在使用图片框来制作蛇。蛇移动得很好,它可以很好地捡起苹果。

但当我将蛇向左或向上转动时,它只显示蛇的前5个部分。当蛇向右或向下行进时,它会显示整条蛇。我完全被所发生的事情困扰了。

这是我移动蛇的代码。

 'Moving the Body Of the Snake
            For i As Integer = (Length - 1) To 1 Step (-1)
                Snake(i).X = Snake(i - 1).X
                Snake(i).Y = Snake(i - 1).Y
                SnakeBody(i).Location = New Point(Snake(i).X, Snake(i).Y)
                Snake(i).Facing = Snake(i - 1).Facing
            Next i

            'Moves the Head of the Snake
            'Moves Left
            If Snake(0).Facing = 1 Then
                Snake(0).X = Snake(0).X - 20
                SnakeBody(0).Location = New Point(Snake(0).X, Snake(0).Y)

                ' Moves Up
            ElseIf Snake(0).Facing = 2 Then
                Snake(0).Y = Snake(0).Y - 20
                SnakeBody(0).Location = New Point(Snake(0).X, Snake(0).Y)

                ' Moves Right
            ElseIf Snake(0).Facing = 3 Then
                Snake(0).X = Snake(0).X + 20
                SnakeBody(0).Location = New Point(Snake(0).X, Snake(0).Y)

                ' Moves Down
            ElseIf Snake(0).Facing = 4 Then
                Snake(0).Y = Snake(0).Y + 20
                SnakeBody(0).Location = New Point(Snake(0).X, Snake(0).Y)
            End If
              Length += 1

此处还有代码,当它与苹果接触时,将一段加入蛇中。

'Checks to see if the head of the snake finds the food


If Snake(0).X = Food.Left And Snake(0).Y = Food.Top Then
        LoadSnakeBody()

        'Placing the new body on its place
        If Snake(Length - 1).Facing = 1 Then        'Looking left
            Snake(Length).X = Snake(Length - 1).X - SnakeBody(0).Width
            Snake(Length).Y = Snake(Length - 1).Y
            SnakeBody(Length).Location = New Point(Snake(Length).X, Snake(Length).Y)
            SnakeBody(Length).Visible = True

        ElseIf Snake(Length - 1).Facing = 2 Then    'Looking up
            Snake(Length).X = Snake(Length - 1).X
            Snake(Length).Y = Snake(Length - 1).Y + SnakeBody(0).Height
            SnakeBody(Length).Location = New Point(Snake(Length).X, Snake(Length).Y)
            SnakeBody(Length).Visible = True

        ElseIf Snake(Length - 1).Facing = 3 Then    'Looking right
            Snake(Length).X = Snake(Length - 1).X - SnakeBody(0).Width
            Snake(Length).Y = Snake(Length - 1).Y
            SnakeBody(Length).Location = New Point(Snake(Length).X, Snake(Length).Y)
            SnakeBody(Length).Visible = True

        ElseIf Snake(Length - 1).Facing = 4 Then    'Looking down
            Snake(Length).X = Snake(Length - 1).X
            Snake(Length).Y = Snake(Length - 1).Y - SnakeBody(0).Height
            SnakeBody(Length).Location = New Point(Snake(Length).X, Snake(Length).Y)
            SnakeBody(Length).Visible = True
        End If

        PlaceFood()
    End If

如果有人能告诉我什么是错的,我会非常感激!

由于

1 个答案:

答案 0 :(得分:1)

有猜测(更多代码可能有帮助; IANA VB专家) - 这是Snake(0).XSnake(0).Y不能低于0或某事的某种边界问题吗?

其他建议:检查左侧身体段添加代码的标志。我认为这可能是一个问题:

Snake(Length).X = Snake(Length - 1).X - SnakeBody(0).Width

如果细分受众群朝左,我们正试图在最后添加一个部分,我们需要向右移动,即加号SnakeBody(0).Width

Snake(Length).X = Snake(Length - 1).X + SnakeBody(0).Width

希望这会有所帮助。我在学校的时候做了一个蛇游戏,所以我觉得你的痛苦在于让所有方向和身体部分的增加都能正常运作。