答案:使用Java Swing创建打地鼠游戏,通过JFrame构建窗口,JButton模拟地洞,随机显示地鼠图标,玩家点击得分,配合Timer实现计时与刷新, JLabel显示分数和倒计时,完成基础逻辑后可扩展音效、图片资源及难度升级功能。

开发一个简单的“打地鼠”小游戏,用Java完全可以实现。你可以使用Swing或JavaFX来构建图形界面。下面以Swing为例,带你一步步完成一个基础版本的打地鼠游戏。
1. 游戏基本逻辑设计
打地鼠的核心机制是:多个地洞中随机出现地鼠,玩家点击地鼠得分,没打中或超时则不加分。可以设定时间限制和计分系统。
关键要素包括:
- 多个按钮代表地洞
- 地鼠随机出现在某个地洞上(可用图标表示)
- 玩家点击后判断是否打中
- 计时器控制游戏时长
- 计分板显示当前分数
2. 创建游戏界面(使用Swing)
使用JFrame作为主窗口,JButton作为地洞,JLabel显示分数和倒计时。
立即学习“Java免费学习笔记(深入)”;
本文档主要讲述的是Android游戏开发之旅;今天Android123开始新的Android游戏开发之旅系列,主要从控制方法(按键、轨迹球、触屏、重力感应、摄像头、话筒气流、光线亮度)、图形View(高效绘图技术如双缓冲)、音效(游戏音乐)以及最后的OpenGL ES(Java层)和NDK的OpenGL和J2ME游戏移植到Android方法,当然还有一些游戏实现惯用方法,比如地图编辑器,在Android OpenGL如何使用MD2文件,个部分讲述下Android游戏开发的过程最终实现一个比较完整的游戏引擎
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class WhackAMole extends JFrame {
private int score = 0;
private int timeLeft = 30; // 游戏总时间(秒)
private Random random = new Random();
private JButton[] holes = new JButton[9]; // 9个地洞
private Timer gameTimer, moleTimer;
private JLabel scoreLabel, timeLabel;
public WhackAMole() {
setTitle("打地鼠");
setSize(400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 分数和时间显示
JPanel topPanel = new JPanel();
scoreLabel = new JLabel("得分: 0");
timeLabel = new JLabel("剩余时间: 30");
topPanel.add(scoreLabel);
topPanel.add(timeLabel);
add(topPanel, BorderLayout.NORTH);
// 地洞区域
JPanel holePanel = new JPanel(new GridLayout(3, 3));
for (int i = 0; i < 9; i++) {
holes[i] = new JButton();
holes[i].setPreferredSize(new Dimension(80, 80));
final int index = i;
holes[i].addActionListener(e -> whack(index));
holePanel.add(holes[i]);
}
add(holePanel, BorderLayout.CENTER);
// 启动游戏
startGame();
}
private void startGame() {
// 每秒更新时间
gameTimer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timeLeft--;
timeLabel.setText("剩余时间: " + timeLeft);
if (timeLeft <= 0) {
((Timer)e.getSource()).stop();
moleTimer.stop();
JOptionPane.showMessageDialog(null, "游戏结束!你的得分: " + score);
}
}
});
gameTimer.start();
// 控制地鼠出现频率(比如每800毫秒换一次位置)
moleTimer = new Timer(800, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 清除所有地鼠
for (JButton hole : holes) {
hole.setIcon(null);
}
// 随机选一个地洞出现地鼠
int r = random.nextInt(9);
holes[r].setIcon(new ImageIcon("mole.png")); // 准备一张mole.png图片
}
});
moleTimer.start();
}
private void whack(int index) {
if (holes[index].getIcon() != null) {
score++;
scoreLabel.setText("得分: " + score);
holes[index].setIcon(null); // 打中后消失
} else {
// 可以加个“打空”提示音或反馈
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeel());
} catch (Exception e) { }
new WhackAMole().setVisible(true);
});
}
}
3. 资源与优化建议
为了让游戏更完整,你可以做这些改进:
-
添加声音效果:使用
javax.sound.sampled播放打中或失败音效 - 美化界面:准备地洞背景图、地鼠图,用透明图标提升视觉效果
- 难度递增:随着时间推移,缩短地鼠出现间隔
- 开始/重启按钮:增加面板让用户控制游戏流程
- 高分记录:用文件保存最高分
4. 注意事项
运行前确保项目目录下有名为“mole.png”的图片资源,或者改用系统图标代替,例如:
// 替代方案:用文字表示地鼠holes[r].setText("?");
如果打包成jar,记得把图片资源放在src/main/resources并用getClass().getResource()加载。









