网络资源模板--Android Studio 打地鼠游戏App
目录
一、项目演示
网络资源模板--基于Android studio 实现打地鼠游戏
二、项目测试环境
三、项目详情
首页
主要功能
-
按钮初始化:
- 在
onCreate
方法中,通过findViewById
获取界面上的按钮控件,包括:startButton
: 开始游戏。setmusicButton
: 设置音乐。overButton
: 结束应用。historyButton
: 查看历史记录。
- 在
-
按钮点击事件:
startButton
: 点击后启动PlayGameActivity
活动。setmusicButton
: 点击后启动SetMusicActivity
活动。overButton
: 点击后释放音乐资源并结束当前活动。historyButton
: 点击后启动HistoryActivity
活动。
-
音乐管理:
- 在活动创建时初始化
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>
游戏页面
主要功能
-
UI 初始化:
- 在
onCreate
方法中,初始化界面组件,包括按钮、分数和时间的显示。 - 设置按钮点击事件的监听器。
- 在
-
游戏逻辑:
- 使用
Handler
更新 UI 和处理游戏状态。 - 创建一个线程来管理倒计时和随机选择下一个需要点击的按钮。
- 使用
-
分数管理:
- 通过
SharedPreferences
记录历史分数。 - 在游戏结束时弹出对话框,允许用户选择保存分数或返回主界面。
- 通过
-
按钮点击事件处理:
- 根据用户点击的按钮是否正确来增加或减少分数,并更新按钮的状态(如正确或错误)。
- 播放相应的音效反馈。
-
重置游戏:
- 提供重置游戏的功能,允许用户在游戏结束后重新开始。
重要方法
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));
}
}
四、完整的项目源码
👇👇👇👇👇快捷获取方式👇👇👇👇👇
原文地址:https://blog.csdn.net/qq_29823791/article/details/142754953
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!