1092 java概論

功能說明

一開始會有輸入玩家暱稱跟寵物暱稱的的介面,進入遊戲畫面後暱稱都還可以更改。
打地鼠遊戲有三個等級可供使用著自由選擇,透過遊戲所累積的分數會轉變成經驗值,當經驗值到達200後,寵物即會升等,升等後的寵物的外觀會更改。
level 1. 只有一種地鼠,打到後會加一分,六十秒後遊戲即會結束。
level 2. 有兩種地鼠,一種打到後會加一分,另一種打到後會扣兩分,六十秒後遊戲即會結束,但若遊戲途中分數到達負十分以下即會立刻結束。
level 3. 有三種地鼠,除了level 2的兩種以外,多了一種地鼠,該地鼠被打到後會立即結束遊戲。

程式碼

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.Image;
import java.awt.Toolkit;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import java.io.*;
import javax.sound.sampled.*;

public class Game extends JFrame implements KeyListener, ActionListener, MouseMotionListener, MouseListener {
private Container c;
private JMenuItem bot, stop, rebot, rule, cuser, cname, restart;
private JLabel lab1, lab2, hightscore;
private JPanel jpane;
private java.util.Random rg;
private int[] x = { 0, 470, 670, 870, 470, 670, 870, 470, 670, 870 };
private int[] y = { 0, 320, 320, 320, 455, 455, 455, 585, 585, 585 };
private int mode1, ms, hscore = -1000, mode2, mode3;
private double mt;
private int tmp1, tmp2, tmp3;
private javax.swing.Timer t;
private Boolean[] point1 = { false, false, false, false, false, false, false, false, false, false };
private Boolean[] point2 = { false, false, false, false, false, false, false, false, false, false };
private Boolean[] point3 = { false, false, false, false, false, false, false, false, false, false };
private Image img1, img2, img3;
private Font font = new Font("Helvetic", Font.BOLD, 16);
private Boolean click = false;
private JMenuItem easy,medium,hard;
String user, name;
JLabel lab11, lab21, lab31, lab41, lab51, lab61, lab71, lab81;
int level = 1;
int exp = 0;
Image img;
Graphics g;
JLabel pic = new JLabel(new ImageIcon("cow.gif"));
String check;
JLabel ava = new JLabel(new ImageIcon("ava1.png"));
Clip bgm;

public Game() {
super("Game");
}

public void open(){
init();
}

public void init()
{
user = JOptionPane.showInputDialog("Welcome!\nPlease inter your name: ");
name = JOptionPane.showInputDialog("Choose a nick name for your pet!");

mode1 = 0;
mode2 = 0;
rg = new Random();

img1 = Toolkit.getDefaultToolkit().getImage("5.png");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
img2 = Toolkit.getDefaultToolkit().getImage("bad.png");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
img3 = Toolkit.getDefaultToolkit().getImage("2.png");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setContentPane(new JLabel(new ImageIcon("bg3.png")));

c = getContentPane();
c.setLayout(null);
c.setBackground(Color.white);
lab1 = new JLabel("60 seconds left");
lab1.setBounds(400, 130, 200, 20);
lab1.setFont(font);
lab1.setForeground(Color.blue);
lab2 = new JLabel("scores:" + ms);
lab2.setBounds(620, 130, 200, 20);
lab2.setFont(font);
lab2.setForeground(Color.blue);
hightscore = new JLabel("highest scores:");
hightscore.setBounds(800, 130, 200, 20);
hightscore.setFont(font);
hightscore.setForeground(Color.blue);
bot = new JMenuItem("Start(Q)", KeyEvent.VK_Q);
stop = new JMenuItem("Pause(W)", KeyEvent.VK_W);
rebot = new JMenuItem("Resume(E)", KeyEvent.VK_E);
restart = new JMenuItem("Restart(R)",KeyEvent.VK_R);
rule = new JMenuItem("Rule(T)", KeyEvent.VK_T);
easy = new JMenuItem("Easy(A)", KeyEvent.VK_A);
medium = new JMenuItem("Medium(S)", KeyEvent.VK_S);
hard = new JMenuItem("Hard(D)", KeyEvent.VK_D);
cuser = new JMenuItem("Rename(U)", KeyEvent.VK_U);
cname = new JMenuItem("Rename(N)", KeyEvent.VK_N);
JMenuBar jmb = new JMenuBar();
setJMenuBar(jmb);
JMenu file3 = new JMenu("Mode(M)");
JMenu file2 = new JMenu("Function(F)");
JMenu file1 = new JMenu("Change(C)");
file2.setMnemonic(KeyEvent.VK_F);
file1.setMnemonic(KeyEvent.VK_C);
file1.add(cuser);
file1.add(cname);
file2.add(bot);
file2.add(stop);
file2.add(rebot);
file2.add(restart);
file2.add(rule);
file3.add(easy);
file3.add(medium);
file3.add(hard);
jmb.add(file1);
jmb.add(file2);
jmb.add(file3);
cuser.setActionCommand("cuser");
cname.setActionCommand("cname");
bot.setActionCommand("bot");
stop.setActionCommand("stop");
rebot.setActionCommand("rebot");
restart.setActionCommand("restart");
rule.setActionCommand("rule");
easy.setActionCommand("easy");
medium.setActionCommand("medium");
hard.setActionCommand("hard");


lab11 = new JLabel("User: ");
lab11.setBounds(20, 90, 65, 20);
lab11.setFont(font);
lab21 = new JLabel(user);
lab21.setBounds(70, 90, 200, 20);
lab21.setFont(font);
lab31 = new JLabel("Level: ");
lab31.setBounds(20, 120, 65, 20);
lab31.setFont(font);
lab41 = new JLabel(Integer.toString(level));
lab41.setBounds(70, 120, 200, 20);
lab41.setFont(font);
lab51 = new JLabel("Exp: ");
lab51.setBounds(20, 150, 200, 20);
lab51.setFont(font);
lab61 = new JLabel(Integer.toString(exp));
lab61.setBounds(70, 150, 200, 20);
lab61.setFont(font);
lab71 = new JLabel("Nick name: ");
lab71.setBounds(1180, 650, 150, 20);
lab71.setFont(font);
lab81 = new JLabel(name);
lab81.setBounds(1270, 650, 200, 20);
lab81.setFont(font);
pic.setBounds(960, 465, 400, 350);
ava.setBounds(20, 20, 60, 60);
c.add(lab11);
c.add(lab21);
c.add(lab31);
c.add(lab41);
c.add(lab51);
c.add(lab61);
c.add(lab71);
c.add(lab81);
c.add(pic);
c.add(ava);
c.add(lab1);
c.add(lab2);
c.add(hightscore);
bot.addKeyListener(this);
bot.addActionListener(this);
stop.addActionListener(this);
rebot.addActionListener(this);
restart.addActionListener(this);
rule.addActionListener(this);
cuser.addActionListener(this);
cname.addActionListener(this);
easy.addActionListener(this);
medium.addActionListener(this);
hard.addActionListener(this);
addMouseMotionListener(this);
addMouseListener(this);
addKeyListener(this);
t = new javax.swing.Timer(800, this);
setResizable(false);
setBounds(0, 0, 1375, 870);
setVisible(true);

try {
bgm = AudioSystem.getClip();
InputStream is = this.getClass().getClassLoader().getResourceAsStream("hello.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(is);
bgm.open(ais);

} catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
e.printStackTrace();
}
}

public void paint(Graphics g) {
super.paint(g);

for (int i = 1; i < 10; i++) {
if (point1[i])
// g.drawImage(img, x[i] - 50, y[i] - 50, 100, 100, this);
g.drawImage(img1, x[i] - 50, y[i] - 100, 130, 150, this);
if (point2[i])
g.drawImage(img2, x[i] - 50, y[i] - 100, 130, 150, this);
if (point3[i])
g.drawImage(img3, x[i] - 50, y[i] - 100, 130, 150, this);
}
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JMenuItem) {
menuItemFun(e);
}
if (e.getSource() == t) {
if (mt != 0.0) {
if (ms < -10) {
mt = 0.5;
ms = 0;
lab2.setText("scores:" + ms);
if (click) {
click = false;
bgm.stop();
String a = "Your score have been under -10 QQ";
JOptionPane.showMessageDialog(jpane, a, "Game over!", JOptionPane.QUESTION_MESSAGE);
if (ms > hscore) {
hscore = ms;
hightscore.setText("highest scores:" + hscore);
}
else if (hscore == 0 && ms < 0)
{
hscore = ms;
hightscore.setText("highest scores:" + hscore);
}
}
}
if(check == "easy")
{
mt = mt - 0.5;
mode1 = rg.nextInt(9) + 1;
tmp1 = mode1;
while (tmp1 > 0) {
if (point1[tmp1 % 10] == true)
point1[tmp1 % 10] = false;
else
point1[tmp1 % 10] = true;
tmp1 /= 10;
}
lab1.setText(mt + " seconds left");
repaint();
}
else if(check == "medium")
{
mt = mt - 0.5;
mode1 = rg.nextInt(9) + 1;
tmp1 = mode1;
mode2 = rg.nextInt(9) + 1;
tmp2 = mode2;
while (tmp1 > 0) {
if (point1[tmp1 % 10] == true)
point1[tmp1 % 10] = false;
else
point1[tmp1 % 10] = true;
tmp1 /= 10;
}

while (tmp2 > 0) {
if (point2[tmp2 % 10] == true)
point2[tmp2 % 10] = false;
else
point2[tmp2 % 10] = true;
tmp2 /= 10;
}

int i = 1;
while (i < 10) {
if (point1[i] == true && point2[i] == true) {
point2[i] = false;
}
i++;
lab1.setText(mt + " seconds left");
repaint();
}
}
else if(check == "hard")
{
mt = mt - 0.5;
mode1 = rg.nextInt(9) + 1;
tmp1 = mode1;
mode2 = rg.nextInt(9) + 1;
tmp2 = mode2;
mode3 = rg.nextInt(9) + 1;
tmp3 = mode3;
while (tmp1 > 0) {
if (point1[tmp1 % 10] == true)
point1[tmp1 % 10] = false;
else
point1[tmp1 % 10] = true;
tmp1 /= 10;
}
while (tmp2 > 0) {
if (point2[tmp2 % 10] == true)
point2[tmp2 % 10] = false;
else
point2[tmp2 % 10] = true;
tmp2 /= 10;
}
while (tmp3 > 0) {
if (point3[tmp3 % 10] == true)
point3[tmp3 % 10] = false;
else
point3[tmp3 % 10] = true;
tmp3 /= 10;
}
int i = 1;
while (i < 10) {
if (point1[i] == true && point2[i] == true) {
point2[i] = false;
}
if(point1[i] == true && point3[i] == true){
point3[i] = false;
}
if(point2[i] == true && point3[i] == true){
point3[i] = false;
}
i++;
lab1.setText(mt + " seconds left");
repaint();
}
}
} else if (mt == 0.0) {
if (click) {
click = false;
bgm.stop();
exp += ms;
lab61.setText(Integer.toString(exp));
String a = "You've got " + ms + " scores";
ms = 0;
JOptionPane.showMessageDialog(jpane, a, "Finish!", JOptionPane.QUESTION_MESSAGE);
if (ms > hscore) {
hscore = ms;
hightscore.setText("highest scores:" + hscore);
}
else if (hscore == 0 && ms < 0)
{
hscore = ms;
hightscore.setText("highest scores:" + hscore);
}

if (exp <= 1000 && exp > 201)
{
level = 2;
lab41.setText(Integer.toString(level));
JOptionPane.showMessageDialog(jpane,
"Your pet has been upgrade!", "Congratulations!",
JOptionPane.INFORMATION_MESSAGE);
pic = new JLabel(new ImageIcon("cow2.gif"));
pic.setBounds(960, 465, 400, 350);
c.add(pic);
}
else if (exp > 1000)
{
level = 3;
lab41.setText(Integer.toString(level));
JOptionPane.showMessageDialog(jpane,
"Your pet has been upgrade!", "Congratulations!",
JOptionPane.INFORMATION_MESSAGE);
pic = new JLabel(new ImageIcon("cow1.gif"));
pic.setBounds(960, 465, 400, 350);
c.add(pic);
}
}
}
}
}

public void menuItemFun(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("bot")) {
mt = 60;
ms = 0;
t.start();
click = true;
bgm.start();
}
if (e.getActionCommand().equalsIgnoreCase("stop")) {
bgm.stop();
t.stop();
}
if (e.getActionCommand().equalsIgnoreCase("rebot")) {
t.start();
bgm.start();
}
if (e.getActionCommand().equalsIgnoreCase("rule")) {
JOptionPane.showMessageDialog(jpane,
"You have 60 seconds, and you need to click the black circle as more as you can.", "How to Play",
JOptionPane.QUESTION_MESSAGE);
}
if (e.getActionCommand().equalsIgnoreCase("cuser")) {
user = JOptionPane.showInputDialog("Please inter new user name: ");
lab21.setText(user);
}
if (e.getActionCommand().equalsIgnoreCase("cname")) {
name = JOptionPane.showInputDialog("Please inter new nick name: ");
lab81.setText(name);
}
if(e.getActionCommand().equalsIgnoreCase("easy")){
check = "easy";
}
if(e.getActionCommand().equalsIgnoreCase("medium")){
check = "medium";
}
if(e.getActionCommand().equalsIgnoreCase("hard")){
check = "hard";
}
if(e.getActionCommand().equalsIgnoreCase("restart")){
bgm.start();
for(int i=0;i<10;i++)
{
point1[i] = false;
point2[i] = false;
point3[i] = false;
}
mt = 60;
ms = 0;
t.stop();
t.start();
}
}

public void mousePressed(MouseEvent e) {
if (click) {
int mx, my;
mx = e.getX();
my = e.getY();
for (int i = 1; i < 10; i++) {
if (point1[i] && !point2[i] && (x[i] - mx) * (x[i] - mx) + (y[i] - my) * (y[i] - my) < 2500) {
point1[i] = false;
ms++;
} else if (!point1[i] && point2[i] && (x[i] - mx) * (x[i] - mx) + (y[i] - my) * (y[i] - my) < 2500) {
point2[i] = false;
ms = ms - 2;
}
else if(!point1[i] && !point2[i] && point3[i] && (x[i] - mx) * (x[i] - mx) + (y[i] - my) * (y[i] - my) < 2500){
point3[i] = false;
mt = 60;
ms = 0;
t.stop();
JOptionPane.showMessageDialog(null,"Game Over!");
click = false;
bgm.stop();
}
lab2.setText("scores:" + ms);
repaint();
}
}

};

public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_NUMPAD7:
if (point1[1]) {
point1[1] = false;
ms++;
} else if (!point1[1]) {
ms--;
}
lab2.setText("scores:" + ms);
repaint();
break;
case KeyEvent.VK_NUMPAD8:
if (point1[2]) {
point1[2] = false;
ms++;
} else if (!point1[2]) {
ms--;
}
lab2.setText("scores:" + ms);
repaint();
break;
case KeyEvent.VK_NUMPAD9:
if (point1[3]) {
point1[3] = false;
ms++;
} else if (!point1[3]) {
ms--;
}
lab2.setText("scores:" + ms);
repaint();
break;
case KeyEvent.VK_NUMPAD4:
if (point1[4]) {
point1[4] = false;
ms++;
} else if (!point1[4]) {
ms--;
}
lab2.setText("scores:" + ms);
repaint();
break;
case KeyEvent.VK_NUMPAD5:
if (point1[5]) {
point1[5] = false;
ms++;
} else if (!point1[5]) {
ms--;
}
lab2.setText("scores:" + ms);
repaint();
break;
case KeyEvent.VK_NUMPAD6:
if (point1[6]) {
point1[6] = false;
ms++;
} else if (!point1[6]) {
ms--;
}
lab2.setText("scores:" + ms);
repaint();
break;
case KeyEvent.VK_NUMPAD1:
if (point1[7]) {
point1[7] = false;
ms++;
} else if (!point1[7]) {
ms--;
}
lab2.setText("scores:" + ms);
repaint();
break;
case KeyEvent.VK_NUMPAD2:
if (point1[8]) {
point1[8] = false;
ms++;
} else if (!point1[8]) {
ms--;
}
lab2.setText("scores:" + ms);
repaint();
break;
case KeyEvent.VK_NUMPAD3:
if (point1[9]) {
point1[9] = false;
ms++;
} else if (!point1[9]) {
ms--;
}
lab2.setText("scores:" + ms);
repaint();
break;
}
};

public static void main(String args[]) {
Game app = new Game();
app.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
app.open();
}

public void mouseDragged(MouseEvent e) {
};

public void mouseMoved(MouseEvent e) {
};

public void mouseReleased(MouseEvent e) {
};

public void mouseClicked(MouseEvent e) {
};

public void mouseEntered(MouseEvent e) {
};

public void mouseExited(MouseEvent e) {
};

public void keyReleased(KeyEvent e) {
};

public void keyTyped(KeyEvent e) {
};
}