【 源代码 】用Eclipse编写的Java小游戏——疯狂猜猜猜

用Eclipse编写的小游戏——疯狂猜猜猜

郑重申明:

本人为Java初学者,该代码是本人在学习Java一周后做出来的,因此非常多的代码可能是有问题的,如有大神指教,本人必将认真听改。

代码简介:

这是一个小游戏,包含了用户在选定难度后进入游戏进行大小判定,且状态栏会显示相关状态,并且使用图片进行反馈提醒,此外,还调用了JRE1.8的音乐播放功能,四首音乐供用户选择。废话不多说,下面公布源代码。

源代码

代码资源问题

游戏调用的资源全部放在项目的文件夹下,与src文件夹同名,这样子调用时只需要使用”images/xxx.jpg”之类的相对地址即可,并且可以方便的导出jar文件和资源包放在一起即可方便运行,该程序的资源包文件夹如下
【 源代码 】用Eclipse编写的Java小游戏——疯狂猜猜猜_第1张图片
资源包下载地址:链接:https://share.weiyun.com/5w77jwR 密码:vnkre6
游戏全包下载地址:链接:https://share.weiyun.com/5ZLzYBx 密码:j678f6

用户启动类

该类包含了用户启动后的代码调用,非常简单,保证了内部代码的安全。

package com.lenovo.demo;
/**
 * 用户启动端
 * @author Administrator
 *
 */
public class Client {
public static void main(String[] args) {
//        Start jk = new Start();
        Start.main(null);
        try {
            Start.jindu();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



}
}

启动内部类调用

该类负责在游戏开场加载动画及下一界面代码的调用

package com.lenovo.demo;

import java.awt.Color;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
/**
 * 游戏加载界面
 * @author Administrator
 *
 */
public class Start {
     public static int width = Toolkit.getDefaultToolkit().getScreenSize().width;
     public static int height = Toolkit.getDefaultToolkit().getScreenSize().height;

    static JFrame start;
    static JProgressBar in;
    public static void main(String[] args) {
        start = new JFrame();
        start.setBounds((width-420)/2, (height-350)/2, 420, 385);
        start.setUndecorated(true);
        start.setLayout(null);

        JLabel startjpg = new JLabel();
        ImageIcon jpg = new ImageIcon("images/start.jpg");
        startjpg.setIcon(jpg);
        startjpg.setBounds(0, 0, 420, 360);
        start.add(startjpg);

        in = new JProgressBar();
        in.setBounds(0, 360, 420, 25);
        in.setMaximum(100);
        in.setMinimum(0);
        in.setStringPainted(true);
        in.setBackground(Color.GREEN);
        start.add(in);
        start.setVisible(true);
    }

        public static int jindu() throws InterruptedException {
        for (int k = 0; k < 101; k++) {
                if (k < 40) {
                    int h = (int) (Math.random() * 150 + 1);
                        Thread.sleep(h);
                } else {
                        Thread.sleep(5);
                }
            in.setValue(k);
            if (k == 100) {
                start.dispose();
                Board.main(null);

                break;
            }

        }
        return 0;
        }
}


    private static void Sleep(int h) {
        // TODO Auto-generated method stub

    }
}

 private static void Sleep(int i) {
 // TODO Auto-generated method stub

 }

 public static void et(int t) {
 if (t == 0) {
 System.exit(0);
 }
 }

 }

主界面类

该类代码负责主程序的界面和音乐控制台的界面建设以及游戏互动的数据传输,是游戏最主要的类,其中代码类型丰富,本人已添加备注

package com.lenovo.demo;

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
 * 游戏主界面UI及算法调用
 * @author Administrator
 *
 */
public class Board {
    static Game game;
    static int repeat;
    static int times = 0;
    public static int width = Toolkit.getDefaultToolkit().getScreenSize().width;
    public static int height = Toolkit.getDefaultToolkit().getScreenSize().height;

    public static void main(String[] args) {
        Music.Music1();
        Music.Music2();
        Music.Music3();
        Music moth = new Music(1);
        // 设置窗体
        JFrame f = new JFrame("疯狂数字猜猜猜————2018最好玩的小游戏————NSG出品       作者:刘统帅");
        f.setSize(1000, 700);
        f.setLocation((width - 1000) / 2 - 150, (height - 700) / 2);
        f.setLayout(null);
        f.setIconImage(new ImageIcon("icons/07.png").getImage());
        f.setBackground(Color.white);
        // 设置图片组件
        JLabel i1 = new JLabel();
        ImageIcon i = new ImageIcon("images/Logo.jpg");
        i1.setIcon(i);
        i1.setBounds(10, 0, 300, 300);
        f.add(i1);
        Label l2 = new Label("疯 狂 数 字 猜 猜 猜");
        l2.setForeground(Color.red);
        l2.setSize(750, 150);
        l2.setLocation(320, 0);
        l2.setFont(new Font("楷体", Font.BOLD, 60));
        Label l22 = new Label("请在下方选择游戏难度");
        l22.setForeground(Color.red);
        l22.setSize(300, 35);
        l22.setLocation(400, 150);
        l22.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
        f.add(l2);
        f.add(l22);
        JRadioButton b31 = new JRadioButton("简单(1-100)");
        JRadioButton b32 = new JRadioButton("一般(1-300)");
        JRadioButton b33 = new JRadioButton("困难(1-1K)");
        JRadioButton b34 = new JRadioButton("究极(1-1W)");
        b31.setLayout(null);
        b32.setLayout(null);
        b33.setLayout(null);
        b34.setLayout(null);
        b31.setSelected(true);
        b31.setBounds(320, 190, 110, 30);
        b32.setBounds(430, 190, 110, 30);
        b33.setBounds(540, 190, 110, 30);
        b34.setBounds(650, 190, 110, 30);
        ButtonGroup bg3 = new ButtonGroup();

        Button s1 = new Button("开始");
        s1.setSize(150, 50);
        s1.setLocation(330, 220);
        s1.setBackground(Color.PINK);
        s1.setForeground(Color.BLUE);
        s1.setFont(new Font("楷体", Font.CENTER_BASELINE, 18));

        Button s2 = new Button("退出");
        s2.setSize(150, 50);
        s2.setLocation(490, 220);
        s2.setBackground(Color.PINK);
        s2.setForeground(Color.BLUE);
        s2.setFont(new Font("楷体", Font.CENTER_BASELINE, 18));

        Button s3 = new Button("打开/隐藏 音乐控制台");
        s3.setBackground(Color.PINK);
        s3.setForeground(Color.BLUE);
        s3.setFont(new Font("楷体", Font.CENTER_BASELINE, 18));
        s3.setSize(200, 50);
        s3.setLocation(750, 160);
        f.add(s3);

        bg3.add(b31);
        bg3.add(b32);
        bg3.add(b33);
        bg3.add(b34);
        f.add(b31);
        f.add(b32);
        f.add(b33);
        f.add(b34);
        Label tip = new Label();
        tip.setText("              请直接在此输入你的答案↓↓↓");
        tip.setForeground(Color.MAGENTA);
        tip.setFont(new Font("黑体", Font.LAYOUT_RIGHT_TO_LEFT, 25));
        Button tijiao = new Button();
        tijiao.setLabel("提交答案");
        tijiao.setBackground(Color.PINK);
        tijiao.setForeground(Color.BLUE);
        tijiao.setFont(new Font("黑体", Font.BOLD, 20));
        tijiao.setBounds(750, 220, 200, 50);
        f.add(s1);
        f.add(s2);
        f.add(tip);
        f.add(tijiao);
        JTextArea t2 = new JTextArea();
        t2.setBackground(Color.getColor(null));
        t2.setSize(250, 350);
        t2.setLocation(10, 320);
        // String g =
        t2.setText("游戏介绍:            \n" + "1.选择你想挑战的难度并按下开始\n" + "2.输入你所猜测的数字\n" + "3.系统提示猜测数字与答案的关系\n"
                + "4.如猜测错误,请再次尝试\n" + "5.猜测成功,下方提示猜测所用次数\n" + "\n" + "NEW·音乐控制台带给你新体验\n\n退出请点击退出按钮\n" + "\n\n\n\n"
                + "\n\n作者:刘统帅\n作者QQ:1271261360\n");
        t2.setFont(new Font("楷体", Font.CENTER_BASELINE, 14));
        t2.setCaretColor(Color.BLUE);
        f.add(t2);
        JLabel i51 = new JLabel();
        ImageIcon i510 = new ImageIcon("images/demo.jpg");
        ImageIcon i511 = new ImageIcon("images/success.jpg");
        ImageIcon i512 = new ImageIcon("images/demobig.jpg");
        ImageIcon i513 = new ImageIcon("images/demosmall.jpg");
        i51.setIcon(i510);
        i51.setBounds(320, 300, 380, 380);
        f.add(i51);
        JTextField answer = new JTextField();
        answer.setSize(200, 250);
        answer.setLocation(750, 300);
        answer.setBackground(Color.PINK);
        answer.setCaretColor(Color.YELLOW);
        answer.setFont(new Font("宋体", Font.CENTER_BASELINE, 60));
        answer.setText("0");
        f.add(answer);
        Label l7 = new Label();
        l7.setFont(new Font("楷体", Font.CENTER_BASELINE, 16));
        l7.setBounds(750, 560, 200, 130);
        l7.setText("当前已尝试次数为: " + times + " 次");
        f.add(l7);

        // 音乐控制台
        JFrame music = new JFrame();
        music.setBounds((width - 1000) / 2 + 850, (height - 700) / 2, 300, 600);
        music.setTitle("音乐控制台");
        JLabel tm = new JLabel();
        tm.setBounds(45, 0, 200, 100);
        tm.setText("音乐控制台");
        music.setBackground(Color.CYAN);
        tm.setFont(new Font("楷体", Font.CENTER_BASELINE, 36));
        tm.setForeground(Color.BLUE);
        music.add(tm);

        JRadioButton m1 = new JRadioButton("默认主题曲");
        JRadioButton m2 = new JRadioButton("Let's not fall in love");
        JRadioButton m3 = new JRadioButton("空空如也");
        JRadioButton m4 = new JRadioButton("1-4-3");
        m1.setSelected(true);
        m1.setBounds(40, 120, 200, 30);
        m2.setBounds(40, 160, 200, 30);
        m3.setBounds(40, 200, 200, 30);
        m4.setBounds(40, 240, 200, 30);
        m1.setFont(new Font("楷体", Font.CENTER_BASELINE, 16));
        m2.setFont(new Font("宋体", Font.CENTER_BASELINE, 12));
        m3.setFont(new Font("楷体", Font.CENTER_BASELINE, 16));
        m4.setFont(new Font("楷体", Font.CENTER_BASELINE, 16));
        music.add(m1);
        music.add(m2);
        music.add(m3);
        music.add(m4);

        JButton play = new JButton();
        play.setText("播放");
        play.setBounds(30, 380, 220, 60);
        play.setBackground(Color.PINK);
        play.setForeground(Color.YELLOW);
        play.setFont(new Font("黑体", Font.BOLD, 28));
        music.add(play);

        JLabel mup = new JLabel();
        ImageIcon mppd = new ImageIcon("images/musicdoo.gif");
        ImageIcon mppp = new ImageIcon("images/musicdoo2.gif");
        mup.setIcon(mppd);
        mup.setBounds(30, 275, 220, 100);
        music.add(mup);

        JButton pause = new JButton();
        pause.setText("停止");
        pause.setBounds(30, 450, 220, 60);
        pause.setBackground(Color.PINK);
        pause.setForeground(Color.YELLOW);
        pause.setFont(new Font("黑体", Font.BOLD, 28));
        music.add(pause);

        ButtonGroup mcc = new ButtonGroup();
        mcc.add(m1);
        mcc.add(m2);
        mcc.add(m3);
        mcc.add(m4);

        music.setIconImage(new ImageIcon("icons/07.png").getImage());

        music.setLayout(null);
        music.setVisible(true);

        // 音乐控制台按钮监听器
        s3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                if (music.isVisible()) {
                    music.setVisible(false);
                } else {
                    music.setVisible(true);
                }
            }

        });

        // 音乐控制台监听器
        play.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                mup.setIcon(mppd);
                if (m1.isSelected()) {
                    Music mji = new Music(1);
                } else if (m2.isSelected()) {

                    Music.Music1();
                } else if (m3.isSelected()) {

                    Music.Music2();
                } else if (m4.isSelected()) {

                    Music.Music3();
                }

            }

        });
        pause.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                mup.setIcon(mppp);
                Music.pause();
            }

        });

        // f.setIconImage("/images/icon.icon");
        // 设置窗体可见
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

        // 计算主体
        // Desktop.getDesktop().open(new File("/images/start.jpg"));
        s1.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent start) {
                // TODO Auto-generated method stub 此处放置start触发新游戏事件
                times = 0;
                f.setTitle("疯狂数字猜猜猜————2018最好玩的小游戏————NSG出品       作者:刘统帅       【 游 戏 中 】");
                l7.setText("当前尝试次数为: " + times + " 次");
                i51.setIcon(i510);
                if (b31.isSelected()) {
                    game = new Game();
                } else if (b32.isSelected()) {
                    game = new Game(1);
                } else if (b33.isSelected()) {
                    game = new Game("");
                } else if (b34.isSelected()) {
                    game = new Game(1, 1);
                }

            }
        });

        tijiao.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent reset) {
                // TODO Auto-generated method stub 此处放置提交答案事件
                // do {

                int inpeat = Integer.valueOf(answer.getText());
                repeat = game.setA(inpeat);
                if (repeat == 0) {
                    i51.setIcon(i511);
                    times = times + 1;
                    JOptionPane.showMessageDialog(f, "小可爱,恭喜你猜对啦!\n正确答案就是" + inpeat + "\n你一共猜了" + times + "次哦", "猜对啦",
                            1);
                    f.setTitle("疯狂数字猜猜猜————2018最好玩的小游戏————NSG出品       作者:刘统帅");
                } else if (repeat == 1) {
                    i51.setIcon(i512);
                } else if (repeat == 2) {
                    i51.setIcon(i513);
                }
                times++;
                l7.setText("当前尝试次数为: " + times + " 次");
                answer.setText(null);
            }
        });

        s2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent reset) {
                // TODO Auto-generated method stub 此处放置重置触发事件
                System.exit(0);
            }
        });

    }

    // private static void Start() {
    // // TODO Auto-generated method stub
    //
    // }
}

游戏计算类

该类为本游戏的核心类,根据传入的数据进行正错大小的判定,并且返回相应的值给前台,前台再解析后输出给用户,保证了核心计算的安全性

package com.lenovo.demo;

/**
 * 游戏计算内核
 * @author Administrator
 *
 */
public class Game {

    private int a;
    private int b;

    public Game() {
        b = (int)(Math.random()*100+1);
    }

    public Game(int r) {
            b = (int)(Math.random()*500+1);
    }

    public Game(String abc) {
        b = (int)(Math.random()*1000+1);
    }

    public Game(int x,int y) {
        b = (int)(Math.random()*10000+1);
    }

    public int getA(int a) {
        int abo=4;
        if(a==b) {
            abo = 0;
        }else if(a>b){
            abo = 1;
        }else if(a2;
        }
        return abo;
    }

    public int setA(int abc) {
        this.a=abc;
        int repe = getA(a);
        return repe;
    }
}

音乐配置类

该类配置了四首音乐的方法,供前台调用

package com.lenovo.demo;

import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.URI;
import java.net.URL;

import javax.swing.JFrame;
/**
 * 音乐配置类
 */
public class Music extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    static AudioClip aau;
    static AudioClip aai;
    static AudioClip aao;
    static AudioClip aap;
    static File f;
    static URI uri;
    static URL url;

    // Music(){
    // bgMusic();
    // }
    @SuppressWarnings("null")
    public Music(int a) {

        try {
            f = new File("music/bar bar bar.wav");
            uri = f.toURI();
            url = uri.toURL(); // 解析地址
            aau = null;
            aau = Applet.newAudioClip(url);
            aai.stop();
        aao.stop();
        aap.stop();
        aau.loop(); // 循环播放
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("null")
    public static void Music1() {

        try {

            f = new File("music/Let's not fall in love.wav");
            uri = f.toURI();
            url = uri.toURL(); // 解析地址
            aai = null;
            aai = Applet.newAudioClip(url);
            aau.stop();
        aao.stop();
        aap.stop();
        aai.loop(); // 循环播放
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("null")
    public static void Music2() {

        try {
            f = new File("music/空空如也.wav");
            uri = f.toURI();
            url = uri.toURL(); // 解析地址
            aao = null;
            aao = Applet.newAudioClip(url);
            aau.stop();
        aai.stop();
        aap.stop();
        aao.loop(); // 循环播放
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("null")
    public static void Music3() {

        try {
            f = new File("music/143.wav");
            uri = f.toURI();
            url = uri.toURL(); // 解析地址
            aap = null;
            aap = Applet.newAudioClip(url);
            aai.stop();
        aao.stop();
        aau.stop();
        aap.loop(); // 循环播放
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void pause() {
        aai.stop();
        aao.stop();
        aap.stop();
        aau.stop();
    }


}

备注

稳定性

该游戏的代码未经任何优化,在加载时会将所有的资源调入内存,因此启动的一瞬间内存会爆满,但一定时间后又会恢复正常

兼容性

由于游戏采用了音乐播放,因此要完整的运行,需要在JRE1.8以上的版本

版本问题

这是本人第一次编写的小游戏,有诸多问题还请大神们海涵,多多指教。

你可能感兴趣的:(源代码开源)