Question 2D Shooting Game Help

Bryce Gough

Active member
Joined
Mar 8, 2011
Messages
36
Location
Perth, Australia
Programming Experience
1-3
hello, I have made a multiplayer (2 people on one keyboard) shooting game in vb.net, and I need help with the movment, at the moment everyone can move fine, but if someone shoots or moves while someone else is moving, then only one person can move at a time.
How would I change the code so more than one keypress can be picked up at once, I would like the code to be short, but any code will do and help me! :D
Here is my code:

VB.NET:
  Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If PauseMenu.Visible = True Then
            If e.KeyValue = Keys.Escape Then
                PauseMenu.Hide()
                gameenabled = True
            End If
        Else
            If e.KeyValue = Keys.Escape Then
                PauseMenu.Show()
                gameenabled = False
            End If
        End If
        If gameenabled = True Then
            'Player1 Controls
            If e.KeyValue = Keys.A Then
                player.Image = My.Resources.stand
                My.Settings.Position = False
                player.Location = New Point(player.Location.X - 10, player.Location.Y - 0.5)
            ElseIf e.KeyValue = Keys.D Then
                player.Image = My.Resources.standow
                My.Settings.Position = True
                player.Location = New Point(player.Location.X + 10, player.Location.Y + 0.5)
            End If
            If e.KeyValue = Keys.Space Then
                JumpUp.Enabled = True
            End If
            If e.KeyValue = Keys.ShiftKey Then
                shoot()
            End If
            'End Player1 Controls
            'Player2 Contols

            If e.KeyValue = Keys.Left Then
                player2.Image = My.Resources.player2stand
                My.Settings.P2Position = False
                player2.Location = New Point(player2.Location.X - 10, player2.Location.Y - 0.5)
            ElseIf e.KeyValue = Keys.Right Then
                player2.Image = My.Resources.player2standow
                My.Settings.P2Position = True
                player2.Location = New Point(player2.Location.X + 10, player2.Location.Y + 0.5)
            End If
            If e.KeyValue = Keys.Up Then
                p2JumpUp.Enabled = True
            End If
            If e.KeyValue = Keys.Down Then
                player2shoot()
            End If
            'End Player2 Controls
        Else
        End If
    End Sub

Please help me :D
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,779
Location
Sydney, Australia
Programming Experience
10+
You can't make multiple key presses detectable at once. What you need to do is not use the KeyPress event. You should create a Timer for each person and, each time that Timer Ticks, move that person in the appropriate direction based on the keys they have depressed. You can use the KeyDown and KeyUp events to detect when a key is depressed and when it is released. You store the depressed keys for each player in one or more variables and test those variables on each Tick.
 
Top Bottom