安卓实现简单砸地鼠游戏

2024-02-17 23:44

本文主要是介绍安卓实现简单砸地鼠游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

效果

布局 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid:id="@+id/scoreTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="得分:0"android:textSize="18sp" /><GridLayoutandroid:id="@+id/gridLayout"android:layout_width="match_parent"android:layout_height="match_parent"android:columnCount="3"android:rowCount="3"><ImageViewandroid:id="@+id/imageView1"android:layout_width="0dp"android:layout_height="0dp"android:layout_columnWeight="1"android:layout_rowWeight="1"android:background="@mipmap/laohu" /><ImageViewandroid:id="@+id/imageView2"android:layout_width="0dp"android:layout_height="0dp"android:layout_columnWeight="1"android:layout_rowWeight="1"android:background="@mipmap/laohu" /><ImageViewandroid:id="@+id/imageView3"android:layout_width="0dp"android:layout_height="0dp"android:layout_columnWeight="1"android:layout_rowWeight="1"android:background="@mipmap/laohu" /><ImageViewandroid:id="@+id/imageView4"android:layout_width="0dp"android:layout_height="0dp"android:layout_columnWeight="1"android:layout_rowWeight="1"android:background="@mipmap/laohu" /><ImageViewandroid:id="@+id/imageView5"android:layout_width="0dp"android:layout_height="0dp"android:layout_columnWeight="1"android:layout_rowWeight="1"android:background="@mipmap/laohu" /><ImageViewandroid:id="@+id/imageView6"android:layout_width="0dp"android:layout_height="0dp"android:layout_columnWeight="1"android:layout_rowWeight="1"android:background="@mipmap/laohu" /><ImageViewandroid:id="@+id/imageView7"android:layout_width="0dp"android:layout_height="0dp"android:layout_columnWeight="1"android:layout_rowWeight="1"android:background="@mipmap/laohu" /><ImageViewandroid:id="@+id/imageView8"android:layout_width="0dp"android:layout_height="0dp"android:layout_columnWeight="1"android:layout_rowWeight="1"android:background="@mipmap/laohu" /><ImageViewandroid:id="@+id/imageView9"android:layout_width="0dp"android:layout_height="0dp"android:layout_columnWeight="1"android:layout_rowWeight="1"android:background="@mipmap/laohu" /></GridLayout></LinearLayout>

实现代码,

public class AttentionQuestionsActivity extends AppCompatActivity {private ImageView[] imageViews; // 地鼠图片数组private ImageView currentImageView; // 当前显示的地鼠图片private int score = 0; // 得分private TextView scoreTextView; // 显示得分的文本视图@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_attention_questions);initImageViews(); // 初始化地鼠图片数组scoreTextView = findViewById(R.id.scoreTextView);showNextImageView(); // 显示第一个地鼠}// 初始化地鼠图片数组private void initImageViews() {imageViews = new ImageView[9];for (int i = 0; i < imageViews.length; i++) {imageViews[i] = findViewById(getResources().getIdentifier("imageView" + (i + 1), "id", getPackageName()));imageViews[i].setVisibility(View.INVISIBLE); // 初始设置地鼠图片为不可见imageViews[i].setOnClickListener(onClickListener);}}// 点击事件监听器private View.OnClickListener onClickListener = new View.OnClickListener() {@Overridepublic void onClick(View v) {if (v == currentImageView) { // 如果点击的是地鼠increaseScore(); // 增加得分hideCurrentImageView(); // 隐藏当前地鼠showNextImageView(); // 显示下一个地鼠}}};// 增加得分private void increaseScore() {score++;scoreTextView.setText("得分:" + score); // 更新得分显示}// 隐藏当前显示的地鼠private void hideCurrentImageView() {if (currentImageView != null) {currentImageView.setVisibility(View.INVISIBLE);currentImageView = null;}}// 显示下一个地鼠private void showNextImageView() {hideCurrentImageView();SecureRandom random = new SecureRandom();int nextIndex;do {nextIndex = random.nextInt(imageViews.length);} while (imageViews[nextIndex].getVisibility() == View.VISIBLE);currentImageView = imageViews[nextIndex];currentImageView.setVisibility(View.VISIBLE);}
}

备注 以上只是简单把功能实现出来,大家有需要可以拿来改为自己想要的

这篇关于安卓实现简单砸地鼠游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/719349

相关文章

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法