贪吃蛇小游戏的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实现贪吃蛇游戏代码,仅供参考: ``` import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.LinkedList; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; public class Snake extends JPanel implements Runnable, KeyListener { private static final long serialVersionUID = 1L; private final int SIZE = 10; // 每个格子的大小 private final int ROWS = 50; // 行数 private final int COLS = 50; // 列数 private final int FPS = 5; // 刷新率 private Thread thread; private LinkedList<Point> snake; private int direction; // 1 上,2 下,3 左,4 右 private Point food; private Random random; private boolean running; private int score; public Snake() { snake = new LinkedList<>(); direction = 4; random = new Random(); food = new Point(random.nextInt(COLS), random.nextInt(ROWS)); running = true; score = 0; snake.add(new Point(COLS / 2, ROWS / 2)); thread = new Thread(this); thread.start(); JFrame frame = new JFrame("Snake"); frame.setSize(SIZE * COLS, SIZE * ROWS); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.addKeyListener(this); frame.add(this); frame.setVisible(true); } @Override public void run() { while (running) { try { Thread.sleep(1000 / FPS); } catch (InterruptedException e) { e.printStackTrace(); } tick(); repaint(); } } private void tick() { Point head = snake.getFirst(); Point next = new Point(head); switch (direction) { case 1: next.y--; break; case 2: next.y++; break; case 3: next.x--; break; case 4: next.x++; break; } if (next.x < 0 || next.x >= COLS || next.y < 0 || next.y >= ROWS) { running = false; // 撞墙了,游戏结束 return; } if (next.equals(food)) { // 吃到食物 score++; food.setLocation(random.nextInt(COLS), random.nextInt(ROWS)); } else { snake.removeLast(); // 没有吃到食物,尾部移除 } if (snake.contains(next)) { running = false; // 撞到自己,游戏结束 return; } snake.addFirst(next); // 头部添加 } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.WHITE); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.BLACK); for (Point p : snake) { g.fillRect(p.x * SIZE, p.y * SIZE, SIZE, SIZE); } g.setColor(Color.RED); g.fillRect(food.x * SIZE, food.y * SIZE, SIZE, SIZE); g.setColor(Color.BLACK); g.drawString("Score: " + score, 5, 15); if (!running) { g.drawString("Game Over!", getWidth() / 2 - 30, getHeight() / 2); } } @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_UP: if (direction != 2) { direction = 1; } break; case KeyEvent.VK_DOWN: if (direction != 1) { direction = 2; } break; case KeyEvent.VK_LEFT: if (direction != 4) { direction = 3; } break; case KeyEvent.VK_RIGHT: if (direction != 3) { direction = 4; } break; } } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } public static void main(String[] args) { new Snake(); } } ``` 这个代码实现的贪吃蛇游戏比较简单,游戏区域是一个50x50的矩形,每个格子的大小是10个像素。游戏开始时,蛇在中央,随机放置一个食物。玩家通过键盘控制蛇的移动方向,吃到食物得分,撞墙或者撞到自己游戏结束。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据智研

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值