实验二实现贪吃蛇小游戏

该博客详细介绍了如何用Java编程实现一个贪吃蛇游戏,包括游戏的基本功能如随机生成食物、控制蛇移动、碰撞检测(边界、自身)以及游戏结束条件。游戏还具备了欢迎界面、游戏界面和结束界面,并且加入了声音效果,玩家吃到食物时和游戏结束时会播放不同音效。此外,游戏界面展示了实时的得分和游戏时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

完整版代码如下

实现贪吃蛇游戏基本功能,屏幕上随机出现一个“食物”,称为豆子,上下左右控制的移动,吃到“豆子以后的身体加长一点,得分增加,碰到边界或,蛇头与蛇身相撞,蛇死亡,游戏结束。为游戏设计初始欢迎界面,游戏界面,游戏结束界面

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
class Tile{
    int x;
    int y;

    public Tile(int x0,int y0){
        x = x0;
        y = y0;
    }
}

class AePlayWave extends Thread {
    private String filename;
    private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb

    public AePlayWave(String wavfile) {
        filename = wavfile;
    }

    public void run() {
        File soundFile = new File(filename);
        AudioInputStream audioInputStream = null;
        try {
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (UnsupportedAudioFileException e1) {
            e1.printStackTrace();
            return;
        } catch (IOException e1) {
            e1.printStackTrace();
            return;
        }

        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine auline = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        try {
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            return;
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        auline.start();
        int nBytesRead = 0;
        byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

        try {
            while (nBytesRead != -1) {
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                if (nBytesRead >= 0)
                    auline.write(abData, 0, nBytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } finally {
            auline.drain();
            auline.close();
        }
    }
}


public class Snake extends JComponent {

    private static final long serialVersionUID = 3794762291171148906L;
    private final int MAX_SIZE = 400;//蛇身体最长为400节
    private Tile temp = new Tile(0, 0);
    private Tile temp2 = new Tile(0, 0);
    private Tile head = new Tile(227, 100);//头部的位置初始化为(227,100)
    private Tile[] body = new Tile[MAX_SIZE];
    private String direction = "R";//默认向右走
    private String current_direction = "R";//当前方向
    private boolean first_launch = false;
    private boolean iseaten = false;
    private boolean isrun = true;
    private int randomx, randomy;
    private int body_length = 4;//身体长度初始化为5
    private Thread run;
    private JLabel label = new JLabel("当前长度:");
    private JLabel label2 = new JLabel("所花时间:");
    private JLabel label3 = new JLabel("说          明:");
    private JTextArea explain = new JTextArea("此游戏是一个贪吃蛇Demo版本,实现简单地移动,得分,判断撞墙和撞自己的功能,"
            + "初始长度为6,头部为蓝,身体的颜色为黄色。\n"
            + "游戏界面按上下左右键实现移动");
    private JLabel Score = new JLabel("6");
    private JLabel Time = new JLabel("");
    private Font f = new Font("微软雅黑", Font.PLAIN, 15);
    private Font f2 = new Font("微软雅黑", Font.PLAIN, 13);
    private JPanel p = new JPanel();
    private int hour = 0;
    private int min = 0;
    private int sec = 0;
    private boolean pause = false;

    public Snake() {


        //布局
        add(label);
        label.setBounds(500, 10, 80, 20);
        label.setFont(f);
        add(Score);
        Score.setBounds(500, 35, 80, 20);
        Score.setFont(f);
        add(label2);
        label2.setBounds(500, 60, 80, 20);
        label2.setFont(f);
        add(Time);
        Time.setBounds(500, 85, 80, 20);
        Time.setFont(f);
        add(p);
        p.setBounds(498, 110, 93, 1);
        p.setBorder(BorderFactory.createLineBorder(Color.black));

        add(label3);
        label3.setBounds(500, 115, 80, 20);
        label3.setFont(f);
        add(explain);
        explain.setBounds(498, 138, 100, 350);
        explain.setFont(f2);
        explain.setLineWrap(true);
        explain.setOpaque(false);

        for (int i = 0; i < MAX_SIZE; i++) {
            body[i] = new Tile(0, 0);
        }

        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    if (isrun && current_direction != "L") {
                        direction = "R";
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    if (isrun && current_direction != "R") {
                        direction = "L";
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_UP) {
                    if (isrun && current_direction != "D") {
                        direction = "U";
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                    if (isrun && current_direction != "U") {
                        direction = "D";
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    direction = "R";//默认向右走
                    current_direction = "R";//当前方向
                    first_launch = false;
                    iseaten = false;
                    isrun = true;
                    body_length = 5;
                    head = new Tile(227, 100);
                    Score.setText("6");
                    hour = 0;
                    min = 0;
                    sec = 0;
                    for (int i = 0; i < MAX_SIZE; i++) {
                        body[i].x = 0;
                        body[i].y = 0;
                    }

                    run = new Thread();
                    run.start();
                    System.out.println("Start again");
                }
                if (e.getKeyCode() == KeyEvent.VK_SPACE)//按空格键开始和暂停暂时没做,还在思考中
                {
                    if (!pause)//暂停
                    {
                        pause = true;
                        isrun = false;
                    } else//开始
                    {
                        pause = false;
                        isrun = true;
                    }
                }
            }
        });

        new Timer();

        setFocusable(true);
    }

    public void paintComponent(Graphics g1) {
        super.paintComponent(g1);
        Graphics2D g = (Graphics2D) g1;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);

        //画头部
        g.setColor(Color.blue);
        g.fillRoundRect(head.x, head.y, 20, 20, 10, 10);

        g.setPaint(new GradientPaint(115, 135, Color.orange, 230, 135, Color.orange, true));
        if (!first_launch) {
            //初始化身体
            int x = head.x;
            for (int i = 0; i < body_length; i++) {
                x -= 22;//相邻两个方块的间距为2个像素,方块宽度都为20像素
                body[i].x = x;
                body[i].y = head.y;
                g.fillRoundRect(body[i].x, body[i].y, 20, 20, 10, 10);
            }
            //初始化食物位置
            ProduceRandom();
            g.fillOval(randomx, randomy, 19, 19);
        } else {
            //每次刷新身体
            for (int i = 0; i < body_length; i++) {
                g.fillRoundRect(body[i].x, body[i].y, 20, 20, 10, 10);
            }

            if (EatFood())//被吃了重新产生食物
            {
                ProduceRandom();
                g.fillOval(randomx, randomy, 19, 19);
                iseaten = false;
            } else {
                g.fillOval(randomx, randomy, 19, 19);
            }
        }
        first_launch = true;
        //墙
        g.setStroke(new BasicStroke(4, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
        g.setBackground(Color.white);
        g.drawRect(2, 7, 491, 469);


    }

    public void ProduceRandom() {
        boolean flag = true;
        Random rand = new Random();
        randomx = (rand.nextInt(21) + 1) * 22 + 7;
        randomy = (rand.nextInt(20) + 1) * 22 + 12;
        while (flag) {
            for (int i = 0; i < body_length; i++) {
                if (body[i].x == randomx && body[i].y == randomy) {
                    randomx = (rand.nextInt(21) + 1) * 22 + 7;
                    randomy = (rand.nextInt(20) + 1) * 22 + 12;
                    flag = true;
                    break;
                } else {
                    if (i == body_length - 1) {
                        flag = false;
                    }
                }
            }
        }
    }

    public void HitWall() {//判断是否撞墙
        if (current_direction == "L") {
            if (head.x < 7) {
                new AePlayWave("over.wav").start();
                isrun = false;
                int result = JOptionPane.showConfirmDialog(null, "Game over! Try again?", "Information", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_NO_OPTION) {
                    direction = "R";//默认向右走
                    current_direction = "R";//当前方向
                    first_launch = false;
                    iseaten = false;
                    isrun = true;
                    body_length = 5;
                    head = new Tile(227, 100);
                    Score.setText("6");
                    hour = 0;
                    min = 0;
                    sec = 0;
                    for (int i = 0; i < MAX_SIZE; i++) {
                        body[i].x = 0;
                        body[i].y = 0;
                    }
                    run = new Thread();
                    run.start();
                    System.out.println("Start again");
                } else {
                    System.exit(0);
                }
            }
        }
        if (current_direction == "R") {
            if (head.x > 489) {
                new AePlayWave("over.wav").start();
                isrun = false;
                int result = JOptionPane.showConfirmDialog(null, "Game over! Try again?", "Information", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_NO_OPTION) {
                    direction = "R";//默认向右走
                    current_direction = "R";//当前方向
                    first_launch = false;
                    iseaten = false;
                    isrun = true;
                    body_length = 5;
                    head = new Tile(227, 100);
                    Score.setText("6");
                    hour = 0;
                    min = 0;
                    sec = 0;
                    for (int i = 0; i < MAX_SIZE; i++) {
                        body[i].x = 0;
                        body[i].y = 0;
                    }

                    run = new Thread();
                    run.start();
                    System.out.println("Start again");
                } else {
                    System.exit(0);
                }
            }
        }
        if (current_direction == "U") {
            if (head.y < 12) {
                new AePlayWave("over.wav").start();
                isrun = false;
                int result = JOptionPane.showConfirmDialog(null, "Game over! Try again?", "Information", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_NO_OPTION) {
                    direction = "R";//默认向右走
                    current_direction = "R";//当前方向
                    first_launch = false;
                    iseaten = false;
                    isrun = true;
                    body_length = 5;
                    head = new Tile(227, 100);
                    Score.setText("6");
                    hour = 0;
                    min = 0;
                    sec = 0;
                    for (int i = 0; i < MAX_SIZE; i++) {
                        body[i].x = 0;
                        body[i].y = 0;
                    }

                    run = new Thread();
                    run.start();
                    System.out.println("Start again");
                } else {
                    System.exit(0);
                }
            }
        }
        if (current_direction == "D") {
            if (head.y > 472) {
                new AePlayWave("over.wav").start();
                isrun = false;
                int result = JOptionPane.showConfirmDialog(null, "Game over! Try again?", "Information", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_NO_OPTION) {
                    direction = "R";//默认向右走
                    current_direction = "R";//当前方向
                    first_launch = false;
                    iseaten = false;
                    isrun = true;
                    body_length = 5;
                    head = new Tile(227, 100);
                    Score.setText("6");
                    hour = 0;
                    min = 0;
                    sec = 0;
                    for (int i = 0; i < MAX_SIZE; i++) {
                        body[i].x = 0;
                        body[i].y = 0;
                    }

                    run = new Thread();
                    run.start();
                    System.out.println("Start again");
                } else {
                    System.exit(0);
                }
            }
        }
    }

    public void HitSelf() {//判断是否撞到自己身上
        for (int i = 0; i < body_length; i++) {
            if (body[i].x == head.x && body[i].y == head.y) {
                new AePlayWave("over.wav").start();
                isrun = false;
                int result = JOptionPane.showConfirmDialog(null, "Game over! Try again?", "Information", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_NO_OPTION) {
                    direction = "R";//默认向右走
                    current_direction = "R";//当前方向
                    first_launch = false;
                    iseaten = false;
                    isrun = true;
                    body_length = 5;
                    head = new Tile(227, 100);
                    Score.setText("6");
                    hour = 0;
                    min = 0;
                    sec = 0;
                    for (int j = 0; j < MAX_SIZE; j++) {
                        body[j].x = 0;
                        body[j].y = 0;
                    }

                    run = new Thread();
                    run.start();
                    System.out.println("Start again");
                } else {
                    System.exit(0);
                }
                break;
            }
        }
    }

    public boolean EatFood() {
        if (head.x == randomx && head.y == randomy) {
            iseaten = true;
            return true;
        }
        else {
            return false;
        }
    }

    public void Thread() {
        long millis = 150;//每隔150毫秒刷新一次
        run = new Thread() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(millis);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }

                    if (!pause) {
                        temp.x = head.x;
                        temp.y = head.y;
                        //头部移动
                        if (direction == "L") {
                            head.x -= 22;
                        }
                        if (direction == "R") {
                            head.x += 22;
                        }
                        if (direction == "U") {
                            head.y -= 22;
                        }
                        if (direction == "D") {
                            head.y += 22;
                        }
                        current_direction = direction;//刷新当前前进方向
                        //身体移动
                        for (int i = 0; i < body_length; i++) {
                            temp2.x = body[i].x;
                            temp2.y = body[i].y;
                            body[i].x = temp.x;
                            body[i].y = temp.y;
                            temp.x = temp2.x;
                            temp.y = temp2.y;
                        }

                        if (EatFood()) {
                            body_length++;
                            body[body_length - 1].x = temp2.x;
                            body[body_length - 1].y = temp2.y;
                            Score.setText("" + (body_length + 1));
                            new AePlayWave("eat.wav").start();
                        }

                        repaint();
                        HitWall();
                        HitSelf();
                    }
                }
            }
        };

        run.start();
    }

    public static void main(String[] args) {
        Snake t = new Snake();
        t.Thread();

        JFrame game = new JFrame();
        Image img = Toolkit.getDefaultToolkit().getImage("title.png");//窗口图标
        game.setIconImage(img);
        game.setTitle("Snake");
        game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        game.setSize(502, 507);
        game.setSize(602, 507);
        game.setResizable(false);
        game.setLocationRelativeTo(null);

        game.add(t);
        game.setVisible(true);
    }

    //计时器类
    class Timer extends Thread {
        public Timer() {
            this.start();
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                if (isrun) {
                    sec += 1;
                    if (sec >= 60) {
                        sec = 0;
                        min += 1;
                    }
                    if (min >= 60) {
                        min = 0;
                        hour += 1;
                    }
                    showTime();
                }

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }

        private void showTime() {
            String strTime = "";
            if (hour < 10)
                strTime = "0" + hour + ":";
            else
                strTime = "" + hour + ":";

            if (min < 10)
                strTime = strTime + "0" + min + ":";
            else
                strTime = strTime + "" + min + ":";

            if (sec < 10)
                strTime = strTime + "0" + sec;
            else
                strTime = strTime + "" + sec;

            //在窗体上设置显示时间
            Time.setText(strTime);
        }
    }
}

思想如下;


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

而何是

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值