贪吃蛇 Java 达到某一条件 弹出窗口game over 怎么实现弹出窗口
时间: 2024-03-24 08:40:08 浏览: 20
要在Java中实现弹出窗口,可以使用Java Swing库中的JOptionPane类。JOptionPane类提供了一种简单的方法来创建和显示对话框(如消息框、输入框、确认框等)。
在贪吃蛇游戏中,如果达到某个条件(例如,碰到边界或碰到自己),可以使用以下代码创建并显示一个提示框:
```java
JOptionPane.showMessageDialog(null, "Game Over", "提示", JOptionPane.ERROR_MESSAGE);
```
其中,第一个参数是父组件,我们可以传入null表示对话框没有父组件,第二个参数是提示消息的内容,第三个参数是对话框的标题,第四个参数是对话框的图标类型,这里使用ERROR_MESSAGE表示错误图标。
这样,当游戏结束时,就会弹出一个带有错误图标的提示框,告诉玩家游戏已经结束了。
相关问题
用java写出一个贪吃蛇小游戏]
好的,我可以用Java编写一个贪吃蛇小游戏。首先,我们需要定义一个类来表示贪吃蛇,它将包括蛇的位置,方向和当前状态(例如长度)。然后,我们需要定义一个函数来更新贪吃蛇的位置,以及一个函数来处理贪吃蛇吃到食物的情况。最后,我们需要定义一个函数来处理贪吃蛇撞到自己的情况,以及一个函数来处理贪吃蛇撞到墙的情况。
使用java实现一个贪吃蛇游戏
好的,以下是一个简单的 Java 实现贪吃蛇游戏的示例代码:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class SnakeGame extends JPanel implements ActionListener {
private final int WIDTH = 300;
private final int HEIGHT = 300;
private final int SIZE = 10;
private final int DOTS = WIDTH * HEIGHT / SIZE / SIZE;
private final int DELAY = 140;
private int x[] = new int[DOTS];
private int y[] = new int[DOTS];
private int dots;
private int appleX;
private int appleY;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private Timer timer;
private Image ball;
private Image apple;
private Image head;
public SnakeGame() {
init();
}
private void init() {
setBackground(Color.black);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
loadImages();
initGame();
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {
leftDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {
rightDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_UP) && (!downDirection)) {
upDirection = true;
rightDirection = false;
leftDirection = false;
}
if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
downDirection = true;
rightDirection = false;
leftDirection = false;
}
}
});
timer = new Timer(DELAY, this);
timer.start();
}
private void loadImages() {
ImageIcon iid = new ImageIcon("dot.png");
ball = iid.getImage();
ImageIcon iia = new ImageIcon("apple.png");
apple = iia.getImage();
ImageIcon iih = new ImageIcon("head.png");
head = iih.getImage();
}
private void initGame() {
dots = 3;
for (int z = 0; z < dots; z++) {
x[z] = 50 - z * 10;
y[z] = 50;
}
locateApple();
}
private void checkApple() {
if ((x[0] == appleX) && (y[0] == appleY)) {
dots++;
locateApple();
}
}
private void move() {
for (int z = dots; z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
}
if (leftDirection) {
x[0] -= SIZE;
}
if (rightDirection) {
x[0] += SIZE;
}
if (upDirection) {
y[0] -= SIZE;
}
if (downDirection) {
y[0] += SIZE;
}
}
private void checkCollision() {
for (int z = dots; z > 0; z--) {
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
inGame = false;
}
}
if (y[0] >= HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] >= WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
if (!inGame) {
timer.stop();
}
}
private void locateApple() {
Random rand = new Random();
int r = rand.nextInt(WIDTH / SIZE);
appleX = r * SIZE;
r = rand.nextInt(HEIGHT / SIZE);
appleY = r * SIZE;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (inGame) {
g.drawImage(apple, appleX, appleY, this);
for (int z = 0; z < dots; z++) {
if (z == 0) {
g.drawImage(head, x[z], y[z], this);
} else {
g.drawImage(ball, x[z], y[z], this);
}
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
private void gameOver(Graphics g) {
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2);
}
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
SnakeGame game = new SnakeGame();
frame.add(game);
frame.setVisible(true);
}
}
```
注意:此代码中使用了三张图片,分别是蛇身、苹果和蛇头,需要将它们放在与代码相同的目录下。