| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
大量收QQ微信精准粉/交友粉,非诚勿扰千里之行 始于足下
共有 257 人关注过本帖
标题:贪吃蛇
只看楼主 加入收藏
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:100
帖 子:1635
专家分:7038
注 册:2015-3-19
结帖率:100%
  已结贴   问题点数:100  回复次数:3   
贪吃蛇
看了渐渐鱼同学的贪吃蛇,调试时候居然写不由自主准备写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();
    }
}
附件: 您没有浏览附件的权限,请 登录注册
2018-10-05 21:14
幻紫灵心
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:山咔咔里面
等 级:版主
威 望:15
帖 子:90
专家分:567
注 册:2018-3-30
  得分:50 
ε=(′ο`*)))100分哎
2018-10-07 20:03
悟空丨
Rank: 2
等 级:论坛游民
帖 子:8
专家分:50
注 册:2018-10-6
  得分:50 
这种小游戏有毒,玩了一下午~~~
2018-10-07 21:03
幻紫灵心
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:山咔咔里面
等 级:版主
威 望:15
帖 子:90
专家分:567
注 册:2018-3-30
  得分:0 
这个画蛇头有点意思...
2018-10-07 21:42







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

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