最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用Java編寫個"不貪吃蛇"小游戲

 更新時間:2022年08月03日 11:58:15   作者:AnLingYi  
貪吃蛇大家一定有玩過了吧,今天小編給大家?guī)睃c不一樣的。本文將用Java編寫一個"不貪吃蛇"小游戲,感興趣的小伙伴可以動手嘗試一下

前言

我寫的這個”貪吃蛇“和小時候玩的”貪吃蛇“有點不一樣,以往的”貪吃蛇“吃了食物蛇身就會變長,而我寫的這個吃了“食物”蛇身會變短,并且勝利條件就是“把蛇變沒”,嘻嘻~

這里的“食物”其實是“藥丸”,初始時,蛇身很長,你要通過食用“藥丸”,來讓自己的身體變短,直到自己消失不見,你就獲勝了。

“藥丸”共有三種,分別為“紅色藥丸、藍色藥丸、綠色藥丸”,對應分值“5分、2分、1分”,蛇吃了“藥丸”會減掉對應分值數(shù)量的身體,并累計分值。

代碼

蛇、藥丸的抽象

坐標 Point.java

記錄橫縱坐標值。

package cn.xeblog.snake.model;

import java.util.Objects;

/**
 * 坐標
 *
 * @author anlingyi
 * @date 2022/8/2 3:35 PM
 */
public class Point {

    public int x;

    public int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Point point = (Point) o;
        return x == point.x && y == point.y;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }

    @Override
    public String toString() {
        return "Point{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }

}

移動方向 Direction.java

提供上、下、左、右四個移動方向的枚舉。

package cn.xeblog.snake.model;

/**
 * @author anlingyi
 * @date 2022/8/2 5:25 PM
 */
public enum Direction {
    UP,
    DOWN,
    LEFT,
    RIGHT
}

蛇 Snake.java

存儲蛇身坐標信息,提供蛇身移動、移除蛇尾坐標、獲取蛇頭、蛇尾坐標、蛇身長度等方法。

這里講一下蛇移動的實現(xiàn)原理:游戲開始時,會固定一個移動方向,蛇會一直朝著這個方向移動,我們可以通過方向鍵改變蛇的移動方向,蛇的移動其實就是將蛇身的坐標移動一下位置,比如蛇身長度(不包含蛇頭)為6,移動時,只需將蛇身位置為5的坐標移到位置為6的坐標去,位置為4的坐標移動到位置為5的坐標去,簡單來說就是將前一個的坐標換到它后面一個去,這是蛇身的移動,蛇頭的移動需要單獨計算,如果是上下方向移動,那就是對y坐標的加減操作,向上運動需要減一個蛇身的高度,向下則與之相反,需要加一個蛇身的高度,左右運動同理。

package cn.xeblog.snake.model;

import java.util.List;

/**
 * 蛇
 *
 * @author anlingyi
 * @date 2022/8/2 3:32 PM
 */
public class Snake {

    public static int width = 10;

    public static int height = 10;

    /**
     * 蛇身坐標列表
     */
    public List<Point> body;

    public Snake(List<Point> body) {
        this.body = body;
    }

    /**
     * 添加蛇身坐標
     *
     * @param x
     * @param y
     */
    public void add(int x, int y) {
        this.body.add(new Point(x * width, y * height));
    }

    /**
     * 移除蛇尾坐標
     */
    public void removeLast() {
        int size = size();
        if (size == 0) {
            return;
        }

        this.body.remove(size - 1);
    }

    /**
     * 獲取蛇頭坐標
     *
     * @return
     */
    public Point getHead() {
        if (size() > 0) {
            return this.body.get(0);
        }

        return null;
    }

    /**
     * 獲取蛇尾坐標
     *
     * @return
     */
    public Point getTail() {
        int size = size();
        if (size > 0) {
            return this.body.get(size - 1);
        }

        return null;
    }

    /**
     * 蛇身長度
     *
     * @return
     */
    public int size() {
        return this.body.size();
    }

    /**
     * 蛇移動
     *
     * @param direction 移動方向
     */
    public void move(Direction direction) {
        if (size() == 0) {
            return;
        }

        for (int i = this.size() - 1; i > 0; i--) {
            // 從蛇尾開始向前移動
            Point point = this.body.get(i);
            Point nextPoint = this.body.get(i - 1);
            point.x = nextPoint.x;
            point.y = nextPoint.y;
        }

        // 蛇頭移動
        Point head = getHead();
        switch (direction) {
            case UP:
                head.y -= height;
                break;
            case DOWN:
                head.y += height;
                break;
            case LEFT:
                head.x -= width;
                break;
            case RIGHT:
                head.x += width;
                break;
        }
    }

}

藥丸 Pill.java

存儲“藥丸“的坐標、類型信息。

package cn.xeblog.snake.model;

/**
 * 藥丸
 *
 * @author anlingyi
 * @date 2022/8/2 4:49 PM
 */
public class Pill {

    public static int width = 10;

    public static int height = 10;

    /**
     * 坐標
     */
    public Point point;

    /**
     * 藥丸類型
     */
    public PillType pillType;

    public enum PillType {
        /**
         * 紅色藥丸
         */
        RED(5),
        /**
         * 藍色藥丸
         */
        BLUE(2),
        /**
         * 綠色藥丸
         */
        GREEN(1),
        ;

        /**
         * 分數(shù)
         */
        public int score;

        PillType(int score) {
            this.score = score;
        }
    }

    public Pill(int x, int y, PillType pillType) {
        this.point = new Point(x * width, y * height);
        this.pillType = pillType;
    }

}

游戲界面

初始化一些信息,比如游戲界面的寬高、定時器、一些狀態(tài)標識(是否停止游戲、游戲是否勝利)、監(jiān)聽一些按鍵事件(空格鍵開始/暫停游戲、四個方向鍵控制蛇移動的方向),繪制游戲畫面。

當檢測到游戲開始時,初始化蛇、“藥丸”的位置信息,然后啟動定時器每隔一段時間重新繪制游戲畫面,讓蛇可以動起來。

當檢測到蛇咬到自己或者是蛇撞墻時,游戲狀態(tài)將會被標記為“游戲失敗”,繪制游戲結束畫面,并且定時器停止,如果蛇身為0,則游戲結束,游戲狀態(tài)標記為“游戲勝利”。

package cn.xeblog.snake.ui;

import cn.xeblog.snake.model.Direction;
import cn.xeblog.snake.model.Pill;
import cn.xeblog.snake.model.Point;
import cn.xeblog.snake.model.Snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

/**
 * @author anlingyi
 * @date 2022/8/2 3:51 PM
 */
public class SnakeGameUI extends JPanel implements ActionListener {

    /**
     * 寬度
     */
    private int width;

    /**
     * 高度
     */
    private int height;


    /**
     * 蛇
     */
    private Snake snake;

    /**
     * 藥丸
     */
    private Pill pill;

    /**
     * 移動方向
     */
    private Direction direction;

    /**
     * 停止游戲標記
     */
    private boolean stop;

    /**
     * 游戲狀態(tài) 0.初始化 1.游戲勝利 2.游戲失敗
     */
    private int state = -1;

    /**
     * 定時器
     */
    private Timer timer;

    /**
     * 移動速度
     */
    private int speed = 100;

    /**
     * 分數(shù)
     */
    private int score;

    /**
     * 特殊藥丸列表
     */
    private ArrayList<Pill.PillType> specialPill;

    public SnakeGameUI(int width, int height) {
        this.width = width;
        this.height = height;
        this.timer = new Timer(speed, this);
        this.stop = true;

        initPanel();
    }

    /**
     * 初始化
     */
    private void init() {
        this.score = 0;
        this.state = 0;
        this.stop = true;
        this.timer.setDelay(speed);

        initSnake();
        initPill();
        generatePill();
        repaint();
    }

    /**
     * 初始化游戲面板
     */
    private void initPanel() {
        this.setPreferredSize(new Dimension(this.width, this.height));
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (stop && e.getKeyCode() != KeyEvent.VK_SPACE) {
                    return;
                }

                switch (e.getKeyCode()) {
                    case KeyEvent.VK_UP:
                        if (direction == Direction.DOWN) {
                            break;
                        }

                        direction = Direction.UP;
                        break;
                    case KeyEvent.VK_DOWN:
                        if (direction == Direction.UP) {
                            break;
                        }

                        direction = Direction.DOWN;
                        break;
                    case KeyEvent.VK_LEFT:
                        if (direction == Direction.RIGHT) {
                            break;
                        }

                        direction = Direction.LEFT;
                        break;
                    case KeyEvent.VK_RIGHT:
                        if (direction == Direction.LEFT) {
                            break;
                        }

                        direction = Direction.RIGHT;
                        break;
                    case KeyEvent.VK_SPACE:
                        if (state != 0) {
                            init();
                        }

                        stop = !stop;
                        if (!stop) {
                            timer.start();
                        }
                        break;
                }
            }
        });
    }

    /**
     * 初始化蛇
     */
    private void initSnake() {
        this.direction = Direction.LEFT;
        int maxX = this.width / Snake.width;
        int maxY = this.height / Snake.height;

        this.snake = new Snake(new ArrayList<>());
        this.snake.add(maxX - 2, 3);
        this.snake.add(maxX - 1, 3);
        this.snake.add(maxX - 1, 2);
        this.snake.add(maxX - 1, 1);
        for (int i = maxX - 1; i > 0; i--) {
            this.snake.add(i, 1);
        }
        for (int i = 1; i < maxY - 1; i++) {
            this.snake.add(1, i);
        }
        for (int i = 1; i < maxX - 1; i++) {
            this.snake.add(i, maxY - 2);
        }
    }

    /**
     * 初始化藥丸
     */
    private void initPill() {
        this.specialPill = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            this.specialPill.add(Pill.PillType.RED);
        }
        for (int i = 0; i < 10; i++) {
            this.specialPill.add(Pill.PillType.BLUE);
        }

        Collections.shuffle(specialPill);
    }

    /**
     * 生成藥丸
     */
    private void generatePill() {
        // 是否獲取特殊藥丸
        boolean getSpecialPill = new Random().nextInt(6) == 3;
        Pill.PillType pillType;
        if (getSpecialPill && this.specialPill.size() > 0) {
            // 生成特殊藥丸
            int index = new Random().nextInt(this.specialPill.size());
            pillType = this.specialPill.get(index);
            this.specialPill.remove(index);
        } else {
            // 生成綠色藥丸
            pillType = Pill.PillType.GREEN;
        }

        // 隨機坐標
        int x = new Random().nextInt(this.width / Pill.width - 1);
        int y = new Random().nextInt(this.height / Pill.height - 1);
        this.pill = new Pill(x, y, pillType);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2.setColor(new Color(66, 66, 66));
        g2.fillRect(0, 0, this.width, this.height);

        if (this.snake != null) {
            // 畫蛇
            g2.setColor(new Color(255, 255, 255));
            for (int i = this.snake.size() - 1; i >= 0; i--) {
                Point point = this.snake.body.get(i);
                if (i == 0) {
                    // 蛇頭
                    g2.setColor(new Color(255, 92, 92));
                } else {
                    g2.setColor(new Color(215, 173, 173));
                }

                g2.fillRect(point.x, point.y, Snake.width, Snake.height);
            }
        }

        if (this.pill != null) {
            // 畫藥丸
            Color pillColor;
            switch (this.pill.pillType) {
                case RED:
                    pillColor = new Color(255, 41, 41);
                    break;
                case BLUE:
                    pillColor = new Color(20, 250, 243);
                    break;
                default:
                    pillColor = new Color(97, 255, 113);
                    break;
            }

            g2.setColor(pillColor);
            g2.fillOval(pill.point.x, pill.point.y, Pill.width, Pill.height);
        }

        if (state > 0) {
            // 顯示游戲結果
            String tips = "游戲失??!";
            if (state == 1) {
                tips = "游戲勝利!";
            }
            g2.setFont(new Font("", Font.BOLD, 20));
            g2.setColor(new Color(208, 74, 74));
            g2.drawString(tips, this.width / 3, this.height / 3);

            g2.setFont(new Font("", Font.PLAIN, 18));
            g2.setColor(Color.WHITE);
            g2.drawString("得分:" + this.score, this.width / 2, this.height / 3 + 50);
        }

        if (stop) {
            g2.setFont(new Font("", Font.PLAIN, 18));
            g2.setColor(Color.WHITE);
            g2.drawString("按空格鍵開始/暫停游戲!", this.width / 4, this.height - 50);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 是否吃藥
        boolean isAte = false;
        if (!this.stop) {
            // 移動蛇
            this.snake.move(this.direction);
            Point head = this.snake.getHead();
            if (head.equals(this.pill.point)) {
                // 吃藥了
                isAte = true;
                // 藥丸分數(shù)
                int getScore = this.pill.pillType.score;
                // 累計分數(shù)
                this.score += getScore;
                for (int i = 0; i < getScore; i++) {
                    // 移除蛇尾
                    this.snake.removeLast();
                    if (this.snake.size() == 0) {
                        // 游戲勝利
                        this.state = 1;
                        this.stop = true;
                        break;
                    }
                }

                pill = null;
                if (this.score % 10 == 0) {
                    int curSpeed = this.timer.getDelay();
                    if (curSpeed > 30) {
                        // 加速
                        this.timer.setDelay(curSpeed - 10);
                    }
                }
            }

            if (state == 0) {
                // 判斷蛇有沒有咬到自己或是撞墻
                int maxWidth = this.width - this.snake.width;
                int maxHeight = this.height - this.snake.height;
                boolean isHitWall = head.x > maxWidth || head.x < 0 || head.y > maxHeight || head.y < 0;
                boolean isBiting = false;
                for (int i = this.snake.size() - 1; i > 0; i--) {
                    if (head.equals(this.snake.body.get(i))) {
                        isBiting = true;
                        break;
                    }
                }

                if (isHitWall || isBiting) {
                    // 游戲失敗
                    this.state = 2;
                    this.stop = true;
                }
            }
        }

        if (this.stop) {
            this.timer.stop();
        } else if (isAte) {
            // 重新生成藥丸
            generatePill();
        }

        repaint();
    }

}

啟動類

游戲啟動入口。

package cn.xeblog.snake;

import cn.xeblog.snake.ui.SnakeGameUI;

import javax.swing.*;

/**
 * 啟動游戲
 *
 * @author anlingyi
 * @date 2022/8/2 3:41 PM
 */
public class StartGame {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setTitle("不貪吃蛇");
        frame.setSize(400, 320);
        frame.setLocationRelativeTo(null);
        JPanel gamePanel = new SnakeGameUI(400, 300);
        frame.add(gamePanel);
        gamePanel.requestFocus();
    }

}

游戲演示

最后

剛開始蛇身很長,蛇身移動緩慢,后面會隨著得分的越來越高,蛇身會移動的越來越快,看著挺容易,真玩起來,還真有“億”點難,目前我玩了好幾把,沒有贏過,最開始那張圖就是我目前的最高分了,嘻嘻~

完整代碼:https://github.com/anlingyi/NonGluttonousSnake

到此這篇關于利用Java編寫個"不貪吃蛇"小游戲的文章就介紹到這了,更多相關Java不貪吃蛇游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • struts2中類型轉(zhuǎn)換實例代碼

    struts2中類型轉(zhuǎn)換實例代碼

    這篇文章主要介紹了struts2中類型轉(zhuǎn)換實例代碼,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • JDK動態(tài)代理之ProxyGenerator生成代理類的字節(jié)碼文件解析

    JDK動態(tài)代理之ProxyGenerator生成代理類的字節(jié)碼文件解析

    這篇文章主要為大家詳細介紹了JDK動態(tài)代理之ProxyGenerator生成代理類的字節(jié)碼文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Nacos配置文件使用經(jīng)驗及CAP原則詳解

    Nacos配置文件使用經(jīng)驗及CAP原則詳解

    這篇文章主要為大家介紹了Nacos配置文件使用經(jīng)驗及CAP規(guī)則詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-02-02
  • jenkins安裝及其配置筆記

    jenkins安裝及其配置筆記

    這篇文章主要介紹了jenkins安裝及其配置筆記,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • 若依前后端打成一個JAR包部署的完整步驟

    若依前后端打成一個JAR包部署的完整步驟

    這篇文章主要介紹了如何將若依前后端分離項目打包成jar,不使用nginx轉(zhuǎn)發(fā),前端修改了路由模式和環(huán)境變量配置,后端增加了依賴、配置了Thymeleaf和訪問路徑,需要的朋友可以參考下
    2025-01-01
  • Java笛卡爾積算法原理與實現(xiàn)方法詳解

    Java笛卡爾積算法原理與實現(xiàn)方法詳解

    這篇文章主要介紹了Java笛卡爾積算法原理與實現(xiàn)方法,結合實例形式較為詳細的分析了笛卡爾積算法的原理及java定義與使用笛卡爾積算法的相關操作技巧,需要的朋友可以參考下
    2017-12-12
  • Java基于Socket的文件傳輸實現(xiàn)方法

    Java基于Socket的文件傳輸實現(xiàn)方法

    這篇文章主要介紹了Java基于Socket的文件傳輸實現(xiàn)方法,結合實例分析了Java使用Socket實現(xiàn)文件傳輸?shù)慕⑦B接、發(fā)送與接收消息、文件傳輸?shù)认嚓P技巧,需要的朋友可以參考下
    2015-12-12
  • Java中實現(xiàn)在一個方法中調(diào)用另一個方法

    Java中實現(xiàn)在一個方法中調(diào)用另一個方法

    下面小編就為大家分享一篇Java中實現(xiàn)在一個方法中調(diào)用另一個方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • SpringBoot向容器注冊bean的方法詳解

    SpringBoot向容器注冊bean的方法詳解

    這篇文章主要利用示例為大家詳細介紹了SpringBoot如何向容器注冊bean(即:將對象加入容器)的四種方法,文中的示例代碼講解詳細,需要的可以參考一下
    2022-05-05
  • Spring boot怎么整合Mybatis

    Spring boot怎么整合Mybatis

    spring boot的簡配置方便的開發(fā),下面通過本文給大家分享Spring boot整合Mybatis的方法,需要的朋友參考下
    2017-07-07

最新評論

收藏| 河源市| 奇台县| 鹤峰县| 西青区| 鄂州市| 彩票| 建阳市| 阿拉善盟| 营山县| 峨边| 桐庐县| 荔波县| 中宁县| 西城区| 万山特区| 客服| 文水县| 长治市| 常山县| 台前县| 汉川市| 湖口县| 胶州市| 阜南县| 高平市| 丁青县| 临湘市| 新郑市| 扬中市| 陇西县| 盈江县| 宽甸| 苍溪县| 瑞昌市| 浙江省| 石泉县| 青州市| 莎车县| 黄龙县| 长寿区|