贪吃蛇小游戏的java代码(代码逐行注释)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class SnakeGame extends JPanel implements KeyListener, ActionListener {

    // 游戏区域的宽度和高度
    private final int BOARD_WIDTH = 600;
    private final int BOARD_HEIGHT = 600;
    // 每个单元格的大小
    private final int UNIT_SIZE = 25;
    // 游戏区域的单元格数
    private final int GAME_UNITS = (BOARD_WIDTH * BOARD_HEIGHT) / (UNIT_SIZE * UNIT_SIZE);
    // 游戏的延迟时间,决定了游戏的速度
    private final int DELAY = 75;
    // 蛇的身体部分数量
    private int bodyParts = 6;
    // 苹果的坐标
    private int appleX;
    private int appleY;
    // 蛇每个部分的坐标
    private final int[] x = new int[GAME_UNITS];
    private final int[] y = new int[GAME_UNITS];
    // 蛇的运动方向
    private char direction = 'R'; // 初始方向为右
    // 游戏是否正在运行
    private boolean running = false;
    // 定时器,用于控制游戏速度
    private Timer timer;

    // 构造方法,初始化游戏界面并开始游戏
    public SnakeGame() {
        this.setPreferredSize(new Dimension(BOARD_WIDTH, BOARD_HEIGHT));
        this.setBackground(Color.black);
        this.setFocusable(true);
        this.addKeyListener(this);
        startGame();
    }

    // 启动游戏
    public void startGame() {
        newApple(); // 生成苹果
        running = true; // 设置游戏状态为运行
        timer = new Timer(DELAY, this); // 创建定时器,控制游戏速度
        timer.start(); // 启动定时器
    }

    // 绘制游戏界面
    public void paintComponent(Graphics g) {
        super.paintCompon

  • 35
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴回答你的问题。以下是一个简单的贪吃蛇小游戏JAVA代码实现: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SnakeGame extends JPanel 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 int x[] = new int[ALL_DOTS]; private 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; private Image ball; private Image apple; private Image head; public SnakeGame() { addKeyListener(new TAdapter()); setBackground(Color.black); setPreferredSize(new Dimension(WIDTH, HEIGHT)); ImageIcon iid = new ImageIcon(this.getClass().getResource("dot.png")); ball = iid.getImage(); ImageIcon iia = new ImageIcon(this.getClass().getResource("apple.png")); apple = iia.getImage(); ImageIcon iih = new ImageIcon(this.getClass().getResource("head.png")); head = iih.getImage(); initGame(); } public void initGame() { dots = 3; for (int z = ; z < dots; z++) { x[z] = 50 - z * 10; y[z] = 50; } locateApple(); timer = new Timer(DELAY, this); timer.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); doDrawing(g); } public void doDrawing(Graphics g) { if (inGame) { g.drawImage(apple, apple_x, apple_y, this); for (int z = ; z < dots; z++) { if (z == ) { g.drawImage(head, x[z], y[z], this); } else { g.drawImage(ball, x[z], y[z], this); } } Toolkit.getDefaultToolkit().sync(); } else { gameOver(g); } } public void gameOver(Graphics g) { String msg = "Game Over"; Font small = new Font("Helvetica", Font.BOLD, 14); FontMetrics metr = getFontMetrics(small); g.setColor(Color.white); g.setFont(small); g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2); } public void checkApple() { if ((x[] == apple_x) && (y[] == apple_y)) { dots++; locateApple(); } } public void move() { for (int z = dots; z > ; z--) { x[z] = x[(z - 1)]; y[z] = y[(z - 1)]; } if (leftDirection) { x[] -= DOT_SIZE; } if (rightDirection) { x[] += DOT_SIZE; } if (upDirection) { y[] -= DOT_SIZE; } if (downDirection) { y[] += DOT_SIZE; } } public void checkCollision() { for (int z = dots; z > ; z--) { if ((z > 4) && (x[] == x[z]) && (y[] == y[z])) { inGame = false; } } if (y[] >= HEIGHT) { inGame = false; } if (y[] < ) { inGame = false; } if (x[] >= WIDTH) { inGame = false; } if (x[] < ) { inGame = false; } if (!inGame) { timer.stop(); } } public void locateApple() { int r = (int) (Math.random() * RAND_POS); apple_x = ((r * DOT_SIZE)); r = (int) (Math.random() * RAND_POS); apple_y = ((r * DOT_SIZE)); } public void actionPerformed(ActionEvent e) { if (inGame) { checkApple(); checkCollision(); move(); } repaint(); } private class TAdapter extends KeyAdapter { 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; rightDirection = false; leftDirection = false; } if ((key == KeyEvent.VK_DOWN) && (!upDirection)) { downDirection = true; rightDirection = false; leftDirection = false; } } } } ``` 希望这个代码能够帮助到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值