📜  如何在Android中构建简单的Reflex游戏?

📅  最后修改于: 2021-05-08 20:52:39             🧑  作者: Mango

反射游戏是一种简单有趣的游戏,可以衡量您的响应速度。制作和理解非常简单。我们将设计一款反身游戏,该游戏将计算您的响应速度。规则很简单,只要看到背景颜色的变化,只需按一下停止按钮,您花费的时间就是您的速度。较快的回应会显示出更好的赞美语录。

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:使用activity_main.xml文件

XML代码用于构建活动的结构及其样式部分。它包含一个TextView 在活动中心以显示游戏说明。然后它包含两个Button ,分别是start和stop。以下是activity_main.xml文件的代码。

XML


  
    
    
  
    
  
        
        


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);
            }
        });
    }
}


步骤3:使用MainActivity。 Java文件

当播放器单击“开始”按钮时,将调用该按钮的onClickListener()函数。在此函数内部,我们将生成一个介于1到10之间的随机整数。该数字将是秒数,在此秒数之后,显示屏的背景将发生变化。为了生成数字,我们将使用Random函数。得到整数后,我们将创建一个Handler类,并在发布延迟后调用Runnable函数。处理程序的主要函数是调度消息并使其可运行。在可运行界面内,我们将使用setBackgroundResource()函数设置屏幕背景。我们将使用System.currentTimeMillis()函数来获取当前时间(以毫秒为单位)。首先,当屏幕背景改变时,我们将获得系统时间,然后,当单击停止按钮时,将获得系统时间。这些时间之间的差异将赋予玩家反应时间。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

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);
            }
        });
    }
}

输出:

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!