编写一个贪吃蛇,用JAVA语言,用IntelliJ IDEA软件编写,要完整的游戏,功能都要有
时间: 2024-05-06 09:15:26 浏览: 161
好的,以下是一个基于Java语言和IntelliJ IDEA软件编写的贪吃蛇游戏的示例代码。这个游戏基本完成所有的功能,包括开始界面、游戏界面、暂停、重新开始、游戏结束等。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
public class SnakeGame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel mainPanel = new JPanel();
private JPanel gamePanel = new JPanel();
private JLabel[][] matrix = new JLabel[20][20];
private JLabel scoreLabel = new JLabel("Score: 0");
private int[][] map = new int[20][20];
private int snakeLen = 3;
private int direction = 0;
private int score = 0;
private int speed = 200;
private boolean isPause = false;
private Random random = new Random();
private Point food = new Point();
public SnakeGame() {
setTitle("Snake Game");
setSize(460, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel.setLayout(new BorderLayout());
gamePanel.setLayout(new GridLayout(20, 20));
mainPanel.add(gamePanel, BorderLayout.CENTER);
mainPanel.add(scoreLabel, BorderLayout.SOUTH);
add(mainPanel);
initMap();
initSnake();
initFood();
updateScore();
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
direction = 0;
break;
case KeyEvent.VK_RIGHT:
direction = 1;
break;
case KeyEvent.VK_DOWN:
direction = 2;
break;
case KeyEvent.VK_LEFT:
direction = 3;
break;
case KeyEvent.VK_SPACE:
if (isPause) {
resumeGame();
} else {
pauseGame();
}
break;
case KeyEvent.VK_ENTER:
restartGame();
break;
}
}
});
setVisible(true);
startGame();
}
private void initMap() {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
JLabel label = new JLabel();
label.setOpaque(true);
label.setBackground(Color.WHITE);
matrix[i][j] = label;
gamePanel.add(label);
}
}
}
private void initSnake() {
int x = random.nextInt(18) + 1;
int y = random.nextInt(18) + 1;
map[x][y] = 1;
matrix[x][y].setBackground(Color.GREEN);
}
private void initFood() {
int x = random.nextInt(18) + 1;
int y = random.nextInt(18) + 1;
while (map[x][y] != 0) {
x = random.nextInt(18) + 1;
y = random.nextInt(18) + 1;
}
food.x = x;
food.y = y;
matrix[x][y].setBackground(Color.RED);
}
private void updateScore() {
scoreLabel.setText("Score: " + score);
}
private void startGame() {
new Thread(() -> {
while (true) {
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!isPause) {
moveSnake();
}
}
}).start();
}
private void pauseGame() {
isPause = true;
}
private void resumeGame() {
isPause = false;
}
private void restartGame() {
map = new int[20][20];
initSnake();
initFood();
snakeLen = 3;
direction = 0;
score = 0;
speed = 200;
updateScore();
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
matrix[i][j].setBackground(Color.WHITE);
}
}
resumeGame();
}
private void moveSnake() {
Point head = findHead();
Point tail = findTail();
int nextX = 0, nextY = 0;
switch (direction) {
case 0:
nextX = head.x - 1;
nextY = head.y;
break;
case 1:
nextX = head.x;
nextY = head.y + 1;
break;
case 2:
nextX = head.x + 1;
nextY = head.y;
break;
case 3:
nextX = head.x;
nextY = head.y - 1;
break;
}
if (nextX < 0 || nextX >= 20 || nextY < 0 || nextY >= 20 || map[nextX][nextY] == 1) {
gameOver();
return;
}
if (nextX == food.x && nextY == food.y) {
snakeLen++;
score += 10;
updateScore();
if (speed > 50) {
speed -= 10;
}
initFood();
} else {
matrix[tail.x][tail.y].setBackground(Color.WHITE);
map[tail.x][tail.y] = 0;
}
map[nextX][nextY] = 1;
matrix[nextX][nextY].setBackground(Color.GREEN);
}
private Point findHead() {
Point p = new Point();
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
if (map[i][j] == 1) {
p.x = i;
p.y = j;
break;
}
}
}
return p;
}
private Point findTail() {
Point p = null;
int count = 0;
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
if (map[i][j] == 1) {
count++;
if (count == snakeLen) {
p = new Point(i, j);
break;
}
}
}
}
return p;
}
private void gameOver() {
JOptionPane.showMessageDialog(this, "Game Over!\nScore: " + score);
pauseGame();
}
public static void main(String[] args) {
new SnakeGame();
}
}
```
希望这个示例代码能够对你有所帮助。如果你有任何疑问或需要进一步的解释,请随时提出。
阅读全文