Results 1 to 6 of 6

Thread: Continuous movement in Pac-man in VB 2010

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2014
    Posts
    7

    Question Continuous movement in Pac-man in VB 2010

    Hi, So I'm attempting to make a working version of Pac-man using visual basic express 2010 and I'm having an issue with movement of pac-man where I can get him to move left right or up and down perfectly but when I try and make him move continuously with one keypress he seems to go on forever or disappears (I assume he goes off in the direction the key was pressed in i.e UP)
    Here is my code:
    vb.net Code:
    1. Select Case e.KeyCode
    2.  
    3.             Case Keys.Up
    4.                 'Do Until e.KeyCode = Keys.Right Or e.KeyCode = Keys.Left Or e.KeyCode = Keys.Down ----- My attempt at continous movement----'
    5.                 If Not picturebox.Location.Y - 15 < 0 Then
    6.  
    7.  
    8.                     Loc = New Point(picturebox.Location.X, picturebox.Location.Y - 15)
    9.                     picturebox.Location = Loc
    10.  
    11.                     'Resets location if collision detected'
    12.                     If picturebox.Bounds.IntersectsWith(picturebox1.Bounds) Then
    13.                         Loc = New Point(picturebox.Location.X, picturebox.Location.Y + 15)
    14.                         picturebox.Location = Loc
    15.                     End If
    16.  
    17.                 End If
    18.                 picturebox.Image = My.Resources.pac_up
    19.                 'Loop'
    20.  
    21.             Case Keys.Down
    22.  
    23.                 If Not picturebox.Location.Y + 15 < 0 Then
    24.  
    25.                     Loc = New Point(picturebox.Location.X, picturebox.Location.Y + 15)
    26.                     picturebox.Location = Loc
    27.                     'Resets location if collision detected'
    28.                     If picturebox.Bounds.IntersectsWith(picturebox1.Bounds) Or picturebox.Bounds.IntersectsWith(PictureBox3.Bounds) Then
    29.                         Loc = New Point(picturebox.Location.X, picturebox.Location.Y - 15)
    30.                         picturebox.Location = Loc
    31.                     End If
    32.                     picturebox.Image = My.Resources.pac_down
    33.                 End If
    34.  
    35.             Case Keys.Left
    36.  
    37.                 If Not picturebox.Location.X - 15 < 0 Then
    38.                     Loc = New Point(picturebox.Location.X - 15, picturebox.Location.Y)
    39.                     picturebox.Location = Loc
    40.                     ' Resets location if collision detected'
    41.                     If picturebox.Bounds.IntersectsWith(picturebox1.Bounds) Or picturebox.Bounds.IntersectsWith(PictureBox6.Bounds) Or picturebox.Bounds.IntersectsWith(PictureBox7.Bounds) Then
    42.                         Loc = New Point(picturebox.Location.X + 15, picturebox.Location.Y)
    43.                         picturebox.Location = Loc
    44.                     End If
    45.                     picturebox.Image = My.Resources.pac_left
    46.                 End If
    47.             Case Keys.Right
    48.  
    49.                 If Not picturebox.Location.X + 15 < 0 Then
    50.  
    51.  
    52.  
    53.                     Loc = New Point(picturebox.Location.X + 15, picturebox.Location.Y)
    54.  
    55.                     picturebox.Location = Loc
    56.  
    57.                     ' Resets location if collision detected'
    58.                     If picturebox.Bounds.IntersectsWith(picturebox1.Bounds) Or picturebox.Bounds.IntersectsWith(PictureBox4.Bounds) Or picturebox.Bounds.IntersectsWith(PictureBox5.Bounds) Then
    59.                         Loc = New Point(picturebox.Location.X - 15, picturebox.Location.Y)
    60.                         picturebox.Location = Loc
    61.                     End If
    62.                 End If
    63.  
    64.                 picturebox.Image = My.Resources.pacman_completed_gif
    65.  
    66.  
    67.  
    68.  
    69.         End Select
    Last edited by dday9; Sep 29th, 2014 at 11:24 AM. Reason: changed [php] to [highlight]

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Continuous movement in Pac-man in VB 2010

    If you're using the Do loop to try and move pac-man continuously then that is the reason why it will go on forever or just disappear. What you need to do is use a game loop to update the location of pac-man. Here is my example while making it work like a timer. Place your code in the GameLoop's tick event and set the RefreshRate to whatever you desire(typically 60 FPS is the desired target).
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Continuous movement in Pac-man in VB 2010

    Wouldn't Pac-man move if you have your finger on the button/key, why would you need a loop?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Continuous movement in Pac-man in VB 2010

    Quote Originally Posted by Nightwalker83 View Post
    Wouldn't Pac-man move if you have your finger on the button/key, why would you need a loop?
    The game mechanics of pacman are that he is always moving, all you do is hit the direction you want him to go.

  5. #5
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Continuous movement in Pac-man in VB 2010

    Correct. Pac-Man can't stop dead in the middle of a tunnel. He will go in the last direction he was told to go unless told to turn around, hits a wall, or told to go in another direction.

    Thus, if you got a tunnel shaped like an "L", and Pac-Man starts at the top, all you need to do is tap "down" once and he'll hit the bottom of the "L". He'll sit there because he hit a wall. If you hold "right" even though he's moving "down" the tunnel, he won't suddenly stop and face the right wall, he'll KEEP moving down and when he hits that wall, immediately turn right and go down the bend.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Continuous movement in Pac-man in VB 2010

    If you want to do Pac-Man "right", I believe the PAC-MAN Dossier site is the most complete information about the classic Pac-Man game, with Ghost behavior, etc. It is an interesting read if you're into that sort of thing.

Posting Permissions

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



Click Here to Expand Forum to Full Width