用JAVA帮我写个贪吃蛇
时间: 2023-12-17 15:03:55 浏览: 65
java写的贪吃蛇
好的,以下是一个简单的Java贪吃蛇实现,代码中有注释说明每一步的作用。
```java
import java.awt.Color;
import java.awt.Dimension;
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.List;
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;
public static final int HEIGHT = 500;
private Thread thread;
private boolean running;
private Node head;
private List<Node> snake;
private Node food;
private Random r;
private int xDir, yDir;
private int size;
private int ticks;
public SnakeGame() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
addKeyListener(this);
snake = new ArrayList<Node>();
r = new Random();
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 tick() {
if (snake.size() == 0) {
head = new Node(0, 0);
snake.add(head);
}
if (food == null) {
int x = r.nextInt(WIDTH / Node.SIZE) * Node.SIZE;
int y = r.nextInt(HEIGHT / Node.SIZE) * Node.SIZE;
food = new Node(x, y);
}
if (ticks % 2 == 0) {
Node tail = null;
tail = snake.remove(snake.size() - 1);
tail.setX(head.getX() + xDir * Node.SIZE);
tail.setY(head.getY() + yDir * Node.SIZE);
head = tail;
snake.add(0, head);
for (int i = 1; i < snake.size(); i++) {
if (head.getX() == snake.get(i).getX() && head.getY() == snake.get(i).getY()) {
stop();
}
}
if (head.getX() < 0) {
head.setX(WIDTH);
}
if (head.getX() > WIDTH) {
head.setX(0);
}
if (head.getY() < 0) {
head.setY(HEIGHT);
}
if (head.getY() > HEIGHT) {
head.setY(0);
}
if (head.getX() == food.getX() && head.getY() == food.getY()) {
size++;
food = null;
}
}
ticks++;
}
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
if (food != null) {
food.draw(g);
}
for (Node n : snake) {
n.draw(g);
}
g.setColor(Color.WHITE);
g.drawString("Score: " + size, 10, 20);
}
public void run() {
while (running) {
tick();
repaint();
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP && yDir != 1) {
xDir = 0;
yDir = -1;
}
if (key == KeyEvent.VK_DOWN && yDir != -1) {
xDir = 0;
yDir = 1;
}
if (key == KeyEvent.VK_LEFT && xDir != 1) {
xDir = -1;
yDir = 0;
}
if (key == KeyEvent.VK_RIGHT && xDir != -1) {
xDir = 1;
yDir = 0;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake");
SnakeGame game = new SnakeGame();
frame.add(game);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
}
class Node {
public static final int SIZE = 10;
private int x, y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void draw(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(x, y, SIZE, SIZE);
}
}
```
运行该程序,你就可以玩一局贪吃蛇了。
阅读全文