Pac-Man in JavaFX
The O'Reilly InsideRIA site has published an interesting series of articles by Haining Henry Zhang titled "Writing the Pac-Man Game in JavaFX." This is yet another illustration of the power of JavaFX, exemplifying the richness and variety of capabilities the framework provides.
To date, four JavaFX Pac-Man articles have been published. The current version of the application can be run in Part 4 of the series. And the source code is available for download.
In "Writing the Pac-Man Game in JavaFX - Part 1, Haining outlines the logical and data structure behind his implementation of the game.
When designing a data model, I usually consider two aspects: performance and space. Performance means that the data should be accessed via an efficient approach. For example, a hash table is usually faster than a linked list when a keyword-based search is performed. Performance is an important consideration for games that are constantly taking a player's input and updating graphical objects. Games like Pac-Man or Space Invaders fall into this category.
Haining notes that "the ability to leverage Java from within JavaFX is one of the very powerful features of JavaFX," and he puts this to use in the application. The article demonstrates the code for drawing the Pac-Man maze and concludes with a runnable JavaFX demo.
In Part 2, Haining tackles some of the Pac-Man animation requirements, including the chomping Pac-Man himself. This is designed looking ahead to the subsequent need to create the "ghosts" later on. Haining creates a MovingObject class with a moveOneStep() function that is called every 200 milliseconds. The Pac-Man is inserted into the maze, and code for letting the user control the Pac-Man's motion via the keyboard is implemented, followed by code to let the Pac-Man grab the dots it passes through. At this point, the program is an interesting toy, but there isn't much challenge in playing it.
Part 3 of the article series brings in those frightening Pac-Man ghosts. The programming challenge is to create the different state representations of the ghosts (normal, hollow, and flashing), and correctly manifest the transitions between the states. This part of the article ends with the ghosts roaming the maze along with the Pac-Man, but these are nice ghosts that don't yet try to eat the Pac-Man.
Writing the Pac-Man Game in JavaFX - Part 4 provides the interactions between the Pac-Man and the ghosts (if the Pac-Man has recently gobbled a dot, he can eat the ghosts; if not, then they can eat him). First, the distance between the Pac-Man and the ghosts is calcuated. A threshold is applied, defining when the Pac-Man and a ghost are considered to be "touching" (in which case somebody is destroyed). Haining also implements the classical death throes of the Pac-Man, when he is eaten by a ghost (his circle unwraps until he disappears). To do this, he creates a DyingPacMan class. Whenever the Pac-Man is eaten by a ghost, an instance of DyingPacMan is inserted into the maze at the appropriate locations, it does its visual activity, and the game ends.
It looks like there will be a Part 5 article, since near the end of Part 4 Haining says:
As we mentioned in previous article, the ghosts are moving in a random fashion. This makes the game less challenging. In the next article, we will discuss a better algorithm of the ghost's moving behavior.
Haining's "Pac-Man in JavaFX" series is an interesting tutorial on JavaFX, worthy of study if you're looking for a fun way to introduce yourself to JavaFX.
In Java Today, Haining Henry Zhang has written a four-part article series on Writing the Pac-Man Game in JavaFX: "When I was young I was fascinated by arcade games. One of my favorites was the Pac-Man game. Recently, when I was learning the JavaFX language, I decided to write the game in JavaFX. Based on my experience in other programming languages, I assumed there would be some amount of work in building a game such as Pac-Man, giving me a good feel for RIA development in JavaFX..."
John Smart talks about JavaOne - my personal favorite sessions: "As usual, JavaOne was a great networking opportunity, and I caught up with old friends, made new ones, and met up with people I had only ever known virtually. This year I was giving a session myself, so I didn't get to as many sessions as I would have liked to. However, I did attend a few gems. Here are some of them..."
Jonathan Allen reports LINQ to XSD Released on CodePlex: "LINQ to XSD is the long awaited follow-up to LINQ to XML. Its primary purpose is to produce LINQ-compatible object models from XSD files, giving developers some measure of static type checking while accessing XML data..."
In today's Weblogs, Joshua Marinacci writes about Top 5 Most Important Features in JavaFX 1.2: "Technically I'm on vacation this week so don't mention this post to my boss. I simply couldn't wait to blog about cool stuff we put into JavaFX 1.2. Shhhhh! Lots of JavaFX related things were announced at JavaOne, many of..."
Alexey Ushakov writes about JWebPane BOF slides at JavaOne 2009: "Slides from BOF-3992 session presented at Thursday, June 4."
And Arun Gupta writes about AGFA Healthcare - Using GlassFish for Field Force Automation, Time Registration, SAP Integration: "Here are some reasons why AGFA Healthcare choose GlassFish for their Field Force Automation, Time Registration and SAP integration: 100% uptime for past 1.5 years with 8 cluster nodes GlassFish's importance in this economy Simple and elegant administrative console High availability, ease-of-use, and..."
In the Forums, Shai Almog responds Re: Fwd: tabbedpane left/right transition problem : "Hi Anson, the solution you suggest is relevant since you allow cyclic tabs which isn't "officially" supported by LWUIT. About the sequence of sending the selectionChanged events, I'm afraid that might be hard to change since this relates to listeners we bind to the internal List component and I can't think of a way to workaround it properly. You can open an RFE for something like that and maybe someone can come up with some idea..."
Rick Moore has found a v0.5 strange startup with newest trunk build: "This is a it strange, I just received the latest trunk version as of an hour ago. I did 'ant' an 'ant run-server. I used the firefox client. It logged me in then I got a popup message "Unable to launch the application. I'd hit OK and the client exited. The next time I ran it, I just minimized the error message and clicked on the client window where the avatar moved around inside the world like nothing was wrong. But,tool items like clicking on "Window" shows nothing. So, with the error message I clicked on "details" and got this..."
And Shreedhar Ganap continues the discussion Re: In memory session management Problem: "No intention to hide - it took a while to decide on a next release on the v2.x train while most of us were focused on v3 and Sailfin. The build promotion notices have been going to the dev alias. Let me see if those notices could be sent to this list and add it to the DL page. glassfish@javadesktop.org wrote: Why does the the nightly/promoted builds cannot be found on main download page ? Many in this forum have faced replication problems and it took so long to release a fix build. More correctly is created a fixed build and hide it from all of us !... "
The current Spotlight is View the JavaOne 2009 General Sessions: "If you weren't able to attend JavaOne 2009, you can still see all the general sessions online..."
This week's java.net Poll asks What was most significant about JavaOne 2009?. Today (Thursday) is the last full day of voting.
Our Feature Articles include today's new article by Thomas Kunneth, Hacking JavaFX Binding. In this article, Thomas describes how to apply binding within JavaFX in a manner similar to what can be accomplished using Beans Binding (JSR-295). We're also featuring Gary Benson's Zero and Shark: a Zero-Assembly Port of OpenJDK, which tells the interesting story of how the Java group at Red Hat developed a cross-platform OpenJDK port.
The latest Java Mobility Podcast is Java Mobility Podcast 80: Java at FIRST 2010 Competition, in which Eric Areseneau talks about Java now being available for the FIRST 2010 Competition.
-->
Current and upcoming Java Events :
- June 12-14: 2009 Central Ohio Software Symposium
- June 16: Report from JavaOne 2009 for the Dutch Java Community
- June 22-25: Jazoon'09
- June 26-28: 2009 Research Triangle Software Symposium
- June 29 - July 3: Java Training Philippines
- July 10-12: Lone Star Software Symposium: Austin
- July 17-18: Salt Lake Software Symposium
- July 14-26: Desert Southwest Software Symposium
- July 27-31: Java Power Tools - Wellington
- August 3-5: Enterprise Java with Spring Training Philippines
- August 3-7: Java Power Tools - Auckland
- August 6-7: Hibernate Training Philippines
- August 7-9: 2009 Central Iowa Software Symposium
- August 21-23: 2009 Central Florida Software Symposium
- August 23-30: WOWODC East 2009
- September 9-11: Java Power Tools - Canberra
- September 14-16: The Ajax Experience
- October 5-9: Java Power Tools - Brisbane
- October 19-23: Java Power Tools - Sydney
Registered users can submit event listings for the java.net Events Page using our events submission form. All submissions go through an editorial review before being posted to the site.
Archives and Subscriptions: This blog is delivered weekdays as the Java Today RSS feed. Also, once this page is no longer featured as the front page of java.net it will be archived along with other past issues in the java.net Archive.
The O'Reilly InsideRIA site has published an interesting series of articles by Haining Henry Zhang titled "Writing the Pac-Man Game in JavaFX"...
- Login or register to post comments
- Printer-friendly version
- editor's blog
- 1448 reads





