-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreen.java
More file actions
455 lines (430 loc) · 16 KB
/
Screen.java
File metadata and controls
455 lines (430 loc) · 16 KB
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
// this cotains the code to draw the game
// all of these import statements import libraries needed to make the game visible and working
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.desktop.QuitEvent;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Image;
import java.awt.Toolkit;
// this class extends JPanel and uses KeyListener and ActionListener to draw and detect button and key inputs
public class Screen extends JPanel implements KeyListener, ActionListener{
// this is the instance varaibels for the screen class
private Player p1;
private Tree t1;
private Tree t2;
private NPC n1;
private int xDiff;
private int yDiff;
private int currentStateIndex;
private int treasuresFound;
private int indexCounter;
private String direction;
private String chatMessage;
private String chatMessage2;
private String[] gameStates = new String[]{"opening", "prolouge", "item1", "item2", "item3"};
private String currentGameState;
private JLabel openingText;
private JLabel openingText2;
private JLabel chatText;
private JLabel chatText2;
private JButton startButton;
private JButton quitButton;
private ArrayList<Item> groundItems;
private ArrayList<Item> inventory;
private ArrayList<NPC> npcs;
private ArrayList<Tree> trees;
private ArrayList<Tree> sortedTrees;
private Image coinIcon;
private Image swordIcon;
private Image potionIcon;
private BufferedImage title;
private boolean rightKeyPressed;
private boolean leftKeyPressed;
private boolean upKeyPressed;
private boolean downKeyPressed;
private boolean[] itemsFound;
// this is the constructor for the screen class which intializes everything and sets up the screen
public Screen(){
setLayout(null);
npcs = new ArrayList<NPC>();
trees = new ArrayList<Tree>();
groundItems = new ArrayList<Item>();
inventory = new ArrayList<Item>();
//sets up the instance variables
p1 = new Player(1000,0);
trees.add(new Tree(1000,150));
trees.add(new Tree(1000,0));
for (int i = 0; i < 50; i++) {
trees.add(new Tree((int)(Math.random()*1921), (int)(Math.random()*1081)));
}
for (int i = 0; i < trees.size()-1; i++) {
int minIndex = i;
for (int j = i+1; j < trees.size(); j++) {
if (trees.get(j).getStartY()+ trees.get(j).getHeight() <= trees.get(minIndex).getStartY() + trees.get(minIndex).getHeight()) {
minIndex = j;
}
}
if (minIndex != i) {
Tree temp = trees.get(i);
trees.set(i, trees.get(minIndex));
trees.set(minIndex, temp);
}
}
groundItems.add(new Coin((int)(Math.random()*1921), (int)(Math.random()*1081)));
groundItems.add(new Sword((int)(Math.random()*1921), (int)(Math.random()*1081)));
groundItems.add(new Potion((int)(Math.random()*1921), (int)(Math.random()*1081)));
for (Tree tree : trees) {
tree.changeX(1000);
tree.changeY(1000);
}
for (Item item : groundItems) {
item.changeX(1000);
item.changeY(1000);
}
npcs.add(new NPC(600, 600, 1, groundItems.get(0), "item1"));
npcs.add(new NPC(0, 0, 2, groundItems.get(1), "item2"));
npcs.add(new NPC(1000, 1000, 3, groundItems.get(2), "item3"));
for (NPC npc : npcs) {
npc.changeX(1000);
npc.changeY(1000);
}
try {
title = ImageIO.read(new File("title.png"));
} catch (IOException e) {
System.out.println(e);
}
coinIcon = Toolkit.getDefaultToolkit().createImage("coin.gif");
swordIcon = Toolkit.getDefaultToolkit().createImage("SWORD.gif");
potionIcon = Toolkit.getDefaultToolkit().createImage("POTION.gif");
xDiff = 0;
yDiff = 0;
currentStateIndex = 0;
treasuresFound = 0;
indexCounter = 0;
direction = "straight";
currentGameState = "opening";
chatMessage = "";
chatMessage2 = "";
rightKeyPressed = false;
leftKeyPressed = false;
upKeyPressed = false;
downKeyPressed = false;
itemsFound = new boolean[]{false, false, false};
openingText = new JLabel("Welcome to Enchanted Quest in this game, you have to go around and talk to npcs");
openingText2 = new JLabel("to complete quests and find items.\n Use the arrow keys to move and space to interact with items");
chatText = new JLabel("");
chatText2 = new JLabel("");
startButton = new JButton("START");
quitButton = new JButton("QUIT");
startButton.setBounds(150, 300, 300, 30);
startButton.addActionListener(this);
quitButton.setBounds(150, 340, 300, 30);
quitButton.addActionListener(this);
openingText.setBounds(30,150,650,30);
openingText2.setBounds(30,185,650,30);
chatText.setBounds(5, 600, 590, 30);
chatText2.setBounds(5, 635, 590, 30);
openingText.setForeground(Color.WHITE);
openingText2.setForeground(Color.WHITE);
chatText.setForeground(Color.BLACK);
chatText2.setForeground(Color.black);
addKeyListener(this);
add(startButton);
add(quitButton);
add(openingText);
add(openingText2);
add(chatText);
add(chatText2);
//sets keylistener
setFocusable(true);
}
// this overrides the JPanel method to set the screen of 600 by 800
@Override
public Dimension getPreferredSize() {
//Sets the size of the panel
return new Dimension(600,800);
}
// this method paints all of the aspects of the game
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//draws background
drawBackground(g);
//Draw the game objects
for (Tree tree : trees) {
tree.drawIt(g, xDiff, yDiff);
}
for (NPC npc : npcs) {
npc.draw(g, xDiff, yDiff);
}
for (Item item : groundItems) {
item.draw(g, xDiff, yDiff);
}
drawBoundaries(g, xDiff, yDiff);
drawInventoryBox(g);
p1.drawIt(g, direction);
drawEndScreen(g);
drawTextbox(g, chatMessage, chatMessage2);
}
// this draws the background
public void drawBackground(Graphics g) {
if (currentGameState.equals("opening")) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 600, 600);
g.drawImage(title, 120, 0, 360, 360, null);
} else {
g.setColor(new Color(56, 183, 100));
g.fillRect(0, 0, 1920, 1080);
}
}
// this method draws the end screen
public void drawEndScreen(Graphics g) {
boolean allItemsFound = true;
for (boolean state : itemsFound) {
if (state == false) {
allItemsFound = false;
}
}
if (allItemsFound) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 600, 600);
openingText.setVisible(true);
openingText2.setVisible(true);
openingText.setText("You win");
openingText2.setText("");
}
}
// this method draws the inventory
public void drawInventoryBox(Graphics g) {
if (!currentGameState.equals("opening")) {
g.setColor(new Color(13, 13, 13, 75));
g.fillRect(200, 530, 200, 70);
g.setColor(new Color(255, 255, 255, 75));
g.fillRect(215, 540, 50, 50);
g.fillRect(275, 540, 50, 50);
g.fillRect(335, 540, 50, 50);
for (int i = 0; i < inventory.size(); i++) {
if (inventory.get(i).getName().equals("Coin")) {
g.drawImage(coinIcon, (i*59)+206, 532, 70, 70, null);
} else if (inventory.get(i).getName().equals("Sword")) {
g.drawImage(swordIcon, (i * 59) + 206, 532, 70, 70, null);
} else {
g.drawImage(potionIcon, (i * 59) + 206, 532, 70, 70, null);
}
}
}
}
// this method draws the dialouge box at the bottom
public void drawTextbox(Graphics g, String message, String message2) {
if (currentGameState.equals("opening") || currentGameState.equals("item3")) {
g.setColor(Color.BLACK);
g.fillRect(0, 600, 600, 200);
chatText.setText("");
chatText2.setText("");
} else {
g.setColor(Color.WHITE);
g.fillRect(0, 600, 600, 200);
chatText.setText(message);
chatText2.setText(message2);
}
}
// this dras the boundaries of the world
public void drawBoundaries(Graphics g, int xDiff, int yDiff) {
g.setColor(Color.BLACK);
g.fillRect(-1000+xDiff, -600+yDiff, 4000, 500);
g.fillRect(-600+xDiff, -1000+yDiff, 500, 4000);
g.fillRect(1980+xDiff, -1000+yDiff, 500, 4000);
g.fillRect(-1000+xDiff, 1200+yDiff, 4000, 500);
}
//implement methods of the KeyListener
public void keyPressed(KeyEvent e) {
// Key codes: 37 left, 38 up, 39 right, 40 down
// System.out.println( "key code: " + e.getKeyCode() );
if (e.getKeyCode() == 39) {
if (!currentGameState.equals("opening")) {
direction = "right";
p1.changeState("walk");
rightKeyPressed = true;
leftKeyPressed = false;
downKeyPressed = false;
upKeyPressed = false;
}
} else if (e.getKeyCode() == 37) {
if (!currentGameState.equals("opening")) {
direction = "left";
p1.changeState("walk");
leftKeyPressed = true;
rightKeyPressed = false;
downKeyPressed = false;
upKeyPressed = false;
}
} else if (e.getKeyCode() == 38) {
if (!currentGameState.equals("opening")) {
direction = "up";
p1.changeState("walk");
upKeyPressed = true;
rightKeyPressed = false;
downKeyPressed = false;
leftKeyPressed = false;
}
} else if (e.getKeyCode() == 40) {
if (!currentGameState.equals("opening")) {
direction = "down";
p1.changeState("walk");
downKeyPressed = true;
rightKeyPressed = false;
upKeyPressed = false;
leftKeyPressed = false;
}
} else if (e.getKeyCode() == 32) {
if (!currentGameState.equals("opening")) {
for (NPC npc : npcs) {
if (npc.getInRange()) {
if (npc.getRelatedItem().isFound()) {
currentStateIndex++;
itemsFound[indexCounter] = true;
indexCounter++;
npc.getRelatedItem().changeFound(false);
}
if (currentStateIndex == npc.getWantedIndex()) {
chatMessage = npc.getName() + ": "+ npc.getMessage();
chatMessage2 = npc.getMessage2();
npc.getRelatedItem().setVisible(true);
} else if (currentStateIndex > npc.getWantedIndex()) {
inventory.remove(npc.getRelatedItem());
chatMessage = npc.getName() + ": "+ npc.getEndMessage();
chatMessage2 = "";
} else {
chatMessage = npc.getName() + ": "+ npc.getPreMessage();
chatMessage2 = npc.getPreMessage2();
}
}
}
for (Item item : groundItems) {
if (item.getInRange()) {
chatMessage = "Found "+item.getName();
chatMessage2 = "";
inventory.add(item);
item.changeFound(true);
// currentStateIndex++;
treasuresFound++;
}
}
}
} else if (e.getKeyCode() == 79) {
if (indexCounter < 3) {
itemsFound[indexCounter] = true;
npcs.get(indexCounter).getRelatedItem().setVisible(false);
if (inventory.size() > 0 && inventory.get(0) == npcs.get(indexCounter).getRelatedItem()) {
inventory.remove(0);
}
indexCounter++;
currentStateIndex++;
}
}
// repaint();
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == 39) {
rightKeyPressed = false;
p1.changeState("idle");
} else if (e.getKeyCode() == 37) {
leftKeyPressed = false;
p1.changeState("idle");
} else if (e.getKeyCode() == 38) {
upKeyPressed = false;
p1.changeState("idle");
} else if (e.getKeyCode() == 40) {
downKeyPressed = false;
p1.changeState("idle");
}
}
public void keyTyped(KeyEvent e) {}
// this detects button presses for the start menu
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
currentStateIndex++;
currentGameState = gameStates[currentStateIndex%gameStates.length];
startButton.setVisible(false);
openingText.setVisible(false);
openingText2.setVisible(false);
quitButton.setVisible(false);
p1.setX(300);
p1.setY(300);
for (Tree tree : trees) {
tree.changeX(tree.getStartX());
tree.changeY(tree.getStartY());
}
for (Item item : groundItems) {
item.changeX(item.getStartX());
item.changeY(item.getStartY());
}
for (NPC npc: npcs) {
npc.changeX(npc.getStartX());
npc.changeY(npc.getStartY());
}
} else if (e.getSource() == quitButton) {
System.out.println("Quit game... Play again soon!");
System.exit(0);
}
}
// this method animates the game
public void animate() {
while (true) {
try {
Thread.sleep(5);
} catch (InterruptedException ex) {
// Thread.currentThread().interrupt();
}
if (rightKeyPressed) {
xDiff -= 1;
}
else if (leftKeyPressed) {
xDiff += 1;
}
else if (upKeyPressed) {
yDiff += 1;
}
else if (downKeyPressed) {
yDiff -= 1;
}
for (NPC npc : npcs) {
p1.checkNPCCollision(npc, xDiff, yDiff);
}
for (Item item : groundItems) {
p1.checkItemCollision(item, xDiff, yDiff);
if (item.isFound()) {
item.changeX(2000);
item.changeY(2000);
}
}
if (yDiff >= 400) {
yDiff -= 1;
}
if (yDiff <= -800) {
yDiff += 1;
}
if (xDiff >= 400) {
xDiff -= 1;
}
if (xDiff <= -1620) {
xDiff += 1;
}
currentGameState = gameStates[currentStateIndex%gameStates.length];
System.out.println(indexCounter);
repaint();
}
}
}