Thursday, December 13th, 2007

LightsOut: JavaFX Script Game

Category: Java

Joshua Marinacci, of Sun, has been playing with JavaFX Script. As a way to learn the new language he developed:

Writing this game really taught me the Zen of JavaFX Script (hmm… sounds like a good book title). I often have to fight my procedural Java instincts and instead use binding and triggers wherever possible. It’s really a different way of thinking, closer to Lisp or Prolog (and even a bit of SQL), but quite powerful. I’m sure I didn’t get it perfect and I bet I could rewrite it in a few more months using a better style, but this is a good start.

So I’d like to share with you my first real JavaFX Script application. It’s a simple puzzle game where you click grid cells to turn off the light. As you click each cell the adjacent cells flip as well. You win the game by turning off all lights (hence the name :).

You can download the code and take a peak at the new language:

  1. class LightsOutCanvas extends Group {
  2.     operation init();
  3.     attribute model: LightsOutModel;
  4. }
  5.  
  6. //set up the main screen
  7. operation LightsOutCanvas.init() {
  8.     var cc = ColorConstants;
  9.     model = LightsOutModel;
  10.     model.init();
  11.     model.randomize();
  12.  
  13.     //background and grid
  14.     insert Rect {width:400,height:300,fill:cc.slate } into content;
  15.     insert model into content;
  16.  
  17.     //reset button
  18.     insert BlueButton {
  19.         text : "Reset"
  20.         font: new Font("Arial","BOLD",20)
  21.         transform: translate(305,15)
  22.         width: 80, height: 30
  23.         onMousePressed : operation(e) { model.randomize(); }
  24.     } into content;
  25.  
  26.     //score
  27.     insert Text {content:"Moves", x:310, y:90, fill:white, font: new Font("Arial","BOLD",20) } into content;
  28.     insert Text {content: bind model.moveCount.intValue().toString(),
  29.         x: 343, y: 120, fill:white, font: new Font("Arial","BOLD",60),  halign: CENTER  } into content;

Posted by Dion Almaer at 6:06 am
4 Comments

++---
2.9 rating from 25 votes

4 Comments »

Comments feed TrackBack URI

JavaFX looks interesting, but reports about it being really slow keeps me from looking more into it: http://metalinkltd.com/?p=139

Also, getting 2-3 scary dialogs to run a JavaFX application makes it unsuitable for the web.

Comment by Jeria — December 13, 2007

The iphone version of this looks *slightly* better.
http://www.deliciousmonster.org/

Comment by puRe — December 13, 2007

#1. old link!
http://blogs.sun.com/chrisoliver/entry/compiled_javafx_script for more accurate performance

Comment by lamboap — December 13, 2007

Another thing, its a 2.5 MB download…

Comment by Jeria — December 14, 2007

Leave a comment

You must be logged in to post a comment.