Java:贪吃蛇游戏的实现

1.游戏界面

import javax.swing.*;
import java.awt.*;

public class StartGame extends JFrame {
    
    
    public static void main(String[] args) {
    
    
        //游戏界面
        JFrame frame = new JFrame();

        frame.setBounds(10, 10, 900, 720);
        frame.setResizable(false);
        frame.setBackground(Color.black);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(new GamePanel());
        frame.setVisible(true);
    }
}

2.游戏数据

import javax.swing.*;
import java.net.URL;

//数据中心
public class Data {
    
    
    public static URL headURL = Data.class.getResource("statics/header.png");
    public static ImageIcon header = new ImageIcon(headURL);

    public static URL bodyURL = Data.class.getResource("statics/body.png");
    public static URL foodURL = Data.class.getResource("statics/food.png");

    public static ImageIcon body = new ImageIcon(bodyURL);
    public static ImageIcon food = new ImageIcon(foodURL);

    public static URL upURL = Data.class.getResource("statics/up.png");
    public static URL downURL = Data.class.getResource("statics/down.png");
    public static URL leftURL = Data.class.getResource("statics/left.png");
    public static URL rightURL = Data.class.getResource("statics/right.png");

    public static ImageIcon up = new ImageIcon(upURL);
    public static ImageIcon down = new ImageIcon(downURL);
    public static ImageIcon left = new ImageIcon(leftURL);
    public static ImageIcon right = new ImageIcon(rightURL);


}

3.游戏面板

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

//游戏面板
public class GamePanel extends JPanel implements KeyListener, ActionListener {
    
    
    public GamePanel() {
    
    
        init();
        //获取键盘的监听事件
        this.setFocusable(true);
        this.addKeyListener(this);
        timer.start();
    }

    //蛇的长度
    int length;
    //蛇的X坐标
    int[] snakeX = new int[600];
    //蛇的Y坐标
    int[] snakeY = new int[500];

    //方向控制,默认为右
    String direction;

    //游戏开始的标志
    boolean isStart = false;

    Timer timer = new Timer(100, this);

    //定义食物
    int foodX;
    int foodY;
    Random random = new Random();

    //判断是否失败
    boolean isFail = false;

    //积分系统
    int score;

    //初始化
    public void init() {
    
    
        //初始长度
        length = 3;
        //蛇的头部
        snakeX[0] = 100;
        snakeY[0] = 100;
        //蛇的第一节
        snakeX[1] = 75;
        snakeY[1] = 100;
        //蛇的第二节
        snakeX[2] = 50;
        snakeY[2] = 100;
        //初始化方向
        direction = "R";
        foodX = 25 + 25 * random.nextInt(34);
        foodY = 75 + 25 * random.nextInt(24);

        score = 0;
    }

    @Override
    protected void paintComponent(Graphics g) {
    
    
        //清屏
        super.paintComponent(g);
        this.setBackground(Color.white);
        //头部信息栏
        Data.header.paintIcon(this, g, 25, 11);
        g.fillRect(25, 75, 850, 600);
        //蛇头的方向控制
        if (direction.equals("R")) {
    
    
            Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if (direction.equals("L")) {
    
    
            Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);

        } else if (direction.equals("U")) {
    
    
            Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);

        } else if (direction.equals("D")) {
    
    
            Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
        }
        //通过length控制蛇的长度
        for (int i = 1; i < length; i++) {
    
    
            Data.body.paintIcon(this, g, snakeX[i], snakeY[i]);
        }

        //画积分
        g.setColor(Color.white);
        g.setFont(new Font("微软雅黑", Font.BOLD, 16));
        g.drawString("长度:" + length, 750, 30);
        g.drawString("积分:" + score, 750, 50);
        //画食物
        Data.food.paintIcon(this, g, foodX, foodY);
        //游戏提示:是否开始
        if (!isStart) {
    
    
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("按下空格开始游戏", 300, 300);
        }
        //失败提示
        if (isFail) {
    
    
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("游戏失败,按下空格重新开始", 200, 300);
        }

    }

    //监听键盘事件
    @Override
    public void keyPressed(KeyEvent e) {
    
    
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_SPACE) {
    
    
            if (isFail) {
    
    
                isFail = false;
                init();
            } else {
    
    
                isStart = !isStart;
            }
            //刷新界面
            repaint();
        }
        //键盘控制走向
        if (keyCode == KeyEvent.VK_RIGHT) {
    
    
            direction = "R";
        } else if (keyCode == KeyEvent.VK_LEFT) {
    
    
            direction = "L";
        } else if (keyCode == KeyEvent.VK_UP) {
    
    
            direction = "U";
        } else if (keyCode == KeyEvent.VK_DOWN) {
    
    
            direction = "D";
        }


    }


    //定时器
    @Override
    public void actionPerformed(ActionEvent e) {
    
    
        if (isStart && !isFail) {
    
    
            for (int i = length - 1; i > 0; i--) {
    
    
                snakeX[i] = snakeX[i - 1];
                snakeY[i] = snakeY[i - 1];
            }


            if (direction.equals("R")) {
    
    
                snakeX[0] = snakeX[0] + 25;
                //边界判断
                if (snakeX[0] > 850) {
    
    
                    snakeX[0] = 25;
                }
            } else if (direction.equals("L")) {
    
    
                //头部移动
                snakeX[0] = snakeX[0] - 25;
                if (snakeX[0] < 25) {
    
    
                    snakeX[0] = 850;
                }
            } else if (direction.equals("U")) {
    
    
                //头部移动
                snakeY[0] = snakeY[0] - 25;
                if (snakeY[0] < 75) {
    
    
                    snakeY[0] = 650;
                }
            } else if (direction.equals("D")) {
    
    
                //头部移动
                snakeY[0] = snakeY[0] + 25;
                if (snakeY[0] > 650) {
    
    
                    snakeY[0] = 75;
                }
            }
            //判断蛇是否吃到了食物
            if (snakeX[0] == foodX && snakeY[0] == foodY) {
    
    
                length++;
                score = score + 10;
                //重新生成食物
                foodX = 25 + 25 * random.nextInt(34);
                foodY = 75 + 25 * random.nextInt(24);
            }
            //判断是否失败
            for (int i = 1; i < length; i++) {
    
    
                if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
    
    
                    isFail = true;
                }
            }
            //刷新界面
            repaint();
        }
        timer.start();
    }

    @Override
    public void keyTyped(KeyEvent e) {
    
    

    }

    @Override
    public void keyReleased(KeyEvent e) {
    
    

    }
}

运行结果为:
在这里插入图片描述
类文件和素材文件的布局为:
在这里插入图片描述
素材文件的百度网盘链接及提取码:
链接:https://pan.baidu.com/s/15BxWoCnfabThd_PEXJ2rJA
提取码:prxy

猜你喜欢

转载自blog.csdn.net/weixin_45631296/article/details/103413970