用JAVA写一个贪吃蛇
时间: 2023-12-10 09:03:07 浏览: 72
java写的贪吃蛇
好的,以下是一个简单的 Java 贪吃蛇游戏的示例代码。
```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 Runnable, KeyListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private static final int BLOCK_SIZE = 10;
private LinkedList<Point> snake;
private Point food;
private int direction = KeyEvent.VK_RIGHT;
public SnakeGame() {
snake = new LinkedList<Point>();
snake.add(new Point(0, 0));
snake.add(new Point(BLOCK_SIZE, 0));
snake.add(new Point(BLOCK_SIZE * 2, 0));
food = createFood();
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT || key == KeyEvent.VK_A) && direction != KeyEvent.VK_RIGHT) {
direction = KeyEvent.VK_LEFT;
} else if ((key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_D) && direction != KeyEvent.VK_LEFT) {
direction = KeyEvent.VK_RIGHT;
} else if ((key == KeyEvent.VK_UP || key == KeyEvent.VK_W) && direction != KeyEvent.VK_DOWN) {
direction = KeyEvent.VK_UP;
} else if ((key == KeyEvent.VK_DOWN || key == KeyEvent.VK_S) && direction != KeyEvent.VK_UP) {
direction = KeyEvent.VK_DOWN;
}
}
private Point createFood() {
Random rand = new Random();
int x = rand.nextInt(WIDTH / BLOCK_SIZE) * BLOCK_SIZE;
int y = rand.nextInt(HEIGHT / BLOCK_SIZE) * BLOCK_SIZE;
return new Point(x, y);
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Point head = snake.peekFirst();
Point newHead = new Point(head);
switch (direction) {
case KeyEvent.VK_LEFT:
newHead.x -= BLOCK_SIZE;
break;
case KeyEvent.VK_RIGHT:
newHead.x += BLOCK_SIZE;
break;
case KeyEvent.VK_UP:
newHead.y -= BLOCK_SIZE;
break;
case KeyEvent.VK_DOWN:
newHead.y += BLOCK_SIZE;
break;
}
if (newHead.x < 0 || newHead.x >= WIDTH || newHead.y < 0 || newHead.y >= HEIGHT || snake.contains(newHead)) {
System.out.println("Game over!");
System.exit(0);
}
snake.addFirst(newHead);
if (newHead.equals(food)) {
food = createFood();
} else {
snake.removeLast();
}
repaint();
}
}
@Override
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.RED);
g.fillRect(food.x, food.y, BLOCK_SIZE, BLOCK_SIZE);
g.setColor(Color.BLACK);
for (Point p : snake) {
g.fillRect(p.x, p.y, BLOCK_SIZE, BLOCK_SIZE);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
SnakeGame game = new SnakeGame();
frame.add(game);
frame.addKeyListener(game);
frame.setVisible(true);
Thread t = new Thread(game);
t.start();
}
}
```
这个程序中,我们使用了 Java 的 Swing 组件来创建窗口和画布,使用了一个链表来存储贪吃蛇的身体,每个节点是一个 Point 对象,表示身体的一个格子。我们还通过 KeyListener 监听键盘事件来控制贪吃蛇的方向。在 run 方法中,我们不断更新贪吃蛇的位置,检测游戏是否结束,然后重新绘制画布。最后在 main 方法中启动一个线程来运行游戏。
阅读全文