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 :)

Edited 5 Years Ago by SoulReaper1680: n/a

Get rid of your async's

Try this

Module Module1

    Dim Box_A(1), Box_B(1) As Byte
    Dim Allow_Draw As Boolean = True

    Sub Main()
    
        Box_B(0) = 79

        Dim Input_Key_K As ConsoleKeyInfo
        Dim Input_Key As String

        Input_Key_K = Console.ReadKey(True)
        Input_Key = Input_Key_K.Key.ToString()

        Do While True

            If Input_Key = "W" Then
                If Box_A(1) = 9 Then
                    Box_A(1) = 0
                    Allow_Draw = True
                Else
                    Box_A(1) += 1
                    Allow_Draw = True
                End If

            ElseIf Input_Key = "A" Then
                If Box_A(0) = 0 Then
                    Box_A(0) = 79
                    Allow_Draw = True
                Else
                    Box_A(0) -= 1
                    Allow_Draw = True
                End If

            ElseIf Input_Key = "S" Then
                If Box_A(1) = 0 Then
                    Box_A(1) = 9
                    Allow_Draw = True
                Else
                    Box_A(1) -= 1
                    Allow_Draw = True
                End If

            ElseIf Input_Key = "D" Then
                If Box_A(0) = 79 Then
                    Box_A(0) = 0
                    Allow_Draw = True
                Else
                    Box_A(0) += 1
                    Allow_Draw = True
                End If

            ElseIf Input_Key = "UpArrow" Then
                If Box_B(1) = 9 Then
                    Box_B(1) = 0
                    Allow_Draw = True
                Else
                    Box_B(1) += 1
                    Allow_Draw = True
                End If

            ElseIf Input_Key = "LeftArrow" Then
                If Box_B(0) = 0 Then
                    Box_B(0) = 79
                    Allow_Draw = True
                Else
                    Box_B(0) -= 1
                    Allow_Draw = True
                End If


            ElseIf Input_Key = "DownArrow" Then
                If Box_B(1) = 0 Then
                    Box_B(1) = 9
                    Allow_Draw = True
                Else
                    Box_B(1) -= 1
                    Allow_Draw = True
                End If

            ElseIf Input_Key = "RightArrow" Then
                If Box_B(0) = 79 Then
                    Box_B(0) = 0
                    Allow_Draw = True
                Else
                    Box_B(0) += 1
                    Allow_Draw = True

                End If
            End If

            If Allow_Draw = True Then
                Console.Clear()

                For Y = 9 To 0 Step -1
                    For X = 0 To Console.WindowWidth - 1

                        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

            Input_Key_K = Console.ReadKey(True)
            Input_Key = Input_Key_K.Key.ToString()

        Loop

    End Sub

End Module

I know I could do it without threads but was just wondering why it was causing a problem when I was using threads.

I had threads because I wanted to try and read two inputs at the same time but that didn't work :/

Thanks for your reply :)

The problem with your threads is you had them in a continous do loop.

Things were buttin heads.

Console app is syncronous. No need to do asnc activity.

The problem with your threads is you had them in a continous do loop.

Things were buttin heads.

Console app is syncronous. No need to do asnc activity.

Fair enough I get it. Thanks for your reply :)

This question has already been answered. Start a new discussion instead.