-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlay.java
373 lines (327 loc) · 11.8 KB
/
Play.java
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
import javax.swing.*;
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class Play extends Frame implements ActionListener {
private TextField dr, lm;
private Button n, e, s, w,fiht, bag, save, load, use, exitbag;
private ImageIcon map;
private JLabel maplbl;
private Frame viewBag;
private Panel BagPanel;
public Label uname, namelbl, racelbl, hplbl, killslbl;
static List items = new List();
private Label uhp, ukills;
private ArrayList userinfo, getinfo;
private int nc, ec, sc, wc;
private int pos = 0, dir;
private String compass, inp = "y";
static Race userrace;
public int currenthp, kills = 0, scount = 0, mcount;
public static void main(String [] args) {
String name, race, gender = "Male";
//Asks the user for their name and race
name = input("What is your name?");
race = input("Hello there " + name + "." + " What is your prefered race?"
+ System.getProperty("line.separator")
+ "Mage, Warrior or Archer");
if(race.equalsIgnoreCase("Mage") || race.equalsIgnoreCase("Warrior")
|| race.equalsIgnoreCase("Archer")){
gender = input("Would you like to play as a male or female?");
if(gender.equalsIgnoreCase("Male") || gender.equalsIgnoreCase("Female")) {
print("You have chosen to be a " + gender + ".");}
else { print("Invalid gender"); System.exit(0);}}
else {print("Incorrect Race"); System.exit(0);}
if(race.equalsIgnoreCase("Mage")) {
userrace = new Mage(gender, name);
print("You have selected a " + gender + " " + race + ": " + userrace.getInfo());
}
else if(race.equalsIgnoreCase("Warrior")) {
userrace = new Warrior(gender, name);
print("You have selected a " + gender + " " + race + ": " + userrace.getInfo());
}
else if(race.equalsIgnoreCase("Archer")) {
userrace = new Archer(gender, name);
print("You have selected a " + gender + " " + race + ": " + userrace.getInfo());
}
else{print("Incorrect selection");}
Play g = new Play("Adventure Game");
g.setSize(500,500);
g.setVisible(true);
g.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);}
});
items.add("Health Potion");
}
//Main GUI for the game
Play(String title) {
super(title);
setLayout(new GridLayout(2,2));
map = new ImageIcon (getClass().getResource("map.png"));
maplbl = new JLabel(map);
add(maplbl);
Panel move = new Panel();
move.setLayout(new GridLayout(3,2));
add(move);
Panel extras = new Panel();
extras.setLayout(new GridLayout(2,2));
add(extras);
Panel stats = new Panel();
stats.setLayout(new GridLayout(3,2));
add(stats);
namelbl = new Label("Name: ");
uname = new Label(userrace.getName());
hplbl = new Label("HP: ");
uhp = new Label("100");
killslbl = new Label("Kills: ");
ukills = new Label("0");
stats.add(namelbl);
stats.add(uname);
stats.add(hplbl);
stats.add(uhp);
stats.add(killslbl);
stats.add(ukills);
n = new Button("North");
e = new Button("East");
s = new Button("South");
w = new Button("West");
fiht = new Button("Fight");
bag = new Button("Bag");
save = new Button("Save");
load = new Button("Load");
n.addActionListener(this);
e.addActionListener(this);
s.addActionListener(this);
w.addActionListener(this);
fiht.addActionListener(this);
bag.addActionListener(this);
save.addActionListener(this);
load.addActionListener(this);
move.add(n);
move.add(e);
move.add(s);
move.add(w);
extras.add(fiht);
extras.add(bag);
extras.add(save);
extras.add(load);
lm = new TextField("You moved: ");
lm.setEditable(false);
move.add(lm);
dr = new TextField("");
dr.setEditable(false);
move.add(dr);
}
//Method that will handle what happens when each button is pressed
public void actionPerformed(ActionEvent event){
if(event.getSource() == n) {
if(nc >= 5) {
System.out.println("You can no longer move north, please move in another direction");
}
else{
dr.setText("North");
nc += 1;
sc -= 1;
UserStats();
}
}
else if(event.getSource() == e) {
if(ec >= 5) {
System.out.println("You can no longer move east, please move in another direction");
}
else{
dr.setText("East");
ec += 1;
wc -= 1;
UserStats();
}
}
else if(event.getSource() == s) {
if(sc >= 5) {
System.out.println("You can no longer move south, please move in another direction");
}
else{
dr.setText("South");
sc += 1;
nc -= 1;
UserStats();
}
}
else if(event.getSource() == w) {
if(wc >= 5) {
System.out.println("You can no longer move west, please move in another direction");
}
else{
dr.setText("West");
wc += 1;
ec -= 1;
UserStats();
}
}
else if(event.getSource() == fiht) {
UserStats();
findFight();
}
else if(event.getSource() == bag) {
UserStats();
//items.add("Health Potion");
viewBag = new Frame();
viewBag.setSize(200,200);
viewBag.setVisible(true);
BagPanel = new Panel();
BagPanel.setLayout(new GridLayout(3,0));
use = new Button("Use Item");
exitbag = new Button("Exit Bag");
use.addActionListener(this);
exitbag.addActionListener(this);
BagPanel.add(items);
BagPanel.add(use);
BagPanel.add(exitbag);
viewBag.add(BagPanel);
}
else if(event.getSource() == save) {
UserStats();
ArrayList<String> userinfo = new ArrayList<String>();
userinfo.add(userrace.getName());
userinfo.add(String.valueOf(userrace.getHP()));
userinfo.add(String.valueOf(userrace.getKills()));
FileOutput(userinfo);
}
else if(event.getSource() == load) {
try {
FileInput();}
catch (IOException ex) {}
}
else if(event.getSource() == use) {
int item = items.getSelectedIndex();
if(item == 0 ) {
if(userrace.getHP() >= 100) {
System.out.println("You are already at maximum hp!");
}
else {
userrace.setHP(userrace.getHP() + 25);
System.out.println("You used a health potion, healing 25 HP!");
}
}
else if(item == 1 || item == 2) {
System.out.println("I need to find somewhere to bury these");
}
}
else if(event.getSource() == exitbag) {
viewBag.dispose();
}
}
//Method which will handle looking fights where the character is standing
public void findFight() {
Fight f;
if(ec > 0 && wc < 0 ) {
pos = 2;
print("You encounter a monkey!");
new Fight(pos, userrace);
}
else if(wc > 0 && ec < 0 ) {
pos = 4;
print("You encounter a Scorpion!");
new Fight(pos, userrace);
}
else{
print("You find nothing to fight");
}
}
//Methods which will add items to the bag depending on what the user has killed
public void addMonkeyItem(int count)
{
if(count == 1){
items.add("Monkey remains");
}
else {
System.out.println("As you already have Monkey remains, you leave the remains");
}
}
public void addScorpionItem(int count)
{
if(count == 1){
items.add("Scorpion remains");
}
else {
System.out.println("As you already have Scorpion remains, you leave the remains");
}
}
//Method that will set the users stats
public void SetStats(int hp, int ukills)
{
currenthp = hp;
kills = ukills;
userrace.setHP(currenthp);
userrace.setKills(kills);
}
//Method will update the GUI labels of the users HP and kills
public void UserStats()
{
uhp.setText(String.valueOf(userrace.getHP()));
ukills.setText(String.valueOf(userrace.getKills()));
}
//Method that will print the final end game messages
public void Gameover()
{
System.out.println("Player Info:");
System.out.println("Name: " + userrace.getName());
System.out.println("Kills: " + userrace.getKills());
System.out.println("");
System.out.println("***** CREDITS *****");
System.out.println("Creator: Ahmad Beldo (130228947)");
}
//Method will print the current users name, hp and amount of kills to a file
public void FileOutput(ArrayList info) {
String filename = "Score";
try {
PrintWriter outputStream = null;
outputStream = new PrintWriter(new FileWriter(filename + ".txt"));
outputStream.println("Name: " + info.get(0));
outputStream.println("HP: " + info.get(1));
outputStream.println("Kills: " + info.get(2));
outputStream.flush();
outputStream.close();
print("The results have successfully been saved to the text document called "
+ filename);
} catch (IOException ex) {
print("Error - The results could not be written to the file.");
ex.printStackTrace();
}
}
//Method will read the previous users name, hp and amount of kills from a file
public void FileInput() throws IOException {
ArrayList<String> getinfo = new ArrayList<String>();
String filename = "Score";
String line = null;
try {
BufferedReader inStream = new BufferedReader(new FileReader(filename + ".txt"));
while ((line = inStream.readLine()) != null) {
String[] info= line.split(" ");
getinfo.add(info[1]);
}
userrace.setName(getinfo.get(0));
uname.setText(getinfo.get(0));
userrace.setHP(Integer.parseInt(getinfo.get(1)));
uhp.setText(getinfo.get(1));
userrace.setKills(Integer.parseInt(getinfo.get(2)));
ukills.setText(getinfo.get(2));
inStream.close();
} catch (IOException ex) {
print("Error - The results could not be read from the file.");
print("If this is the first time running, please play the game first!");
}
}
//Method allow print and input to be used globally
public static String input(String a)
{
return JOptionPane.showInputDialog(a);
}
public static void print(String a)
{
JOptionPane.showMessageDialog(null, a);
}
}