Java实现贪吃蛇代码
时间: 2023-11-13 16:11:19 浏览: 216
好的,下面是一个简单的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 Snake extends JPanel implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private LinkedList<Point> snake;
private Point food;
private Random rand;
private int direction;
private boolean gameOver;
public Snake() {
snake = new LinkedList<Point>();
snake.add(new Point(WIDTH/2, HEIGHT/2));
snake.add(new Point(WIDTH/2, HEIGHT/2+10));
snake.add(new Point(WIDTH/2, HEIGHT/2+20));
food = new Point(0, 0);
rand = new Random();
direction = KeyEvent.VK_UP;
gameOver = false;
setPreferredSize(new Dimension(WIDTH, HEIGHT));
addKeyListener(this);
}
public void start() {
Thread t = new Thread(this);
t.start();
}
public void run() {
while (!gameOver) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
moveSnake();
checkCollision();
repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
for (Point p : snake) {
g.fillRect(p.x, p.y, 10, 10);
}
g.setColor(Color.RED);
g.fillRect(food.x, food.y, 10, 10);
if (gameOver) {
g.setColor(Color.BLACK);
g.drawString("Game Over!", WIDTH/2-30, HEIGHT/2);
}
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
if (direction != KeyEvent.VK_DOWN) {
direction = KeyEvent.VK_UP;
}
break;
case KeyEvent.VK_DOWN:
if (direction != KeyEvent.VK_UP) {
direction = KeyEvent.VK_DOWN;
}
break;
case KeyEvent.VK_LEFT:
if (direction != KeyEvent.VK_RIGHT) {
direction = KeyEvent.VK_LEFT;
}
break;
case KeyEvent.VK_RIGHT:
if (direction != KeyEvent.VK_LEFT) {
direction = KeyEvent.VK_RIGHT;
}
break;
}
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
private void moveSnake() {
Point head = snake.getFirst();
int x = head.x;
int y = head.y;
switch (direction) {
case KeyEvent.VK_UP:
y -= 10;
break;
case KeyEvent.VK_DOWN:
y += 10;
break;
case KeyEvent.VK_LEFT:
x -= 10;
break;
case KeyEvent.VK_RIGHT:
x += 10;
break;
}
snake.addFirst(new Point(x, y));
if (food.equals(head)) {
generateFood();
} else {
snake.removeLast();
}
}
private void generateFood() {
int x = rand.nextInt(WIDTH/10)*10;
int y = rand.nextInt(HEIGHT/10)*10;
food.setLocation(x, y);
}
private void checkCollision() {
Point head = snake.getFirst();
if (head.x < 0 || head.x >= WIDTH || head.y < 0 || head.y >= HEIGHT) {
gameOver = true;
}
for (Point p : snake.subList(1, snake.size())) {
if (head.equals(p)) {
gameOver = true;
break;
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake");
Snake snake = new Snake();
frame.add(snake);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
snake.start();
}
}
```
这个示例代码实现了一个简单的贪吃蛇游戏,使用了Java Swing绘制界面。游戏的主要逻辑在`moveSnake()`方法中实现,包括蛇的移动、吃食物等操作。`checkCollision()`方法用于检测游戏是否结束,如果蛇碰到墙壁或自己的身体,则游戏结束。
你可以将这个代码示例保存为`Snake.java`文件,并在命令行中编译运行它。
阅读全文