| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6558 人关注过本帖
标题:贪吃蛇
取消只看楼主 加入收藏
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
结帖率:100%
收藏
已结贴  问题点数:100 回复次数:4 
贪吃蛇
看了渐渐鱼同学的贪吃蛇,调试时候居然写不由自主准备写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: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
吃不吃的到用另外一个词汇描述叫碰撞检测,我这里的碰撞检测是直接判断坐标相等,比较简单
常规的做法是判断两个对象有无重叠区域。这个视形状而论了
你改后的值应该不是20的倍数了吧,恩,这个感兴趣可以优化一下。

剑栈风樯各苦辛,别时冰雪到时春
2018-10-27 22:02
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
半个小时吧,编译器有快捷键的

剑栈风樯各苦辛,别时冰雪到时春
2018-10-29 22:00
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
回复 10楼 tbsuluul123
thanks

剑栈风樯各苦辛,别时冰雪到时春
2018-11-28 23:30
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
回复 12楼 zfx187558656
なぜ

剑栈风樯各苦辛,别时冰雪到时春
2019-02-27 22:03
快速回复:贪吃蛇
数据加载中...
 
   



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

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