贪吃蛇java1000行
时间: 2023-10-11 18:28:09 浏览: 33
以下是一个简单的贪吃蛇Java代码示例,它包含了不到1000行的代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JFrame implements ActionListener {
private final int BOARD_WIDTH = 400;
private final int BOARD_HEIGHT = 400;
private final int UNIT_SIZE = 10;
private final int GAME_UNITS = (BOARD_WIDTH * BOARD_HEIGHT) / UNIT_SIZE;
private final int DELAY = 75;
private final int x[] = new int[GAME_UNITS];
private final int y[] = new int[GAME_UNITS];
private int bodyParts = 6;
private int applesEaten = 0;
private int appleX;
private int appleY;
private Direction direction = Direction.RIGHT;
private boolean running = false;
private Timer timer;
public SnakeGame() {
setTitle("Snake Game");
setSize(BOARD_WIDTH, BOARD_HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
addKeyListener(new MyKeyAdapter());
startGame();
}
public void startGame() {
newApple();
running = true;
timer = new Timer(DELAY, this);
timer.start();
}
public void paint(Graphics g) {
// 绘制游戏面板
g.setColor(Color.black);
g.fillRect(0, 0, BOARD_WIDTH, BOARD_HEIGHT);
// 绘制苹果
g.setColor(Color.red);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
// 绘制蛇
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(Color.green);
} else {
g.setColor(Color.yellow);
}
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
// 绘制得分
g.setColor(Color.white);
g.drawString("Score: " + applesEaten, 10, 20);
}
public void newApple() {
appleX = (int) (Math.random() * (BOARD_WIDTH / UNIT_SIZE)) * UNIT_SIZE;
appleY = (int) (Math.random() * (BOARD_HEIGHT / UNIT_SIZE)) * UNIT_SIZE;
}
public void move() {
for (int i = bodyParts; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
switch (direction) {
case UP:
y[0] = y[0] - UNIT_SIZE;
break;
case DOWN:
y[0] = y[0] + UNIT_SIZE;
break;
case LEFT:
x[0] = x[0] - UNIT_SIZE;
break;
case RIGHT:
x[0] = x[0] + UNIT_SIZE;
break;
}
}
public void checkApple() {
if ((x[0] == appleX) && (y[0] == appleY)) {
bodyParts++;
applesEaten++;
newApple();
}
}
public void checkCollisions() {
// 检查是否撞到墙壁
if (x[0] < 0 || x[0] >= BOARD_WIDTH || y[0] < 0 || y[0] >= BOARD_HEIGHT) {
running = false;
timer.stop();
}
// 检查是否撞到自己的身体
for (int i = bodyParts; i > 0; i--) {
if ((x[0] == x[i]) && (y[0] == y[i])) {
running = false;
timer.stop();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
move();
checkApple();
checkCollisions();
}
repaint();
}
public class MyKeyAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if (direction != Direction.RIGHT) {
direction = Direction.LEFT;
}
break;
case KeyEvent.VK_RIGHT:
if (direction != Direction.LEFT) {
direction = Direction.RIGHT;
}
break;
case KeyEvent.VK_UP:
if (direction != Direction.DOWN) {
direction = Direction.UP;
}
break;
case KeyEvent.VK_DOWN:
if (direction != Direction.UP) {
direction = Direction.DOWN;
}
break;
}
}
}
public static void main(String[] args) {
new SnakeGame().setVisible(true);
}
}
enum Direction {
UP,
DOWN,
LEFT,
RIGHT
}
```
这个示例中,我们使用Java Swing库来实现游戏界面和用户输入的处理。我们使用一个定时器来处理游戏的逻辑和动画。游戏包含一个蛇,一个苹果,以及若干个障碍物,玩家需要控制蛇吃掉苹果并避免与障碍物相撞。游戏使用键盘上的箭头键来控制蛇的方向。在游戏中,玩家的得分将会随着吃掉苹果的数量而增加。