java 贪吃蛇

1 先定义好方向的枚举

public enum Dir {
    L,R,U,D
}

2 定义贪吃蛇活动的范围

复制代码
public class Yard extends Frame {

    //
    public static final int NodeSize = 15;
    //
    public static final int NodeCount = 30;
    // 蛇活动的范围
    public static final int AreaSize = NodeSize * NodeCount;

    public static int x = AreaSize / 2;
    public static int y = AreaSize / 2;

    Egg egg = new Egg(10, 10);
    Snake snake = new Snake();

    Yard() {
        this.setSize(2 * AreaSize, 2 * AreaSize);
        this.setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }

        });
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                snake.keyPressed(e);
            }
        });
        
        // 窗口重画
        while (true) {
            try {
                // 50毫秒重画一次
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.repaint();
        }

    }

    
    @Override
    public void paint(Graphics graphics) {
        Color color = graphics.getColor();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, this.getWidth(), this.getHeight());
        graphics.setColor(color);

        for (int i = 0; i <= NodeCount; i++) {
            graphics.drawLine(x, y + NodeSize * i, x + AreaSize, y + NodeSize * i);
            graphics.drawLine(x + NodeSize * i, y, x + NodeSize * i, x + AreaSize);
        }

        egg.paint(graphics);
        snake.paint(graphics);
        snake.eat(egg);
    }


    // 游戏画面双缓冲
    Image offScreenImage;
    @Override
    public void update(Graphics graphics) {
        if (null == offScreenImage) {
            offScreenImage = this.createImage(this.getWidth(), this.getHeight());
        }
        Graphics gps = offScreenImage.getGraphics();
        paint(gps);
        graphics.drawImage(offScreenImage, 0, 0,null);
    }
    

    public static void main(String[] args) {
        new Yard();
    }
}
复制代码

  3 定义贪吃蛇

复制代码
public class Snake {

    // 头 尾巴
    Node head, tail;

    // 初始方向
    Dir dir = Dir.L;

    Snake() {
        head = new Node(20,20);
        tail = head;
    }

    public void  addToHead() {
        Node node = null;
        switch (dir) {
            case L:
                node = new Node(head.row, head.col - 1);
                break;
            case R:
                node = new Node(head.row, head.col + 1);
                break;
            case U:
                node = new Node(head.row - 1, head.col);
                break;
            case D:
                node = new Node(head.row + 1, head.col);
                break;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }

    private void boundaryCheck() {
        if (head.row < 0) {
            head.row = Yard.NodeCount - 1;
        }
        if (head.col < 0) {
            head.col = Yard.NodeCount - 1;
        }
        if (head.row > Yard.NodeCount - 1) {
            head.row = 0;
        }
        if (head.col > Yard.NodeCount - 1) {
            head.col = 0;
        }
    }
    

    private void deleteTail() {
        if (null == tail) {
            return;
        }
        tail = tail.prev;
        tail.next.prev = null;
        tail.next = null;
    }


    public void paint(Graphics graphics) {
        Node node = head;
        while (node != null) {
            node.paint(graphics);
            node = node.next;
        }
        
        move();
    }

    private void move() {
        addToHead();
        deleteTail();
        boundaryCheck();
    }

    public void keyPressed(KeyEvent keyEvent) {
        int key = keyEvent.getKeyCode();
        switch (key) {
            case KeyEvent.VK_LEFT:
                dir = Dir.L;
                break;
            case KeyEvent.VK_RIGHT:
                dir = Dir.R;
                break;
            case KeyEvent.VK_UP:
                dir = Dir.U;
                break;
            case KeyEvent.VK_DOWN:
                dir = Dir.D;
                break;
        }
    }

    public void eat(Egg egg) {
        if (head.row == egg.row && head.col == egg.col) {
            addToHead();
            egg.reAppear();
        }
    }
}
复制代码

4 定义蛇的节点

复制代码
public class Node {

    // 行,列
    int row, col;

    Node prev, next;

    public Node(int row, int col) {
        this.row = row;
        this.col = col;
    }

    public void paint(Graphics graphics) {
        int x = Yard.x + col * Yard.NodeSize;
        int y = Yard.y + row * Yard.NodeSize;

        Color color = graphics.getColor();
        graphics.setColor(Color.BLACK);
        graphics.fillRect(x, y, Yard.NodeSize, Yard.NodeSize);
        graphics.setColor(color);
    }
}
复制代码

5 定义蛇吃的节点

复制代码
public class Egg {
    int row, col;

    Random random = new Random();

    public Egg(int row, int col) {
        this.row = row;
        this.col = col;
    }

    public void paint(Graphics graphics) {
        int x = Yard.x + col * Yard.NodeSize;
        int y = Yard.y + row * Yard.NodeSize;

        Color color = graphics.getColor();
        graphics.setColor(Color.RED);
        graphics.fillOval(x, y, Yard.NodeSize, Yard.NodeSize);
        graphics.setColor(color);
    }


    public void reAppear() {
        this.row = random.nextInt(Yard.NodeCount);
        this.col = random.nextInt(Yard.NodeCount);
    }
}
复制代码

简单版不死贪吃蛇就完成了

 

posted on   胡子就不刮  阅读(60)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2017-04-25 Docker

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示