我创建了一个简单的VB.Net Windows Form应用程序来演示使用Tic Tac Toe的类层次结构 - 相互依赖的玩家策略。我想让人类玩,所以我需要从键盘上获取一个值:1-9表示位置。我不想要一个表单字段或精心设计的钩子方案等等。是否有我可以使用的Console ReadKey功能?
答案 0 :(得分:0)
将表单的KeyPreview
属性设置为True,然后在表单KeyUp
事件中解析结果。
答案 1 :(得分:0)
像这样覆盖ProcessCmdKey()
:
Public Class Form1
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
Select Case keyData
Case Keys.Home
Debug.Print("7")
Case Keys.Up
Debug.Print("8")
Case Keys.PageUp
Debug.Print("9")
Case Keys.Left
Debug.Print("4")
Case Keys.Clear
Debug.Print("5")
Case Keys.Right
Debug.Print("6")
Case Keys.End
Debug.Print("1")
Case Keys.Down
Debug.Print("2")
Case Keys.PageDown
Debug.Print("3")
End Select
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
End Class
----------编辑----------
我可以打电话给它,它会等待按键吗? - 没有comprende
不是这种形式,不是。然而,实现此目的的一种方法是让游戏循环在另一个THREAD中运行然后使其阻塞 在等待设置ManualResetEvent时。我将用a更新 我回到家的简短例子。 - Idle_Mind
以下是模拟 TicTacToe游戏的简单示例:
Public Class Form1
Private Selection As Integer = -1
Private MRE As New ManualResetEvent(False)
Private Sub btnStartGame_Click(sender As Object, e As EventArgs) Handles btnStartGame.Click
btnStartGame.Enabled = False
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim TTT As New TicTacToe
While Not TTT.GameOver
UpdateLabel("Computer Thinking...")
System.Threading.Thread.Sleep(2000)
TTT.ComputerMove()
UpdateLabel("Computer Moved")
System.Threading.Thread.Sleep(1000)
GetHumanMove("Use the KeyPad to indicate your move!")
TTT.HumanMove(Selection)
UpdateLabel("Move Selected: " & Selection)
System.Threading.Thread.Sleep(2000)
End While
e.Result = TTT.Result
End Sub
Private Sub UpdateLabel(ByVal msg As String)
Label1.Invoke(Sub()
Label1.Text = msg
End Sub)
End Sub
Private Sub GetHumanMove(ByVal msg As String)
UpdateLabel(msg)
MRE.Reset()
MRE.WaitOne()
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
Select Case keyData
Case Keys.Home
Selection = 7
MRE.Set()
Case Keys.Up
Selection = 8
MRE.Set()
Case Keys.PageUp
Selection = 9
MRE.Set()
Case Keys.Left
Selection = 4
MRE.Set()
Case Keys.Clear
Selection = 5
MRE.Set()
Case Keys.Right
Selection = 6
MRE.Set()
Case Keys.End
Selection = 1
MRE.Set()
Case Keys.Down
Selection = 2
MRE.Set()
Case Keys.PageDown
Selection = 3
MRE.Set()
End Select
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Dim result As String = e.Result
MessageBox.Show(result, "Game Over!")
btnStartGame.Enabled = True
End Sub
End Class
Public Class TicTacToe
Private MoveCounter As Integer
Public ReadOnly Property GameOver As Boolean
Get
Return MoveCounter = 6
End Get
End Property
Public Sub ComputerMove()
MoveCounter = MoveCounter + 1
End Sub
Public Sub HumanMove(ByVal selection As Integer)
MoveCounter = MoveCounter + 1
End Sub
Public ReadOnly Property Result As String
Get
Return "Draw"
End Get
End Property
End Class