Create a Roll Dice Game on Android with Shake Effect

A Roll Dice Game is a simple game in which you have to roll some dice and trying to make the highest score possible by adding the values of the two dice. In this tutorial, you are going to learn how to create a Roll Dice Game on Android. It’s a good way to discover how to manipulate simple view and how to get resources dynamically.

Besides, we are going to add a shake effect on the dice when the user will roll them to add a more realistic effect.

Note that you can enjoy this tutorial in video on YouTube via the SSaurel’s Channel :

 

Get Dice Images

Obviously, we need dice images for our Roll Dice Game. We are going to use these images with values from one to six :

Create the Shake Effect

To make our Roll Dice game more realistic, we are going to create a Shake Effect which will be applied on the dice when the user will roll them. For that, we create a shake.xml file under an anim folder on the resources directory of our Android project.

The Shake Animation is a set with a rotate animation and a translate animation for which we add a linear interpolator to make the effect smoother. It gives us the following XML code :

 

Create the User Interface

The User Interface of our Roll Dice Game will be pretty simple with two image views components to display the dice and a button to let the users to start rolling the dice. It looks like this :

 

Write the Java Code

The last part of our tutorial consists to write the Java Code of our Main Activity. We start by getting the references of both Image Views and of the Button.

Then, we set an OnClickListener on the Button to write the code letting the dice to be rolled. First, we load the Shake Animation thanks to the static method loadAnimation of the AnimationUtils class from the standard SDK.

After that, we create an AnimationListener implementation. It will let us to change the value of the dice just after the end of the Shake Effect. So, we write our Java code in the onAnimationEnd method of the AnimationListener implementation.

In this method, we generate a new random value for the dice. It will be a value between 1 and 6. Then, we get the corresponding dice image dynamically by using the getIdentifier method of the Resources object got via the getResources method call. Last step is to set the image loaded on the correct imageView instance according the value of the animation just ended.

In the onClick method, we continue by setting this AnimationListener implementation on the both animatons loaded previously. Finally, we start the animation for each dice image by calling the startAnimation method with the correct animation in parameter.

This gives us the following code for our MainActivity :

 

Play to Roll Dice Game

Finally, we can play to our Roll Dice Game and enjoy a double six. What a luck !

To discover more tutorials on Android Development, don’t hesitate to visit the SSaurel’s Channel : https://www.youtube.com/user/sylsau

You can also visit the SSaurel’s Blog : https://www.ssaurel.com/blog

Leave a Reply