-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava-for-beginner-text-adventure-game.html
437 lines (372 loc) · 11.3 KB
/
java-for-beginner-text-adventure-game.html
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
https://www.ryisnow.online/2021/04/java-for-beginner-text-adventure-game.html
<pre><code><span style="font-family: Arial; font-size: 15px;">import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Game {
JFrame window;
Container con;
JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel, playerPanel;
JLabel titleNameLabel, hpLabel, hpLabelNumber, weaponLabel, weaponLabelName;
Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
Font normalFont = new Font("Times New Roman", Font.PLAIN, 28);
JButton startButton, choice1, choice2, choice3, choice4;
JTextArea mainTextArea;
int playerHP, monsterHP, silverRing;
String weapon, position;
TitleScreenHandler tsHandler = new TitleScreenHandler();
ChoiceHandler choiceHandler = new ChoiceHandler();
ImageIcon logo = new ImageIcon(".//res//jackfrost.jpg");
public static void main(String[] args) {
new Game();
}
public Game(){
window = new JFrame();
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setLayout(null);
window.setIconImage(logo.getImage());
con = window.getContentPane();
titleNamePanel = new JPanel();
titleNamePanel.setBounds(100, 100, 600, 150);
titleNamePanel.setBackground(Color.black);
titleNameLabel = new JLabel("ADVENTURE");
titleNameLabel.setForeground(Color.white);
titleNameLabel.setFont(titleFont);
startButtonPanel = new JPanel();
startButtonPanel.setBounds(300, 400, 200, 100);
startButtonPanel.setBackground(Color.black);
startButton = new JButton("START");
startButton.setBackground(Color.black);
startButton.setForeground(Color.white);
startButton.setFont(normalFont);
startButton.addActionListener(tsHandler);
startButton.setFocusPainted(false);
titleNamePanel.add(titleNameLabel);
startButtonPanel.add(startButton);
con.add(titleNamePanel);
con.add(startButtonPanel);
window.setVisible(true);
}
public void createGameScreen(){
titleNamePanel.setVisible(false);
startButtonPanel.setVisible(false);
mainTextPanel = new JPanel();
mainTextPanel.setBounds(100, 100, 600, 250);
mainTextPanel.setBackground(Color.black);
con.add(mainTextPanel);
mainTextArea = new JTextArea("This is the main text are. This game is going to be great. I'm sure of it!!!!!!!");
mainTextArea.setBounds(100, 100, 600, 250);
mainTextArea.setBackground(Color.black);
mainTextArea.setForeground(Color.white);
mainTextArea.setFont(normalFont);
mainTextArea.setLineWrap(true);
mainTextArea.setWrapStyleWord(true);
mainTextArea.setEditable(false);
mainTextPanel.add(mainTextArea);
choiceButtonPanel = new JPanel();
choiceButtonPanel.setBounds(250, 350, 300, 150);
choiceButtonPanel.setBackground(Color.black);
choiceButtonPanel.setLayout(new GridLayout(4,1));
con.add(choiceButtonPanel);
choice1 = new JButton("Choice 1");
choice1.setBackground(Color.black);
choice1.setForeground(Color.white);
choice1.setFont(normalFont);
choice1.setFocusPainted(false);
choice1.addActionListener(choiceHandler);
choice1.setActionCommand("c1");
choiceButtonPanel.add(choice1);
choice2 = new JButton("Choice 2");
choice2.setBackground(Color.black);
choice2.setForeground(Color.white);
choice2.setFont(normalFont);
choice2.setFocusPainted(false);
choice2.addActionListener(choiceHandler);
choice2.setActionCommand("c2");
choiceButtonPanel.add(choice2);
choice3 = new JButton("Choice 3");
choice3.setBackground(Color.black);
choice3.setForeground(Color.white);
choice3.setFont(normalFont);
choice3.setFocusPainted(false);
choice3.addActionListener(choiceHandler);
choice3.setActionCommand("c3");
choiceButtonPanel.add(choice3);
choice4 = new JButton("Choice 4");
choice4.setBackground(Color.black);
choice4.setForeground(Color.white);
choice4.setFont(normalFont);
choice4.setFocusPainted(false);
choice4.addActionListener(choiceHandler);
choice4.setActionCommand("c4");
choiceButtonPanel.add(choice4);
// choice4.setContentAreaFilled(false); // Disable highlighting on press!!!
playerPanel = new JPanel();
playerPanel.setBounds(100, 15, 600, 50);
playerPanel.setBackground(Color.black);
playerPanel.setLayout(new GridLayout(1,4));
con.add(playerPanel);
hpLabel = new JLabel("HP:");
hpLabel.setFont(normalFont);
hpLabel.setForeground(Color.white);
playerPanel.add(hpLabel);
hpLabelNumber = new JLabel();
hpLabelNumber.setFont(normalFont);
hpLabelNumber.setForeground(Color.white);
playerPanel.add(hpLabelNumber);
weaponLabel = new JLabel("Weapon:");
weaponLabel.setFont(normalFont);
weaponLabel.setForeground(Color.white);
weaponLabel.setBackground(Color.red);
playerPanel.add(weaponLabel);
weaponLabelName = new JLabel();
weaponLabelName.setFont(normalFont);
weaponLabelName.setForeground(Color.white);
playerPanel.add(weaponLabelName);
playerSetup();
}
public void playerSetup(){
playerHP = 15;
monsterHP = 20;
weapon = "Knife";
weaponLabelName.setText(weapon);
hpLabelNumber.setText("" + playerHP);
townGate();
}
public void townGate(){
position = "townGate";
mainTextArea.setText("You are at the gate of the town. \nA guard is standing in front of you. \n\nWhat do you do?");
choice1.setText("Talk to the guard");
choice2.setText("Attack the guard");
choice3.setText("Leave");
choice4.setText("");
}
public void talkGuard(){
position = "talkGuard";
mainTextArea.setText("Guard: Hello stranger. I have never seen your face. \nI'm sorry but we cannot let a stranger enter our town.");
choice1.setText(">");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}
public void attackGuard(){
position = "attackGuard";
mainTextArea.setText("Guard: Hey don't be stupid!\n\nThe guard fought back and hit you hard.\n(You receive 3 damage)");
//playerHP = playerHP -3;
playerHP -=3;
hpLabelNumber.setText(""+playerHP);
choice1.setText(">");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}
public void crossRoad(){
position = "crossRoad";
mainTextArea.setText("You are at a crossroad.\nIf you go south, you will go back to the town.");
choice1.setText("Go north");
choice2.setText("Go east");
choice3.setText("Go south");
choice4.setText("Go west");
}
public void north(){
position = "north";
mainTextArea.setText("There is a river. \nYou drink the water and rest at the riverside. \n\n(Your HP is recovered by 2)");
playerHP = playerHP + 2;
hpLabelNumber.setText(""+playerHP);
choice1.setText("Go south");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}
public void east(){
position = "east";
mainTextArea.setText("You walked into a forest and found a Long Sword!\n\n(You obtained a Long Sword)");
weapon = "Long Sword";
weaponLabelName.setText(weapon);
choice1.setText("Go west");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}
public void west(){
position = "west";
mainTextArea.setText("You encounter a goblin!");
choice1.setText("Fight");
choice2.setText("Run");
choice3.setText("");
choice4.setText("");
}
public void fight(){
position = "fight";
mainTextArea.setText("Monter HP: " + monsterHP + "\n\nWhat do you do?");
choice1.setText("Attack");
choice2.setText("Run");
choice3.setText("");
choice4.setText("");
}
public void playerAttack(){
position = "playerAttack";
int playerDamage = 0;
if(weapon.equals("Knife")){
playerDamage = new java.util.Random().nextInt(3);
}
else if(weapon.equals("Long Sword")){
playerDamage = new java.util.Random().nextInt(12);
}
mainTextArea.setText("You attacked the monster and gave " + playerDamage + " damage!");
monsterHP = monsterHP - playerDamage;
choice1.setText(">");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}
public void monsterAttack(){
position = "monsterAttack";
int monsterDamage = 0;
monsterDamage = new java.util.Random().nextInt(6);
mainTextArea.setText("The monster attacked you and gave " + monsterDamage + " damage!");
playerHP = playerHP - monsterDamage;
hpLabelNumber.setText(""+playerHP);
choice1.setText(">");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}
public void win(){
position = "win";
mainTextArea.setText("You defeated the monster!\nThe monster dropped a ring!\n\n(You obtained a Silver Ring)");
silverRing = 1;
choice1.setText("Go east");
choice2.setText("");
choice3.setText("");
choice4.setText("");
}
public void lose(){
position = "lose";
mainTextArea.setText("You are dead!\n\nGAME OVER");
choice1.setText("");
choice2.setText("");
choice3.setText("");
choice4.setText("");
choice1.setVisible(false);
choice2.setVisible(false);
choice3.setVisible(false);
choice4.setVisible(false);
}
public void ending(){
position = "ending";
mainTextArea.setText("Guard: Oh you killed that goblin!?\nThank you so much. You are true hero!\nWelcome to our town!\n\nTHE END");
choice1.setText("");
choice2.setText("");
choice3.setText("");
choice4.setText("");
choice1.setVisible(false);
choice2.setVisible(false);
choice3.setVisible(false);
choice4.setVisible(false);
}
public class TitleScreenHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
createGameScreen();
}
}
public class ChoiceHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String yourChoice = event.getActionCommand();
switch(position){
case "townGate":
switch(yourChoice){
case "c1":
if(silverRing==1){
ending();
}
else{
talkGuard();
}
break;
case "c2": attackGuard();break;
case "c3": crossRoad();break;
}
break;
case "talkGuard":
switch(yourChoice){
case "c1": townGate(); break;
}
break;
case "attackGuard":
switch(yourChoice){
case "c1": townGate(); break;
}
break;
case "crossRoad":
switch(yourChoice){
case "c1": north(); break;
case "c2": east();break;
case "c3": townGate(); break;
case "c4": west();break;
}
break;
case "north":
switch(yourChoice){
case "c1": crossRoad(); break;
}
break;
case "east":
switch(yourChoice){
case "c1": crossRoad(); break;
}
break;
case "west":
switch(yourChoice){
case "c1": fight(); break;
case "c2": crossRoad(); break;
}
break;
case "fight":
switch(yourChoice){
case "c1": playerAttack();break;
case "c2": crossRoad(); break;
}
break;
case "playerAttack":
switch(yourChoice){
case "c1":
if(monsterHP <1 ){
win();
}
else{
monsterAttack();
}
break;
}
break;
case "monsterAttack":
switch(yourChoice){
case "c1":
if(playerHP <1 ){
lose();
}
else{
fight();
}
break;
}
break;
case "win":
switch(yourChoice){
case "c1": crossRoad();
}
break;
}
}
}