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 2021-04-25 19:02  胡子就不刮  阅读(79)  评论(0)    收藏  举报

导航