1

I'm making a Console game where a moving character has to move left and right to intercept falling 'fruit'/ASCII characters, only I'm having trouble. I'm using a timer with a 1 second interval, and every time it elapses it's supposed to check a list of fruit that's already on the board and move each fruit down by one, and then it randomly inserts a new fruit onto the board. Fruits are all kept as objects in a class.

Here's the timer code:

Sub FruitTick() Handles FruitTimer.Elapsed
    Dim RandomNumber As Integer
    Dim Fruit As Fruit

    For i = 0 To FruitList.Count - 1
        If FruitList(i).Position.Y < FruitBoard.Height - 1 Then
            FruitList(i).LowerFruitByOne()
        End If
    Next

    PeriodUntilFruitAppears -= 1
    If PeriodUntilFruitAppears <= 0 Then
        PeriodUntilFruitAppears = FruitFrequency
        RandomNumber = New Random().Next(1, 5)
        If RandomNumber = 1 Then
            Fruit = New Fruit()
            Fruit.AddToList()
            Fruit.PlaceOnBoard()
        End If
    End If
End Sub

And here's the class for Fruit:

Public Class Fruit
    Private FruitIcons() As Char = {"#", "ð", "ó", "ç", "%", "$"}
    Public Icon As Char
    Public Position As Location
    Public Colour As ConsoleColor
    Sub New()
        Me.Icon = FruitIcons(New Random().Next(FruitIcons.Length))
        Me.Position = New Location(New Random().Next(FruitBoard.Width), 0)
        Me.Colour = New Random().Next(1, 16)
    End Sub
    Sub New(_Icon As Char, _
            _Position As Location, _
            _Colour As ConsoleColor)

        Me.Icon = _Icon
        Me.Position = New Location(_Position.X, 0)
        Me.Colour = _Colour
    End Sub
    Sub PlaceOnBoard()
        Console.SetCursorPosition(FruitBoard.Position.X + Me.Position.X, FruitBoard.Position.Y + Me.Position.Y)
        Console.ForegroundColor = Me.Colour
        Console.BackgroundColor = FruitBoard.BackColour
        Console.Write(Me.Icon)
    End Sub
    Sub AddToList()
        FruitList.Add(Me)
    End Sub
    Sub LowerFruitByOne()
        Dim DrawInstruction As Instruction
        DrawInstruction = New Instruction(" ", _
                                          New Location(FruitBoard.Position.X + Me.Position.X, _
                                                       FruitBoard.Position.Y + Me.Position.Y), _
                                          FruitBoard.BackColour, _
                                          FruitBoard.BackColour)
        DrawInstruction.Execute()
        Me.Position.Y += 1
        DrawInstruction = New Instruction(Me.Icon, _
                                          New Location(FruitBoard.Position.X + Me.Position.X, _
                                                       FruitBoard.Position.Y + Me.Position.Y), _
                                          Me.Colour, _
                                          FruitBoard.BackColour)
        DrawInstruction.Execute()
    End Sub
End Class

The Instruction class referred to is simply used to redraw characters in the Console.

I'm having weird problems, such as trailing characters where they should have been drawn over by a blank space, the fruit falling two characters instead of one, fruit spawning to the left of the previous fruit and then stopping, etc... but I'm especially having a problem debugging it. When I put a breakpoint in and step into the code, the debugger seems to go from place to place erratically, as if the timer's still running while it's paused and I'm too slow.

Is there any way to debug it properly, line-by-line, or am I going to have to make intelligent guesses about what's going on?

4

1 回答 1

1

您应该在 elapsed 方法中停止计时器。尝试在开始时停止计时器并在最后一行启用它。

Sub FruitTick() Handles FruitTimer.Elapsed
    FruitTimer.Enabled = False

    ' Your actual code

    FruitTimer.Enabled = True
End Sub

可能,您的代码持续了超过一秒钟,并且代码在最后一次执行完成之前再次启动。调试时更明显。它可能会产生你所有的问题,最终会导致内存问题。

于 2013-08-05T15:38:52.667 回答