JAVA 实现贪吃蛇小游戏的制作(附源码)(GUI)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
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;

public class SnakeGame extends JPanel implements KeyListener {

    private static final int CELL_SIZE = 20;
    private static final int WIDTH = 30;
    private static final int HEIGHT = 20;

    private int[] dx = { 0, 1, 0, -1 };
    private int[] dy = { -1, 0, 1, 0 };

    private int direction = 1;
    private ArrayList<int[]> snake = new ArrayList<>();
    private int[] food = new int[2];

    public SnakeGame() {
        setPreferredSize(new Dimension(WIDTH * CELL_SIZE, HEIGHT * CELL_SIZE));
        setFocusable(true);
        addKeyListener(this);

        initGame();
    }

    private void initGame() {
        snake.add(new int[] { WIDTH / 2, HEIGHT / 2 });
        generateFood();
    }

    private void generateFood() {
        Random random = new Random();
        food[0] = random.nextInt(WIDTH);
        food[1] = random.nextInt(HEIGHT);
    }

    private void move() {
        int[] head = new int[2];
        head[0] = snake.get(0)[0] + dx[direction];
        head[1] = snake.get(0)[1] + dy[direction];

        if (head[0] < 0 || head[0] >= WIDTH || head[1] < 0 || head[1] >= HEIGHT || checkCollision(head)) {
            System.out.println("游戏结束!");
            return;
        }

        if (head[0] == food[0] && head[1] == food[1]) {
            snake.add(0, head);
            generateFood();
        } else {
            for (int i = snake.size() - 1; i > 0; i--) {
                snake.set(i, snake.get(i - 1));
            }
            snake.set(0, head);
        }
    }

    private boolean checkCollision(int[] head) {
        for (int i = 1; i < snake.size(); i++) {
            if (head[0] == snake.get(i)[0] && head[1] == snake.get(i)[1]) {
                return true;
            }
        }
        return false;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.BLACK);
        for (int[] cell : snake) {
            g.fillRect(cell[0] * CELL_SIZE, cell[1] * CELL_SIZE, CELL_SIZE, CELL_SIZE);
        }

        g.setColor(Color.RED);
        g.fillRect(food[0] * CELL_SIZE, food[1] * CELL_SIZE, CELL_SIZE, CELL_SIZE);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_UP && direction!= 2) {
            direction = 0;
        } else if (keyCode == KeyEvent.VK_RIGHT && direction!= 3) {
            direction = 1;
        } else if (keyCode == KeyEvent.VK_DOWN && direction!= 0) {
            direction = 2;
        } else if (keyCode == KeyEvent.VK_LEFT && direction!= 1) {
            direction = 3;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("贪吃蛇游戏");
        SnakeGame game = new SnakeGame();
        frame.add(game);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        while (true) {
            game.move();
            game.repaint();

            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行截图:

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值