-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypingGame.java
More file actions
261 lines (220 loc) · 8 KB
/
TypingGame.java
File metadata and controls
261 lines (220 loc) · 8 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
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class TypingGame extends JFrame implements KeyListener {
private List<String> allWords;
private List<TypeText> activeTypeTexts;
private JPanel gamePanel;
private Timer updateTimer;
private int index;
private Panel[][] panels;
private GridLayout panelsLayout;
private Player player;
private JLabel livesLabel;
private JLabel wordsTypedLabel;
private JLabel currWaveLabel;
private JButton pauseButton;
private int wordsReleasedInCurrWave;
private int waveWordCount;
private int currWave;
private boolean paused;
public TypingGame() {
this.setTitle("Typing Game");
this.setSize(1000, 900);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addKeyListener(this);
paused = false;
currWave = 1;
index = 0;
wordsReleasedInCurrWave = 0;
waveWordCount = 10; // first wave is 10 words
player = new Player("ding");
allWords = new ArrayList<String>();
activeTypeTexts = new ArrayList<TypeText>();
gamePanel = new JPanel();
panelsLayout = new GridLayout(35, 10);
panels = new Panel[panelsLayout.getRows()][panelsLayout.getColumns()];
player = new Player(JOptionPane.showInputDialog(gamePanel, "Enter your name: "));
livesLabel = new JLabel("Lives: " + player.getLives());
wordsTypedLabel = new JLabel("Score: " + player.getWordsTyped());
currWaveLabel = new JLabel("Wave: " + currWave);
pauseButton = new JButton("Pause");
pauseButton.addActionListener(makePauseListener());
gamePanel.setBackground(Color.WHITE);
gamePanel.setLayout(panelsLayout);
addPanelsToGamePanel();
panels[0][0].setLayout(new GridLayout(0, 1)); // so that the button will be aligned to the top left corner
panels[0][0].add(pauseButton);
panels[0][2].add(currWaveLabel);
panels[0][4].add(livesLabel);
panels[0][6].add(wordsTypedLabel);
readFile();
this.add(gamePanel);
this.setVisible(true);
startGame();
}
// starts the game
public void startGame() {
updateTimer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// add a TypeText to a random Panel in gamePanel every second, as long as max words for the wave has
// not been reached
if(wordsReleasedInCurrWave < waveWordCount) {
int randomCol = (int) (Math.random() * panelsLayout.getColumns()); // random column in first row
Panel parentPanel = panels[0][randomCol];
TypeText currTypeText = new TypeText(allWords.get(index), panels, player, wordsTypedLabel, livesLabel, activeTypeTexts);
parentPanel.add(currTypeText); // add the TypeText to the Panel
activeTypeTexts.add(currTypeText); // add the TypeText to activeTypeTexts
parentPanel.updateUI(); // update the UI of the Panel
currTypeText.setCurrPanel(parentPanel); // set the currPanel of the TypeText to the Panel it was added to
index++;
wordsReleasedInCurrWave++;
// if waveCount has been reached, decrease time between each TypeText/increase their speed,
// increase waveCount, and reset wordsReleasedInCurrWave to 0
} else if (wordsReleasedInCurrWave == waveWordCount && activeTypeTexts.isEmpty()) {
updateTimer.setInitialDelay(updateTimer.getInitialDelay() - 50);
waveWordCount += 10;
currWave++;
currWaveLabel.setText("Wave: " + currWave);
wordsReleasedInCurrWave = 0;
}
// updates all active TypeTexts
for(Iterator<TypeText> iterator = activeTypeTexts.iterator(); iterator.hasNext();) {
TypeText currTypeText = iterator.next();
currTypeText.update(iterator);
}
// if index reached the last word, reset it to 0 so words don't run out and reshuffle the words
if(index == allWords.size() - 1) {
index = 0;
Collections.shuffle(allWords);
}
updateTimer.restart();
// if user paused the game, stop the timer
if(paused) {
updateTimer.stop();
}
// if player ran out of lives, end the game
if(player.getLives() == 0) {
endGame();
}
}
});
updateTimer.start();
}
// read file and add the words to the array list of TypeTexts, also randomize the array list so that words
// will not always be in the same order
public void readFile() {
Scanner scanner;
try {
scanner = new Scanner(new File("words.txt"));
while(scanner.hasNextLine()) {
String word = scanner.nextLine();
allWords.add(word);
}
Collections.shuffle(allWords);
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
// adds a Panel (JPanel) to each box in GridLayout of gamePanel
public void addPanelsToGamePanel() {
for(int r = 0; r < panelsLayout.getRows(); r++) {
for(int c = 0; c < panelsLayout.getColumns(); c++) {
Panel panel = new Panel(r, c);
panels[r][c] = panel;
gamePanel.add(panel);
}
}
}
public void endGame() {
updateTimer.stop();
// Write name and score to the scores.txt file
try {
PrintWriter writer = new PrintWriter(new FileWriter("scores.txt", true));
writer.println(player.getName() + " " + player.getWordsTyped());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
List<Pair> nameScorePairs = new ArrayList<Pair>();
// read the names and scores, add to nameScorePairs ArrayList
try {
Scanner scoreScanner = new Scanner(new File("scores.txt"));
while(scoreScanner.hasNextLine()) {
String line = scoreScanner.nextLine();
String[] lineSplit = line.split(" ");
String name = lineSplit[0];
int score = Integer.parseInt(lineSplit[1]);
nameScorePairs.add(new Pair(name, score));
}
scoreScanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// sort the scores list in descending order
Collections.sort(nameScorePairs, Collections.reverseOrder());
// Heading for high scores
panels[7][4].setLayout(new FlowLayout(FlowLayout.RIGHT));
panels[7][4].add(new JLabel("High"));
panels[7][5].setLayout(new FlowLayout(FlowLayout.LEFT));
panels[7][5].add(new JLabel("Scores"));
panels[9][4].add(new JLabel("Name"));
panels[9][5].add(new JLabel("Score"));
// Show top 10 scores or however many scores there are in scores.txt if less than 10
for(int i = 0; i < 10 && i < nameScorePairs.size(); i++) {
panels[10 + i][4].add(new JLabel(nameScorePairs.get(i).getName()));
panels[10 + i][5].add(new JLabel("" + nameScorePairs.get(i).getScore()));
}
}
public ActionListener makePauseListener() {
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(paused == true) {
paused = false;
updateTimer.start();
} else if(paused == false) {
paused = true;
}
}
};
return listener;
}
@Override
public void keyPressed(KeyEvent e) {
// Have to use iterator because removing while iterating causes ConcurrentModificationException
for(Iterator<TypeText> iterator = activeTypeTexts.iterator(); iterator.hasNext();) {
TypeText currTypeText = iterator.next();
currTypeText.keyPressed(e, iterator);
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
new TypingGame();
}
}