How to Build a Simple Reflex Game in Android?
A reflex game is a simple fun game that measures your responding speed. It is quite simple to make and understand. We will be designing a reflex game that will calculate your responding speed. The rules are simple just press the stop button when you see the change in background color, and the time you took in doing so will be your speed. A faster response will show a better quote of praising than a slower one.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
The XML codes are used to build the structure of the activity as well as its styling part. It contains a TextView in the center of the activity to show the game instructions. Then it contains two Buttons, start and stop. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:id = "@+id/rlVar1" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > <!--TextView to display game instruction--> < TextView android:id = "@+id/tvVar1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:padding = "20dp" android:text="Click on Start first, and wait until the background color changes. As soon as it changes hit Stop" android:textSize = "25dp" /> < LinearLayout android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_below = "@id/tvVar1" android:layout_centerHorizontal = "true" android:orientation = "horizontal" android:padding = "20dp" > <!--start button--> < Button android:id = "@+id/btVar1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_margin = "10dp" android:text = "Start" /> <!--stop button--> < Button android:id = "@+id/btVar2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_margin = "10dp" android:text = "Stop" /> </ LinearLayout > </ RelativeLayout > |
Step 3: Working with the MainActivity.java file
When a player clicks the Start button the onClickListener() function for that button is called. Inside this function, we will generate a random integer number from 1-10. This number will be the seconds after which the background of the display will change. To generate the number we will be using the Random function. After getting the integer number we will create a Handler class and call a Runnable function after a post delay. The main function of the handler is to schedule messages and runnable. Inside the runnable interface, we will set the background of the screen using the setBackgroundResource() function. We will use System.currentTimeMillis() function to get the current time in milliseconds. First, we will get the time of the system when the background of the screen changes, and then we will get the time of the system when the stop button is clicked. The difference between these times will give the reaction time of the player. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.Random; public class MainActivity extends AppCompatActivity { public Button button1, button2; public RelativeLayout relativeLayout; // runnable function Runnable runnable = new Runnable() { @Override public void run() { // set the background on the screen relativeLayout.setBackgroundResource(R.color.green); // get the system time in milli second // when the screen background is set final long time = System.currentTimeMillis(); // function when stop button is clicked button2.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { // get the system time in milli second // when the stop button is clicked long time1 = System.currentTimeMillis(); // display reflex time in toast message Toast.makeText(getApplicationContext(), "Your reflexes takes " + (time1 - time) + " time to work" , Toast.LENGTH_LONG).show(); // remove the background again relativeLayout.setBackgroundResource( 0 ); } }); } }; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); relativeLayout = findViewById(R.id.rlVar1); button1 = findViewById(R.id.btVar1); button2 = findViewById(R.id.btVar2); // function when the start button is clicked button1.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { // generate a random number from 1-10 Random random = new Random(); int num = random.nextInt( 10 ); // call the runnable function after // a post delay of num seconds Handler handler = new Handler(); handler.postDelayed(runnable, num * 1000 ); } }); } } |