Tuesday, December 3, 2019

Some fun with JavaFX Canvas - SpaceFX

Aloha,

Last weekend I decided to write a little game because I never did that before. Saturday and Sunday morning I've spend some time to create a little space game where you have to dodge asteroids or shoot them. In addition there might be enemy ships that will fire back if you are in line of sight.
To realize that game I made use of the JavaFX Canvas node which seem to be a perfect fit for games like stuff.
In the Canvas node the redraw is in your hands (more or less) but at least you can decide when to redraw and how often. So what I did was to create an AnimationTimer and simply call a draw() method as often as possible (which usually should be around 60 times per second).
In this draw method I set the coordinates of all my objects and draw them, do the collision detection and play the sounds.
For the sprites I use simple images which I've found on the internet.
Long story short, here is a little video of the game...



If you hit an asteroid or an enemy ship you will be destroyed. Also when an enemy torpedo hits you you will be destroyed. The score will always be reset to 0 when you have been destroyed and the game will start over and over again...like I said only for the fun of it.
You can use the cursor keys to maneuver the ship and the space key to fire torpedos and that's it...as you can see...really simple :)
If you are interested in the game you can find the source code over at github.

For those of you that are also interested in jpro (running JavaFX apps in the browser), the latest version of jpro makes it possible to also run JavaFX apps that use the Canvas node!!! Oh yeah!!!

UPDATE:
Well after the last weekend the game now looks a bit more advanced and therefor I've created a new little video that shows the current version...here you go...



Well that's it for today...so keep coding...

1 comment:

  1. Gerrit, you did it again! Vielen lieben herzlichen Dank!! This is a wonderful example project,on the side you are presenting us recent deploy methods already configured (JPro, Exe-builder, etc.).
    Under Linux Debian Buster I have under JDK+JavaFX 13 MediaPlayer issues, the sound was annoying the colleagues anyway :), so commented the MediaPlayer stuff out. With hybrid graphics on my laptop it is running on an old NV GTX 960M GPU like a breeze, also without AnimationTimer fps counter (the Intel HD GPU needs the counter though). Martin Stein

    ReplyDelete