【源代码】⽤Eclipse 编写的Java ⼩游戏——疯狂猜猜猜
⽤编写的⼩游戏——疯狂猜猜猜
郑重申明:
本⼈为Java初学者,该代码是本⼈在学习Java⼀周后做出来的,因此⾮常多的代码可能是有问题的,如有⼤神指教,本⼈必将认真听改。代码简介:
这是⼀个⼩游戏,包含了⽤户在选定难度后进⼊游戏进⾏⼤⼩判定,且状态栏会显⽰相关状态,并且使⽤图⽚进⾏反馈提醒,此外,还调⽤了JRE1.8的⾳乐播放功能,四⾸⾳乐供⽤户选择。废话不多说,下⾯公布源代码。
源代码
代码资源问题
⽤户启动类
该类包含了⽤户启动后的代码调⽤,⾮常简单,保证了内部代码的安全。
启动内部类调⽤
该类负责在游戏开场加载动画及下⼀界⾯代码的调⽤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(); }}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1718
19
20
21package 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 = DefaultToolkit().getScreenSize().width;入门的java游戏小程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static int width = DefaultToolkit().getScreenSize().width; public static int height = DefaultToolkit().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) { it(0); } }
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
主界⾯类
该类代码负责主程序的界⾯和⾳乐控制台的界⾯建设以及游戏互动的数据传输,是游戏最主要的类,其中代码类型丰富,本⼈已添加备注 }
81
82package 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 .Music 1(); Music .Music 2(); Music .Music 3(); 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));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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 (33
0, 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);
5960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
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 作者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));124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论