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

郑重申明:

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

代码简介:

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

源代码

代码资源问题

游戏调用的资源全部放在项目的文件夹下,与src文件夹同名,这样子调用时只需要使用”images/xxx.jpg”之类的相对地址即可,并且可以方便的导出jar文件和资源包放在一起即可方便运行,该程序的资源包文件夹如下

资源包下载地址:链接: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 blocke.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() {@Overridepublic void actionPerformed(ActionEvent arg0) {// TODO Auto-generated method stubif (music.isVisible()) {music.setVisible(false);} else {music.setVisible(true);}}});// 音乐控制台监听器play.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {// TODO Auto-generated method stubmup.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() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubmup.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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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(a<b) {abo = 2;}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以上的版本

版本问题

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

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

  1. eclipse编写java游戏_Ballgame eclipse编写的java小游戏 - 下载 - 搜珍网

    压缩包 : BallGame.rar 列表 BallGame/.classpath BallGame/.project BallGame/AndroidManifest.xml BallGame/bi ...

  2. 【源代码】 用Eclipse编写的Java小程序——自动售卖机

    代码简介 郑重声明 本人为Java初学者,该代码仅为三小时作品,其中的写法不免会有不太妥当的地方,接受大神的指导,拒绝杠精 代码简介 该程序代码实现了自动售卖机的基本功能,如售卖饮料.商品补货.商品列 ...

  3. java写手机游戏_如何将自己编写的JAVA小游戏写到手机里?

    2019-06-19 怎么用java编写获取星期几的程序? import java.util.*; public class WeekDay { Calendar date = Calendar.ge ...

  4. main java game,playgame 一个JAVA编写的飞行小游戏,有基本完整的 框架,适合初学者参照学习 Other s 其他 238万源代码下载- www.pudn.com...

    文件名称: playgame下载 收藏√  [ 5  4  3  2  1 ] 开发工具: Java 文件大小: 7050 KB 上传时间: 2013-06-06 下载次数: 3 提 供 者: Lyq ...

  5. eclipse如何做java游戏,:#急需基于eclipse的JAVA小游戏源代码!!!#-南开游戏网...

    #急需基于eclipse的JAVA小游戏源代码!!!# 2020-11-07 10:21:13 广告 急需基于eclipse的JAVA小游戏源代码!!! maxueruby001@163.COM 楼主 ...

  6. java面向对象思想编写原谅帽小游戏 原谅帽游戏思路解析

    java面向对象思想编写原谅帽小游戏 原谅帽游戏思路解析: 面向对象思想 作品展示 类(游戏端)继承Frame: 首先要创建一个方法去让游戏开始和结束 使用text执行这个游戏的方法(需要有游戏标题和 ...

  7. 各种经典java小游戏_Java是这个世界上最好的语言!

    为什么? 请看TIOBE最新发布的编程语言排行榜: TIOBE开发语言排行榜每月更新一次,其结果可以用来检阅开发者的编程技能能否跟上趋势,或是否有必要作出战略改变,以及什么编程语言是应该及时掌握的. ...

  8. Java小游戏——贪吃蛇

    Java小游戏之贪吃蛇 系统目标 贪吃蛇是一个益智类游戏,通过本游戏的设计和实现,可以提升Java技术能力,提升自己独立开发的能力及掌握项目的开发流程. 开发环境 系统环境:Windows 开发工具: ...

  9. linux小型游戏系统设计,Linux平台下基于JAVA小游戏_设计文档.doc

    Linux课程设计报告 课题名称:<Linux平台下基于java小游戏设计> 专 业:2011级计算机科学与技术 组 长:043佘清泉 组 员:007陈威达 008陈学仁 026赖华标 0 ...

最新文章

  1. docker搭建ssr
  2. mac os10.11上使用proxychains
  3. 【控制】《多智能体系统的动力学分析与设计》徐光辉老师-第9章-不确定分数阶系统的包含控制
  4. QT:触摸屏支持手指触摸,增加touch事件touchevent,记录前后touch坐标并处理
  5. 在消费女性身材焦虑上,大码女装和BM风没有本质区别
  6. matlab都有什么接口,介绍MATLAB与C++的几种接口方式
  7. shell date常用运算命令
  8. socket 获取回传信息_java中使用网络通信(Socket)来传输对象
  9. linux下的备份管理rsync
  10. 【 Linux 网络虚拟化 】Openvswitch
  11. 常用screen参数
  12. win32com下载地址
  13. 双硬盘安装双系统 win7 + Ubuntu12
  14. Halo2学习笔记——设计之Proof和Field实现(3)
  15. OneNote笔记使用技巧/快捷键
  16. Cyberspace_Security_Learning
  17. 用开源的协同办公OA项目,做一个考勤系统
  18. 从三室心脏MRI影像检测主动脉瓣病变
  19. mac怎么做一段卡点音乐
  20. javascript匿名函数传值问题

热门文章

  1. 初识游戏客户端与服务器
  2. 区块链共识算法之POW
  3. 2021江西高考成绩查询微信可以吗,『江西高考成绩排名分数』2021年江西高考成绩几号可以查?...
  4. Oracle 11.2.0.4打PSU 11.2.0.4.161018
  5. 给你的电脑降降温!!!!!!
  6. 2024东南大学计算机考研信息汇总
  7. 今麦郎VS五谷道场:闯入者的天堂与地狱
  8. MISC device
  9. Excel功能的强大
  10. Mac文件夹图标修改软件:Folder Factory