Skip to content

Commit

Permalink
updated timer
Browse files Browse the repository at this point in the history
  • Loading branch information
BooleanCube authored Feb 20, 2022
1 parent 2c05e17 commit 6327ff8
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ public class Frame extends JPanel implements ActionListener, MouseListener, Mous
Node cursor = null, goal = null;
String typeAttempt = "";
String mode = "";
double averageTime = 0;
long startTime = 0;
long goalCounter = 0;

boolean tempToggle = true;
double totalTime = 0d;
long tempStartTime = 0l;

public static void main(String[] args) {
new Frame();
}
Expand Down Expand Up @@ -54,7 +56,6 @@ public Frame() {
goal = new Node((int)(Math.random()*((this.getWidth()/size)-2)+1), (int)(Math.random()*((this.getHeight()/size)-2)+1), getRandomText());
this.revalidate();
this.repaint();
startTime = System.currentTimeMillis();
}

public void paintComponent(Graphics g) {
Expand Down Expand Up @@ -88,6 +89,7 @@ public void paintComponent(Graphics g) {
g.drawString(mode, 10, 800);
g.setFont(new Font("default", Font.BOLD, 16));
g.drawString(goalCounter == 1 ? goalCounter + " goal" : goalCounter + " goals", 400, 800);
double averageTime = goalCounter == 0 ? totalTime : totalTime/goalCounter;
g.drawString(
averageTime == 1 ? "Average Time: " + String.format("%.2f", averageTime) + " second" : "Average Time: " + String.format("%.2f", averageTime) + " seconds",
600, 800);
Expand Down Expand Up @@ -128,16 +130,21 @@ public void keyPressed(KeyEvent e) {
else if(currentKey == KeyEvent.VK_J) cursor.y++;
else if(currentKey == KeyEvent.VK_K) cursor.y--;
else if(currentKey == KeyEvent.VK_L) cursor.x++;
else return;
if(tempToggle) {
tempStartTime = System.currentTimeMillis();
tempToggle = false;
}
}
}

if(mode.equalsIgnoreCase("Insert")) {
if(currentKey == KeyEvent.VK_ESCAPE) mode = "Normal";
else {
if(currentKey == KeyEvent.VK_LEFT) cursor.x--;
if(currentKey == KeyEvent.VK_DOWN) cursor.y++;
if(currentKey == KeyEvent.VK_UP) cursor.y--;
if(currentKey == KeyEvent.VK_RIGHT) cursor.x++;
else if(currentKey == KeyEvent.VK_DOWN) cursor.y++;
else if(currentKey == KeyEvent.VK_UP) cursor.y--;
else if(currentKey == KeyEvent.VK_RIGHT) cursor.x++;
else {
char currentChar = e.getKeyChar();
if(typeAttempt.length() == 1 && currentChar == goal.text.charAt(1)) typeAttempt += currentChar;
Expand All @@ -149,10 +156,8 @@ public void keyPressed(KeyEvent e) {
goal = new Node((int)(Math.random()*((this.getWidth()/size))), (int)(Math.random()*((this.getHeight()/size))), getRandomText());
typeAttempt = "";
++goalCounter;
double time = (System.currentTimeMillis()-startTime)/1000.0;
if(averageTime == 0) averageTime = time;
else { averageTime += time; averageTime /= 2; }
startTime = System.currentTimeMillis();
totalTime += (System.currentTimeMillis()-tempStartTime)/1000.0;
tempToggle = true;
}
}
}
Expand Down

0 comments on commit 6327ff8

Please sign in to comment.