The full source code to create game buttons and the title page with JavaFx

As my game progress I will publish a new game source code everyday until I have completed the whole game. I am simply too lazy (maybe too tired because I am developing both JavaFx game as well as android game at the same time) to explain to you what I am actually doing here therefore the title of this post should not be called the javafx gaming tutorial but instead the full javafx gaming source code instead. The game code should be easy to understand if you are already familiar with the Java program.

In this episode my mission is simple, which is to create the buttons on top of the game title page which I have created earlier.

Take note that I have changed the previous Group object to StackPane object which is more suitable for this game.

Also make sure your project contains the below folder tree.

follow these structure!
follow these structure!

Basically under the asset folder there are three files 1) 550 x 550 pixels title screen 2) 142 x 69 pixels play button 3) 142 x 69 pixels score button. More buttons will be added in the future…

Here is the source code

package mightyrock;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class MightyRock extends Application {

	@Override
	public void start(Stage theStage) throws Exception {
		
		final double CANVAS_WIDTH = 550;
		final double CANVAS_HEIGHT = 550;
		
		final Image titleScreen = new Image( "asset/title.png" ); //title screen image
		final Image playButton = new Image("asset/play.png"); //the play button image
		final Image scoreButton = new Image("asset/score.png"); //the score button image

		final ImageView flashScreen_node = new ImageView();
		flashScreen_node.setImage(titleScreen); //set the image of the title screen
	
		final Button play_button  = new Button();
		final ImageView play_button_node = new ImageView(); 
		
		final Button score_button = new Button();
		final ImageView score_button_node = new ImageView(); 
		
		play_button_node.setImage(playButton); //set the image of the play button
		score_button_node.setImage(scoreButton); //set the image of the score button
		
		play_button.setGraphic(play_button_node);
		play_button.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY))); //this is to make the button background transparent
		
		score_button.setGraphic(score_button_node);
		score_button.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY)));
		
		/*
		 * create the container of those buttons
		 */
		final VBox buttonContainer = new VBox(1);
		buttonContainer.setAlignment(Pos.TOP_CENTER);
		Insets buttonContainerPadding = new Insets(220, 1, 1, 1);
		buttonContainer.setPadding(buttonContainerPadding);
		buttonContainer.getChildren().addAll(play_button,score_button);
		
		theStage.setTitle( "The Mighty Rock!" );
		theStage.getIcons().add(titleScreen); //stage icon

		StackPane root = new StackPane();
		
		root.getChildren().addAll(flashScreen_node, buttonContainer); //add the title screen and button container to the stackpane
		Scene theScene = new Scene( root, CANVAS_WIDTH, CANVAS_HEIGHT, Color.CHOCOLATE );
		theStage.setScene( theScene );
		theStage.show(); 

	} 

	public static void main(String[] args) {
		launch(args);
	}

}

By some unknown reason the color which is the last parameter inside the Scene constructure does not work after the Button object has been added.

OK if you run the above program here is what you get…

This is my new game
Two buttons on the title page

Hey dude if you feel high about the above program then don’t forget to share it with your friend. This gaming series will continue until I have finished the entire game!

Create a game start screen in JavaFx

In this tutorial I will show you how to create a game title screen in JavaFx, every game will need a start screen, either an introduction screen or just a simple menu screen and in this game I will use the 550 x 550 pixels title screen.

I assume you have already get familiar with eclipse and also know how to create a java project with eclipse so I will go straight into business. After you have created your project and a new java class file as well as uploaded a 550px x 550px png image into the project asset folder of the game project this is what your project folder looks like.

The tree structure of project folder
The tree structure of project folder

I call my new game project MightyRock! and therefore the class file also get the same name. Here is the entire code of the MightyRock.java file.

package mightyrock;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class MightyRock extends Application {

	@Override
	public void start(Stage theStage) throws Exception {
		
		final Image titleScreen = new Image( "asset/title.png" ); //title screen image

		final ImageView flashScreen_node = new ImageView();
		flashScreen_node.setImage(titleScreen); //set the image of the title screen
		
		theStage.setTitle( "The Mighty Rock!" );
		theStage.getIcons().add(titleScreen); //stage icon

		final double CANVAS_WIDTH = 550;
		final double CANVAS_HEIGHT = 550;

		Group root = new Group();
		root.getChildren().addAll(flashScreen_node); //add the title screen to the root
		
		Scene theScene = new Scene( root, CANVAS_WIDTH, CANVAS_HEIGHT, Color.BLACK );
		theStage.setScene( theScene );
		theStage.show(); 

	} 

	public static void main(String[] args) {
		launch(args);
	}

}

Run the above program will create the below scene.

MightyRock!
MightyRock!

Hope you like this tutorial which is part of the JavaFx game project tutorial which I will present to you on this blog.

A fun chrome browser game for you and me!

Welcome to the homepage of Monster Academy. The Game which I have developed a few years ago and put online for people to enjoy. If you are the old user of this game then you can just skip the following introduction and go directly to the what is new part.

How to play the Game 

You will use the left right up and down arrow key to move the player around the board and collect the bouncing rocks, once you have collected all the rocks on the board you will proceed to the next level. There are 11 levels in total. Actually you do not need to collect all the rocks by your own because you have a helper, duck robot is willing to help you out by collecting those rocks but be careful because once the rock deference’s between you and the robot duck has exceeded 2 then it will be game over. Make sure you don’t get hit by the monster or else it will be game over as well.

What is new

Nothing is new in this version but I have fixed the bug which I found in the game, this time I have rewritten some old code and made the game user interface more compact than the previous one. Hope you like it!

Create animated images with JavaFx

I have just started to learn JavaFx so I think it will be nice if I can share what I have learned on my blog with you all. Below is a simple example on how to create an animation with JavaFx. What the program below does is to animate a sprite sheet which consists of various images from one end to another and then back again. I am just using the images from the first row of the sprite sheet to do this simple JavaFx demo, in my next post I will show you how to make the game character moving around on the JavaFx scene. Enjoy!

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
 
public class SkyRocket extends Application 
{
 
    public void start(Stage theStage) 
    {
    	theStage.setTitle( "JavaFx Example!" );
    	 
        Group root = new Group();
        Scene theScene = new Scene( root );
        theStage.setScene( theScene );
        
        final double WIDTH = 64; // image width
        final double HEIGHT = 64; // image height
        
        final double CANVAS_WIDTH = 300;
        final double CANVAS_HEIGHT = 300;
     
        Canvas canvas = new Canvas( CANVAS_WIDTH, CANVAS_HEIGHT ); // Create new canvas
        root.getChildren().add( canvas );
     
        final GraphicsContext gc = canvas.getGraphicsContext2D();
     
        final Image monster = new Image( "asset/monster_main_1.png" );
     
        new AnimationTimer() //use AnimationTimer to continue the game loop
        {
        	double frame_number = 0;
        	double x = 0;
        	double y = 0;
        	boolean reverse = false;
        	
            public void handle(long currentNanoTime)
            {
            	
            	if(frame_number > 24) 
            	{
            		frame_number -= 2;
            		reverse = true;
            	} 
            	else if(frame_number < 0) 
            	{
            		frame_number += 2;
            		reverse = false;
            	}
            	
                x = frame_number * WIDTH;
               
                if(reverse == true)
                	frame_number--;
                else
                	frame_number++;
                
                // Clear the canvas
                gc.setFill( Color.BLACK );
                gc.fillRect(0,0, CANVAS_WIDTH, CANVAS_HEIGHT);
                
                // Draw next image
                gc.drawImage(monster, x, y, WIDTH, HEIGHT, CANVAS_WIDTH/2 - WIDTH/2, CANVAS_HEIGHT/2 - HEIGHT/2, WIDTH, HEIGHT);
                
            }
        }.start();
     
        theStage.show();
    }
    
    public static void main(String[] args) 
    {
        launch(args);
    }
}

I am using eclipse to write the entire program and here is the file structure…

image asset and file
image asset and file

Here is the sprite sheet and I am only using the first row…

monster_main_1
monster_main_1

If you run the above program you will get this outcome…

Javafx
Javafx

Hope you like it!

Insert keyframe for the light in Cycles Render

I have just discovered this very nice method to insert keyframe to a light when we are in the Cycles Render mode so that the lamp will change color from one frame to the next. Here is how to insert a keyframe to the light.

i) In cycles render select the light by left clicking on it.

ii) Before you do anything make sure you are at frame 1 by looking at the timeline editor.

current frame
current time frame

iii) Select the object data tab of the lamp.

object data
object data

 

 

 

Under Nodes click on the Use Nodes button then change the color of the lamp to any color, next press ‘i’ when the cursor is on the color box to insert a keyframe. After you have inserted a keyframe to the lamp the color box will get a border which indicates that you have successfully inserted a keyframe.

iv) You can now move to frame number two under the timeline editor and insert the next keyframe using the same method.

v) It is simple to change the color of a mesh by just changing the color of the lamp as mentioned above!

Now switch to the Rendered viewport and press Alt-A to render the animation and see how the lamp changes color through time.

 

 

 

 

Alarm Clock for Web Browser

Welcome to the homepage of Browser Clock, I had created this clock a few years ago and had added in new features every once and a while. I do hope you enjoy using this clock and if you have any suggestion or comment then please leave it at the end of this post.

Click on the small green arrow icon on the browser bar to open up the clock.

click on this icon
click on this icon to open the clock!

 

browser clock
browser clock pop up

Next click on the downward green arrow on top of that clock to switch to different part of the clock. Basically the clock allows you to…

1) You can switch to digital clock if you like.
2) You can select different theme.
3) You can set Alarm!

Let us do the alarm setting,  watch below video to set the alarm.

After you have set the alarm then you will see the pop up notification on the windows taskbar based on the time you have set! It doesn’t matter whether you have closed the browser or not, once you have set a time then the alarm will pop up even if you have closed the web browser!

There are many other features that you can use besides just clock, for example if you highlighted a text on any page you will see the tweet and the voice features on the context menu, go ahead and explore all those cool features by left clicking on the browser to bring up the context menu then hover the Browser Clock tab to locate those features!

Hope you do enjoy using the clock and do tell your friend about this addon.

Cut the mesh in Blender

If you want to produce a cut on the mesh in Blender you can press the blender’s hot key “K”. Once you have pressed the ‘k’ key on the keyboard you will see a green square and a knife, if you left click on the mesh and move the knife you will see a line which will help you to better position the cutting edge, left click again to confirm the final position and then just press the enter key to finish the cutting process.

knife tool
knife tool

The Sniper Elite 4 Review

The Sniper Elite 4 is a perfect example of one of those games that capture both the mind as well as the soul of the player! In this virtual world, you are under no limitation as to how much damage you can do to your enemy. In fact, the presence of the X-ray in the game makes the damage thrilling, more detailed and exciting. This game is solely based on the skills and how well you are able to eliminate your enemy. Any move you make, such as a 200m shot away from the target while dangling on a ledge and so forth, is brought out in a dramatical point of view.

The main characters of this game are you, the soldier and the enemy. The game is set in Italy, where you’ll be sneaking cautiously through vineyards, towns in ruins and other well-detailed sites. These sites, for the most part, are surprisingly breath-taking. The campaign lasts approximately ten hours, so you’ll be doing this for a while. The maps, however, are rather vast and complex when it comes to determining your current location. A game like this is obviously supposed to have big and wide battlegrounds. The Sniper Elite 4 delivers when it comes to this. There is always an option e.g a mountain, window, or rooftop to take the best shot.

The game allows you to cover your position with trip mines as you move to different positions between shots. There’s an array of sounds you could use to mask the sound of shots you make to ensure your position is not at all compromised. Examples of these sounds include; the sound of an overhead plane, a tampered generator’s bang or a gun firing at distant allies. There’s also a quiet way to take out your enemies – the supersonic ammo. You’ll always end up going back to normal sniping, despite the extra tools in your possession.

The difficulty setting allows you to adjust your gravity shots, by estimating your target range. There is also a provision of an aiming assist by simply tapping the shoulder button that empties your lung, thus giving you a hint on the landing position of the shot. This gives an indication to shoot when it turns red.

The well put and intricate maps can feel unfortunately useless especially when there are no enemy to snipe. With the help of objective markers, you basically get rid of the enemy equipment, get hold of their documents and assassinate specific figures. The objective markers are identified by a huge question mark. It is, therefore, your obligation to roam the area in pursuit of the different locations of the markers. The maps, as mentioned earlier, do very little to make your way around the area. You find yourself going the wrong way multiple times hence the irritating need to go back. Ensure the path you’re treading on isn’t swarming with enemy before engaging in the search for the markers.

The game awards you with a weapon upgrade upon completion of certain milestones including the damage caused or increased zoom. Unfortunately, this gives you more reason to stick with the initial rifle; besides, other rifles are no different from this one.

If you’re up for the thrill of up-close or exciting assassination, this game is definitely for you.