贪吃蛇源代码

package com.jinchao.game;

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

public class Images {
    public static URL bodyURL = Images.class.getResource("/images/1.png");
    public static ImageIcon bodyImg = new ImageIcon(bodyURL);

    public static URL body1URL = Images.class.getResource("/images/2.png");
    public static ImageIcon body1Img = new ImageIcon(body1URL);

    public static URL body2URL = Images.class.getResource("/images/3.png");
    public static ImageIcon body2Img = new ImageIcon(body2URL);

    public static URL body3URL = Images.class.getResource("/images/4.png");
    public static ImageIcon body3Img = new ImageIcon(body3URL);

    public static URL body4URL = Images.class.getResource("/images/5.png");
    public static ImageIcon body4Img = new ImageIcon(body4URL);

    public static URL body5URL = Images.class.getResource("/images/6.png");
    public static ImageIcon body5Img = new ImageIcon(body5URL);

    public static URL body6URL = Images.class.getResource("/images/7.png");
    public static ImageIcon body6Img = new ImageIcon(body6URL);

    public static URL body7URL = Images.class.getResource("/images/8.png");
    public static ImageIcon body7Img = new ImageIcon(body7URL);

    public static URL body8URL = Images.class.getResource("/images/9.png");
    public static ImageIcon body8Img = new ImageIcon(body8URL);

    public static URL headURL = Images.class.getResource("/images/head.png");
    public static ImageIcon headImg = new ImageIcon(headURL);




}
package com.jinchao.game;

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;

public class Music {
    void playMusic(String musicLocation)
    {
        try {
            File musicPath = new File(musicLocation);
            if (musicPath.exists()){
                AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
                Clip clip = AudioSystem.getClip();
                clip.open(audioInput);
                clip.start();
                clip.loop(Clip.LOOP_CONTINUOUSLY);
            }else{

            }
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
package com.jinchao.game;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.Random;

public class Panel extends JPanel {
    int length = 3;
    int[] snakeX = new int[200];
    int[] snakeY = new int[200];

    String direction;

    boolean isStart = false;

    Timer timer;

    int foodX;
    int foodY;
    int score;
    boolean isDie = false;

    public void init(){
        snakeX[0] = 175;
        snakeY[0] = 275;

        snakeX[1] = 150;
        snakeY[1] = 275;

        snakeX[2] = 125;
        snakeY[2] = 275;

        direction = "R";

        foodX = 100;
        foodY = 100;



    }



    public Panel(){
        init();
        this.setFocusable(true);
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                int keyCode = e.getKeyCode();
                if (keyCode==KeyEvent.VK_SPACE){
                    if (isDie){
                        init();
                        isDie = false;
                    }else {
                        isStart = !isStart;
                    }
                    repaint();
                }
                if (keyCode==KeyEvent.VK_UP){
                    if (!direction.equals("D"))
                    direction = "U";

                }
                if (keyCode==KeyEvent.VK_DOWN){
                    if (!direction.equals("U"))
                    direction = "D";
                }
                if (keyCode==KeyEvent.VK_LEFT){
                    if (!direction.equals("R"))
                    direction = "L";
                }
                if (keyCode==KeyEvent.VK_RIGHT){
                    if (!direction.equals("L"))
                    direction = "R";
                }
            }
        });
        timer = new Timer(150, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isStart&&isDie==false){
                    for (int i = length-1;i>0;i--){
                        snakeX[i] = snakeX[i-1];
                        snakeY[i] = snakeY[i-1];
                    }
                    if ("R".equals(direction)){
                        snakeX[0] += 25;
                    }
                    if ("L".equals(direction)){
                        snakeX[0] -= 25;
                    }
                    if ("U".equals(direction)){
                        snakeY[0] -= 25;
                    }
                    if ("D".equals(direction)){
                        snakeY[0] += 25;
                    }
                    if (snakeX[0]<25){
                        snakeX[0] = 750;
                    }

                    if (snakeY[0]<100){
                        snakeY[0] = 725;
                    }

                    if (snakeY[0]>725){
                        snakeY[0] = 100;
                    }


                    if (snakeX[0]>750){
                        snakeX[0]= 25;
                    }
                    if (snakeX[0]==foodX&&snakeY[0]==foodY){
                        length++;
                        foodX = ((int)(Math.random()*30)+1)*25;
                        foodY = (new Random().nextInt(26)+4)*25;
                        score+=10;
                    }

                    for (int i = 1;i<length;i++){
                        if (snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
                            isDie = true;
                        }
                    }
                    repaint();
                }
            }
        });
        timer.start();
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(new Color(193, 219, 155));
        Images.headImg.paintIcon(this,g,10,10);
        g.setColor(new Color(0xABC9E7));
        g.fillRect(10,70,770,685);


        for (int i = 0; i < length; i++) {
            Images.bodyImg.paintIcon(this,g,snakeX[i],snakeY[i]);

        }
        if (isStart==false){
            g.setColor(new Color(0x1010E2));
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("点击空格开始游戏",250,330);
        }

        Images.body3Img.paintIcon(this,g,foodX,foodY);
        g.setColor(new Color(0x1010E2));
        g.setFont(new Font("微软雅黑",Font.BOLD,20));
        g.drawString("积分:"+score,620,40);

        if (isDie){
            g.setColor(new Color(0x1010E2));
            g.setFont(new Font("微软雅黑",Font.BOLD,20));
            g.drawString("失败,按下空格重新游戏",200,330);

        }


    }
}
package com.jinchao.game;

import sun.awt.WindowClosingListener;

import javax.swing.*;

public class Start {
    public static void main(String[] args) {
        String filepath = "Images/植松伸夫 (うえまつ のぶお) - プレリュード (序曲).wav";
        Music music = new Music();
        music.playMusic(filepath);
        JFrame jf = new JFrame();
        jf.setTitle("贪吃蛇 最终幻想");
        jf.setBounds(400,100,800,800);
        jf.setResizable(false);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Panel gp = new Panel();
        jf.add(gp);
        jf.setVisible(true);

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值