用eclipse写贪吃蛇游戏的代码
时间: 2024-03-23 22:43:17 浏览: 111
以下是使用 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 Snake extends JPanel implements KeyListener, Runnable {
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 = (WIDTH * HEIGHT) / (DOT_SIZE * DOT_SIZE);
private static final int RAND_POS = WIDTH / DOT_SIZE;
private static final int DELAY = 140;
private LinkedList<Point> snake;
private Point fruit;
private boolean left = false;
private boolean right = true;
private boolean up = false;
private boolean down = false;
private boolean inGame = true;
private Thread game;
public Snake() {
snake = new LinkedList<Point>();
addKeyListener(this);
setFocusable(true);
setBackground(Color.BLACK);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
initGame();
}
public void initGame() {
snake.clear();
snake.add(new Point(START_X, START_Y));
generateFruit();
left = false;
right = true;
up = false;
down = false;
inGame = true;
game = new Thread(this);
game.start();
}
public void generateFruit() {
Random rand = new Random();
int x = rand.nextInt(RAND_POS) * DOT_SIZE;
int y = rand.nextInt(RAND_POS) * DOT_SIZE;
fruit = new Point(x, y);
}
public void move() {
Point head = snake.peekFirst();
Point newHead = head;
if (left) {
newHead = new Point(head.x - DOT_SIZE, head.y);
}
if (right) {
newHead = new Point(head.x + DOT_SIZE, head.y);
}
if (up) {
newHead = new Point(head.x, head.y - DOT_SIZE);
}
if (down) {
newHead = new Point(head.x, head.y + DOT_SIZE);
}
if (newHead.equals(fruit)) {
snake.addFirst(newHead);
generateFruit();
} else if (newHead.x < 0 || newHead.x > WIDTH - DOT_SIZE || newHead.y < 0 || newHead.y > HEIGHT - DOT_SIZE
|| snake.contains(newHead)) {
inGame = false;
} else {
snake.removeLast();
snake.addFirst(newHead);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (inGame) {
g.setColor(Color.RED);
g.fillRect(fruit.x, fruit.y, DOT_SIZE, DOT_SIZE);
g.setColor(Color.GREEN);
for (Point p : snake) {
g.fillRect(p.x, p.y, DOT_SIZE, DOT_SIZE);
}
} else {
g.setColor(Color.WHITE);
g.drawString("Game Over", 215, 240);
}
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!right)) {
left = true;
up = false;
down = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!left)) {
right = true;
up = false;
down = false;
}
if ((key == KeyEvent.VK_UP) && (!down)) {
up = true;
right = false;
left = false;
}
if ((key == KeyEvent.VK_DOWN) && (!up)) {
down = true;
right = false;
left = false;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void run() {
while (inGame) {
move();
repaint();
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.getContentPane().add(new Snake());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
这个代码包括游戏的核心逻辑和界面的绘制,可以在 Eclipse 中直接运行。其中,贪吃蛇的身体由链表 `LinkedList<Point>` 维护,蛇头和食物的位置由 `Point` 类表示。在 `move()` 方法中,根据当前蛇头的位置和方向计算出新的蛇头位置,并判断游戏是否结束。在 `paintComponent()` 方法中,根据游戏是否结束绘制相应的界面元素。在 `keyPressed()` 方法中,处理玩家的按键输入,更新蛇的移动方向。 `run()` 方法中,通过不断调用 `move()` 和 `repaint()` 方法来更新游戏状态和界面。最后,在 `main()` 方法中创建一个 JFrame 窗口,并将 Snake 对象添加到窗口中。
阅读全文