Results 1 to 7 of 7

Thread: 2- Player Simletaneous movement

Hybrid View

  1. #1
    New Member
    Join Date
    May 12
    Posts
    6

    2- Player Simletaneous movement

    Is there a tutorial/can someone help me with running two instances of code at the same time?
    Im trying to make two panels move on a form, one for each player, as part of a game. Thanks!

  2. #2
    New Member
    Join Date
    May 12
    Posts
    10

    Re: 2- Player Simletaneous movement

    There are a few ways to do this:
    1) you could create separate forms for each of the players, so it "looks" like they're moving around.
    2) you could create one class and several iterations of that class, painting each in panels and moving the panels in response to key inputs.
    3) you could just paint each image into a bitmap with the onPaint override and let that handle your movement.

    It all depends on the demands of your game.

  3. #3
    New Member
    Join Date
    May 12
    Posts
    6

    Re: 2- Player Simletaneous movement

    could you explain each in a bit more detail?

  4. #4
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 04
    Location
    Miami Beach, FL
    Posts
    4,836

    Re: 2- Player Simletaneous movement

    Actually you dont really need what Laimbrane said. But on the other hand the question was a tad vague so I'm gonna assume 2 things. First thing Im gonna assume is you meaning one player controlling 2 players simultaneously. It's easier than you think to move 2 players at the same time. Thats why there are no tutorials on the subject /rollseyes. Assuming you have a proper Game Loop, in where you are controlling the controls, heres some pseudo code of how it can be done:

    vb.net Code:
    1. Public Sub Keyboard_Controls()
    2.  
    3.     If Key = keyleft Then
    4.          Player1.Position.X -= 1
    5.          Player2.Position.X -= 1
    6.     End If
    7.  
    8.     If Key = keyright Then
    9.          Player1.Position.X += 1
    10.          Player2.Position.X += 1
    11.     End If
    12.  
    13.     If Key = keyup Then
    14.          Player1.Position.Y -= 1
    15.          Player2.Position.Y -= 1
    16.     End If
    17.  
    18.     If Key = keydown Then
    19.          Player1.Position.Y += 1
    20.          Player2.Position.Y += 1
    21.     End If
    22.  
    23. End Sub

    This sub gets run before everything is rendered so the movement is updated then collision is dectected, then drawn.

    Now thats assuming you meant that. If you mean Keycode events not firing 2 players controls at the same time then you can do one of 2 things. You can use this old vb6 code I have so you can have more than one key pressed at the same time:
    vb Code:
    1. Option Explicit
    2.  
    3. 'Key Flags //Must be in binary format. 1 2 4 8 16 32 64, etc
    4. Const UP_FLAG = 1
    5. Const DOWN_FLAG = 2
    6. Const LEFT_FLAG = 4
    7. Const RIGHT_FLAG = 8
    8.  
    9. Dim Neutral_Flag As Boolean
    10. Dim Key_State As Long, Key_Flag As Integer
    11.  
    12. Private Function Check_Key(Key_Flag As Integer)
    13.  
    14.     Check_Key = Key_State And Key_Flag
    15.  
    16. End Function
    17.  
    18. Private Sub Check_Key_Events()
    19.  
    20.     If Check_Key(UP_FLAG) Then Print "Up"
    21.     If Check_Key(DOWN_FLAG) Then Print "Down"
    22.     If Check_Key(LEFT_FLAG) Then Print "Left"
    23.     If Check_Key(RIGHT_FLAG) Then Print "Right"
    24.     If Neutral_Flag = True Then Print "Neutral"
    25.  
    26. End Sub
    27.  
    28. Private Sub Form_Activate()
    29.  
    30.     Neutral_Flag = True
    31.    
    32.     Do
    33.         Cls
    34.         Check_Key_Events
    35.         DoEvents
    36.     Loop
    37.  
    38. End Sub
    39.  
    40. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    41.  
    42.     Neutral_Flag = False
    43.    
    44.     Select Case KeyCode
    45.         Case vbKeyUp
    46.             Key_State = Key_State Or UP_FLAG
    47.         Case vbKeyDown
    48.             Key_State = Key_State Or DOWN_FLAG
    49.         Case vbKeyLeft
    50.             Key_State = Key_State Or LEFT_FLAG
    51.         Case vbKeyRight
    52.             Key_State = Key_State Or RIGHT_FLAG
    53.     End Select
    54.  
    55. End Sub
    56.  
    57. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    58.  
    59.     Neutral_Flag = True
    60.  
    61.     Select Case KeyCode
    62.         Case vbKeyUp
    63.             Key_State = Key_State And (Not UP_FLAG)
    64.         Case vbKeyDown
    65.             Key_State = Key_State And (Not DOWN_FLAG)
    66.         Case vbKeyLeft
    67.             Key_State = Key_State And (Not LEFT_FLAG)
    68.         Case vbKeyRight
    69.             Key_State = Key_State And (Not RIGHT_FLAG)
    70.     End Select
    71.  
    72. End Sub

    Or you can dive into DirectX9 and use DirectInput, which I find easier than the above method.
    Last edited by Jacob Roman; May 19th, 2012 at 11:20 AM.
    Useful Forum Tips:

    - If you found any post that was useful, please consider rating their post

    - Give sensible thread titles, please!
    - Be sure to use [HIGHLIGHT=VB][/HIGHLIGHT] tags when posting code written in VB.
    - Write posts like a conversation, not like an email. That way you can avoid saying "Dear so and so" in every post.
    - When your problem is solved, go up to Thread Tools and click on Mark Thread Resolved

    My Contributions: Massive DirectX 2D Tutorials For VB5/VB6/VB.NET || 3D Engine In Pure VB || Friction Force ||DJ Turntable Simulation || Scratching Wavs || Time Based Movement || Newton Physics Simulation || Managed Game Loop || Rigid Body Collision Detection || World of Warcraft RGB Battle System || Bosskillers (World of Warcraft 2D Clone) || Street Fighter Controls (Flawless Simulation) || A* Pathfinding (VB6 & VB.NET) *UPDATED || *NEW* Nintendo Entertainment System Emulator

    Expert. Ask me anything NES!

    My Smilies:


  5. #5
    New Member
    Join Date
    May 12
    Posts
    6

    Re: 2- Player Simletaneous movement

    yeah, that second one was the sort of thing I was looking for, i already have the prior code + loop implemented. can you explain that code for me though, id rather know how it actually works as opposed to copypasta, and there arent many comments on it, sorry if thats being a pain.
    Last edited by Dalolguru; May 19th, 2012 at 06:41 PM.

  6. #6
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 04
    Location
    Miami Beach, FL
    Posts
    4,836

    Re: 2- Player Simletaneous movement

    What my program does is a binary check. First of all ignore the Form_Activate. Use Form_Load event cause I was sort of "new" back when I wrote this. The first part of your game loop should check if those keys have been pressed. If you need more keys, add more flags. The Key_State variable is your binary check. And and Or are binary operators. By default, neutral is true unless a key has been pressed. The more keys that are pressed, the more thats included in the binary check. When keys are pressed in the key down event, its added to the Key_State. When keys are released in the KeyUp event, its eliminated from Key_State.

    Ok now to check if somethings true. In your loop it'll do a comparison to whatever key you wanna check. For example Up. If Check_Key(Up_Flag) > 0 Then Do whatever movement you want to do. Or for short we can also say If Check_Key(Up_Flag) Then Do whatever movement you want to do. Its real easy. DirectInput in DirectX is easier though cause you dont need the lousy Form_KeyDown and KeyUp events. Its just initializing Directinput and using a function to check keys. It doesnt use event tags and communicates directly with the keyboard.
    Useful Forum Tips:

    - If you found any post that was useful, please consider rating their post

    - Give sensible thread titles, please!
    - Be sure to use [HIGHLIGHT=VB][/HIGHLIGHT] tags when posting code written in VB.
    - Write posts like a conversation, not like an email. That way you can avoid saying "Dear so and so" in every post.
    - When your problem is solved, go up to Thread Tools and click on Mark Thread Resolved

    My Contributions: Massive DirectX 2D Tutorials For VB5/VB6/VB.NET || 3D Engine In Pure VB || Friction Force ||DJ Turntable Simulation || Scratching Wavs || Time Based Movement || Newton Physics Simulation || Managed Game Loop || Rigid Body Collision Detection || World of Warcraft RGB Battle System || Bosskillers (World of Warcraft 2D Clone) || Street Fighter Controls (Flawless Simulation) || A* Pathfinding (VB6 & VB.NET) *UPDATED || *NEW* Nintendo Entertainment System Emulator

    Expert. Ask me anything NES!

    My Smilies:


  7. #7
    New Member
    Join Date
    May 12
    Posts
    6

    Re: 2- Player Simletaneous movement

    cheers man, im with you now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •