我試圖用VB控制檯(我很奇怪)構建一個康威的遊戲人生遊戲。事情是種類的工作。該程序在四個循環內發生,具有不同的布爾條件:暫停,完成,清除和退出,從最裏層到最外層。控制所有東西都是一個BackgroundWorker,它讀取按鍵並更改所有布爾變量。有時只有在使用BackgroundWorker和Console.Readkey時,我會在需要兩次讀取按鍵的地方得到這個東西。我也在其他項目中使用過,我無法弄清楚原因。當我暫停程序時,它顯然正在等待兩個線程上的按鍵,但只有在第一次按調試器提示我進行按鍵時才按下它。這不是一個大問題,但都是一樣的,我想知道是否有什麼我可以做的,以消除這一點。在VB.Net中使用Console.Readkey方法的問題BackgroundWorker
這是主要的子,不聲明:
Do Until Quit = True 'This block repeats until Quit is true
Start = False : Finish = False : Pause = False : Quit = False : Iterate = False : Clear = False
Generation = 0 'This resets the Generation counter to 0
ClearArray(Board) 'This resets a 2D boolean array to false.
UpdateTitle(Generation) 'This updates the Console.Title to show the current generation.
DrawBorder(Board, Offset, ConsoleColor.Cyan, BorderBlock) 'This draws an ASCII border around the grid.
DrawBoard(Board, Offset) 'This draws the physical grid itself.
WriteMessage(1, BoardDimensions(1) - 1, Offset) 'This writes a line below the grid prompting user input.
ObtainInitialSetup(Board, Offset) 'This sub links to a bunch of other subs allowing the user to input an initial pattern.
TransferArrayContents(Board, BoardMemory) 'This stores the contents of the pattern the user has entered into a memory array.
Clear = False
Do Until Clear = True 'This block repeats until Clear is true
TransferArrayContents(BoardMemory, Board) 'This recalls the saved pattern from memory. If a user presses "Stop" then all generation
'will cease and this is where the board is reset.
DrawBoard(Board, Offset) 'The board is redrawn.
WriteMessage(2, BoardDimensions(1) - 1, Offset)
Do 'This block waits for the user to confirm before starting.
Keypress = Console.ReadKey(True)
If Keypress.Key = StartKey Then Start = True : Finish = False : Clear = False : Quit = False : Pause = False
If Keypress.Key = QuitKey Then Finish = True : Clear = True : Quit = True
If Keypress.Key = ClearKey Then Finish = True : Clear = True
'The program does not start until a user presses Start, Clear or Quit.
Loop Until Start = True Or Finish = True Or Clear = True Or Quit = True
If Quit = False Then WriteMessage(3, BoardDimensions(1) - 1, Offset)
If Finish = False Then If BK.IsBusy = False Then BK.RunWorkerAsync()
'The backgroundworker is called so long as it isn't already running.
Do Until Finish = True 'This block repeats until Stop is true
Iterate = False 'The Iterate condition allows the Loop to go through once.
UpdateTitle(Generation) 'This updates the title.
Generate(Board, CoordList, Generation, Survivals, Births) 'This sub does the calculation across the whole Board and changes
'the values in the board for the next generation.
DrawBoard(Board, Offset) 'This draws the new board.
CheckPause(Offset, BoardDimensions(1) - 1) 'This sub holds the loop if Pause is true by inserting a new Do Loop.
Loop
Loop
Loop
BK.CancelAsync()
這是BackgroundWorker的子:
Sub ReadBackgroundKey() Handles BK.DoWork
Dim Keypress As ConsoleKeyInfo
Do
Keypress = Console.ReadKey(True)
If Keypress.Key = StopKey Then 'Finish
Quit = False
Clear = False
Finish = True
ElseIf Keypress.Key = PauseKey Then 'Pause the generation.
Quit = False
Clear = False
Finish = False
Pause = Not Pause
ElseIf Keypress.Key = QuitKey Then 'Quit the game.
Quit = True
Clear = True
Finish = True
Pause = False
ElseIf Keypress.Key = StepKey Then 'Step a generation.
Quit = False
Clear = False
Finish = False
Pause = True
Iterate = True
ElseIf Keypress.Key = ClearKey Then 'Clear the board.
Quit = False
Clear = True
Finish = True
Pause = False
End If
Loop Until Quit = True Or Clear = True
End Sub
另外,如果你發現任何可怕的,沒有體面的程序員應該永遠做,所有的批評都是受歡迎的 - 我只做了半年的VB,而且我還不是很好。
我欣賞所有想法和反饋:)。
編輯:另外,我不能在帖子的開頭放置「你好」或任何愉快的問候,它會被消滅o.O.