Reputation: 2519
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?
Upvotes: 1
Views: 930
Reputation: 9888
You should stop the timer while in the elapsed method. Try to stop the timer on the beggning and enabling it on the last line.
Sub FruitTick() Handles FruitTimer.Elapsed
FruitTimer.Enabled = False
' Your actual code
FruitTimer.Enabled = True
End Sub
Probably, your code last more than a second and the code starts again before the last execution is complete. Which is more evident when debugging. It will probably be generating all your problems and it will cause memory issues on the end.
Upvotes: 1