使用java 编写一个贪吃蛇的游戏
时间: 2023-07-20 12:06:05 浏览: 84
用java编写的贪吃蛇游戏
好的,下面是使用 Java 编写的贪吃蛇游戏代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JFrame implements KeyListener {
private JPanel gamePanel;
private JLabel scoreLabel;
private int score;
private int direction;
private Timer timer;
private boolean gameOver;
private int width = 20;
private int height = 20;
private int blockSize = 20;
private int[][] field = new int[width][height];
private int[] snakeX = new int[width * height];
private int[] snakeY = new int[width * height];
private int snakeLength;
public SnakeGame() {
setTitle("贪吃蛇游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(width * blockSize + 20, height * blockSize + 70);
setLocationRelativeTo(null);
gamePanel = new JPanel();
gamePanel.setBackground(Color.WHITE);
gamePanel.setFocusable(true);
gamePanel.addKeyListener(this);
getContentPane().add(gamePanel, BorderLayout.CENTER);
scoreLabel = new JLabel("得分: 0");
getContentPane().add(scoreLabel, BorderLayout.SOUTH);
initGame();
setVisible(true);
}
private void initGame() {
gameOver = false;
score = 0;
direction = KeyEvent.VK_RIGHT;
snakeLength = 3;
snakeX[0] = width / 2;
snakeY[0] = height / 2;
snakeX[1] = snakeX[0] - 1;
snakeY[1] = snakeY[0];
snakeX[2] = snakeX[1] - 1;
snakeY[2] = snakeY[1];
generateFood();
timer = new Timer(200, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveSnake();
repaint();
if (gameOver) {
timer.stop();
JOptionPane.showMessageDialog(SnakeGame.this, "游戏结束,得分: " + score);
}
}
});
timer.start();
}
private void generateFood() {
int x, y;
do {
x = (int)(Math.random() * width);
y = (int)(Math.random() * height);
} while (field[x][y] != 0);
field[x][y] = 2;
}
private void moveSnake() {
int dx = 0, dy = 0;
switch (direction) {
case KeyEvent.VK_UP: dy = -1; break;
case KeyEvent.VK_DOWN: dy = 1; break;
case KeyEvent.VK_LEFT: dx = -1; break;
case KeyEvent.VK_RIGHT: dx = 1; break;
}
int newHeadX = snakeX[0] + dx;
int newHeadY = snakeY[0] + dy;
if (newHeadX < 0 || newHeadX >= width || newHeadY < 0 || newHeadY >= height) {
gameOver = true;
return;
}
if (field[newHeadX][newHeadY] == 1) {
gameOver = true;
return;
}
if (field[newHeadX][newHeadY] == 2) {
score++;
scoreLabel.setText("得分: " + score);
generateFood();
snakeLength++;
}
for (int i = snakeLength - 1; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
snakeX[0] += dx;
snakeY[0] += dy;
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
field[i][j] = 0;
for (int i = 0; i < snakeLength; i++)
field[snakeX[i]][snakeY[i]] = 1;
}
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++) {
if (field[i][j] == 2) {
g.setColor(Color.RED);
g.fillRect(i * blockSize, j * blockSize, blockSize, blockSize);
}
if (field[i][j] == 1) {
g.setColor(Color.BLACK);
g.fillRect(i * blockSize, j * blockSize, blockSize, blockSize);
}
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP && direction != KeyEvent.VK_DOWN)
direction = KeyEvent.VK_UP;
if (e.getKeyCode() == KeyEvent.VK_DOWN && direction != KeyEvent.VK_UP)
direction = KeyEvent.VK_DOWN;
if (e.getKeyCode() == KeyEvent.VK_LEFT && direction != KeyEvent.VK_RIGHT)
direction = KeyEvent.VK_LEFT;
if (e.getKeyCode() == KeyEvent.VK_RIGHT && direction != KeyEvent.VK_LEFT)
direction = KeyEvent.VK_RIGHT;
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
new SnakeGame();
}
}
```
运行上述代码,即可开始游戏。
阅读全文