Writing the Pac-Man Game Part 4

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

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

No Comments

No comments yet.

RSS feed for comments on this post.