In that tutorial, you are going to learn how to create a Reflex Game for Android. Our Reflex Game will be a casual game in which the user must touch the screen as fast as you can.
Rules are simple : When the game will start, users are going to test their reflexes by touching the screen as fast as they can when it becomes red. The red screen will appear 5 times. If users touch the screen before it becomes red, they lose.
Note that you can also watch this tutorial in video on YouTube :
Adding Butter Knife as dependency
In our project, we will use the Butter Knife Library for binding views easily. So, we add the following dependencies in the build.gradle file :
Creating the User Interface
Our User Interface will be quite simple with just 3 views :
- A LinearLayout to display the touch area. We will create a dedicated dotted background for this LinearLayout later.
- An ImageView to display a target drawable at the center of the screen.
- A TextView to display some messages to the user like the start message or the time made by the users.
First step is to create a dedicated dotted background for our layout in a XML file placed in /res/drawable folder :
For more details on the creation of this dotted background, you can also watch the following tutorial on YouTube :
The User Interface of our Reflex Game will have the following form :
Writing the Java Code of the Reflex Game
The last step is to write the Java Code of the Reflex Game in the MainActivity. We start by defining a some constants for our game :
- MIN_DELAY to define the minimum delay before the touching area be displayed on the screen
- MAX_DELAY to define the maximum delay before the touching area be displayed on the screen
- NB_TRIES to define the number of tries for our Reflex Game
We will use some properties to store the best time and the average time for the tries of the Reflex Game.
At the start of the game, we display a message to the user explaning him the rules of the game. Then, after the first touch on the screen, the game starts. We post a delayed task, via a Handler instance, in a random delay between MIN_DELAY and MAX_DELAY.
If the user touches the screen before the touching area is displayed, he loses. We display a message to the screen. If the user touches the screen when the touching area is displayed, we take the delay between the moment when the touching area has been displayed and the moment when the user has touched the area. It is the time for the current try.
After that, we increment the current try and then, we plan a new random delay for the next displaying of the touching area. When the user has reached the number of tries of the Reflex Game, we display the best time and average time to the screen.
It gives us the following code for the MainActivity :
Our Reflex Game in Action
Now, it’s time to play to our Reflex Game. Note that you can try it directly by getting it on the Google Play Store :
Reflex Game - Apps on Google Play
Reflex Game is a casual game in which you must touch the screen as fast as you can. When the game starts, you are going…
When the Reflex Game is launched, the rules are displayed to the screen :
Then, the user touches the screen and the touching area is displayed in a delay between MIN_DELAY and MAX_DELAY :
When the Reflex Game is ended, we display the best time and the average time of the user :
To go further
That’s all for that tutorial. To go further, you can imagine the implementation of an online ranking for users. The Reflex Game published on the Google Play Store offers this ranking. Thus, the best time is 29 milliseconds and the best average time is 210 milliseconds.
If you are interested by a tutorial learning you how to implement this online ranking with a PHP backend, feel free to tell me in comments.