TGB - Building a Pacman clone - Phase 1, 2, 3 and 4
by Gavin · 07/27/2010 (2:46 pm) · 4 comments
Phase I
I think that most who purchased a new copy of TGB would very much want to familiarise themselves with what they have bought. Well, there are of course the requisite official tutorials as well as a handful more available on TDN. The resourceful ones would take a look at some of the free source code released in the forum.
But frankly once those have been looked through, you may ask what's next?
For me, the next logical step was to write a clone of an existing retro game. And in this case, I took up the challenge to implement a Pacman clone. I decided to christen the name of my second project "Boxy". Why Boxy? Well, the Pacman I drew using TileStudio purposely employed the Round Rectangle tool - making Pacman really look boxy. The development of Boxy took about 1 day of coding (300+ lines of torquescript to perfect, optimize and to drop dead code), plus another 1 - 2 hours dabbling with Tilestudio, Fraps, and VirtualDub to create a movie for all to see.
You can see Boxy in action below. Boxy is able to traverse down curving passageways without me requiring to press any keys. The only time Boxy stops his movement is when he reaches a T-junction. A problem with Boxy is that he practically "vacuums" all the dots in his path before they enter his mouth - that's a result of implementing a collision on a square block.
All in, I'd say the result I achieved is truly quite hilarious!
Some lessons I like to share with you:
1. Expand on what you learnt (from the tutorials/TDN/etc.) by applying it to your game.
2. Place comments aplenty and try to adopt a coding standard (refer to TDN for suggestions on Best Practices).
3. Search the forums for prior help (surely someone out there had thought about writing a pacman clone).
4. Really ask for help after exhausting all avenues (see #3 above) OR read the TGB engine source code to see what is happening under the hood - the documentation is not always that clear.
5. Tile-laying is very useful for TGB games (I hope this is improved upon for the next generation of T2D, eg. no world coordinates for a tile - except through a forum article). And, I didn't use the .moveto method as suggested in the forums, hence you may notice some "clamping" of Boxy occuring at junctions.
6. Research about the subject matter... in this case, there is a ton of information and inspiration out there on pacman and its variations.
7. And if you write a blog, try to add a screenshot or movie... it really helps!
As you can see, Boxy is quite lonely in the game for now. The next step of course is to add in Ghost clones (I mean none other than the infamous quartet - Inky, Blinky, Pinky, and Clyde) and some background music and sound effects.
Phase II
Hi again, I just spent about an hour looking around and drawing Ghosts! And, I also drew out a pair of eyes for them. Incidentally I have nicknamed my Ghost clones Binky, Plinky, Winky, and Slyde. As you can see in the second video, the Ghosts aren't moving yet, but I think Boxy feels like somebody's watching him... "and there ain't no privacy". Look at the eyes carefully and see how they follow the movement of Boxy around the maze. This is achieved by a simple call to the .setlinkpoint method to offset the ghosts eyes. The eyes are mounted on the Ghosts.My wife did ask me, "Would I ever release Boxy?"
Well with a great big laugh, definitely not commercially... it's TRULY just a learning exercise.
Phase III
I just spent about 4 hours trying to figure out how to get the ghosts to use the "mysterious" AStar code. Why is it mysterious? Well, there's not much in terms of documentation for it (both in TDN and the tutorials - other than the obvious AStarDemo).After much tinkering, I got all the ghosts to chase Boxy around the maze. Take a look at the video below.
Getting to use the AStar pathfinding routines was not too difficult. I made some minor tweaks to give Binky, Plinky, Winky, and Slyde some character. For example, Binky will move around the maze the fastest and decide to switchback more often based on where Boxy is, while Slyde would once in a while follow where Binky is heading, etc.
Incidentally, if you were wondering, well your eyes weren't fooling you - Boxy was not killed when he collided with the ghosts - that was intentional. I will implement the "death" of Boxy next time.
Phase IV
The final part is to implement the superdot (or also known as "power pellets" in the original Pacman game). When a superdot is eaten, the ghosts hightail it out of the vicinity of Boxy. Implementing this was a simple exercise after I had finished implementing the hardest part above (in Phase III).I have also released a tutorial for Boxy over at TDN. And you will find a link to the source files and artwork over there. Have fun and share any feedback or queries in this blog.
About the author
Skilled Game Programmer/Tester/Gamer and Budding Game Designer at large <big grin>... and an ecstatic owner of a shiny license plate which says - I am a TGB Pro + T3D Pro + Torsion owner!
#2
How did you handle the physics settings for collisions with the dots?
I started a pac man clone and couldn't figure out how to have the collisions with the dots send a callback but have no physics, but have collisions with the walls have physics.
The pacman could navigate the maze (with the mouth animation in the correct direction) I had the tunnel part working ok, but it ended up looking sort of goofy when my pacman collided with a dot as it would bring his velocity to a stop and then go back to normal.
07/27/2010 (10:52 pm)
That looks great. How did you handle the physics settings for collisions with the dots?
I started a pac man clone and couldn't figure out how to have the collisions with the dots send a callback but have no physics, but have collisions with the walls have physics.
The pacman could navigate the maze (with the mouth animation in the correct direction) I had the tunnel part working ok, but it ended up looking sort of goofy when my pacman collided with a dot as it would bring his velocity to a stop and then go back to normal.
#3
new t2dAnimatedSprite(pac) {
animationName = "sBoxyPacAnimation";
canSaveDynamicFields = "1";
class = "pacclass";
Position = "2.500 12.500";
size = "4.900 4.900";
CollisionActiveSend = "1";
CollisionGroups = "1";
CollisionCallback = "1";
CollisionDetectionMode = "FULL";
mountID = "5";
};
new t2dTileLayer(Layer) {
LayerFile = "~/data/tilemaps/level1.lyr";
canSaveDynamicFields = "1";
Position = "-2.500 -7.500";
size = "125.000 115.000";
Layer = "1";
CollisionActiveReceive = "1";
CollisionPhysicsSend = "0";
CollisionPhysicsReceive = "0";
CollisionGroups = "1";
CollisionMaxIterations = "1";
CollisionCircleSuperscribed = "0";
mountID = "2";
};
As you can see, Boxy is a four niner by four niner and fits inside a five by five tile. The walls and dots are all on the tilelayer.
I had that spate of goofy movement too, but its settled by callbacks. Take a look at the platformer tutorial and implement a callback that immediately will make a call to setlinearvelocity in the x and y directions. You also need a priority level when dealing with collisions. I deal with collision with dots first before taking care of collision with walls.
Hope that gives an idea.
07/28/2010 (12:02 am)
Here are the details of Boxy and the tilelayer straight from the level:new t2dAnimatedSprite(pac) {
animationName = "sBoxyPacAnimation";
canSaveDynamicFields = "1";
class = "pacclass";
Position = "2.500 12.500";
size = "4.900 4.900";
CollisionActiveSend = "1";
CollisionGroups = "1";
CollisionCallback = "1";
CollisionDetectionMode = "FULL";
mountID = "5";
};
new t2dTileLayer(Layer) {
LayerFile = "~/data/tilemaps/level1.lyr";
canSaveDynamicFields = "1";
Position = "-2.500 -7.500";
size = "125.000 115.000";
Layer = "1";
CollisionActiveReceive = "1";
CollisionPhysicsSend = "0";
CollisionPhysicsReceive = "0";
CollisionGroups = "1";
CollisionMaxIterations = "1";
CollisionCircleSuperscribed = "0";
mountID = "2";
};
As you can see, Boxy is a four niner by four niner and fits inside a five by five tile. The walls and dots are all on the tilelayer.
I had that spate of goofy movement too, but its settled by callbacks. Take a look at the platformer tutorial and implement a callback that immediately will make a call to setlinearvelocity in the x and y directions. You also need a priority level when dealing with collisions. I deal with collision with dots first before taking care of collision with walls.
Hope that gives an idea.
#4
Now I'll have to go back and finish up the rest of the game.
07/29/2010 (12:14 am)
Thanks! Setting the linear velocity back to where it should be as you enter the collision callback works great. No more bouncing off the dots as PacMan eats them.Now I'll have to go back and finish up the rest of the game.

Torque Owner Luis Rodrigues
WinterLeaf Entertainment
nice going - great result
one tip i would like to give
reduce the colison box on boxy to be smaller at center of character and the dots will start going into the mouth
you'll just have to be carefull not to make it too small or will look weird whe you have the ghosts and they are touching boxy and don't het him.