1

I am currently working on a maze game in Visual Basic 2010 Express. I am currently working on the keyboard controls for the character within the maze. I have succeeded...somewhat. I can get the Picturebox object to move using the following code:

Private Sub Lvl1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select e.KeyCode
            Case Keys.W
                Player.Location = New Point(Player.Location.X, Player.Location.Y - 2)
            Case Keys.S
                Player.Location = New Point(Player.Location.X, Player.Location.Y + 2)
            Case Keys.D
                Player.Location = New Point(Player.Location.X + 2, Player.Location.Y)
            Case Keys.A
                Player.Location = New Point(Player.Location.X - 2, Player.Location.Y)
        End Select
    End Sub

With that being said, I am not very satisfied with the results. When I run the application, the Picturebox moves very slowly and verrrryyyy jerky. On top of that, it can't move in diagonal directions. I think this makes for a very unpractical game, since the user will probably become extremely bored with the slug-like pace of movement. Is there any way to simply program a Picturebox to move quickly by 2-pixel increments and smoothly (i.e. no delay before changing directions)? Thanks!

3programmer5u
  • 13
  • 1
  • 3

3 Answers3

2

Yes, that doesn't work well. You need to decouple the keyboard events from the control movement so they can occur at different rates. Declare two variables that keep track of the last user command:

Dim xdir, ydir As Integer

Easy to set them in the KeyDown event handler:

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    Select Case e.KeyCode
        Case Keys.W : ydir = -1
        Case Keys.A : xdir = -1
        Case Keys.S : ydir = 1
        Case Keys.D : xdir = 1
    End Select
End Sub

A wee bit trickier in the KeyUp event handler, the user doesn't necessarily release them in a predictable order:

Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
    Select Case e.KeyCode
        Case Keys.W : If ydir = -1 Then ydir = 0
        Case Keys.A : If xdir = -1 Then xdir = 0
        Case Keys.S : If ydir = 1 Then ydir = 0
        Case Keys.D : If xdir = 1 Then xdir = 0
    End Select
End Sub

Now you need a "game loop" that uses these commands to implement the movement of the control. That can be as simple as a Timer, set the Interval to 15 or 31 milliseconds to get a predictable update rate.

Dim velocity As Integer = 4

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim newx = PictureBox1.Location.X + velocity * xdir
    newx = Math.Min(Math.Max(0, newx), Me.ClientSize.Width - PictureBox1.Width)
    Dim newy = PictureBox1.Location.Y + velocity * ydir
    newy = Math.Min(Math.Max(0, newy), Me.ClientSize.Height - PictureBox1.Height)
    PictureBox1.Location = New Point(newx, newy)
End Sub
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Actually, I tried to use this method, but the compiler gave me an error, saying "Handles clause requires a WithEvents variable defined in the containing type or one of its base types" and I'm not quite sure what this means... – 3programmer5u Nov 21 '14 at 22:56
  • I could use some help with this^ – 3programmer5u Dec 03 '14 at 22:56
0

Try using the KeyPress method instead of the KeyDown,

if that doesn't work, you will have to add delegates to your code.

Check MSDN for refences http://msdn.microsoft.com/en-us/library/ms172879.aspx

0

In response to your question about how to move a picturebox quickly by 2-pixel increments, I would suggest that you would follow one of the following alternative strategies to a picturebox, as I know no way to animate something in an optimized fashion with a picturebox.

  1. override the onpaint or paint function of a single picturebox, and draw the entire scene, layering sprites imported from image files. If you did this the rectangle object would come in handy for collision, especially with its Rectangle.Intersect() function.
  2. Use XNA. XNA is compatible with Visual Basic, and I managed to find a relevant video tutorial on Youtube, which shows how to create a tile-based game in XNA with VB. It shows you how to do movement in video 34, here. The playlist for the tutorial series is here.
  3. Use XNA, but inside a WinForms app. How to do so is answered here, although for C#. The code is quite simple, it shouldn't be hard to convert to VB.
Community
  • 1
  • 1
user2469456
  • 281
  • 1
  • 3
  • 8
  • 1
    Thanks for taking the time to help me! However, this method is a bit over my head, as I wasn't really looking for something of this caliber. Nonetheless, it was still helpful, and I might use it in the future! – 3programmer5u Nov 20 '14 at 23:27