PacMan in vb2008
PacMan in vb2008
PacMan in vb2008
PacMan in vb2008
PacMan in vb2008
PacMan in vb2008 PacMan in vb2008 PacMan in vb2008 PacMan in vb2008 PacMan in vb2008 PacMan in vb2008 PacMan in vb2008 PacMan in vb2008
PacMan in vb2008 PacMan in vb2008
PacMan in vb2008
Go Back  Xtreme Visual Basic Talk > > > PacMan in vb2008


Reply
 
Thread Tools Display Modes
  #1  
Old 03-22-2011, 08:56 AM
Collingwood2011 Collingwood2011 is offline
Newcomer
 
Join Date: Mar 2011
Posts: 4
Default PacMan in vb2008


I am having some trouble working out how to make the ghosts chase PacMan.
I have made all the code and stuff work with needing a grid maker thingy.
I have do in all by placing the walls (pb's) myself and also with the pelets (food)
Though I have a problem with getting the ghosts doing their job, they only just change image with a timer tick.

Can someone please help me with a simple way of making the ghosts chase you?
Reply With Quote
  #2  
Old 03-22-2011, 09:51 AM
Qua's Avatar
QuaPacMan in vb2008 Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

The simplest approach is always moving the ghost towards your position. This require nothing but collision detection against walls to be available:
Code:
if (ghost.IsAbovePacman) Then
  ghost.SetDirection(Down)
else if (ghost.IsLeftofPacman)
  ghost.SetDirection(Right)
else if (ghost.IsRightofPacman)
  ghost.SetDirection(Left)
else if (ghoist.IsBelowPacman)
  ghost.SetDirection(Up)
Else
  ' ghost is right on top of pacman
It's hard to give you more concise help without you revealing a bit more about your code.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #3  
Old 03-22-2011, 10:52 AM
AtmaWeapon's Avatar
AtmaWeaponPacMan in vb2008 AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

You generally get better results if you display some effort of your own. Note that there are many ways to implement Pac-Man, and accurate answers depend wholly on your implementation.

In this case, the best opening post would involve explaining how you think the ghosts should work and why you find this difficult to implement. That's often my first step when approaching a difficult new problem. It also helps to start by relaxing the rules of the game to make the AI simple, then introduce rules one at a time to see how they affect the design.

For example, consider the rules of how a ghost can move; these affect how a ghost thinks:
  • Ghosts can move in four directions we'll call up, down, left, and right (UDLR).
  • Ghosts may not travel through a wall.
If we imagine a playfield with no walls, an algorithm to make a ghost seek Pac-Man (PM) would be like so:
Code:
* If PM is to the right:
    * Head right
* Else If PM is to the left:
    * Head left
* Else If PM is up:
    * Head up
* Else
    * Head down
If PM sits still, the ghost will eventually catch him. This is really simple. However, since the game's going to have walls, you have to modify it. Let's add more rules:
  • When a ghost encounters an intersection, it *must* decide on a new direction.
  • The ghost may continue to keep moving in the same direction or choose a new direction, but is not allowed to reverse its course. (There are situations in the real Pac-Man where this rule is broken but I'm omitting them for simplicity.)
This leads to a more complicated movement algorithm. Let's ignore PM and make a ghost that moves randomly:
Code:
* Move some distance in the current direction.
* If you are at an intersection:
    * Build a list of valid directions.
    * Select a random valid direction.
    * Make the current direction that valid direction.

Build a list of valid directions:
* Let valid directions be U, D, L, and R.
* Remove the current direction from the set.
* Check all valid directions for walls; if a direction is blocked remove it.
* Select a direction from the remaining directions.
This is easy to walk through. Let's say our ghost gets to an intersection like this:
Code:
 _______
*__   __
   | |
First, the ghost moves into the intersection; let's say its current direction is R. Next, we need to choose a new direction. We start with valid directions U, D, L, and R. Since R is the current direction, L is dropped. There is a wall U from the ghost, so it's dropped. So the valid directions are R and D. The ghost will pick one of these two at random.

A randomly moving ghost can be challenging to avoid, but in the real game each ghost had specific behaviors that served two purposes. First, the behaviors made it more likely the ghost could trap Pac-Man in certain situations. Second, if the player learned enough about the behaviors this knowledge could be exploited to keep Pac-Man safe. For example, Blinky always heads directly to the tile that Pac-Man occupies, but Clyde will either run to a specific corner if Pac-Man is close or charge directly at Pac-Man if distant. (The other two ghosts have interesting behaviors as well.) Blinky is easy to manage, but Clyde's dual behaviors requires more finesse (in general, you want to avoid his corner if he's close to it since his AI makes him stick to that area.)

Anyway, I got sidetracked. Let's try to write Blinky's behavior. It's similar to the last AI, but rather than picking random directions it chooses the direction that will get him closest to PM.
Code:
* Move a space in the current direction.
* If Blinky is at an intersection:
    * Build a list of valid directions.
    * For each valid direction, calculate PM's distance in that direction.
    * Select the direction with the shortest distance to PM.
This is tough to illustrate. In the following diagram, @ is PM and G is Blinky.
Code:
--------| |---------|
         *          |
--------| |-------  |
        | |      |  |
        |  -------  |
        |       @   |
        -------------
Let's say PM is down 3 squares and left 2 squares from Blinky, regardless of the diagram's scale.

If Blinky is moving R when he hits the intersection, his valid directions will be U, D, and R. PM is below Blinky, so the distance for U is infinity. PM's other distances are D3 and R2. Since PM is closer in the R direction, Blinky will move R through the intersection. You could change it so the higher distance is favored; in this case Blinky would go D. Obviously the "better" direction might depend on which way PM is headed; you can add this to the algorithm. Note that if Blinky was moving L in this case, valid directions would be L, D, and U and he could never choose to move R; he'd always go D in this case. See how knowing the ghost's AI intimately can save Pac-Man's hide?

I can't really show you code because the way it's written changes based on how you implemented the rest of the game. You can write your game in a way that makes AI difficult; if you've done this you might want to rewrite parts of the game. In particular, your life will be miserable if a ghost doesn't have easy access to the following facts:
  • Where is Pac-Man?
  • Is there a wall in a certain direction?
  • Which direction am I currently traveling?
  • Which direction is Pac-Man facing?
Access to these four facts allow you to write AI that approximates the behavior of the four ghosts. Those are outlined in excruciating detail at The Pac-Man Dossier.

If you are still confused, try asking more specific questions or posting what you have tried and why you think it doesn't work.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 03-23-2011, 04:19 AM
Collingwood2011 Collingwood2011 is offline
Newcomer
 
Join Date: Mar 2011
Posts: 4
Default

I'll try that, Thanks
Then I'll return with a copy of the code if it doesn't work.
Reply With Quote
  #5  
Old 03-23-2011, 05:21 AM
Collingwood2011 Collingwood2011 is offline
Newcomer
 
Join Date: Mar 2011
Posts: 4
Default

See below for edit
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
PacMan in vb2008
PacMan in vb2008
PacMan in vb2008 PacMan in vb2008
PacMan in vb2008
PacMan in vb2008
PacMan in vb2008 PacMan in vb2008 PacMan in vb2008 PacMan in vb2008 PacMan in vb2008 PacMan in vb2008 PacMan in vb2008
PacMan in vb2008
PacMan in vb2008
 
PacMan in vb2008
PacMan in vb2008
 
-->