网络资源模板--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));
        }
    }

四、完整的项目源码 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程乐学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值