Create Your Own Game Using JavaFX 2
Join the DZone community and get the full member experience.Join For Free
i believe most software developers at one point in their lives as a youngster (young person) may become compelled to create games to help them learn a programming languages (i know i did). back in the day my first computer was actually a franklin ace 1000 and later an apple ][ . while developing games on those systems it was pretty challenging. for starters you had to learn assembly language (6502) and there were virtually little to no tools to create sprites (graphics assets). one of my favorite games that i believe was probably the first real time strategy (rts) game was rescue raiders (1984).
let's fast forward to 2012 where computers, graphics tool kits, libraries, and game engines have come a long way since then. many apis will provide much of the plumbing that will shield the user of the api so that they may focus on making their games fun and exciting. speaking of apis javafx 2.x is not only a great ui toolkit to create nice looking applications, but you can make fun games. with javafx 2.x you will be able to create games that can kill time with hours of fun!
growing up i was always fascinated with science fiction movies such as star wars and star trek . i've always wanted to create a simple top view display game (2d) where i could control my spaceship similar to the classic game asteroids. however as time went by a friend shared with me the game star craft 1 and brood wars i was just astonished. i really like the game play still to this very day, so i wanted to adopt some of the elements of the game such as navigating units and troops using the mouse pointer and buttons (ie: the terran battle cruiser).
in this blog entry (part 1) i will be briefly explain the game play or navigation of a simple spaceship using simple shapes. there isn't code to show in part 1 (this blog entry), but a simple application to demonstrate how the ship will behave in the final game. as we progress through the series you will notice incremental changes such as cool sprites, sounds, etc. remember the final game will just be a spaceship avoiding enemy ships and firing back with sound effects. the ship will appear like the one depicted at the beginning of this blog entry (figure 1).
i would like to create a series of blog entries ( six parts ) detailing tutorials to create a javafx 2.x game. below is a brief summary of the series:
part 1 -
click here to run demo
part 2 - game loop
part 3 - input / (mouse, keyboard)
part 4 - sprites / collision
part 5 - sound
part 6 - concluding thoughts
requirements & design
- create a prototype of a spaceship using basic shapes.
- rotate the spaceship clockwise or counter clockwise depending on the screen location of a right mouse click.
- fire a projectile when primary button is pressed.
- display mouse press (x, y) screen coordinates
- display angles to rotate the ships nose (front of the ship)
- display the direction (clockwise or counter clockwise) of the rotation of the spaceship
shown in figure 2 is a simple prototype using simple shapes to help us focus on the math. a good principle is to create a functional prototype before investing a lot of time in drawing your graphics assets.
- (mx, my) – mouse press (x, y) coordinate space on the javafx scene.
- (vx, vy) – end angle or mouse press(x, y) coordinate converted to cartesian coordinates relative to the center of the ship.
- (ux, uy) – start angle or previous mouse press(x, y) coordinate converted to cartesian coordinates relative to the center of the ship.
- u’s angle: the angle of the start of the ships nose rotation. in a cartesian coordinate system (1,0) the nose is pointing west or zero degrees. as the ship rotates counter clockwise the angle increases. when moving in a clockwise direction the rotation angle will be negative numbers.
- v’s angle: the angle of the ships nose rotation where it should stop. in a cartesian coordinate system (1,0) the nose is pointing east or zero degrees. as the ship rotates counter clockwise the angle increases. when moving in a clockwise direction the rotation angle will be negative numbers.
- direction: the rotation of the ship nose to turn the ship clockwise or counter clockwise. when clicking the mouse to turn the ship when less than 180 degrees the ship will turn towards the mouse click instead of turning the other way which is greater than 180 degrees (the long way).
- java 7 or later
- javafx 2.0 or later
- windows xp or later (should be available soon for linux/macos)
a simple prototype of the navigation and weapon systems for the spaceship.
- right click (on windows) mouse to fly ship.
- primary (left click on windows mouse) to fire weapon.
franklin ace – vintage computer : http://www.vintage-computer.com/franklin.shtml
apple ][ – vintage computer: http://en.wikipedia.org/wiki/apple_ii
rescue raiders - wikipedia: http://en.wikipedia.org/wiki/rescue_raiders
star wars – movie database: http://www.imdb.com/title/tt0076759/
star trek – movie database: http://www.imdb.com/title/tt0796366/
star craft – wikipedia: http://en.wikipedia.org/wiki/star_craft
star craft brood wars – wikipedia: http://en.wikipedia.org/wiki/starcraft:_brood_war
Published at DZone with permission of Carl Dea, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.