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!
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!
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.![]()
could you explain each in a bit more detail?
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:
Public Sub Keyboard_Controls() If Key = keyleft Then Player1.Position.X -= 1 Player2.Position.X -= 1 End If If Key = keyright Then Player1.Position.X += 1 Player2.Position.X += 1 End If If Key = keyup Then Player1.Position.Y -= 1 Player2.Position.Y -= 1 End If If Key = keydown Then Player1.Position.Y += 1 Player2.Position.Y += 1 End If 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:
Option Explicit 'Key Flags //Must be in binary format. 1 2 4 8 16 32 64, etc Const UP_FLAG = 1 Const DOWN_FLAG = 2 Const LEFT_FLAG = 4 Const RIGHT_FLAG = 8 Dim Neutral_Flag As Boolean Dim Key_State As Long, Key_Flag As Integer Private Function Check_Key(Key_Flag As Integer) Check_Key = Key_State And Key_Flag End Function Private Sub Check_Key_Events() If Check_Key(UP_FLAG) Then Print "Up" If Check_Key(DOWN_FLAG) Then Print "Down" If Check_Key(LEFT_FLAG) Then Print "Left" If Check_Key(RIGHT_FLAG) Then Print "Right" If Neutral_Flag = True Then Print "Neutral" End Sub Private Sub Form_Activate() Neutral_Flag = True Do Cls Check_Key_Events DoEvents Loop End Sub Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Neutral_Flag = False Select Case KeyCode Case vbKeyUp Key_State = Key_State Or UP_FLAG Case vbKeyDown Key_State = Key_State Or DOWN_FLAG Case vbKeyLeft Key_State = Key_State Or LEFT_FLAG Case vbKeyRight Key_State = Key_State Or RIGHT_FLAG End Select End Sub Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) Neutral_Flag = True Select Case KeyCode Case vbKeyUp Key_State = Key_State And (Not UP_FLAG) Case vbKeyDown Key_State = Key_State And (Not DOWN_FLAG) Case vbKeyLeft Key_State = Key_State And (Not LEFT_FLAG) Case vbKeyRight Key_State = Key_State And (Not RIGHT_FLAG) End Select 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 Toolsand 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:
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.
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 Toolsand 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:
cheers man, im with you now![]()