以下是我的代码,我的这个小游戏的操作是在屏幕上有三个随机的上下左右的键,在一定的时间内让用户输入那几个键,规定的时间内是否完成有不同的结果,我的随机出现都实现了,可就是键盘事件没有起到作用,看看我的是什么问题啊,各位,我的这个问题已经在这里问了两遍了。
哦,忘了,那个a2,b2和c2我后来改成了静态变量,我想应该改成静态的
Public Sub Command1_Click()
Timer1 = True
Picture13.Width = 0
Dim a%, b%, c%, a2%, b2%, c2%
Dim file As String
file = App.Path
a = Int((4 - 1 + 1) * Rnd() + 1)
b = Int((4 - 1 + 1) * Rnd() + 1)
c = Int((4 - 1 + 1) * Rnd() + 1)
Select Case a
Case 1
a2 = 118Form1.Picture10 = LoadPicture(file & ("\00\lanup.bmp"))
Case 2
a2 = 114
Form1.Picture10 = LoadPicture(file & ("\00\landown.bmp"))
Case 3
a2 = 96
Form1.Picture10 = LoadPicture(file & ("\00\lanleft.bmp"))
Case 4
a2 = 100
Form1.Picture10 = LoadPicture(file & ("\00\lanright.bmp"))
End Select
Select Case b
Case 1
b2 = 118
Form1.Picture11 = LoadPicture(file & ("\00\lanup.bmp"))
Case 2
b2 = 114
Form1.Picture11 = LoadPicture(file & ("\00\landown.bmp"))
Case 3
b2 = 96
Form1.Picture11 = LoadPicture(file & ("\00\lanleft.bmp"))
Case 4
b2 = 100
Form1.Picture11 = LoadPicture(file & ("\00\lanright.bmp"))
End SelectSelect Case c
Case 1
c2 = 118
Form1.Picture12 = LoadPicture(file & ("\00\\lanup.bmp"))
Case 2
c2 = 114
Form1.Picture12 = LoadPicture(file & ("\00\landown.bmp"))
Case 3
c2 = 96
Form1.Picture12 = LoadPicture(file & ("\00\lanleft.bmp"))
Case 4
c2 = 100
Form1.Picture12 = LoadPicture(file & ("\00\lanright.bmp"))
End SelectEnd SubPrivate Sub form1_keypress(keyascill As Integer)
Dim file As String
file = App.Path
If keyascii = a2 Then
Form1.Picture10 = LoadPicture(file & ("\00\up.bmp"))
If keyCode = b2 Then
Form1.Picture11 = LoadPicture(file & ("\00\down.bmp"))
If keyCode = c2 Then
Form1.Picture11 = LoadPicture(file & ("\00\left.bmp"))
If keyCode = d2 Then
Form1.Picture11 = LoadPicture(file & ("\00\right.bmp"))
Else
End IfElse
End IfElse
End IfElse
End If
End Sub
Private Sub Timer1_Timer()
If Picture13.Width >= 4000 ThenTimer1 = FalseEnd If
Picture13.Width = Picture13.Width + 100
End Sub
哦,忘了,那个a2,b2和c2我后来改成了静态变量,我想应该改成静态的
Public Sub Command1_Click()
Timer1 = True
Picture13.Width = 0
Dim a%, b%, c%, a2%, b2%, c2%
Dim file As String
file = App.Path
a = Int((4 - 1 + 1) * Rnd() + 1)
b = Int((4 - 1 + 1) * Rnd() + 1)
c = Int((4 - 1 + 1) * Rnd() + 1)
Select Case a
Case 1
a2 = 118Form1.Picture10 = LoadPicture(file & ("\00\lanup.bmp"))
Case 2
a2 = 114
Form1.Picture10 = LoadPicture(file & ("\00\landown.bmp"))
Case 3
a2 = 96
Form1.Picture10 = LoadPicture(file & ("\00\lanleft.bmp"))
Case 4
a2 = 100
Form1.Picture10 = LoadPicture(file & ("\00\lanright.bmp"))
End Select
Select Case b
Case 1
b2 = 118
Form1.Picture11 = LoadPicture(file & ("\00\lanup.bmp"))
Case 2
b2 = 114
Form1.Picture11 = LoadPicture(file & ("\00\landown.bmp"))
Case 3
b2 = 96
Form1.Picture11 = LoadPicture(file & ("\00\lanleft.bmp"))
Case 4
b2 = 100
Form1.Picture11 = LoadPicture(file & ("\00\lanright.bmp"))
End SelectSelect Case c
Case 1
c2 = 118
Form1.Picture12 = LoadPicture(file & ("\00\\lanup.bmp"))
Case 2
c2 = 114
Form1.Picture12 = LoadPicture(file & ("\00\landown.bmp"))
Case 3
c2 = 96
Form1.Picture12 = LoadPicture(file & ("\00\lanleft.bmp"))
Case 4
c2 = 100
Form1.Picture12 = LoadPicture(file & ("\00\lanright.bmp"))
End SelectEnd SubPrivate Sub form1_keypress(keyascill As Integer)
Dim file As String
file = App.Path
If keyascii = a2 Then
Form1.Picture10 = LoadPicture(file & ("\00\up.bmp"))
If keyCode = b2 Then
Form1.Picture11 = LoadPicture(file & ("\00\down.bmp"))
If keyCode = c2 Then
Form1.Picture11 = LoadPicture(file & ("\00\left.bmp"))
If keyCode = d2 Then
Form1.Picture11 = LoadPicture(file & ("\00\right.bmp"))
Else
End IfElse
End IfElse
End IfElse
End If
End Sub
Private Sub Timer1_Timer()
If Picture13.Width >= 4000 ThenTimer1 = FalseEnd If
Picture13.Width = Picture13.Width + 100
End Sub
放到最上面通用区,否则 form1_keypress事件 调用不到
Form_KeyPress是捕捉不到上下左右的。
暂不谈你的 Form1_Keypress() 事件的逻辑有什么问题(说得不客气点,就是“完全是错误的!”),先说一下你的
a2、b2、c2赋的值对应的按键:
上----118 字符为v(只能是小写)
下----114 字符为r(只能是小写)
左---- 96 字符为`(Esc下面的那个键)
右----100 字符为d(只能是小写)这样的“分布”合理吗?
这是我修改后的,还是不行!看看把?问题这么解决呢
Dim file As String
Dim a%, b%, c%, a2%, b2%, c2%Public Sub Command1_Click()
file = App.Path
Timer1 = True
Picture13.Width = 0Randomize
a = Int((4 - 1 + 1) * Rnd() + 1)
b = Int((4 - 1 + 1) * Rnd() + 1)
c = Int((4 - 1 + 1) * Rnd() + 1)
Select Case a
Case 1
a2 = vbKeyUpForm1.Picture10 = LoadPicture(file & ("\00\lanup.bmp"))
Case 2
a2 = vbKeyDown
Form1.Picture10 = LoadPicture(file & ("\00\landown.bmp"))
Case 3
a2 = vbKeyLeft
Form1.Picture10 = LoadPicture(file & ("\00\lanleft.bmp"))
Case 4
a2 = vbKeyRight
Form1.Picture10 = LoadPicture(file & ("\00\lanright.bmp"))
End Select
Select Case b
Case 1
b2 = vbKeyUp
Form1.Picture11 = LoadPicture(file & ("\00\lanup.bmp"))
Case 2
b2 = vbKeyDown
Form1.Picture11 = LoadPicture(file & ("\00\landown.bmp"))
Case 3
b2 = vbKeyLeft
Form1.Picture11 = LoadPicture(file & ("\00\lanleft.bmp"))
Case 4
b2 = vbKeyRight
Form1.Picture11 = LoadPicture(file & ("\00\lanright.bmp"))
End SelectSelect Case c
Case 1
c2 = vbKeyUp
Form1.Picture12 = LoadPicture(file & ("\00\\lanup.bmp"))
Case 2
c2 = vbKeyDown
Form1.Picture12 = LoadPicture(file & ("\00\landown.bmp"))
Case 3
c2 = vbKeyLeft
Form1.Picture12 = LoadPicture(file & ("\00\lanleft.bmp"))
Case 4
c2 = vbKeyRight
Form1.Picture12 = LoadPicture(file & ("\00\lanright.bmp"))
End Select
Text1.Text = Str(a2) '测试通过可以
Text2.Text = Str(b2)
Text3.Text = Str(c2)
End SubPrivate Sub form1_keydown(keycode As Integer)
file = App.Path
If keycode = a2 Then
Form1.Picture10 = LoadPicture(file & ("\00\up.bmp"))
If keycode = b2 Then
Form1.Picture11 = LoadPicture(file & ("\00\down.bmp"))
If keycode = c2 Then
Form1.Picture11 = LoadPicture(file & ("\00\left.bmp"))
If keycode = d2 Then
Form1.Picture11 = LoadPicture(file & ("\00\right.bmp"))
Else
End If
Else
End If
Else
End If
Else
End If
End SubPrivate Sub Timer1_Timer()
If Picture13.Width >= 4000 Then
Timer1 = False
End If
Picture13.Width = Picture13.Width + 100
End SubPrivate Sub Form_Load()
Me.KeyPreview = True
End Sub
vbKeyUp、vbKeyLeft、vbKeyRight 在VB中都是已经定义的常量,是可以用的。它们分别对应箭头键的KeyCode ,只是这个只有在 KeyDown()、KeyUp()事件中才能应用的。我不明白你的“但是我要KEYCODE的值是被赋值”是什么意思。(感觉有点不对劲 ^_^)反正你似乎得好生考虑一下你的“程序逻辑”的问题。
这个比较好抓用户按的按键。
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37: Label1.Caption = "←"
Case 38: Label1.Caption = "↑"
Case 39: Label1.Caption = "→"
Case 40: Label1.Caption = "↓"
End Select
End Sub
'当Form1的 .KeyPreview 为 True 时,下面这段代码可以不要
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode >= 37 And KeyCode <= 40) Then
Form_KeyDown KeyCode, 0
KeyCode = 0 '使本控件不响应这些键
End If
End Sub运行程序时,你按键盘上的箭头键看效果。
我真不明白你说的究竟是什么了! -_-!--------------------------------------我的代码在 Text1 的 KeyDown() 事件中,显式调用了 Form1 的 KeyDown() 事件。这不是“在别处调用”?
Form_KeyDown KeyCode, 0 = Call Form_KeyDown(KeyCode, 0)
窗体内各个对象(包括窗体)的事件代码,只能是 Private 的,也就是说,只有“本模块”中才能调用。不知道你的“在别处调用”是啥意思。如果你“在别处”要调用的代码与某个对象的事件代码相同,你应该把这段代码放到一个过程中,在对象的事件过程中调用它,“在别处”也可以调用它。
[email protected]
你会使用 DirectX 3D 的那些接口吗?