当前位置: 首页 > article >正文

网络资源模板--Android Studio 打地鼠游戏App

目录

一、项目演示

二、项目测试环境

三、项目详情

四、完整的项目源码 


一、项目演示

网络资源模板--基于Android studio 实现打地鼠游戏

二、项目测试环境

三、项目详情

首页

主要功能

  1. 按钮初始化

    • 在 onCreate 方法中,通过 findViewById 获取界面上的按钮控件,包括:
      • startButton: 开始游戏。
      • setmusicButton: 设置音乐。
      • overButton: 结束应用。
      • historyButton: 查看历史记录。
  2. 按钮点击事件

    • startButton: 点击后启动 PlayGameActivity 活动。
    • setmusicButton: 点击后启动 SetMusicActivity 活动。
    • overButton: 点击后释放音乐资源并结束当前活动。
    • historyButton: 点击后启动 HistoryActivity 活动。
  3. 音乐管理

    • 在活动创建时初始化 musicPlayer 和 VolumsPlayer,并开始播放音乐。
    • 通过 onBackPressed 方法在返回时释放音乐资源,确保不再播放。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@mipmap/background"
    android:gravity="center">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/startButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10.0dip"
            android:background="@drawable/btn_menu"
            android:padding="20.0dip"
            android:text="开始游戏"
            android:textColor="#fff5fc11"
            android:textSize="30.0sp" />

        <Button
            android:id="@+id/historyButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10.0dip"
            android:background="@drawable/btn_menu"
            android:padding="20.0dip"
            android:text="历史分数"
            android:textColor="#fff5fc11"
            android:textSize="30.0sp" />

        <Button
            android:id="@+id/setmusicButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10.0dip"
            android:background="@drawable/btn_menu"
            android:padding="20.0dip"
            android:text="音效设置"
            android:textColor="#fff5fc11"
            android:textSize="30.0sp" />

        <Button
            android:id="@+id/overButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10.0dip"
            android:background="@drawable/btn_menu"
            android:padding="20.0dip"
            android:text="退出游戏"
            android:textColor="#fff5fc11"
            android:textSize="30.0sp" />
    </LinearLayout>
</LinearLayout>

游戏页面

主要功能

  1. UI 初始化

    • 在 onCreate 方法中,初始化界面组件,包括按钮、分数和时间的显示。
    • 设置按钮点击事件的监听器。
  2. 游戏逻辑

    • 使用 Handler 更新 UI 和处理游戏状态。
    • 创建一个线程来管理倒计时和随机选择下一个需要点击的按钮。
  3. 分数管理

    • 通过 SharedPreferences 记录历史分数。
    • 在游戏结束时弹出对话框,允许用户选择保存分数或返回主界面。
  4. 按钮点击事件处理

    • 根据用户点击的按钮是否正确来增加或减少分数,并更新按钮的状态(如正确或错误)。
    • 播放相应的音效反馈。
  5. 重置游戏

    • 提供重置游戏的功能,允许用户在游戏结束后重新开始。

重要方法

  • initButtons: 初始化游戏按钮,并设置点击监听。
  • initBattleMap / initNextMap: 初始化按钮与对应的战斗ID之间的映射关系。
  • changeUI: 更新 UI 显示,包括剩余时间和按钮状态。
  • gameOver: 处理游戏结束,显示结果对话框。
  • recordScore: 将当前分数和时间记录到 SharedPreferences
 private void initButtons() {
        // 初始化按钮数组
        buttons = new ImageButton[]{
                findViewById(R.id.first), findViewById(R.id.second),
                findViewById(R.id.three), findViewById(R.id.four),
                findViewById(R.id.five), findViewById(R.id.six),
                findViewById(R.id.seven), findViewById(R.id.eight),
                findViewById(R.id.nine), findViewById(R.id.ten),
                findViewById(R.id.eleven), findViewById(R.id.twelve)
        };

        showTime = findViewById(R.id.showtime); // 获取显示时间的文本框
        score = findViewById(R.id.score); // 获取显示分数的文本框

        // 为每个按钮设置点击事件监听器
        for (ImageButton button : buttons) {
            button.setOnClickListener(this);
        }
    }

    private void initBattleMap() {
        // 初始化战斗映射,将按钮与ID关联
        for (int i = 0; i < buttons.length; i++) {
            battle.put(buttons[i], i + 1);
        }
    }

    private void initNextMap() {
        // 初始化下一个映射,将ID与按钮关联
        for (int i = 0; i < buttons.length; i++) {
            nextMap.put(i + 1, buttons[i]);
        }
    }

    private void changeUI() {
        // 更新UI显示时间和按钮状态
        showTime.setText(time + "s");
        if (next != -1) {
            resetButtonImages(); // 重置按钮图片
            nextMap.get(next).setBackground(getDrawable(R.drawable.end)); // 设置当前按钮为结束状态
        }
    }

    private void resetButtonImages() {
        // 重置所有按钮为初始状态
        for (ImageButton button : buttons) {
            button.setBackground(getDrawable(R.drawable.start));
        }
    }

四、完整的项目源码 

👇👇👇👇👇快捷获取方式👇👇👇👇👇


http://www.kler.cn/news/341209.html

相关文章:

  • LVS-DR+Keepalived 高可用群集部署
  • 通过PyTorch 手写数字识别 入门神经网络 详细讲解
  • 【网站架构部署与优化】keepalived高可用
  • 深度学习常见问题
  • SQL第15课挑战题
  • C#中的static关键字:静态成员与单例模式的实现
  • 【读书笔记·VLSI电路设计方法解密】问题7:什么是基于标准单元的专用集成电路 (ASIC) 设计方法论
  • C# 字符串(string)三个不同的处理方法:IsNullOrEmpty、IsInterned 、IsNullOrWhiteSpace
  • 网络高危端口
  • 怎么在单片机裸机程序中移植EasyLogger?
  • 作业4-23
  • springboot网站开发-mysql数据库字段varchar类型存储汉字的长度关系
  • Qt C++设计模式->模板方法模式
  • Gin项目的初始化步骤和常见错误记录
  • python爬虫题目
  • Pymysql cur.fetchall() 返回 None
  • 哪个编程工具让你的工作效率翻倍?
  • 前端页面模块修改成可动态生成数据模块——大部分数据为GPT生成(仅供学习参考)
  • Oceanbase学习之—手工搭建oceanbaes测试
  • Pikachu-目录遍历