用Java写一个自动的贪吃蛇游戏
时间: 2023-09-07 18:11:28 浏览: 32
好的,以下是一个简单的Java自动贪吃蛇游戏示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AutoSnakeGame extends JFrame implements KeyListener, ActionListener {
private static final long serialVersionUID = 1L;
private final int WIDTH = 500, HEIGHT = 500;
private final int UNIT_SIZE = 20;
private final int GAME_UNITS = (WIDTH * HEIGHT) / UNIT_SIZE;
private int[] x = new int[GAME_UNITS];
private int[] y = new int[GAME_UNITS];
private int bodyParts = 6;
private int applesEaten;
private int appleX;
private int appleY;
private char direction = 'R';
private boolean running = false;
private Timer timer;
private Random random;
public AutoSnakeGame() {
setTitle("Auto Snake Game");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
random = new Random();
timer = new Timer(100, this);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
startGame();
}
public void startGame() {
newApple();
running = true;
timer.start();
}
public void paint(Graphics g) {
// Draw border
g.setColor(Color.gray);
g.drawRect(0, 0, WIDTH, HEIGHT);
// Draw apple
g.setColor(Color.red);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
// Draw snake
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(Color.green);
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
} else {
g.setColor(new Color(45, 180, 0));
g.fillRect(x[i], y[i], 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 'U':
y[0] = y[0] - UNIT_SIZE;
break;
case 'D':
y[0] = y[0] + UNIT_SIZE;
break;
case 'L':
x[0] = x[0] - UNIT_SIZE;
break;
case 'R':
x[0] = x[0] + UNIT_SIZE;
break;
}
}
public void checkApple() {
if (x[0] == appleX && y[0] == appleY) {
bodyParts++;
applesEaten++;
newApple();
}
}
public void checkCollisions() {
// Check if head collides with body
for (int i = bodyParts; i > 0; i--) {
if (x[0] == x[i] && y[0] == y[i]) {
running = false;
}
}
// Check if head touches left border
if (x[0] < 0) {
running = false;
}
// Check if head touches right border
if (x[0] > WIDTH) {
running = false;
}
// Check if head touches top border
if (y[0] < 0) {
running = false;
}
// Check if head touches bottom border
if (y[0] > HEIGHT) {
running = false;
}
if (!running) {
timer.stop();
}
}
public void newApple() {
appleX = random.nextInt((int) (WIDTH / UNIT_SIZE)) * UNIT_SIZE;
appleY = random.nextInt((int) (HEIGHT / UNIT_SIZE)) * UNIT_SIZE;
}
public void actionPerformed(ActionEvent e) {
if (running) {
move();
checkApple();
checkCollisions();
}
repaint();
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if (direction != 'R') {
direction = 'L';
}
break;
case KeyEvent.VK_RIGHT:
if (direction != 'L') {
direction = 'R';
}
break;
case KeyEvent.VK_UP:
if (direction != 'D') {
direction = 'U';
}
break;
case KeyEvent.VK_DOWN:
if (direction != 'U') {
direction = 'D';
}
break;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
new AutoSnakeGame();
}
}
```
这个自动贪吃蛇游戏可以使用方向键控制方向,可以自动吃苹果并增加身体长度。你可以根据需要进行修改和扩展。