📜  Android中的击球游戏

📅  最后修改于: 2022-05-13 01:55:42.154000             🧑  作者: Mango

Android中的击球游戏

Android 是一个开源操作系统,基于 Linux 内核,用于智能手机、平板电脑等移动设备。此外,它是为智能手表和 Android TV 开发的。他们每个人都有一个专门的接口。 Android 一直是最畅销的智能手机操作系统之一。 Android OS 由 Google 于 2005 年收购的 Android Inc. 开发。

我们将在本文中构建什么?

在这个游戏中,将运行一个 10 秒的计时器,如果我们不能点击它,我们必须点击球图像来增加我们的分数,那么我们的分数保持为零。这是该游戏的示例视频。请注意,我们将使用Java语言构建此应用程序。

分步实施

步骤 1. 创建一个新项目

  • 打开一个新项目。
  • 我们将使用Java语言开发 Empty Activity。保持所有其他选项不变。
  • 将应用程序命名为 user_application。
  • 将有两个名为 activity_main.xml 和 MainActivity 的默认文件。Java

如果您不知道如何在 Android Studio 中创建新项目,可以参考如何在 Android Studio 中创建/启动新项目?

步骤 2. 添加所需的依赖项

导航到 Gradle scripts > build.gradle(module) 并在其中使用以下依赖项-

implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.gridlayout:gridlayout:1.0.0'

步骤 3. 处理 activity_main.xml 文件-

导航到app > res > layout > activity_main.xml并将以下代码添加到该文件。下面是activity_main.xml文件的代码。

XML



  
    
    
  
        
        
        
        
        
  
        
        
        
        
        
        
  
    
  
    
    
  


Java
package com.example.coronavirusgame;
  
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
  
import android.content.DialogInterface;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
  
import java.util.Random;
  
public class MainActivity extends AppCompatActivity {
  
    // initialize variables
    ImageView[] imageList;
    Handler handler;
    TextView scoring,killno;
    int score;
    ImageView imageView,imageView2,imageView3,imageView4,imageView5,imageView6,imageView7,imageView8,imageView9;
    Runnable runnable;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  
        // hiding Action bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
  
        // assigning variables
        scoring=findViewById(R.id.score);
        killno=findViewById(R.id.time);
        imageView=findViewById(R.id.image_view1);
        imageView2=findViewById(R.id.image_view2);
        imageView3=findViewById(R.id.image_view3);
        imageView4=findViewById(R.id.image_view4);
        imageView5=findViewById(R.id.image_view5);
        imageView6=findViewById(R.id.image_view6);
        imageView7=findViewById(R.id.image_view7);
        imageView8=findViewById(R.id.image_view8);
        imageView9=findViewById(R.id.image_view9);
  
        imageList=new ImageView[]{imageView,imageView2,imageView3,imageView4,imageView5,imageView6,imageView7,imageView8,imageView9};
        makeitgone();
  
        // setting timer to play game
        new CountDownTimer(10000,1000)
        {
  
            // increasing time
            @Override
            public void onTick(long l) {
                killno.setText("Time : "+l/1000);
            }
  
            // When time is finished
            @Override
            public void onFinish() {
                killno.setText("Time Over");
                handler.removeCallbacks(runnable);
                  
                // using for loop
                for (ImageView image:imageList)
                {
                    image.setVisibility(View.INVISIBLE);
                }
  
                // dialog box to ask user's input
                AlertDialog.Builder alert=new AlertDialog.Builder(MainActivity.this);
                alert.setTitle("Try Again!");
                alert.setMessage("Do you want to restart?");
                  
                // if user want to restart game
                alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent=getIntent();
                        finish();
                        startActivity(intent);
                    }
                });
                
                // When user not want to play again
                alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "Game Over!!!", Toast.LENGTH_SHORT).show();
                    }
                });
                alert.show();
            }
        }.start();
    }
  
    private void makeitgone() {
        handler=new Handler();
        runnable=new Runnable() {
            @Override
            public void run() {
                for(ImageView image:imageList)
                {
                    image.setImageResource(R.drawable.ball);
                    final Handler handler=new Handler(Looper.getMainLooper());
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            image.setImageResource(R.drawable.ball);
                        }
                    },900);
                    image.setVisibility(View.INVISIBLE);
                }
  
                // making image visible at random positions
                Random random=new Random();
                int i=random.nextInt(9);
                imageList[i].setVisibility(View.VISIBLE);
                handler.postDelayed(this,600);
            }
        };
        handler.post(runnable);
    }
  
    // increasing score
    public void increaseScore(View view) {
        score=score+1;
        scoring.setText("Score : "+score);
    }
}


执行上述代码后,activity_main.xml 文件的 UI 将如下所示——

第 4 步。在 Mainactivity 上工作。Java

转到MainActivity。 Java文件并参考以下代码。下面是MainActivity 的代码。 Java文件。代码中添加了注释以更详细地理解代码。

Java

package com.example.coronavirusgame;
  
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
  
import android.content.DialogInterface;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
  
import java.util.Random;
  
public class MainActivity extends AppCompatActivity {
  
    // initialize variables
    ImageView[] imageList;
    Handler handler;
    TextView scoring,killno;
    int score;
    ImageView imageView,imageView2,imageView3,imageView4,imageView5,imageView6,imageView7,imageView8,imageView9;
    Runnable runnable;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  
        // hiding Action bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
  
        // assigning variables
        scoring=findViewById(R.id.score);
        killno=findViewById(R.id.time);
        imageView=findViewById(R.id.image_view1);
        imageView2=findViewById(R.id.image_view2);
        imageView3=findViewById(R.id.image_view3);
        imageView4=findViewById(R.id.image_view4);
        imageView5=findViewById(R.id.image_view5);
        imageView6=findViewById(R.id.image_view6);
        imageView7=findViewById(R.id.image_view7);
        imageView8=findViewById(R.id.image_view8);
        imageView9=findViewById(R.id.image_view9);
  
        imageList=new ImageView[]{imageView,imageView2,imageView3,imageView4,imageView5,imageView6,imageView7,imageView8,imageView9};
        makeitgone();
  
        // setting timer to play game
        new CountDownTimer(10000,1000)
        {
  
            // increasing time
            @Override
            public void onTick(long l) {
                killno.setText("Time : "+l/1000);
            }
  
            // When time is finished
            @Override
            public void onFinish() {
                killno.setText("Time Over");
                handler.removeCallbacks(runnable);
                  
                // using for loop
                for (ImageView image:imageList)
                {
                    image.setVisibility(View.INVISIBLE);
                }
  
                // dialog box to ask user's input
                AlertDialog.Builder alert=new AlertDialog.Builder(MainActivity.this);
                alert.setTitle("Try Again!");
                alert.setMessage("Do you want to restart?");
                  
                // if user want to restart game
                alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent=getIntent();
                        finish();
                        startActivity(intent);
                    }
                });
                
                // When user not want to play again
                alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "Game Over!!!", Toast.LENGTH_SHORT).show();
                    }
                });
                alert.show();
            }
        }.start();
    }
  
    private void makeitgone() {
        handler=new Handler();
        runnable=new Runnable() {
            @Override
            public void run() {
                for(ImageView image:imageList)
                {
                    image.setImageResource(R.drawable.ball);
                    final Handler handler=new Handler(Looper.getMainLooper());
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            image.setImageResource(R.drawable.ball);
                        }
                    },900);
                    image.setVisibility(View.INVISIBLE);
                }
  
                // making image visible at random positions
                Random random=new Random();
                int i=random.nextInt(9);
                imageList[i].setVisibility(View.VISIBLE);
                handler.postDelayed(this,600);
            }
        };
        handler.post(runnable);
    }
  
    // increasing score
    public void increaseScore(View view) {
        score=score+1;
        scoring.setText("Score : "+score);
    }
}

我们的游戏已经准备就绪,这是我们应用程序的最终输出。

输出: