用java写一个打地鼠
时间: 2024-03-09 10:05:14 浏览: 123
好的,以下是一个简单的Java打地鼠游戏的实现代码,你可以根据自己的需求进行修改和完善:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class WhacAMole extends JFrame {
private static final int WIDTH = 640; // 窗口宽度
private static final int HEIGHT = 480; // 窗口高度
private static final int HOLES = 9; // 地鼠洞数量
private static final int HOLE_SIZE = 80; // 地鼠洞大小
private static final int MOLE_SIZE = 60; // 地鼠大小
private static final int MOLE_TIME = 1000; // 地鼠出现时间
private static final int GAME_TIME = 30; // 游戏时间
private static final int SCORE_PER_HIT = 10; // 每次打中得分
private static final String MOLE_IMAGE_PATH = "/mole.png"; // 地鼠图片路径
private static final String BACKGROUND_IMAGE_PATH = "/background.jpg"; // 背景图片路径
private static final String FONT_NAME = "Arial"; // 字体名称
private static final int FONT_SIZE = 24; // 字体大小
private static final Color FONT_COLOR = Color.WHITE; // 字体颜色
private static final String START_LABEL_TEXT = "Start"; // 开始按钮文本
private static final String RESTART_LABEL_TEXT = "Restart"; // 重新开始按钮文本
private static final String SCORE_LABEL_TEXT = "Score: "; // 分数标签文本
private static final String TIME_LABEL_TEXT = "Time: "; // 时间标签文本
private static final String GAME_OVER_LABEL_TEXT = "Game Over"; // 游戏结束标签文本
private static final String GAME_OVER_SCORE_LABEL_TEXT = "Final Score: "; // 游戏结束分数标签文本
private List<Point> holes; // 地鼠洞坐标列表
private BufferedImage moleImage; // 地鼠图片
private BufferedImage backgroundImage; // 背景图片
private JButton startButton; // 开始按钮
private JLabel scoreLabel; // 分数标签
private JLabel timeLabel; // 时间标签
private JLabel gameOverLabel; // 游戏结束标签
private JLabel gameOverScoreLabel; // 游戏结束分数标签
private int score; // 分数
private int time; // 剩余时间
private Timer moleTimer; // 地鼠出现定时器
private Timer gameTimer; // 游戏时间定时器
private Random random; // 随机数生成器
public WhacAMole() {
super("Whac-A-Mole");
setSize(WIDTH, HEIGHT);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
holes = new ArrayList<Point>();
holes.add(new Point(20, 70));
holes.add(new Point(220, 70));
holes.add(new Point(420, 70));
holes.add(new Point(120, 170));
holes.add(new Point(320, 170));
holes.add(new Point(520, 170));
holes.add(new Point(220, 270));
holes.add(new Point(420, 270));
holes.add(new Point(620, 270));
try {
URL moleImageUrl = getClass().getResource(MOLE_IMAGE_PATH);
moleImage = ImageIO.read(moleImageUrl);
URL backgroundImageUrl = getClass().getResource(BACKGROUND_IMAGE_PATH);
backgroundImage = ImageIO.read(backgroundImageUrl);
} catch (IOException e) {
e.printStackTrace();
}
startButton = new JButton(START_LABEL_TEXT);
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startGame();
}
});
scoreLabel = new JLabel(SCORE_LABEL_TEXT + score);
scoreLabel.setFont(scoreLabel.getFont().deriveFont(FONT_SIZE));
scoreLabel.setForeground(FONT_COLOR);
timeLabel = new JLabel(TIME_LABEL_TEXT + time);
timeLabel.setFont(timeLabel.getFont().deriveFont(FONT_SIZE));
timeLabel.setForeground(FONT_COLOR);
gameOverLabel = new JLabel(GAME_OVER_LABEL_TEXT);
gameOverLabel.setFont(gameOverLabel.getFont().deriveFont(FONT_SIZE));
gameOverLabel.setForeground(FONT_COLOR);
gameOverLabel.setVisible(false);
gameOverScoreLabel = new JLabel();
gameOverScoreLabel.setFont(gameOverScoreLabel.getFont().deriveFont(FONT_SIZE));
gameOverScoreLabel.setForeground(FONT_COLOR);
gameOverScoreLabel.setVisible(false);
JPanel buttonPanel = new JPanel();
buttonPanel.add(startButton);
buttonPanel.add(scoreLabel);
buttonPanel.add(timeLabel);
JPanel gamePanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), null);
for (Point hole : holes) {
g.setColor(Color.BLACK);
g.fillOval(hole.x, hole.y, HOLE_SIZE, HOLE_SIZE);
g.drawImage(moleImage, hole.x + (HOLE_SIZE - MOLE_SIZE) / 2, hole.y + (HOLE_SIZE - MOLE_SIZE) / 2, MOLE_SIZE, MOLE_SIZE, null);
}
}
};
gamePanel.setPreferredSize(new Dimension(WIDTH, HEIGHT - buttonPanel.getPreferredSize().height));
gamePanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
Point point = e.getPoint();
for (Point hole : holes) {
if (point.x >= hole.x && point.x <= hole.x + HOLE_SIZE && point.y >= hole.y && point.y <= hole.y + HOLE_SIZE) {
score += SCORE_PER_HIT;
scoreLabel.setText(SCORE_LABEL_TEXT + score);
break;
}
}
}
});
JPanel gameOverPanel = new JPanel(new BorderLayout());
gameOverPanel.add(gameOverLabel, BorderLayout.NORTH);
gameOverPanel.add(gameOverScoreLabel, BorderLayout.CENTER);
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.add(buttonPanel, BorderLayout.NORTH);
contentPane.add(gamePanel, BorderLayout.CENTER);
contentPane.add(gameOverPanel, BorderLayout.SOUTH);
setContentPane(contentPane);
random = new Random();
}
private void startGame() {
score = 0;
scoreLabel.setText(SCORE_LABEL_TEXT + score);
time = GAME_TIME;
timeLabel.setText(TIME_LABEL_TEXT + time);
startButton.setEnabled(false);
moleTimer = new Timer(MOLE_TIME, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (Point hole : holes) {
if (random.nextInt(100) < 50) {
holeMole(hole);
break;
}
}
}
});
moleTimer.start();
gameTimer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
time--;
timeLabel.setText(TIME_LABEL_TEXT + time);
if (time == 0) {
endGame();
}
}
});
gameTimer.start();
}
private void endGame() {
moleTimer.stop();
gameTimer.stop();
startButton.setEnabled(true);
gameOverLabel.setVisible(true);
gameOverScoreLabel.setText(GAME_OVER_SCORE_LABEL_TEXT + score);
gameOverScoreLabel.setVisible(true);
}
private void holeMole(Point hole) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(random.nextInt(1000) + 500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!moleTimer.isRunning()) {
return;
}
gamePanel.getGraphics().drawImage(moleImage, hole.x + (HOLE_SIZE - MOLE_SIZE) / 2, hole.y + (HOLE_SIZE - MOLE_SIZE) / 2, MOLE_SIZE, MOLE_SIZE, null);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(random.nextInt(500) + 500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!moleTimer.isRunning()) {
return;
}
gamePanel.getGraphics().drawOval(hole.x, hole.y, HOLE_SIZE, HOLE_SIZE);
}
}).start();
}
}).start();
}
public static void main(String[] args) {
WhacAMole game = new WhacAMole();
game.setVisible(true);
}
}
```
这个游戏的实现使用了Java的Swing组件,创建了一个窗口,包含游戏画面和按钮等控件,使用定时器控制地鼠的出现和消失,响应点击事件判断是否打中地鼠,计算得分,游戏结束时显示分数等信息。
阅读全文