Jun 06

Writing the Pac-Man Game Part 4

Posted: under JavaFX, JavaFX Coding, Javafx Games.
Tags: , , , June 6th, 2009

My latest article of a series, “Writing the Pac-Man Game in JavaFX Part 4“, was out on June 4. As we continue on bulding the game, the articles are getting more and more interesting now.

In this fourth article, the interaction between Pac-Man character and the four ghosts was implemented. The article showed how to determine whether the Pac-man character and a ghost touched each other. A simplified equation was applied to achieve better performance. When Pac-man touches a ghost, he can eat it if the ghost is hollow. The ghost then is thrown back to the cage again. Otherwise, the ghost eats the Pac-man, at this moment, an animation of showing a dying Pac-Man appears. This is in fact a shrinking circle which disappears at the end of the animation. The animation is accomplished by the DyingPacMan class.

The below figure depicts the animation process of the dying Pac-man character.

shriking pac-man

The code of DyingPacMan.fx is listed below:

public class DyingPacMan extends Arc {

  var timeline = Timeline {
     repeatCount: 1
     keyFrames: [
        KeyFrame {
           time: 600ms
           action: function() {
           // hide the pacMan character & ghosts before the animation
              maze.pacMan.visible = false;

              for ( g in maze.ghosts ) {
                 g.hide();
              }
              visible = true;
            }
           values: [ startAngle => 90, length=>360 ];
         },
        KeyFrame {
           time: 1800ms
           action: function() {
              visible = false;
            }
           values: [ startAngle => 270 tween Interpolator.LINEAR,
                     length => 0 tween Interpolator.LINEAR ]
         },
      ]
    }

 ... code omitted ...
}

As you can see it in the code, there are two keyframes of the animation. Interpolations of two instance variables, startAngle and length, are involved during the animation. To better illustrate this process, the below figure shows the change of the shape against a timeline. The animation started with a pause of 600ms and then the first key frame appears, which is a full circle. After that, the full circle will gradually turns into nothing(empty circle). This effect is done by the interpolation provided by JavaFX API.

timeline pac-man

Hope you enjoy reading the articles. You can use arrow keys to play the current version of the game. The ghosts are moving randomly which makes the game less challenging. In the next article, I will introduced a better algorithm. Try it by clicking the below screenshot:


click to run
click to run

Related Articles:

More articles on JavaFX Games: Developing Games in JavaFX
Source of the Pac-Man Game
JavaFX Demo Game: MineSweeper
JavaFX Demo Game: LinkUP
Game Widgets for WidgetFX: Pac-Man
My JavaFX Demo Game: Pac-Man

Comments (0)

May 16

Articles on Writing the JavaFX Pac-Man Game

Posted: under JavaFX, JavaFX Coding, Javafx Games.
Tags: , , , May 16th, 2009

After the release of JavaFX 1.0, I wrote a Pac-Man game using the JavaFX API. Many people were quite interested in the game. They either enjoyed playing it or asked me for the details fo the JavaFX code. JavaFX expert Jim Weaver invited me to write some articles about the process of building the game. After a few weeks’ hard work, with Jim’s constructive ideas and great help in proofreading, I finished the articles. Now they are published on insideRIA.com, an O’Reilly’s web site, as featured articles. There will be 5 articles in total and they will run through the coming 5 weeks.

In each article, there are a few web start links that you can click on and start a JavaFX program to see how it works. If you follow the 5 articles, you will find out how the game is built bit by bit. I hope the articles can help people who want to learn JavaFX or want to develop games in JavaFX. Thanks Jim for making these articles possible. My thanks also goes to Rich, the editor of insideRIA.com.

Here is the links for the articles:

May 14, 2009:
Writing the Pac-Man Game in JavaFX - Part 1
May 21, 2009:
Writing the Pac-Man Game in JavaFX - Part 2
May 28, 2009:
Writing the Pac-Man Game in JavaFX - Part 3
June 4, 2009:
Writing the Pac-Man Game in JavaFX - Part 4
June 11, 2009:
Writing the Pac-Man Game in JavaFX - Part 5

Related articles:
Answer: Blinky, Pinky, Inky and Clyde from Jim Weaver’s Blog

My JavaFX Demo Game: PACMAN

JavaFX Demo Game: PAC-MAN

Comments (0)