java贪吃蛇代码
时间: 2023-07-03 09:27:41 浏览: 45
以下是一个简单的 Java 贪吃蛇游戏的代码,仅供参考:
```java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SnakeGame extends JPanel implements ActionListener, KeyListener {
private static final long serialVersionUID = 1L;
public static final int SCALE = 10; // 每个方格的大小
public static final int WIDTH = 50; // 游戏界面的宽度,单位为方格
public static final int HEIGHT = 50; // 游戏界面的高度,单位为方格
public static final int SPEED = 100; // 蛇移动的速度,单位为毫秒
private ArrayList<Point> snake; // 蛇的身体
private Point food; // 食物
private int direction; // 蛇的方向:0-上,1-右,2-下,3-左
private boolean gameOver; // 游戏是否结束
public SnakeGame() {
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
snake = new ArrayList<Point>();
snake.add(new Point(10, 10));
snake.add(new Point(10, 11));
snake.add(new Point(10, 12));
spawnFood();
direction = 2;
gameOver = false;
Timer timer = new Timer(SPEED, this);
timer.start();
}
public void spawnFood() {
Random random = new Random();
int x = random.nextInt(WIDTH);
int y = random.nextInt(HEIGHT);
food = new Point(x, y);
}
public void move() {
Point head = snake.get(0);
Point newHead = null;
switch (direction) {
case 0:
newHead = new Point(head.x, head.y - 1);
break;
case 1:
newHead = new Point(head.x + 1, head.y);
break;
case 2:
newHead = new Point(head.x, head.y + 1);
break;
case 3:
newHead = new Point(head.x - 1, head.y);
break;
}
if (newHead.x < 0 || newHead.x >= WIDTH || newHead.y < 0 || newHead.y >= HEIGHT) {
gameOver = true;
return;
}
if (snake.contains(newHead)) {
gameOver = true;
return;
}
snake.add(0, newHead);
if (newHead.equals(food)) {
spawnFood();
} else {
snake.remove(snake.size() - 1);
}
}
public void paint(Graphics g) {
super.paint(g);
for (Point p : snake) {
g.setColor(Color.WHITE);
g.fillRect(p.x * SCALE, p.y * SCALE, SCALE, SCALE);
}
g.setColor(Color.RED);
g.fillRect(food.x * SCALE, food.y * SCALE, SCALE, SCALE);
if (gameOver) {
g.setColor(Color.WHITE);
g.drawString("Game Over", WIDTH * SCALE / 2 - 30, HEIGHT * SCALE / 2);
}
}
public void actionPerformed(ActionEvent e) {
move();
repaint();
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP && direction != 2) {
direction = 0;
} else if (key == KeyEvent.VK_RIGHT && direction != 3) {
direction = 1;
} else if (key == KeyEvent.VK_DOWN && direction != 0) {
direction = 2;
} else if (key == KeyEvent.VK_LEFT && direction != 1) {
direction = 3;
}
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new SnakeGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```