用java写一个贪吃蛇的代码
时间: 2024-01-02 08:05:13 浏览: 26
好的,以下是一个简单的Java贪吃蛇游戏的示例代码。由于篇幅限制,我只提供了核心代码,您可以基于这个代码进行修改和拓展。
```
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SnakeGame extends JPanel implements KeyListener, Runnable {
private static final long serialVersionUID = 1L;
private final int BOX_SIZE = 10;
private final int WIDTH = 600;
private final int HEIGHT = 400;
private final int GAME_WIDTH = WIDTH / BOX_SIZE;
private final int GAME_HEIGHT = HEIGHT / BOX_SIZE;
private int x, y;
private int dx, dy;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private Thread thread;
private LinkedList<Point> snake = new LinkedList<Point>();
private Point food;
private Random random = new Random();
public SnakeGame() {
addKeyListener(this);
setFocusable(true);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
initGame();
}
private void initGame() {
x = (GAME_WIDTH / 2) * BOX_SIZE;
y = (GAME_HEIGHT / 2) * BOX_SIZE;
dx = BOX_SIZE;
dy = 0;
snake.clear();
snake.add(new Point(x, y));
generateFood();
inGame = true;
}
private void generateFood() {
int fx = random.nextInt(GAME_WIDTH) * BOX_SIZE;
int fy = random.nextInt(GAME_HEIGHT) * BOX_SIZE;
food = new Point(fx, fy);
}
private void move() {
Point head = snake.getFirst();
int nx = head.x + dx;
int ny = head.y + dy;
if (nx < 0 || nx >= WIDTH || ny < 0 || ny >= HEIGHT) {
inGame = false;
} else if (snake.contains(new Point(nx, ny))) {
inGame = false;
} else if (nx == food.x && ny == food.y) {
snake.addFirst(food);
generateFood();
} else {
snake.removeLast();
snake.addFirst(new Point(nx, ny));
}
}
private void checkCollision() {
if (!inGame) {
thread.interrupt();
}
}
private void draw(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
snake.forEach(p -> g.fillRect(p.x, p.y, BOX_SIZE, BOX_SIZE));
g.setColor(Color.RED);
g.fillRect(food.x, food.y, BOX_SIZE, BOX_SIZE);
}
private void gameLoop() {
while (inGame) {
move();
checkCollision();
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT && !rightDirection) {
leftDirection = true;
upDirection = false;
downDirection = false;
dx = -BOX_SIZE;
dy = 0;
} else if (key == KeyEvent.VK_RIGHT && !leftDirection) {
rightDirection = true;
upDirection = false;
downDirection = false;
dx = BOX_SIZE;
dy = 0;
} else if (key == KeyEvent.VK_UP && !downDirection) {
upDirection = true;
leftDirection = false;
rightDirection = false;
dx = 0;
dy = -BOX_SIZE;
} else if (key == KeyEvent.VK_DOWN && !upDirection) {
downDirection = true;
leftDirection = false;
rightDirection = false;
dx = 0;
dy = BOX_SIZE;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void run() {
gameLoop();
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
SnakeGame snakeGame = new SnakeGame();
frame.add(snakeGame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
snakeGame.start();
}
public void start() {
thread = new Thread(this);
thread.start();
}
}
```
这个代码实现了一个简单的贪吃蛇游戏,使用了Java Swing库来进行游戏界面的绘制和更新。您可以在自己的计算机上运行这个代码,看一下这个示例程序的效果。