写一个贪吃蛇,java
时间: 2024-01-05 20:03:35 浏览: 28
好的,下面是一个简单的贪吃蛇的 Java 实现:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class SnakeGame extends JFrame implements ActionListener {
private final int WIDTH = 800;
private final int HEIGHT = 600;
private final int DOT_SIZE = 10; // 每个格子的大小
private final int MAX_DOTS = WIDTH * HEIGHT / DOT_SIZE / DOT_SIZE;
private final int RAND_POS = WIDTH / DOT_SIZE; // 随机生成食物和蛇头的位置范围
private final int DELAY = 140; // 控制蛇移动速度的延迟时间
private Timer timer;
private boolean inGame = true;
private int[] x = new int[MAX_DOTS];
private int[] y = new int[MAX_DOTS];
private int dots;
private int appleX;
private int appleY;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
public SnakeGame() {
initGame();
}
private void initGame() {
setTitle("贪吃蛇");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setResizable(false);
setLocationRelativeTo(null);
initSnake();
newApple();
timer = new Timer(DELAY, this);
timer.start();
addKeyListener(new TAdapter());
setFocusable(true);
}
private void initSnake() {
dots = 3;
for (int i = 0; i < dots; i++) {
x[i] = 50 - i * DOT_SIZE;
y[i] = 50;
}
}
private void newApple() {
int r = (int) (Math.random() * RAND_POS);
appleX = r * DOT_SIZE;
r = (int) (Math.random() * RAND_POS);
appleY = r * DOT_SIZE;
}
private void checkApple() {
if (x[0] == appleX && y[0] == appleY) {
dots++;
newApple();
}
}
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;
break;
}
}
if (y[0] >= HEIGHT || y[0] < 0 || x[0] >= WIDTH || x[0] < 0) {
inGame = false;
}
if (!inGame) {
timer.stop();
}
}
private void doDrawing(Graphics g) {
if (inGame) {
g.setColor(Color.red);
g.fillOval(appleX, appleY, DOT_SIZE, DOT_SIZE);
for (int i = 0; i < dots; i++) {
if (i == 0) {
g.setColor(Color.green);
} else {
g.setColor(Color.blue);
}
g.fillRect(x[i], y[i], DOT_SIZE, DOT_SIZE);
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
private void gameOver(Graphics g) {
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.black);
g.setFont(small);
g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2);
}
@Override
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
private class TAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT || key == KeyEvent.VK_A) && !rightDirection) {
leftDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_D) && !leftDirection) {
rightDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_UP || key == KeyEvent.VK_W) && !downDirection) {
upDirection = true;
leftDirection = false;
rightDirection = false;
}
if ((key == KeyEvent.VK_DOWN || key == KeyEvent.VK_S) && !upDirection) {
downDirection = true;
leftDirection = false;
rightDirection = false;
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
SnakeGame game = new SnakeGame();
game.setVisible(true);
});
}
}
```
这个实现使用了 Java Swing 框架来绘制游戏界面,使用了定时器来控制蛇的移动。你可以将代码复制到一个 Java 项目中运行。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)