java贪吃蛇代码eclipse
时间: 2023-07-09 10:31:03 浏览: 73
以下是一个简单的贪吃蛇游戏的Java代码,可以在Eclipse中运行:
```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 DOT_SIZE = 10;
private static final int ALL_DOTS = 900;
private static final int RAND_POS = 29;
private static final int DELAY = 100;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private int dots;
private int apple_x;
private int apple_y;
private int[] x = new int[ALL_DOTS];
private int[] y = new int[ALL_DOTS];
private Thread animator;
public SnakeGame() {
addKeyListener(this);
setBackground(Color.black);
setFocusable(true);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
initGame();
}
private void initGame() {
dots = 3;
for (int i = 0; i < dots; i++) {
x[i] = 50 - i * DOT_SIZE;
y[i] = 50;
}
generateApple();
if (animator == null) {
animator = new Thread(this);
animator.start();
}
}
private void generateApple() {
Random r = new Random();
apple_x = r.nextInt(RAND_POS) * DOT_SIZE;
apple_y = r.nextInt(RAND_POS) * DOT_SIZE;
}
private void checkApple() {
if (x[0] == apple_x && y[0] == apple_y) {
dots++;
generateApple();
}
}
private void move() {
for (int i = dots; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
if (leftDirection) {
x[0] -= DOT_SIZE;
}
if (rightDirection) {
x[0] += DOT_SIZE;
}
if (upDirection) {
y[0] -= DOT_SIZE;
}
if (downDirection) {
y[0] += DOT_SIZE;
}
}
private void checkCollision() {
for (int i = dots; i > 0; i--) {
if (i > 4 && x[0] == x[i] && y[0] == y[i]) {
inGame = false;
}
}
if (y[0] >= HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] >= WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
if (!inGame) {
animator = null;
}
}
private void doDrawing(Graphics g) {
if (inGame) {
g.setColor(Color.red);
g.fillOval(apple_x, apple_y, DOT_SIZE, DOT_SIZE);
for (int i = 0; i < dots; i++) {
if (i == 0) {
g.setColor(Color.green);
g.fillRect(x[i], y[i], DOT_SIZE, DOT_SIZE);
} else {
g.setColor(Color.yellow);
g.fillRect(x[i], y[i], DOT_SIZE, DOT_SIZE);
}
}
} else {
gameOver(g);
}
}
private void gameOver(Graphics g) {
String msg = "Game Over";
g.setColor(Color.white);
g.drawString(msg, (WIDTH - g.getFontMetrics().stringWidth(msg)) / 2, HEIGHT / 2);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
@Override
public void run() {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
while (animator != null) {
repaint();
move();
checkApple();
checkCollision();
timeDiff = System.currentTimeMillis() - beforeTime;
sleep = DELAY - timeDiff;
if (sleep < 0) {
sleep = 2;
}
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
System.out.println("Interrupted: " + e.getMessage());
}
beforeTime = System.currentTimeMillis();
}
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT && !rightDirection) {
leftDirection = true;
upDirection = false;
downDirection = false;
}
if (key == KeyEvent.VK_RIGHT && !leftDirection) {
rightDirection = true;
upDirection = false;
downDirection = false;
}
if (key == KeyEvent.VK_UP && !downDirection) {
upDirection = true;
rightDirection = false;
leftDirection = false;
}
if (key == KeyEvent.VK_DOWN && !upDirection) {
downDirection = true;
rightDirection = false;
leftDirection = false;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SnakeGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
这个代码实现了一个简单的贪吃蛇游戏,使用了JFrame和JPanel等Swing组件。你可以在Eclipse中创建一个新的Java项目,并将这个代码复制到一个Java文件中,然后运行它。