当我在外面时应该显示一条没有显示的消息,请检查它。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SnakeGame extends JPanel implements ActionListener {
// game variables
private int score;
private boolean gameOver;
private Timer timer;
// board variables
private static final int ROWS = 20;
private static final int COLS = 20;
private static final int CELL_SIZE = 30;
private int[][] board;
// snake variables
private int snakeLength;
private int[] snakeX;
private int[] snakeY;
private int dx;
private int dy;
public SnakeGame() {
// initialize variables
score = 0;
gameOver = false;
board = new int[ROWS][COLS];
snakeLength = 3;
snakeX = new int[ROWS*COLS];
snakeY = new int[ROWS*COLS];
dx = CELL_SIZE;
dy = 0;
// create the game board
setPreferredSize(new Dimension(COLS*CELL_SIZE, ROWS*CELL_SIZE));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(new SnakeKeyListener());
// start the game timer
timer = new Timer(100, this);
timer.start();
// generate initial snake and food
generateSnake();
generateFood();
}
public void actionPerformed(ActionEvent e) {
// update the snake position
int headX = snakeX[0] + dx;
int headY = snakeY[0] + dy;
// check for collision with walls or body
if (headX < 0 || headX >= COLS*CELL_SIZE || headY < 0 || headY >= ROWS*CELL_SIZE || board[headY/CELL_SIZE][headX/CELL_SIZE] > 0) {
gameOver = true;
return;
}
if (gameOver) {
return;
}
// check for food
if (board[headY/CELL_SIZE][headX/CELL_SIZE] == -1) {
snakeLength++;
score++;
generateFood();
}
// move the snake
for (int i = snakeLength-1; i > 0; i--) {
snakeX[i] = snakeX[i-1];
snakeY[i] = snakeY[i-1];
}
snakeX[0] = headX;
snakeY[0] = headY;
// update the board
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] > 0) {
board[i][j]--;
}
}
}
for (int i = 0; i < snakeLength; i++) {
board[snakeY[i]/CELL_SIZE][snakeX[i]/CELL_SIZE] = snakeLength-i;
}
// repaint the board
repaint();
}
private void generateSnake() {
// generate initial snake position and direction
int snakeStartX = (COLS/2) * CELL_SIZE;
int snakeStartY = (ROWS/2) * CELL_SIZE;
snakeX[0] = snakeStartX;
snakeY[0] = snakeStartY;
snakeX[1] = snakeStartX - CELL_SIZE;
snakeY[1] = snakeStartY;
snakeX[2] = snakeStartX - (2 * CELL_SIZE);
snakeY[2] = snakeStartY;
}
private void generateFood() {
// generate food at a random position on the board
int foodX = (int) (Math.random() * COLS) * CELL_SIZE;
int foodY = (int) (Math.random() * ROWS) * CELL_SIZE;
// check that food is not generated on the snake
if (board[foodY/CELL_SIZE][foodX/CELL_SIZE] != 0) {
generateFood();
} else {
board[foodY/CELL_SIZE][foodX/CELL_SIZE] = -1;
}
}
private class SnakeKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT && dx == 0) {
dx = -CELL_SIZE;
dy = 0;
} else if (key == KeyEvent.VK_RIGHT && dx == 0) {
dx = CELL_SIZE;
dy = 0;
} else if (key == KeyEvent.VK_UP && dy == 0) {
dx = 0;
dy = -CELL_SIZE;
} else if (key == KeyEvent.VK_DOWN && dy == 0) {
dx = 0;
dy = CELL_SIZE;
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// draw the board
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == -1) {
g.setColor(Color.GREEN);
g.fillRect(j*CELL_SIZE, i*CELL_SIZE, CELL_SIZE, CELL_SIZE);
} else if (board[i][j] > 0) {
g.setColor(Color.WHITE);
g.fillRect(j*CELL_SIZE, i*CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
}
}
// draw the snake
for (int i = 0; i < snakeLength; i++) {
g.setColor(Color.YELLOW);
g.fillRect(snakeX[i], snakeY[i], CELL_SIZE, CELL_SIZE);
}
// draw the score
g.setColor(Color.WHITE);
g.drawString("Score: " + score, 10, 20);
// draw game over message
if (gameOver) {
g.setColor(Color.WHITE);
g.drawString("Game Over!", COLS*CELL_SIZE/2 - 30, ROWS*CELL_SIZE/2);
JOptionPane.showMessageDialog(this, "Game Over! Your score is " + score, "Snake Game", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.getContentPane().add(new SnakeGame(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
我尝试了所有可能的解决方案。 该项目的目标是开发一款经典的街机风格游戏,玩家可以在其中控制一条“蛇”,它会在进食时变长,同时避免与墙壁和自己的身体发生碰撞。游戏应使用 Java 编程语言开发,并应设计有图形用户界面,包括可玩游戏区域、游戏控件和计分系统。游戏还应该具有不同的难度级别,并且应该能够保存和加载高分。此外,游戏应该能够处理用户输入并对碰撞和蛇的移动等事件做出响应。该项目的目的是开发一款引人入胜且有趣的游戏,为玩家提供有趣且具有挑战性的体验。
你不应该在 paintComponent 中调用 JOptionPane.showMessageDialog。它应该只用于绘画。
添加
if(gameOver) {
JOptionPane.showMessageDialog(this, "Game Over! Your score is " + score, "Snake Game", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
开始执行 actionPerformed 并从 paintComponent 中删除 Dialog 和 exit 部分