我正在用VB.NET编写游戏。它涉及在产卵的敌人身上发射射弹。我有一个能量棒应该在你拿着空格键时填满,然后在你放手时发射弹丸。但是,当您按下键和进度条开始前进之间似乎有1-2秒的延迟。有谁知道为什么会这样?
这是我的代码:
Public Class Game
Dim rad As Double = 60 * (Math.PI / 180)
Dim v As Double = 0
Dim vx As Integer = 0
Dim vy As Integer = 0
Const g As Double = -4.9
Dim deg As Char = Chr(176)
Dim Goblins As List(Of PictureBox) = New List(Of PictureBox)
Dim launching As Boolean = False
Private Sub frmProjectilePT_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tmeCreateGoblin.Enabled = True
tmeMoveGoblin.Enabled = True
tmeDetect.Enabled = True
End Sub
Private Sub frmProjectilePT_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Space Then
v += 1
tmeIncSpeed.Enabled = True
End If
End Sub
Private Sub frmProjectilePT_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Space And Not v = 0 Then
tmeIncSpeed.Enabled = False
box.Top = 300
box.Left = 0
vx = (v * Math.Abs(Math.Cos(rad))) / 2
vy = v * Math.Abs(Math.Sin(rad))
My.Computer.Audio.Play("G:\Course\Buhot Computer Sci 1 P1 J441-1\Drop Off\Project\App Project 2\Ben And Liams Experiments Continued\11B-X-1371\11B-X-1371\Projects\New folder\New folder\WindowsApplication1\WindowsApplication1\My Project\ni.wav", AudioPlayMode.Background)
tmeProjMove.Enabled = True
launching = True
v = 0
End If
End Sub
Private Sub tmeProjMove_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles tmeProjMove.Tick
If box.Top > Me.Height Then
tmeProjMove.Enabled = False
launching = False
v = 0
Else
box.Left += vx
box.Top -= vy
vy += g
End If
End Sub
Private Sub tmeCreateGoblin_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmeCreateGoblin.Tick
CreateGoblin()
Dim r As Integer = Int((7001 - 1000) * Rnd()) + 1000
tmeCreateGoblin.Interval = r
End Sub
Private Sub tmeMoveGoblin_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmeMoveGoblin.Tick
For Each goblin As PictureBox In Goblins
goblin.Left -= 50
Next
End Sub
Sub CreateGoblin()
Dim goblin As PictureBox = New PictureBox()
With goblin
.Size = New Size(50, 50)
.Location = New Point(Me.Width, Me.Height - .Height)
.BackColor = Color.Red
End With
Me.Controls.Add(goblin)
Goblins.Add(goblin)
End Sub
Sub RemoveGoblin(ByRef goblin As PictureBox)
Me.Controls.Remove(goblin)
Goblins.Remove(goblin)
End Sub
Private Sub tmeDetect_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmeDetect.Tick
meter.Value = If(v <= 100, v, 100)
For Each goblin As PictureBox In Goblins
If goblin.Left <= 0 Then
RemoveGoblin(goblin)
Exit For
End If
Next
If launching Then
For Each goblin As PictureBox In Goblins
If box.Top < (goblin.Top + goblin.Height) And box.Left < (goblin.Left + goblin.Width) And (box.Top + box.Height) > goblin.Top And (box.Left + box.Width) > goblin.Left Then
RemoveGoblin(goblin)
Exit For
End If
Next
End If
For Each goblin As PictureBox In Goblins
If goblin.Top < (knightsofni.Top + knightsofni.Height) And goblin.Left < (knightsofni.Left + knightsofni.Width) And (goblin.Top + goblin.Height) > knightsofni.Top And (goblin.Left + goblin.Width) > knightsofni.Left Then
RemoveGoblin(goblin)
health.Value -= 1
If health.Value = 0 Then
tmeMoveGoblin.Enabled = False
tmeCreateGoblin.Enabled = False
tmeDetect.Enabled = False
MsgBox("Game over")
TitleScreen.Show()
Me.Close()
End If
Exit For
End If
Next
End Sub
Private Sub tmeIncSpeed_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmeIncSpeed.Tick
v += 1
End Sub
Private Sub ExitToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
End Class