贪吃蛇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.ArrayList;
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;

public static final int WIDTH = 500, HEIGHT = 500;

private Thread thread;
private boolean running = false;

private Point head;
private ArrayList<Point> snake;
private int direction;

private Point food;

private Random random;

public SnakeGame() {
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    setBackground(Color.BLACK);
    setFocusable(true);
    addKeyListener(this);

    head = new Point(WIDTH / 2, HEIGHT / 2);
    snake = new ArrayList<Point>();
    snake.add(head);

    direction = KeyEvent.VK_RIGHT;

    random = new Random();

    food = new Point(random.nextInt(WIDTH), random.nextInt(HEIGHT));

    start();
}

public void start() {
    running = true;
    thread = new Thread(this);
    thread.start();
}

public void stop() {
    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public void run() {
    while (running) {
        update();
        repaint();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public void update() {
    if (direction == KeyEvent.VK_LEFT) {
        head.x -= 10;
    } else if (direction == KeyEvent.VK_RIGHT) {
        head.x += 10;
    } else if (direction == KeyEvent.VK_UP) {
        head.y -= 10;
    } else if (direction == KeyEvent.VK_DOWN) {
        head.y += 10;
    }

    if (head.x < 0) {
        head.x = WIDTH - 10;
    } else if (head.x >= WIDTH) {
        head.x = 0;
    } else if (head.y < 0) {
        head.y = HEIGHT - 10;
    } else if (head.y >= HEIGHT) {
        head.y = 0;
    }

    if (head.equals(food)) {
        food.setLocation(random.nextInt(WIDTH), random.nextInt(HEIGHT));
        snake.add(new Point(head));
    }

    for (int i = snake.size() - 1; i > 0; i--) {
        snake.get(i).setLocation(snake.get(i - 1));
    }
    snake.get(0).setLocation(head);

    for (int i = 1; i < snake.size(); i++) {
        if (head.equals(snake.get(i))) {
            stop();
        }
    }
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.setColor(Color.RED);
    g.fillRect(food.x, food.y, 10, 10);

    g.setColor(Color.WHITE);
    for (Point p : snake) {
        g.fillRect(p.x, p.y, 10, 10);
    }
}

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_LEFT && direction != KeyEvent.VK_RIGHT) {
        direction = key;
    } else if (key == KeyEvent.VK_RIGHT && direction != KeyEvent.VK_LEFT) {
        direction = key;
    } else if (key == KeyEvent.VK_UP && direction != KeyEvent.VK_DOWN) {
        direction = key;
    } else if (key == KeyEvent.VK_DOWN && direction != KeyEvent.VK_UP) {
        direction = key;
    }
}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
}

public static void main(String[] args) {
    JFrame frame = new JFrame("Snake Game");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.add(new SnakeGame());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

}

以上代码实现了一个简单的贪吃蛇游戏,具体实现如下:

定义了窗口的大小为500x500,并创建了一个JPanel作为游戏界面。

在JPanel中定义了贪吃蛇的头部和身体,以及食物的位置和方向。

在JPanel中实现了游戏的主循环,每隔100毫秒更新一次游戏状态,并重绘游戏界面。

在JPanel中实现了键盘监听器,用于控制贪吃蛇的移动方向。

在JPanel中实现了游戏的更新逻辑,包括贪吃蛇的移动、食物的生成和贪吃蛇与食物的碰撞检测等。

在JPanel中实现了游戏的绘制逻辑,包括绘制贪吃蛇和食物等。

在main方法中创建了一个JFrame窗口,并将JPanel添加到窗口中,最后显示窗口。

需要注意的是,以上代码只是一个简单的示例,还有很多细节需要完善,比如贪吃蛇的长度增加、游戏结束的判断等。