用java实现贪吃蛇代码
时间: 2023-06-26 20:06:54 浏览: 108
以下是一个简单的 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.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Snake extends JPanel implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
private Thread thread;
private boolean running;
private boolean right = true, left = false, up = false, down = false;
private Point fruit;
private int length = 5;
private Point[] snake = new Point[100];
private Random random;
public Snake() {
setFocusable(true);
addKeyListener(this);
random = new Random();
fruit = new Point(random.nextInt(49) + 1, random.nextInt(49) + 1);
snake[0] = new Point(0, 0);
for (int i = 1; i < snake.length; i++) {
snake[i] = new Point(-1, -1);
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
for (int x = 0; x < getSize().width; x += 10) {
g.drawLine(x, 0, x, getSize().height);
}
for (int y = 0; y < getSize().height; y += 10) {
g.drawLine(0, y, getSize().width, y);
}
g.setColor(Color.RED);
g.fillRect(fruit.x * 10, fruit.y * 10, 10, 10);
for (int i = 0; i < snake.length; i++) {
if (snake[i].x >= 0 && snake[i].y >= 0) {
g.setColor(Color.GREEN);
g.fillRect(snake[i].x * 10, snake[i].y * 10, 10, 10);
}
}
}
public void start() {
running = true;
thread = new Thread(this);
thread.start();
}
public void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (running) {
if (right) {
for (int i = length - 1; i > 0; i--) {
snake[i].x = snake[i - 1].x;
snake[i].y = snake[i - 1].y;
}
snake[0].x++;
if (snake[0].x > 49) {
snake[0].x = 0;
}
}
if (left) {
for (int i = length - 1; i > 0; i--) {
snake[i].x = snake[i - 1].x;
snake[i].y = snake[i - 1].y;
}
snake[0].x--;
if (snake[0].x < 0) {
snake[0].x = 49;
}
}
if (up) {
for (int i = length - 1; i > 0; i--) {
snake[i].x = snake[i - 1].x;
snake[i].y = snake[i - 1].y;
}
snake[0].y--;
if (snake[0].y < 0) {
snake[0].y = 49;
}
}
if (down) {
for (int i = length - 1; i > 0; i--) {
snake[i].x = snake[i - 1].x;
snake[i].y = snake[i - 1].y;
}
snake[0].y++;
if (snake[0].y > 49) {
snake[0].y = 0;
}
}
if (snake[0].x == fruit.x && snake[0].y == fruit.y) {
length++;
fruit.setLocation(random.nextInt(49) + 1, random.nextInt(49) + 1);
}
for (int i = 1; i < length; i++) {
if (snake[0].x == snake[i].x && snake[0].y == snake[i].y) {
System.out.println("You Lose!");
stop();
}
}
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT && !left) {
right = true;
up = false;
down = false;
}
if (key == KeyEvent.VK_LEFT && !right) {
left = true;
up = false;
down = false;
}
if (key == KeyEvent.VK_UP && !down) {
up = true;
left = false;
right = false;
}
if (key == KeyEvent.VK_DOWN && !up) {
down = true;
left = false;
right = false;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake");
Snake snake = new Snake();
frame.add(snake);
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
snake.start();
}
}
```
注意:此代码只是一个简单的实现,可能存在一些问题和不足之处,需要根据实际情况进行调整和优化。
阅读全文