java贪吃蛇(跟着狂神做的,附贪吃蛇素材)

这是一个使用Java Swing开发的贪吃蛇游戏,游戏面板包含头部广告栏、游戏界面、分数显示和食物图标。玩家通过键盘控制蛇的方向,碰撞到边界或自身会导致游戏结束。游戏增加了回头判定,防止蛇撞到自己。代码中包含了游戏的初始化、蛇的移动、食物生成、碰撞检测等功能,并支持游戏的重新开始。
摘要由CSDN通过智能技术生成

在狂神的基础上只增加了回头判定(不能回头撞自己)

项目结构

 

项目代码

package com.jian.snake;

import javax.swing.*;

public class StartGame {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();

        jFrame.setBounds(10,10,900,720);
        jFrame.setResizable(false);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.add(new GamePanel());
        jFrame.setVisible(true);
    }
}
package com.jian.snake;

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 {
    //蛇的数据结构
    int length;
    int[] snakeX=new int[600];
    int[] snakeY=new int[500];
    String direction;
    int foodX;
    int foodY;
    int score;
    Random random=new Random();

    boolean isfalse=false;
    boolean isStart =false;
    Timer timer=new Timer(100,this);


    //构造器
    public GamePanel() {
        init();
        this.setFocusable(true);
        this.addKeyListener(this);
    }

    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;
        timer.start();
    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(Color.white);
        Date.header.paintIcon(this,g,25,11);//头部广告栏画上去
        g.fillRect(25,75,850,600);//默认的游戏界面

        g.setColor(Color.white);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度:"+length,750,35);
        g.drawString("分数:"+score,750,50);
        //食物
        Date.food.paintIcon(this,g,foodX,foodY);


        if (direction.equals("R")){
            Date.right.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else if(direction.equals("L")){
            Date.left.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else if(direction.equals("U")){
            Date.up.paintIcon(this,g,snakeX[0],snakeY[0]);
        }else if(direction.equals("D")){
            Date.down.paintIcon(this,g,snakeX[0],snakeY[0]);
        }

        //蛇
        for (int i = 1; i < length; i++) {
            Date.body.paintIcon(this,g,snakeX[i],snakeY[i]);
        }

        if (isStart==false){
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始游戏",300,300);
        }

        if (isfalse){
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("失败,按下空格重新开始 ",300,300);
        }

    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if(keyCode==KeyEvent.VK_SPACE){
            if (isfalse){
                isfalse=false;
                init();
            }else {
                isStart=!isStart;
            }
            repaint();
        }
        if(keyCode==KeyEvent.VK_UP){
            if (direction!="D"){
                direction="U";
                repaint();
            }
        }
        if(keyCode==KeyEvent.VK_LEFT){
            if (direction!="R"){
                direction="L";
                repaint();
            }
        }
        if(keyCode==KeyEvent.VK_DOWN){
            if (direction!="U"){
                direction="D";
                repaint();
            }
        }
        if(keyCode==KeyEvent.VK_RIGHT){
            if (direction!="L"){
                direction="R";
                repaint();
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override//事件监听
    public void actionPerformed(ActionEvent e) {
        if (isStart  && isfalse==false){

            for (int i = length-1; i >0; i--) {
                snakeX[i]=snakeX[i-1];
                snakeY[i]=snakeY[i-1];
            }
            //吃食物
            if (snakeX[0]==foodX && snakeY[0]==foodY){
                length++;
                score+=10;
                foodX=25 + 25*random.nextInt(34);
                foodY=75 + 25*random.nextInt(24);
            }


            if(direction.equals("R")){
                snakeX[0]+=25;
                if (snakeX[0]>850){
                    snakeX[0]=25;
                }
            }else if (direction.equals("L")){
                snakeX[0]-=25;
                if (snakeX[0]<25){
                    snakeX[0]=850;
                }
            }else if (direction.equals("U")){
                snakeY[0]-=25;
                if (snakeY[0]<75){
                    snakeY[0]=650;
                }
            }else if (direction.equals("D")){
                snakeY[0]+=25;
                if (snakeY[0]>650){
                    snakeY[0]=75;
                }
            }
            for (int i = 1; i < length; i++) {
                if (snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
                    isfalse=true;
                }
            }

            repaint();

        }
        timer.start();
    }
}


package com.jian.snake;

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

public class Date {
    public static URL headerURL=Date.class.getResource("statics/header.png");
    public static ImageIcon header=new ImageIcon(headerURL);

    public static URL upURL=Date.class.getResource("statics/up.png");
    public static URL downURL=Date.class.getResource("statics/down.png");
    public static URL leftURL=Date.class.getResource("statics/left.png");
    public static URL rightURL=Date.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);

    public static URL bodyURL=Date.class.getResource("statics/body.png");
    public static ImageIcon body=new ImageIcon(bodyURL);

    public static URL foodURL=Date.class.getResource("statics/food.png");
    public static ImageIcon food=new ImageIcon(foodURL);
}

打包运行

 素材

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值