forked from DVedaa/Type-Speed-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TyperManGame.java
326 lines (266 loc) · 7.36 KB
/
TyperManGame.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
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Timer;
public class TyperManGame extends JPanel implements KeyListener, ActionListener {
/* A class that inherits JPanel and defines methods of KeyListener and
ActionListener */
JTextField currentString;
JTextArea pointBox;
JTextField pauseBox;
ArrayList<String> bank;
/* An ArrayList is used here since it doesn't have a defined size and is
allocated dynamically. bank is used to store the dictionary words */
ArrayList<FallingWord> wordsOnBoard;
private int points;
private Timer time;
private int currentTime;
private int difficulty;
private boolean timerstate;
public TyperManGame() throws FileNotFoundException {
setSize(400, 400);
setLayout(null);
bank = Dictionary.getWords("words.txt");
setBackground(Color.BLACK);
currentString = new JTextField("");
// Initializes field to type the words
currentString.addActionListener(new ActionListener () {
@Override
public void actionPerformed(ActionEvent arg0) {
sendString();
}
});
currentString.setSize(400, 30);
currentString.setLocation(0, 342);
currentString.setBackground(Color.BLUE);
currentString.setEditable(true);
currentString.setForeground(Color.white);
currentString.setFont(currentString.getFont().deriveFont(20f));
pointBox = new JTextArea("Points");
pointBox.setEditable(false);
pointBox.setSize(40, 40);
pointBox.setBackground(Color.PINK);
pointBox.setForeground(Color.white);
pointBox.setLocation(360, 0);
pauseBox = new JTextField("Pause");
pauseBox.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (timerstate == false) {
resumeGame();
} else if (timerstate == true) {
pauseGame();
}
}
});
pauseBox.setEditable(false);
pauseBox.setSize(60,30);
pauseBox.setBackground(Color.MAGENTA);
pauseBox.setForeground(Color.white);
pauseBox.setLocation(0, 10);
add(pauseBox);
add(pointBox);
add(currentString);
setVisible(true);
time = new Timer(100, this);
startNewGame();
}
public void startNewGame() {
points = 0;
currentTime = 0;
wordsOnBoard = new ArrayList<FallingWord>();
difficulty = 0;
time.start();
timerstate = true;
}
public void pauseGame() {
time.stop();
timerstate = false;
pauseBox.setText("Resume");
}
public void resumeGame() {
time.start();
timerstate = true;
pauseBox.setText("Pause");
}
public void sendString() {
// Get the word typed by the user
String entry = currentString.getText();
// Clears the text field to allow the user to type further words
currentString.setText("");
if(wordIsOnBoard(entry)) {
// Points are increased based on length of the word and the difficulty.
points = points + entry.length() + difficulty;
// Update the points
pointBox.setText("" + points);
// Remove the word if successfully typed
removeWord(entry);
// The UI is reset
updateUI();
}
}
public boolean wordIsOnBoard(String entry) {
// This object traverses a collection of objects one by one.
java.util.Iterator<FallingWord> it = wordsOnBoard.iterator();
while(it.hasNext()) {
// Returns the next element
FallingWord current = it.next();
if(current.equals(entry)) {
return true;
}
}
// False if entered word does not match
return false;
}
private void removeWord(String entry) {
java.util.Iterator<FallingWord> it = wordsOnBoard.iterator();
boolean found = false;
while(it.hasNext() && !found) {
FallingWord current = it.next();
if(current.equals(entry)) {
// Checks if the current word is equal to the entered word
remove(current.box);
// If equal, it removes the word from the current iteration.
it.remove();
// Reinitialized to true so that not all the words are removed.
found = true;
}
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
currentTime++;
moveAllDown();
if(collison()) {
endGame();
}
adjustDifficulty();
}
private void adjustDifficulty() {
/* wordFrequency keeps decreasing as difficulty increases until it
* reaches 9 when it is again set to 10. It does not make sense dropping
* wordFrequency's value below 10 as the velocity of the falling words also
* increases with time (difficulty). */
int wordFrequency = 40 - ((difficulty * 2) / 5);
/* The counter increases from 1 to wordFrequency and then, as soon as
* currentTime is a multiple of wordFrequency, it resets to 0 and is
* responsible for creation of a new word. */
int counter = currentTime % wordFrequency;
if(wordFrequency < 10) {
wordFrequency = 10;
}
/* A new word is created (with increased velocity -- difficulty) as soon
* as the counter resets to 0. This 0 value will increasingly recur,
* causing an increased rate of word creation as well as an increased
* rate of their descent. */
if(counter == 0) {
difficulty++;
makeNewWord();
}
}
private void makeNewWord() {
String randomWord = getRandomWord();
FallingWord newWord = new FallingWord(randomWord, difficulty + 1);
wordsOnBoard.add(newWord);
}
private void endGame() {
time.stop();
End game = new End();
this.setVisible(false);
game.setVisible(true);
}
public boolean collison() {
java.util.Iterator<FallingWord> it = wordsOnBoard.iterator();
while(it.hasNext()) {
FallingWord current = it.next();
if(current.atBottom()) {
return true;
}
}
return false;
}
private void moveAllDown() {
java.util.Iterator<FallingWord> it = wordsOnBoard.iterator();
while(it.hasNext()) {
FallingWord current = it.next();
current.updateBox();
}
updateUI();
}
private String getRandomWord() {
Random ran = new Random();
int randomIndex = ran.nextInt(bank.size());
return bank.get(randomIndex);
}
@Override
public void keyPressed(KeyEvent arg0) {
;
}
@Override
public void keyReleased(KeyEvent arg0) {
;
}
@Override
public void keyTyped(KeyEvent key) {
;
}
private class FallingWord {
private String word;
private JTextField box;
private int boxVel;
private int xLoc;
private int yLoc;
public FallingWord(String word, int boxVel) {
Random ran = new Random();
xLoc = ran.nextInt(300);
yLoc = 0;
this.word = word;
this.boxVel = boxVel;
createBox();
}
public boolean atBottom() {
if(yLoc >=340) {
return true;
} else {
return false;
}
}
@Override
public boolean equals(Object other) {
if(other instanceof String) {
String otherword = (String) other;
return this.word.equals(otherword);
} else {
return false;
}
}
public void updateBox() {
yLoc = yLoc + boxVel;
box.setLocation(xLoc, yLoc);
if(yLoc > 235) {
box.setForeground(Color.white);
box.setBackground(Color.red);
} else if(yLoc > 110) {
box.setBackground(Color.yellow);
}
}
public void createBox() {
box = new JTextField(word);
box.setLocation(xLoc, yLoc);
box.setSize(8 * word.length() + 10, 30);
box.setBackground(Color.GREEN);
add(box);
}
}
}