Skip to content

Commit 6327ff8

Browse files
authored
updated timer
1 parent 2c05e17 commit 6327ff8

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/Frame.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ public class Frame extends JPanel implements ActionListener, MouseListener, Mous
2121
Node cursor = null, goal = null;
2222
String typeAttempt = "";
2323
String mode = "";
24-
double averageTime = 0;
25-
long startTime = 0;
2624
long goalCounter = 0;
2725

26+
boolean tempToggle = true;
27+
double totalTime = 0d;
28+
long tempStartTime = 0l;
29+
2830
public static void main(String[] args) {
2931
new Frame();
3032
}
@@ -54,7 +56,6 @@ public Frame() {
5456
goal = new Node((int)(Math.random()*((this.getWidth()/size)-2)+1), (int)(Math.random()*((this.getHeight()/size)-2)+1), getRandomText());
5557
this.revalidate();
5658
this.repaint();
57-
startTime = System.currentTimeMillis();
5859
}
5960

6061
public void paintComponent(Graphics g) {
@@ -88,6 +89,7 @@ public void paintComponent(Graphics g) {
8889
g.drawString(mode, 10, 800);
8990
g.setFont(new Font("default", Font.BOLD, 16));
9091
g.drawString(goalCounter == 1 ? goalCounter + " goal" : goalCounter + " goals", 400, 800);
92+
double averageTime = goalCounter == 0 ? totalTime : totalTime/goalCounter;
9193
g.drawString(
9294
averageTime == 1 ? "Average Time: " + String.format("%.2f", averageTime) + " second" : "Average Time: " + String.format("%.2f", averageTime) + " seconds",
9395
600, 800);
@@ -128,16 +130,21 @@ public void keyPressed(KeyEvent e) {
128130
else if(currentKey == KeyEvent.VK_J) cursor.y++;
129131
else if(currentKey == KeyEvent.VK_K) cursor.y--;
130132
else if(currentKey == KeyEvent.VK_L) cursor.x++;
133+
else return;
134+
if(tempToggle) {
135+
tempStartTime = System.currentTimeMillis();
136+
tempToggle = false;
137+
}
131138
}
132139
}
133140

134141
if(mode.equalsIgnoreCase("Insert")) {
135142
if(currentKey == KeyEvent.VK_ESCAPE) mode = "Normal";
136143
else {
137144
if(currentKey == KeyEvent.VK_LEFT) cursor.x--;
138-
if(currentKey == KeyEvent.VK_DOWN) cursor.y++;
139-
if(currentKey == KeyEvent.VK_UP) cursor.y--;
140-
if(currentKey == KeyEvent.VK_RIGHT) cursor.x++;
145+
else if(currentKey == KeyEvent.VK_DOWN) cursor.y++;
146+
else if(currentKey == KeyEvent.VK_UP) cursor.y--;
147+
else if(currentKey == KeyEvent.VK_RIGHT) cursor.x++;
141148
else {
142149
char currentChar = e.getKeyChar();
143150
if(typeAttempt.length() == 1 && currentChar == goal.text.charAt(1)) typeAttempt += currentChar;
@@ -149,10 +156,8 @@ public void keyPressed(KeyEvent e) {
149156
goal = new Node((int)(Math.random()*((this.getWidth()/size))), (int)(Math.random()*((this.getHeight()/size))), getRandomText());
150157
typeAttempt = "";
151158
++goalCounter;
152-
double time = (System.currentTimeMillis()-startTime)/1000.0;
153-
if(averageTime == 0) averageTime = time;
154-
else { averageTime += time; averageTime /= 2; }
155-
startTime = System.currentTimeMillis();
159+
totalTime += (System.currentTimeMillis()-tempStartTime)/1000.0;
160+
tempToggle = true;
156161
}
157162
}
158163
}

0 commit comments

Comments
 (0)