Creating a Reflex Game for Android

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.

Image for post
Image for post

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 :

implementation ‘com.jakewharton:butterknife:8.8.1’
annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’

Creating the User Interface

Our User Interface will be quite simple with just 3 views :

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 :

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 :

When the Reflex Game is launched, the rules are displayed to the screen :

Image for post
Image for post
Reflex Game launched

Then, the user touches the screen and the touching area is displayed in a delay between MIN_DELAY and MAX_DELAY :

Image for post
Image for post
Touching Area displayed

When the Reflex Game is ended, we display the best time and the average time of the user :

Image for post
Image for post
Best Time and Average Time displayed

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.

Written by

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust: https://www.inbitcoinwetrust.net

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store