Skip to main content

Have fun learning JavaFX!

Posted by spericas on February 27, 2009 at 2:00 PM PST

As many of you out there, I wanted to explore the world of JavaFX. After looking at a few demos here and there, I decided it was time to write some code; there is simply no better way to learn a new technology like this than writing something. A quick glance over the language primitives and programming model made my decision very easy: a simple video game was best choice to learn JavaFX!

I am not a game programmer, but just like many of you, I wrote a couple of simple games in the past. So it took some GIFs from an old game (which are share by most Space Invader-like games out there!) and put together a very simple game in 400+ likes of JavaFX code. I was amazed by the result. JavaFX is a incredible powerful platform, so I decided to share with you the source code to hopefully encourage you to get started as well. Here's what the game looks like:


It should be pretty easy to port this game to JavaFX Mobile, but I haven't had the time myself (so if you do it, please send it to me!). The controls are straightforward: UP ARROW, DOWN ARROW, LEFT ARROW, RIGHT ARROW and SPACE (fire).

I believe the source code is pretty easy to read, so I hope a quick glance at it we'll get you intrigued and help you get started. Have fun!



, thanks for that, I eventually figured it by exploring your source, sorry for the late comes out that do not inform you by email when a comment is posted, Thanks again.

Glad you found the example useful. The purpose of the KeyboardManager class is to record keys (one or more) that have been pressed but not released yet. This information is used in the game's main loop to determine how to move the ship. Because multiple keys are recorded, the ship can move diagonally in one iteration (e.g. if UP and LEFT are being pressed). Hope this helps.

Thank you very much for this nice example, I am a beginner in game development and it is through your entry I came to know about Keyboard events and custom keyboard manager class, I may never figure it out by myself, and really I am still not fully understanding why does it work

The game looks great. Would you consider joining and posting it there? Thanks, Josh