Java GUI黄金矿工代码
时间: 2023-07-09 13:42:41 浏览: 178
以下是一个简单的 Java GUI 黄金矿工游戏代码,供参考:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GoldMinerGame extends JFrame {
private JButton startButton;
private JLabel scoreLabel;
private GoldMinerPanel gamePanel;
private Timer timer;
private int score;
public GoldMinerGame() {
super("Gold Miner Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 添加开始按钮
startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startGame();
}
});
add(startButton, BorderLayout.NORTH);
// 添加分数标签
scoreLabel = new JLabel("Score: 0");
add(scoreLabel, BorderLayout.SOUTH);
// 添加游戏面板
gamePanel = new GoldMinerPanel();
add(gamePanel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
// 开始游戏
private void startGame() {
// 初始化分数
score = 0;
scoreLabel.setText("Score: " + score);
// 开始游戏定时器
timer = new Timer(50, new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 更新游戏面板
gamePanel.update();
// 检查游戏是否结束
if (gamePanel.isGameOver()) {
endGame();
}
}
});
timer.start();
// 禁用开始按钮
startButton.setEnabled(false);
}
// 结束游戏
private void endGame() {
// 停止游戏定时器
timer.stop();
// 显示结束消息框
JOptionPane.showMessageDialog(this, "Game Over! Your score is " + score);
// 启用开始按钮
startButton.setEnabled(true);
}
// 增加分数
public void addScore(int points) {
score += points;
scoreLabel.setText("Score: " + score);
}
public static void main(String[] args) {
new GoldMinerGame();
}
}
class GoldMinerPanel extends JPanel {
private static final int PANEL_WIDTH = 800;
private static final int PANEL_HEIGHT = 600;
private static final int HOOK_SPEED = 5;
private static final int GOLD_SPEED = 2;
private static final int GOLD_POINTS = 10;
private static final int STONE_POINTS = -5;
private static final int DIAMOND_POINTS = 20;
private static final int HOOK_LENGTH = 200;
private static final int HOOK_WIDTH = 10;
private static final int GOLD_WIDTH = 40;
private static final int GOLD_HEIGHT = 40;
private static final int STONE_WIDTH = 60;
private static final int STONE_HEIGHT = 60;
private static final int DIAMOND_WIDTH = 80;
private static final int DIAMOND_HEIGHT = 80;
private Image background;
private Image hookImage;
private Image goldImage;
private Image stoneImage;
private Image diamondImage;
private int hookX;
private int hookY;
private int goldX;
private int goldY;
private int stoneX;
private int stoneY;
private int diamondX;
private int diamondY;
private int hookLength;
private boolean hookGoingDown;
private boolean hookHasGold;
private boolean gameOver;
private GoldMinerGame game;
public GoldMinerPanel() {
setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
setOpaque(true);
setBackground(Color.WHITE);
// 加载图片
background = new ImageIcon("background.jpg").getImage();
hookImage = new ImageIcon("hook.png").getImage();
goldImage = new ImageIcon("gold.png").getImage();
stoneImage = new ImageIcon("stone.png").getImage();
diamondImage = new ImageIcon("diamond.png").getImage();
// 初始化钩子位置和长度
hookX = PANEL_WIDTH / 2;
hookY = 0;
hookLength = HOOK_LENGTH;
hookGoingDown = true;
hookHasGold = false;
// 随机生成金块、石头和钻石的位置
goldX = (int)(Math.random() * (PANEL_WIDTH - GOLD_WIDTH));
goldY = (int)(Math.random() * (PANEL_HEIGHT - GOLD_HEIGHT - 100));
stoneX = (int)(Math.random() * (PANEL_WIDTH - STONE_WIDTH));
stoneY = (int)(Math.random() * (PANEL_HEIGHT - STONE_HEIGHT - 100));
diamondX = (int)(Math.random() * (PANEL_WIDTH - DIAMOND_WIDTH));
diamondY = (int)(Math.random() * (PANEL_HEIGHT - DIAMOND_HEIGHT - 100));
gameOver = false;
}
// 更新游戏面板
public void update() {
// 移动钩子
if (hookGoingDown) {
hookY += HOOK_SPEED;
if (hookY >= PANEL_HEIGHT) {
hookGoingDown = false;
}
} else {
hookY -= HOOK_SPEED;
if (hookY <= 0) {
hookGoingDown = true;
}
}
// 检查钩子是否抓到了金块、石头或钻石
if (hookX >= goldX && hookX <= goldX + GOLD_WIDTH && hookY >= goldY && hookY <= goldY + GOLD_HEIGHT) {
addScore(GOLD_POINTS);
hookHasGold = true;
}
if (hookX >= stoneX && hookX <= stoneX + STONE_WIDTH && hookY >= stoneY && hookY <= stoneY + STONE_HEIGHT) {
addScore(STONE_POINTS);
hookHasGold = false;
}
if (hookX >= diamondX && hookX <= diamondX + DIAMOND_WIDTH && hookY >= diamondY && hookY <= diamondY + DIAMOND_HEIGHT) {
addScore(DIAMOND_POINTS);
hookHasGold = true;
}
// 移动金块、石头和钻石
if (hookHasGold) {
if (hookGoingDown) {
goldY += GOLD_SPEED;
diamondY += GOLD_SPEED;
} else {
goldY -= GOLD_SPEED;
diamondY -= GOLD_SPEED;
}
}
// 检查游戏是否结束
if (goldY >= PANEL_HEIGHT || diamondY >= PANEL_HEIGHT || score <= -50) {
gameOver = true;
}
// 重绘游戏面板
repaint();
}
// 绘制游戏面板
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0, PANEL_WIDTH, PANEL_HEIGHT, null);
g.drawImage(hookImage, hookX, hookY, HOOK_WIDTH, hookLength, null);
g.drawImage(goldImage, goldX, goldY, GOLD_WIDTH, GOLD_HEIGHT, null);
g.drawImage(stoneImage, stoneX, stoneY, STONE_WIDTH, STONE_HEIGHT, null);
g.drawImage(diamondImage, diamondX, diamondY, DIAMOND_WIDTH, DIAMOND_HEIGHT, null);
}
// 增加分数
private void addScore(int points) {
game.addScore(points);
}
// 检查游戏是否结束
public boolean isGameOver() {
return gameOver;
}
// 设置游戏对象
public void setGame(GoldMinerGame game) {
this.game = game;
}
}
```
这个游戏使用了 Java 的 Swing GUI 库,包含了一个游戏窗口、一个开始按钮、一个分数标签和一个游戏面板。游戏面板中包含了一个钩子、一些金块、石头和钻石。通过按下开始按钮开始游戏,钩子会不断上下移动,玩家需要通过移动钩子抓住金块、石头和钻石来得分,但如果得分低于 -50 分或者金块或钻石掉落到屏幕底部,游戏就会结束。在游戏结束后,会显示一个消息框告诉玩家得分情况,并重新启用开始按钮。
阅读全文