-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainScreen.java
282 lines (221 loc) · 8.58 KB
/
MainScreen.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
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.awt.Color;
/**
* Main Menu Screen
*
* @author Ilya Kozorezov
* @author Rin Pham
* @version 1.0
* @since 1.0
*/
public class MainScreen extends JPanel {
private final int myMazeSize;
private RoomPanel myRoomPanel;
private static JLabel myStreakLabel;
private static final int STREAK_COMPLETE = 5;
private JButton myChallengeButton;
private static JLabel myScoreLabel;
private static final JButton[] myAnswerButtons = new JButton[4];
private static final JLabel[] myOptions = new JLabel[4];
private static Question myCurrentQuestion;
private static final JButton[] myMoveButtons = new JButton[4];
private static final Directions[] myDirections = new Directions[]{Directions.RIGHT, Directions.LEFT, Directions.UP, Directions.DOWN};
private static Directions myAttemptMove;
private static boolean myAnsweredQuestion = true;
private final int WINNER_POS;
private static JTextArea questionText;
/**
* Constructor
*
* @param theSize the size of the maze
*/
public MainScreen(final int theSize) {
super();
myMazeSize = theSize;
createState();
WINNER_POS = (theSize + 3) * (theSize + 3) - 1;
this.setLayout(null);
}
/**
* put new question on the GUI
*/
private void updateQuestion() {
if (!myAnsweredQuestion)
return;
myCurrentQuestion = Controller.getQuestion();
questionText.setText(myCurrentQuestion.getMyQuestion());
for (int i = 0; i < 4; i++) {
myOptions[i].setText(myCurrentQuestion.getMyOptions()[i]);
myOptions[i].setForeground(Color.yellow);
if (Controller.cheatsAllowed() && myCurrentQuestion.getMyAnswer() == i)
myOptions[i].setForeground(Color.green);
myAnswerButtons[i].setVisible(myCurrentQuestion.getMyOptions()[i] != null && !myCurrentQuestion.getMyOptions()[i].equals(""));
}
myAnsweredQuestion = false;
}
/**
* Remove question from GUI
*/
private void clearQuestion() {
questionText.setText("");
for (int i = 0; i < 4; i++) {
myOptions[i].setText("");
myMoveButtons[i].setVisible(Controller.canPlayerGo(myDirections[i]));
}
myAnsweredQuestion = true;
}
/**
* Only allow movement where player can move on maze
*/
private void limitMovement() {
for (int i = 0; i < 4; i++) {
myMoveButtons[i].setVisible(Controller.canPlayerGo(myDirections[i]));
}
}
/**
* See if game was won
*
* @return a boolean if the game has been won
*/
private boolean isGameWon() {
if (Controller.getPlayerPos() != WINNER_POS)
return false;
JOptionPane.showMessageDialog(this, "You have won Python Trivia Maze with a score of " + Controller.getScore() + "!", "WINNER", JOptionPane.PLAIN_MESSAGE);
Controller.quitGame();
MainFrame.goTo(new MainMenu());
return true;
}
/**
* See if answer is correct
*
* @param theChoice as the users choice
*/
private void checkAnswer(final int theChoice) {
clearQuestion();
if (myCurrentQuestion.getMyAnswer() == theChoice) {
myRoomPanel.updatePlayerPos(myAttemptMove);
myScoreLabel.setText("Score: " + Controller.updateScore(100));
myStreakLabel.setText("Streak: " + Controller.updateStreak());
if (Controller.getStreak() >= STREAK_COMPLETE) {
myChallengeButton.setVisible(true);
}
if (isGameWon())
return;
} else {
if (isGameLost())
return;
myRoomPanel.lockRoom(myAttemptMove);
myScoreLabel.setText("Score: " + Controller.updateScore(-100));
myStreakLabel.setText("Streak: " + Controller.resetStreak());
}
allowPress(myAnswerButtons, false);
allowPress(myMoveButtons, true);
limitMovement();
}
/**
* See if game is lost
*
* @return a boolean if the game has been lost
*/
private boolean isGameLost() {
if (!Controller.answeredWrong(myAttemptMove)) {
JOptionPane.showMessageDialog(this, "You Have Lost!\n You will be directed to the main menu", "Game Lost", JOptionPane.WARNING_MESSAGE);
Controller.quitGame();
MainFrame.goTo(new MainMenu());
return true;
}
return false;
}
/**
* Move player
*
* @param theDir the direction to move the player
*/
private void movePlayer(final Directions theDir) {
if (Controller.isRoomUnlocked(theDir)) {
myRoomPanel.updatePlayerPos((theDir));
limitMovement();
} else {
allowPress(myMoveButtons, false);
allowPress(myAnswerButtons, true);
updateQuestion();
myAttemptMove = theDir;
}
}
/**
* Allow buttons to be pressed
*
* @param theButtons the buttons to be allowed or denied
* @param areEnabled if the buttons should be enabled or not
*/
public void allowPress(final JButton[] theButtons, final boolean areEnabled) {
for (JButton button : theButtons) {
button.setEnabled(areEnabled);
}
}
/**
* Create the GUI
*/
private void createState() {
myRoomPanel = new RoomPanel(myMazeSize);
myRoomPanel.setBounds(10, 10, 400, 400);
this.add(myRoomPanel);
myMoveButtons[0] = Components.createNewButton("Right", 420, 100, e -> movePlayer(Directions.RIGHT));
this.add(myMoveButtons[0]);
myMoveButtons[1] = Components.createNewButton("Left", 420, 150, e -> movePlayer(Directions.LEFT));
this.add(myMoveButtons[1]);
myMoveButtons[2] = Components.createNewButton("Up", 420, 200, e -> movePlayer(Directions.UP));
this.add(myMoveButtons[2]);
myMoveButtons[3] = Components.createNewButton("Down", 420, 250, e -> movePlayer(Directions.DOWN));
this.add(myMoveButtons[3]);
myChallengeButton = Components.createNewButton("Challenge", 420, 300, e -> {
new PythonChallengeFrame();
myChallengeButton.setVisible(false);
myStreakLabel.setText("Streak: " + Controller.resetStreak());
});
this.add(myChallengeButton);
myChallengeButton.setVisible(false);
limitMovement();
JButton setting = Components.createNewButton("Setting", 600, 400, e -> new Settings());
this.add(setting);
JButton save = Components.createNewButton("Save", 705, 400, e -> Controller.saveGame());
this.add(save);
JButton exit = Components.createNewButton("Exit", 810, 400, e -> {
Controller.quitGame();
MainFrame.goTo(new MainMenu());
myAnsweredQuestion = true;
});
this.add(exit);
myStreakLabel = Components.createLabel(200, 410, 150, 50);
myStreakLabel.setText("Streak: " + Controller.getStreak());
this.add(myStreakLabel);
myScoreLabel = Components.createLabel(10, 410, 150, 50);
myScoreLabel.setText("Score: " + Controller.getScore());
this.add(myScoreLabel);
myAnswerButtons[0] = Components.createAnswerButton("A", 525, 150, e -> checkAnswer(0));
this.add(myAnswerButtons[0]);
myAnswerButtons[1] = Components.createAnswerButton("B", 525, 200, e -> checkAnswer(1));
this.add(myAnswerButtons[1]);
myAnswerButtons[2] = Components.createAnswerButton("C", 525, 250, e -> checkAnswer(2));
this.add(myAnswerButtons[2]);
myAnswerButtons[3] = Components.createAnswerButton("D", 525, 300, e -> checkAnswer(3));
this.add(myAnswerButtons[3]);
allowPress(myAnswerButtons, false);
myOptions[0] = Components.createAnswerLabel(605, 120, 400, 100);
this.add(myOptions[0]);
myOptions[1] = Components.createAnswerLabel(605, 170, 400, 100);
this.add(myOptions[1]);
myOptions[2] = Components.createAnswerLabel(605, 220, 400, 100);
this.add(myOptions[2]);
myOptions[3] = Components.createAnswerLabel(605, 270, 400, 100);
this.add(myOptions[3]);
questionText = Components.createTextArea(525, 20, 370, 120);
this.add(questionText);
JLabel myBackground = Components.createBackground("static/images/Background_2.jpg", 1000, 500);
this.add(myBackground);
}
}