用Java语言编写一个简单的贪吃蛇游戏
时间: 2023-08-02 09:10:42 浏览: 68
用Java语言编写的贪吃蛇游戏
4星 · 用户满意度95%
好的,以下是一个简单的贪吃蛇游戏的Java代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SnakeGame extends JFrame implements KeyListener, ActionListener {
private int width = 15;
private int height = 15;
private int size = 20;
private int[][] map = new int[width][height];
private int snakeLength = 3;
private int[] snakeX = new int[width * height];
private int[] snakeY = new int[width * height];
private int foodX;
private int foodY;
private int direction = 39;
private Timer timer;
public SnakeGame() {
setTitle("贪吃蛇游戏");
setSize(width * size + 6, height * size + 28);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
init();
timer = new Timer(200, this);
timer.start();
setVisible(true);
}
private void init() {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
map[i][j] = 0;
}
}
for (int i = 0; i < snakeLength; i++) {
snakeX[i] = width / 2 - i;
snakeY[i] = height / 2;
map[snakeX[i]][snakeY[i]] = 1;
}
generateFood();
}
private void generateFood() {
boolean valid = false;
while (!valid) {
foodX = (int) (Math.random() * width);
foodY = (int) (Math.random() * height);
if (map[foodX][foodY] == 0) {
valid = true;
}
}
}
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, width * size, height * size);
g.setColor(Color.BLACK);
for (int i = 0; i <= width; i++) {
g.drawLine(i * size, 0, i * size, height * size);
}
for (int i = 0; i <= height; i++) {
g.drawLine(0, i * size, width * size, i * size);
}
g.setColor(Color.GREEN);
g.fillRect(foodX * size, foodY * size, size, size);
g.setColor(Color.RED);
for (int i = 0; i < snakeLength; i++) {
g.fillRect(snakeX[i] * size, snakeY[i] * size, size, size);
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT || key == KeyEvent.VK_A) && direction != 39) {
direction = 37;
}
if ((key == KeyEvent.VK_UP || key == KeyEvent.VK_W) && direction != 40) {
direction = 38;
}
if ((key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_D) && direction != 37) {
direction = 39;
}
if ((key == KeyEvent.VK_DOWN || key == KeyEvent.VK_S) && direction != 38) {
direction = 40;
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void actionPerformed(ActionEvent e) {
int x = snakeX[0];
int y = snakeY[0];
if (direction == 37) {
x--;
}
if (direction == 38) {
y--;
}
if (direction == 39) {
x++;
}
if (direction == 40) {
y++;
}
if (x < 0 || x >= width || y < 0 || y >= height || map[x][y] == 1) {
timer.stop();
JOptionPane.showMessageDialog(null, "游戏结束");
System.exit(0);
}
if (x == foodX && y == foodY) {
snakeLength++;
map[foodX][foodY] = 1;
generateFood();
} else {
map[snakeX[snakeLength - 1]][snakeY[snakeLength - 1]] = 0;
}
for (int i = snakeLength - 1; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
snakeX[0] = x;
snakeY[0] = y;
map[x][y] = 1;
repaint();
}
public static void main(String[] args) {
new SnakeGame();
}
}
```
代码中使用了Java的Swing库来实现界面,在主函数中创建了一个`SnakeGame`对象,该对象实现了`KeyListener`和`ActionListener`接口,分别用于处理键盘事件和定时器事件。
游戏中使用一个二维数组`map`来表示地图,其中0表示空格,1表示蛇身,使用`snakeX`和`snakeY`两个数组来记录蛇身的位置,使用`snakeLength`来记录蛇身的长度。在定时器事件中,根据当前方向移动蛇身,检测是否与食物碰撞,更新蛇身位置和长度,重新生成食物,最后重绘整个界面。
具体实现细节请参考代码注释。
阅读全文