标题:贪吃蛇
只看楼主
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
结帖率:100%
已结贴  问题点数:100 回复次数:18 
贪吃蛇
看了渐渐鱼同学的贪吃蛇,调试时候居然写不由自主准备写console.log自己都发笑
代码有点多,感觉还行凑活着看吧。
发完继续刷剧咯
程序代码:
package com.huawei.game.snake;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.stream.Collectors;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Test2 extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static final int DEFAULT_WIDTH = 400;
    private static final int DEFAULT_HEIGHT = 400;
    private static final int UP = 0;
    private static final int LEFT = 1;
    private static final int DOWN = 2;
    private static final int RIGHT = 3;
    private static JFrame jframe = new JFrame("Snake");
    private static int dir;
    private static LinkedList<NodeObj> snake = new LinkedList<>();
    private static NodeObj apple;
    private Random rand = new Random();
    static class NodeObj {
        int x;
        int y;
        int width = 20;
        int height = 20;
        public NodeObj(int x, int y) {
            this.x = x;
            this.y = y;
        }
        public void moveUp() {
            y -= height;
            if(y < 0) {
                y = DEFAULT_HEIGHT - height;
            }
        }
        public void moveDown() {
            y += height;
            if(y + height > DEFAULT_HEIGHT) {
                y = 0;
            }
        }
        public void moveLeft() {
            x -= width;
            if(x < 0) {
                x = DEFAULT_WIDTH-width;
            }
        }
        public void moveRight() {
            x += width;
            if(x + width > DEFAULT_WIDTH) {
                x = 0;
            }
        }
    }
    public Test2() {
        NodeObj head = new NodeObj(DEFAULT_WIDTH/2, DEFAULT_HEIGHT/2);
        snake.add(head);
        apple = new NodeObj(DEFAULT_WIDTH/4, DEFAULT_HEIGHT/2);
        jframe.setAlwaysOnTop(true);
        jframe.setSize(DEFAULT_WIDTH + 20, DEFAULT_HEIGHT + 40);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setVisible(true);
        jframe.setResizable(false);
        jframe.add(this);
        jframe.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch(e.getKeyCode()) {
                case KeyEvent.VK_LEFT:
                    if(dir != RIGHT)
                        dir = LEFT;
                    break;
                case KeyEvent.VK_RIGHT:
                    if(dir != LEFT)
                        dir = RIGHT;
                    break;
                case KeyEvent.VK_DOWN:
                    if(dir != UP)
                        dir = DOWN;
                    break;
                case KeyEvent.VK_UP:
                    if(dir != DOWN)
                        dir = UP;
                    break;
                }
            }
        });
        new Timer().schedule(new TimerTask() {
            
            @Override
            public void run() {
                moveAction();
                checkEatAction();
                checkOver();
                repaint();
            }
        }, 10, 10);
    }
    protected void checkOver() {
        Iterator<NodeObj> iter = snake.iterator();
        NodeObj head = iter.next();
        while(iter.hasNext()) {
            NodeObj s = iter.next();
            if(s.x == head.x && s.y == head.y) {
                JOptionPane.showMessageDialog(null,"Game Over");
                System.exit(0);
            }
        }
    }
    protected void checkEatAction() {
        NodeObj head = snake.peek();
        if(apple.x == head.x && apple.y == head.y) {
            int x = head.x;
            int y = head.y;
            switch (dir) {
            case UP:
                y -= head.height;
                break;
            case DOWN:
                y += head.height;
                break;
            case LEFT:
                x -= head.width;
                break;
            case RIGHT:
                x += head.width;
                break;
            }
            NodeObj newNode = new NodeObj(x, y);
            snake.push(newNode);
            x = rand.nextInt(DEFAULT_WIDTH);
            apple.x = x - x % apple.width;
            y = rand.nextInt(DEFAULT_HEIGHT);
            apple.y = y - y % apple.height;
        }
    }
    long moveIndex = 0;
    protected void moveAction() {
        moveIndex++;
        if(moveIndex % 20 == 0) {
            // move snake
            moveBody(snake);
            moveHead(snake.peek());
        }
    }
    
    private void moveHead(NodeObj head) {
        switch (dir) {
        case UP:
            if(dir != DOWN)
                head.moveUp();
            break;
        case DOWN:
            if(dir != UP)
                head.moveDown();
            break;
        case LEFT:
            if(dir != RIGHT)
                head.moveLeft();
            break;
        case RIGHT:
            if(dir != LEFT)
                head.moveRight();
            break;
        }
    }
    private void moveBody(LinkedList<NodeObj> nodes) {
        List<NodeObj> list = nodes.stream()
                .sorted((a,b)->-1).collect(Collectors.toList());
        Iterator<NodeObj> iter = list.listIterator();
        int i = nodes.size() - 1;
        for(iter.next(); iter.hasNext(); i--) {
            NodeObj cur = iter.next();
            nodes.get(i).x = cur.x;
            nodes.get(i).y = cur.y;
        }
    }
    @Override
    public void paint(Graphics g) {
        paintArea(g);
        paintObjs(g);
    }
    
    private void paintObjs(Graphics g) {
        g.setColor(Color.BLACK);
        boolean isFirst = true;
        for(NodeObj s : snake) {
            if(isFirst) {
                g.fillArc(s.x, s.y, s.width, s.height, dir*90 + 135, 270);
            } else
                g.fillRect(s.x, s.y, s.width - 1, s.height - 1);
            isFirst = false;
        }
        g.setColor(Color.GREEN);
        g.fillOval(apple.x, apple.y, apple.width, apple.height);
    }
    
    private void paintArea(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }
    
    public static void main(String[] args) {
        new Test2();
    }
}
搜索更多相关主题的帖子: import private int void head 
2018-10-05 21:14
幻紫灵心
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山咔咔里面
等 级:贵宾
威 望:53
帖 子:395
专家分:2640
注 册:2018-3-30
得分:50 
ε=(′ο`*)))100分哎

saber,别哭.
2018-10-07 20:03
悟空丨
Rank: 2
等 级:论坛游民
帖 子:18
专家分:50
注 册:2018-10-6
得分:50 
这种小游戏有毒,玩了一下午~~~
2018-10-07 21:03
幻紫灵心
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山咔咔里面
等 级:贵宾
威 望:53
帖 子:395
专家分:2640
注 册:2018-3-30
得分:0 
这个画蛇头有点意思...

saber,别哭.
2018-10-07 21:42
zhouzj
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2018-10-27
得分:0 
楼主,为什么把default_width和height大小改变,贪吃蛇吃不到果子呢?新手求助
2018-10-27 14:11
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
得分:0 
吃不吃的到用另外一个词汇描述叫碰撞检测,我这里的碰撞检测是直接判断坐标相等,比较简单
常规的做法是判断两个对象有无重叠区域。这个视形状而论了
你改后的值应该不是20的倍数了吧,恩,这个感兴趣可以优化一下。

剑栈风樯各苦辛,别时冰雪到时春
2018-10-27 22:02
幻紫灵心
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山咔咔里面
等 级:贵宾
威 望:53
帖 子:395
专家分:2640
注 册:2018-3-30
得分:0 
回复 6楼 林月儿
好奇问下,写这个贪吃蛇用了多久?

saber,别哭.
2018-10-29 20:01
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
得分:0 
半个小时吧,编译器有快捷键的

剑栈风樯各苦辛,别时冰雪到时春
2018-10-29 22:00
陈无
Rank: 2
等 级:论坛游民
帖 子:29
专家分:35
注 册:2018-10-10
得分:0 
厉害,你让我们望尘莫及,让我又有了方向,那就是好好玩这个游戏😂,待我改天设计出和升级版的
2018-10-30 19:01
tbsuluul123
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-11-14
得分:0 
2018-11-28 05:37



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-490186-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.074712 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved