Movement in a Game
Movement in a Game
Movement in a Game
Movement in a Game
Movement in a Game
Movement in a Game Movement in a Game Movement in a Game Movement in a Game Movement in a Game Movement in a Game Movement in a Game Movement in a Game
Movement in a Game Movement in a Game
Movement in a Game
Go Back  Xtreme Visual Basic Talk > > > Movement in a Game


Reply
 
Thread Tools Display Modes
  #1  
Old 03-05-2012, 06:30 AM
eclifox eclifox is offline
Newcomer
 
Join Date: Mar 2012
Location: Australia
Posts: 6
Default Movement in a Game


Hi, I am making a game as a hobby in Visual Studio. It is a top down shooter.
Can someone help me to allow more than left, right, up, down movements at once since currently with this code l can only move at one direction at a time

PictureBox1 is the character
PictureBox2 is the Projectile
PictureBox3 is the Projectile Fliped around(not sure if this is necessary)
Code:
Public Class Form1

    Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.W
                PictureBox1.Top = PictureBox1.Top - 5
                PictureBox2.Top = PictureBox2.Top - 5
                PictureBox3.Top = PictureBox3.Top - 5
            Case Keys.S
                PictureBox1.Top = PictureBox1.Top + 5
                PictureBox2.Top = PictureBox2.Top + 5
                PictureBox3.Top = PictureBox3.Top + 5
            Case Keys.A
                PictureBox1.Image = Image.FromFile("C:\Users\Neville.Chim\Desktop\Wailmer Left.png")
                PictureBox1.Left = PictureBox1.Left - 5
                PictureBox2.Left = PictureBox2.Left - 5
                PictureBox3.Left = PictureBox3.Left - 5
            Case Keys.D
                PictureBox1.Image = Image.FromFile("C:\Users\Neville.Chim\Desktop\Wailmer Right.png")
                PictureBox1.Left = PictureBox1.Left + 5
                PictureBox2.Left = PictureBox2.Left + 5
                PictureBox3.Left = PictureBox3.Left + 5
        End Select
    End Sub
End Class
And can someone explain how the projectiles going to work, is Looping going the work for the movement?
Reply With Quote
  #2  
Old 03-05-2012, 12:22 PM
passel's Avatar
passelMovement in a Game passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

Why didn't you post this in the Gaming forum? I'll move it.

Since it looks like you're just beginning, then for now I would go with the simple approach of adding a timer control to your project, set the interval to something between 16 and 30 (like 20), which should produce 32 ticks per second (the resolution of the timer control will only tick on a multiple of 64hz on most machines, so your specific interval is limited).
Setting to 15 or below for 64hz, or 16 to 30 for 32hz, are your best two choices, for a dynamic movement game, using the timer.

You would add code to set flags to indicate whether a key is down or not.
The timer tick event should call a routine to process movement, which would look at the state of the keys.
That way you can have more than one direction set at the same time.
Example from some code I have lying around:
Code:
  Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    'code for detecting the key presses of the appropiate directional key
    Select Case e.KeyCode
      Case Keys.Up
        UpKey = True
      Case Keys.Down
        DownKey = True
      Case Keys.Left
        LeftKey = True
      Case Keys.Right
        RightKey = True
      Case Keys.Space
        BrakeKey = True
    End Select
  End Sub

  Private Sub frmMain_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    'code for detecting the key release of the appropiate directional key
    Select Case e.KeyCode
      Case Keys.Up
        UpKey = False
      Case Keys.Down
        DownKey = False
      Case Keys.Left
        LeftKey = False
      Case Keys.Right
        RightKey = False
      Case Keys.Space
        BrakeKey = False
    End Select
  End Sub

  Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
     MoveCar()
  End Sub
I would show the MoveCar routine, but it would likely be overkill, since it includes things like acceleration, deceleration, braking, turn rate based on speed, etc...
Suffice it to say, it is checking the above flags and reacting to them. Some of the code looks like:
Code:
'
    If UpKey = True Then
      If Speed < MAXSPEED Then
        Speed += (MAXAccel * pastTime)
      '....
      End If
    End If

    If DownKey = True Then
      If Speed > -MAXSPEED Then
        Speed -= (MAXAccel * pastTime)
        '.....
      End If
    End If

    If Not (UpKey Or DownKey) Then
      accel = 0
    End If
    If Not (LeftKey Or RightKey) Then
      Turnrate = 0
    Else
      Turnrate += (pastTime * 0.5)
      If Turnrate > 180 Then Turnrate = 180
    End If

'etc.......
In your periodic code, called from the timer, if a projectile is active, then you would move it as well, check for collisions, etc.

I suspect if you search for collision, movement, bullets, ... you may find threads which discuss some of these already.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 03-05-2012 at 12:27 PM.
Reply With Quote
  #3  
Old 03-05-2012, 05:47 PM
eclifox eclifox is offline
Newcomer
 
Join Date: Mar 2012
Location: Australia
Posts: 6
Default

Thanks so much ^_^ It cleared a lot up
Reply With Quote
  #4  
Old 03-07-2012, 06:32 PM
eclifox eclifox is offline
Newcomer
 
Join Date: Mar 2012
Location: Australia
Posts: 6
Default

Thanks so much but when I change/add a direction it has a slight pause can this be fixed?
Reply With Quote
  #5  
Old 03-07-2012, 09:04 PM
passel's Avatar
passelMovement in a Game passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

What does your code look like now?
Is the delay associated with keys that load images from a file?
Perhaps load the images into bitmaps at initialization, so you don't do file reads while running the game.
If you're still using the "repeat" function of the key to move the pictureboxes, then there will be a delay before the key starts repeating.

As a test case, start a new project and paste in the following code.
Start it up, and use either the arrow keys or ASDW keys to move the red picturebox around.
Is there a delay with this example?
Code:
Public Class Form1

  Dim pb As PictureBox
  Dim UpKey, DownKey, LeftKey, RightKey As Boolean

  WithEvents frameTimer As New Timer

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    pb = New PictureBox
    pb.BackColor = Color.Red
    pb.Width = 50
    pb.Height = 50
    pb.Location = New Point(100, 100)
    pb.Visible = True
    Controls.Add(pb)

    Me.KeyPreview = True
    frameTimer.Interval = 15
    frameTimer.Start()
  End Sub

  Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    Select Case e.KeyCode
      Case Keys.Up, Keys.W
        UpKey = False
      Case Keys.Down, Keys.S
        DownKey = False
      Case Keys.Left, Keys.A
        LeftKey = False
      Case Keys.Right, Keys.D
        RightKey = False
    End Select

  End Sub

  Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    Select Case e.KeyCode
      Case Keys.Up, Keys.W
        UpKey = True
      Case Keys.Down, Keys.S
        DownKey = True
      Case Keys.Left, Keys.A
        LeftKey = True
      Case Keys.Right, Keys.D
        RightKey = True
    End Select

  End Sub

  Private Sub frameTimer_Tick() Handles frameTimer.Tick
    If UpKey Then
      If pb.Top > 0 Then
        pb.Top -= 5
      End If
    End If
    If DownKey Then
      If pb.Bottom < Me.ClientSize.Height Then
        pb.Top += 5
      End If
    End If
    If LeftKey Then
      If pb.Left > 0 Then
        pb.Left -= 5
      End If
    End If
    If RightKey Then
      If pb.Right < Me.ClientSize.Width Then
        pb.Left += 5
      End If
    End If

  End Sub
End Class
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #6  
Old 03-11-2012, 05:28 PM
eclifox eclifox is offline
Newcomer
 
Join Date: Mar 2012
Location: Australia
Posts: 6
Default

Sorry about the delay of reply >_< thanks for helping
Heres my code:
Code:
    Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.W
                upkey = True
            Case Keys.S
                downkey = True
            Case Keys.A
                leftkey = True
            Case Keys.D
                rightkey = True
            Case Keys.Space

        End Select

        If upkey = True Then
            PictureBox1.Top = PictureBox1.Top - 5
            PictureBox2.Top = PictureBox2.Top - 5
            PictureBox3.Top = PictureBox3.Top - 5
            PictureBox4.Top = PictureBox4.Top - 5
            PictureBox5.Top = PictureBox5.Top - 5
            direction = "up"
        End If
        If downkey = True Then
            PictureBox1.Top = PictureBox1.Top + 5
            PictureBox2.Top = PictureBox2.Top + 5
            PictureBox3.Top = PictureBox3.Top + 5
            PictureBox4.Top = PictureBox4.Top + 5
            PictureBox5.Top = PictureBox5.Top + 5
            direction = "down"
        End If
        If leftkey = True Then
            PictureBox1.Image = Image.FromFile("C:\Users\Neville.Chim\Desktop\Wailmer Left.png")
            PictureBox1.Left = PictureBox1.Left - 5
            PictureBox2.Left = PictureBox2.Left - 5
            PictureBox3.Left = PictureBox3.Left - 5
            PictureBox4.Left = PictureBox4.Left - 5
            PictureBox5.Left = PictureBox5.Left - 5
            direction = "left"
        End If
        If rightkey = True Then
            PictureBox1.Image = Image.FromFile("C:\Users\Neville.Chim\Desktop\Wailmer Right.png")
            PictureBox1.Left = PictureBox1.Left + 5
            PictureBox2.Left = PictureBox2.Left + 5
            PictureBox3.Left = PictureBox3.Left + 5
            PictureBox4.Left = PictureBox4.Left + 5
            PictureBox5.Left = PictureBox5.Left + 5
            direction = "right"
        End If
    End Sub
    Sub spacekey()
        If direction = "up" & PictureBox4.Visible = True Then
            PictureBox4.Top = PictureBox4.Top - 5
        End If
    End Sub

    Private Sub Form1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Select Case e.KeyCode
            Case Keys.W
                upkey = False
            Case Keys.S
                downkey = False
            Case Keys.A
                leftkey = False
            Case Keys.D
                rightkey = False

        End Select
    End Sub
End Class
Reply With Quote
  #7  
Old 03-11-2012, 05:31 PM
eclifox eclifox is offline
Newcomer
 
Join Date: Mar 2012
Location: Australia
Posts: 6
Default

no there is no delay in your code but why does my code do this?
Code:
Public Class Form1
    Dim direction As String
    Dim upkey As Boolean
    Dim downkey As Boolean
    Dim leftkey As Boolean
    Dim rightkey As Boolean

    Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.W
                upkey = True
            Case Keys.S
                downkey = True
            Case Keys.A
                leftkey = True
            Case Keys.D
                rightkey = True
            Case Keys.Space

        End Select
    End Sub
    Private Sub Form1()

        If upkey = True Then
            PictureBox1.Top = PictureBox1.Top - 5
            PictureBox2.Top = PictureBox2.Top - 5
            PictureBox3.Top = PictureBox3.Top - 5
            PictureBox4.Top = PictureBox4.Top - 5
            PictureBox5.Top = PictureBox5.Top - 5
            direction = "up"
        End If
        If downkey = True Then
            PictureBox1.Top = PictureBox1.Top + 5
            PictureBox2.Top = PictureBox2.Top + 5
            PictureBox3.Top = PictureBox3.Top + 5
            PictureBox4.Top = PictureBox4.Top + 5
            PictureBox5.Top = PictureBox5.Top + 5
            direction = "down"
        End If
        If leftkey = True Then
            PictureBox1.Image = Image.FromFile("C:\Users\Neville.Chim\Desktop\Wailmer Left.png")
            PictureBox1.Left = PictureBox1.Left - 5
            PictureBox2.Left = PictureBox2.Left - 5
            PictureBox3.Left = PictureBox3.Left - 5
            PictureBox4.Left = PictureBox4.Left - 5
            PictureBox5.Left = PictureBox5.Left - 5
            direction = "left"
        End If
        If rightkey = True Then
            PictureBox1.Image = Image.FromFile("C:\Users\Neville.Chim\Desktop\Wailmer Right.png")
            PictureBox1.Left = PictureBox1.Left + 5
            PictureBox2.Left = PictureBox2.Left + 5
            PictureBox3.Left = PictureBox3.Left + 5
            PictureBox4.Left = PictureBox4.Left + 5
            PictureBox5.Left = PictureBox5.Left + 5
            direction = "right"
        End If
    End Sub
    Sub spacekey()
        If direction = "up" & PictureBox4.Visible = True Then
            PictureBox4.Top = PictureBox4.Top - 5
        End If
    End Sub

    Private Sub Form1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Select Case e.KeyCode
            Case Keys.W
                upkey = False
            Case Keys.S
                downkey = False
            Case Keys.A
                leftkey = False
            Case Keys.D
                rightkey = False

        End Select
    End Sub
End Class
Thanks ^_^
PS: sorry about the delay on replying >_<
Reply With Quote
  #8  
Old 03-11-2012, 10:33 PM
passel's Avatar
passelMovement in a Game passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

You haven't fixed the things I mentioned.
You are loading the picture from the disk continually when moving left or right, rather than loading it only when needed.
And I don't see that you are calling the movement code from a timer so that you have periodic movement.

Rather than write a game for a hobby, why not learn to program as a hobby first.
Once you learn what the language can do, and what capabilities are available, then perhaps you can choose how to apply that knowledge toward implementing a game.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Movement in a Game
Movement in a Game
Movement in a Game Movement in a Game
Movement in a Game
Movement in a Game
Movement in a Game Movement in a Game Movement in a Game Movement in a Game Movement in a Game Movement in a Game Movement in a Game
Movement in a Game
Movement in a Game
 
Movement in a Game
Movement in a Game
 
-->