Hey,
I was trying to make a program which displays two chars A and B on the screen. Using the WASD keys, it lets you move the A char around and using the arrow keys it lets you move the B char around.
I wrote the following:
Module Module1
Dim Box_A(1), Box_B(1) As Byte
Dim Allow_Draw As Boolean
'Declare Function GetTickCount Lib "kernel32.dll" () As Integer
Sub Main()
Dim Thread_Draw As New Threading.Thread(AddressOf Draw)
Dim Thread_Move_A As New Threading.Thread(AddressOf Move_A)
Dim Thread_Move_B As New Threading.Thread(AddressOf Move_B)
Box_B(0) = 79
Thread_Draw.Start()
Thread_Move_A.Start()
Thread_Move_B.Start()
End Sub
Sub Draw()
Do
If Allow_Draw = True Then
Console.Clear()
For Y = 9 To 0 Step -1
For X = 0 To 79
If Box_A(0) = X And Box_A(1) = Y Then
Console.Write("A")
ElseIf Box_B(0) = X And Box_B(1) = Y Then
Console.Write("B")
Else
Console.Write(" ")
End If
Next
Next
Allow_Draw = False
End If
Loop
End Sub
Sub Move_A()
Dim Input_Key_A As ConsoleKeyInfo
Dim Input_Key_String_A As String
Do
Input_Key_A = Console.ReadKey(True)
Input_Key_String_A = Input_Key_A.Key.ToString()
If Input_Key_String_A = "W" Then
If Box_A(1) = 9 Then
Box_A(1) = 0
Else
Box_A(1) += 1
End If
ElseIf Input_Key_String_A = "A" Then
If Box_A(0) = 0 Then
Box_A(0) = 79
Else
Box_A(0) -= 1
End If
ElseIf Input_Key_String_A = "S" Then
If Box_A(1) = 0 Then
Box_A(1) = 9
Else
Box_A(1) -= 1
End If
ElseIf Input_Key_String_A = "D" Then
If Box_A(0) = 79 Then
Box_A(0) = 0
Else
Box_A(0) += 1
End If
End If
Allow_Draw = True
Loop
End Sub
Sub Move_B()
Dim Input_Key As ConsoleKeyInfo
Dim Input_Key_String As String
Do
Input_Key = Console.ReadKey(True)
Input_Key_String = Input_Key.Key.ToString()
If Input_Key_String = "UpArrow" Then
If Box_B(1) = 9 Then
Box_B(1) = 0
Else
Box_B(1) += 1
End If
ElseIf Input_Key_String = "LeftArrow" Then
If Box_B(0) = 0 Then
Box_B(0) = 79
Else
Box_B(0) -= 1
End If
ElseIf Input_Key_String = "DownArrow" Then
If Box_B(1) = 0 Then
Box_B(1) = 9
Else
Box_B(1) -= 1
End If
ElseIf Input_Key_String = "RightArrow" Then
If Box_B(0) = 79 Then
Box_B(0) = 0
Else
Box_B(0) += 1
End If
End If
Allow_Draw = True
Loop
End Sub
End Module
When I run the program, if I press any of the WASD or arrow keys the corresponding char doesn't move. To make it move, I have to hold down any of its keys for a bit after which I can move the char just by pressing its keys. But then, I have to do the same for the other char.
So, if I want to move B I have to hold any of the arrow keys for a bit. Then I can easily move B just by pressing any arrow key once. Then to move A I have to hold any of the WASD keys for a bit and then I can move A just by pressing any WASD key once.
Is there any way I make make each of the char move just by pressing its ket once? I know it's most likely the way I have programmed it.
Also, I know I could have made it easier by not using multithreading but I just wanted to try it out. Thanks for your time :)