用java写一个贪吃蛇游戏
时间: 2024-05-01 09:17:07 浏览: 13
很抱歉,作为AI语言模型,我无法编写和运行代码。以下是一个可能的Java贪吃蛇游戏的代码示例,供参考:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Snake extends JFrame implements ActionListener {
private final int WIDTH = 300; // 窗口宽度
private final int HEIGHT = 300; // 窗口高度
private final int DOT_SIZE = 10; // 蛇身大小
private final int ALL_DOTS = 900; // 蛇身总数
private final int RAND_POS = 29; // 随机产生食物的格子数
private final int DELAY = 140; // 蛇移动速度
private final int x[] = new int[ALL_DOTS]; // 蛇身横坐标
private final int y[] = new int[ALL_DOTS]; // 蛇身纵坐标
private int dots; // 蛇身长度
private int apple_x; // 食物横坐标
private int apple_y; // 食物纵坐标
private boolean leftDirection = false; // 左方向标志
private boolean rightDirection = true; // 右方向标志
private boolean upDirection = false; // 上方向标志
private boolean downDirection = false; // 下方向标志
private boolean inGame = true; // 游戏是否结束标志
private Timer timer; // 蛇移动定时器
public Snake() {
initGame(); // 初始化游戏
addKeyListener(new TAdapter()); // 添加键盘监听器
setTitle("贪吃蛇"); // 设置标题
setSize(WIDTH, HEIGHT); // 设置窗口大小
setResizable(false); // 禁止调整窗口大小
setLocationRelativeTo(null); // 将窗口置于屏幕中央
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口关闭操作
}
private void initGame() {
dots = 3; // 初始蛇身长度为3
for (int i = 0; i < dots; i++) {
x[i] = 50 - i * DOT_SIZE; // 初始蛇身为横坐标50,纵坐标为50、60、70
y[i] = 50;
}
locateApple(); // 随机放置食物
timer = new Timer(DELAY, this); // 创建蛇移动定时器
timer.start(); // 启动蛇移动定时器
}
private void locateApple() {
int r = (int) (Math.random() * RAND_POS); // 随机产生食物的行列号
apple_x = r * DOT_SIZE; // 食物横坐标为r * 10
r = (int) (Math.random() * RAND_POS);
apple_y = r * DOT_SIZE; // 食物纵坐标为r * 10
}
private void checkApple() {
if (x[0] == apple_x && y[0] == apple_y) { // 如果蛇头碰到食物
dots++; // 蛇身长度加1
locateApple(); // 再次随机放置食物
}
}
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 (x[0] >= WIDTH || x[0] < 0 || y[0] >= HEIGHT || y[0] < 0) { // 如果蛇头碰到边缘
inGame = false; // 游戏结束
}
if (!inGame) {
timer.stop(); // 停止蛇移动定时器
}
}
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 doDrawing(Graphics g) {
if (inGame) { // 如果游戏没有结束
g.setColor(Color.red); // 画食物
g.fillOval(apple_x, apple_y, DOT_SIZE, DOT_SIZE);
for (int i = 0; i < dots; i++) { // 画蛇身
g.setColor(Color.black);
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) && (!rightDirection)) { // 如果按下左方向键且当前不向右
leftDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) { // 如果按下右方向键且当前不向左
rightDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_UP) && (!downDirection)) { // 如果按下上方向键且当前不向下
upDirection = true;
leftDirection = false;
rightDirection = false;
}
if ((key == KeyEvent.VK_DOWN) && (!upDirection)) { // 如果按下下方向键且当前不向上
downDirection = true;
leftDirection = false;
rightDirection = false;
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Snake ex = new Snake();
ex.setVisible(true); // 显示窗口
});
}
}
```