我正在尝试使用 VB 控制台构建康威人生游戏的模拟(我很奇怪。)事情有点像在职的。该程序发生在四个具有单独布尔条件的循环中:暂停、完成、清除和退出,从最里面到最外面。控制一切的是一个 BackgroundWorker,它读取 Keypresses 并更改所有布尔变量。只有有时在使用 BackgroundWorker 和 Console.Readkey 时,我才会得到这个需要两次才能读取 Keypress 的东西。我在其他项目中也有过,我不知道为什么。当我暂停程序时,它显然在等待两个线程上的按键,但只有当我第一次按下它时,调试器才会提示我按键。这不是一个大问题,但无论如何,我想知道我是否可以做些什么来消除这个问题。
这是主要的子,不包括声明:
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 半年,我还不是很好。
我感谢所有的想法和反馈:)。
编辑:另外,我不能在帖子的开头加上“你好”或任何愉快的问候,它会被抹去 oO