Pausing a timer and displaying time correctly.
in
Programming Questions
•
4 months ago
I'm trying to make a score board that also has a timer which can be played paused and stopped. I've the score board bit working perfectly and certain parts of the timer but I have two problems.
The first one is that when a new minute starts the minutes column shows up correctly but the seconds carry on past 60, making it say something like 0:1:78. How can I make this change so it would display correctly?
My second issue is that I have no idea on how to pause the stopwatch. The length it goes on for is determined by a variable called 'length' which is set by the user. So far I have the start and stop button working but that's it, and the pause does the same as stop.
Here's my code:
- color fill1 = color(255,255,255);
- color fill2 = color(255,255,255);
- color text = color(0,0,0);
- color white = color(255,255,255);
- color yellow = color(255,255,255);
- color bar = color(0,0,0);
- color play = color(0,0,0);
- int score = 0;
- int score2 = 0;
- boolean mymouse = true;
- int five = 5;
- int half = 1;
- boolean release;
- String team1 = "Score";
- String team2 = "Score";
- int startTime =-1;
- int lenght=1;
- boolean start = false;
- void setup(){
- size(380,235);
- }
- void draw(){
- background(200);
- teamScoreone(width*1, height*1, 10, 10);
- teamScoretwo(width*1, height*1, 10, 10);
- int time = millis()-startTime;
- float barlenght = (((time/1000)/lenght)*4.25);
- int ellapsed = (millis()-startTime)/1000;
- fill(100,100,100,50);
- rect(5,220,250,10);
- fill(255);
- rect(230,160,25,22);
- rect(230,188,25,22);
- if(startTime==-1){
- time=0;
- }
- float barprog = barlenght;
- if (barprog > 255){
- barprog=255;
- }
- fill(0);
- //stop sign
- rect(237,194,10,10);
- //pause sign
- rect(237,165,4,10);
- rect(245,165,4,10);
- //starts the timer
- fill(255);
- rect(140,160,85,50);
- fill(0);
- if( startTime !=-1){
- text(ellapsed, 95, 195);
- text(ellapsed/60, 50, 195);
- text((millis()-startTime)/3600000, 5, 195);
- fill(bar);
- rect(5,220,barprog,10);
- } else {
- text("0", 95, 195);
- text("0", 50, 195);
- text("0", 5, 195);
- }
- if ((lenght*60-time/1000) <= 10){
- bar = color(255,0,0);
- }
- else{
- bar = color(255,255,255);
- }
- if ((lenght*60-time/1000) <= 20 && (lenght*60-time/1000) > 10){
- bar = color(255,210,0);
- }
- fill(play);
- //play sign
- triangle(165,165,165,205,205,185);
- fill(0,0,0,0);
- rect(265,5,110,150);
- fill(255);
- rect(270,10,50,50);
- rect(270,100,50,50);
- fill(0);
- textSize(32);
- text(lenght,270,90);
- text("+", 282,45);
- text("-", 285,135);
- text("min", 315,90);
- if (lenght*60==time/1000){
- startTime=-1;
- }
- }
- void teamScoretwo(float xloc, float yloc, int size, int num){
- fill(white);
- rect(140,100,110,50);
- textSize(32);
- fill(0,0,0);
- text(team1, 10+130, 90);
- text(score2, 30+130, 140);
- fill(0,0,0,0);
- rect(5+130,5,120,150);
- //minus
- fill(fill1);
- rect(140,10,50,50);
- textSize(32);
- fill(0,0,0);
- text("-", 25+130, 45);
- //plus
- fill(fill2);
- rect(70+130,10 ,50,50);
- textSize(32);
- fill(0,0,0);
- text("+", 85+130, 45);
- }
- //end of scoreboard two and start of score baord one
- //score board one
- void teamScoreone(float xloc, float yloc, int size, int num){
- fill(white);
- rect(10,100,110,50);
- textSize(32);
- fill(0,0,0);
- text(team2, 10, 90);
- text(score, 30, 140);
- fill(0,0,0,0);
- rect(5,5,120,150);
- // plus button
- fill(fill1);
- rect(10,10,50,50);
- textSize(32);
- fill(0,0,0);
- text("-", 25, 45);
- // minus button
- fill(fill2);
- rect(70,10,50,50);
- textSize(32);
- fill(0,0,0);
- text("+", 85, 45);
- }
- void mousePressed(){
- //mymouse=true;
- //score 2
- if ( mouseX > 10+130 && mouseX < 60+130 && mouseY > 10 && mouseY < 60){
- fill1 = yellow;
- score2 = score2-1;
- mymouse = false;
- }
- else{
- fill1 = color(255,255,255);
- }
- if ( mouseX > 70+130 && mouseX < 120+130 && mouseY > 10 && mouseY < 60){
- fill2 = yellow;
- score2 = score2 + 1;
- mymouse = false;
- }
- else{
- fill2 = color(255,255,255);
- }
- //scrore 1
- if ( mouseX > 10 && mouseX < 60 && mouseY > 10 && mouseY < 60){
- fill1 = yellow;
- score = score - 1;
- mymouse = false;
- }
- else{
- fill1 = color(255,255,255);
- }
- if ( mouseX > 70 && mouseX < 120 && mouseY > 10 && mouseY < 60){
- fill2 = yellow;
- score = score + 1;
- mymouse = false;
- }
- else{
- fill2 = color(255,255,255);
- }
- if(score <= 0){
- score=0;
- }
- if(score2 <= 0){
- score2=0;
- }
- if(mouseX > 140 && mouseX < 250 && mouseY > 160 && mouseY < 210){
- start = true;
- }
- //play button
- if( isOver() && startTime==-1 ){
- startTime = millis();
- play=color(0,255,0);
- }
- //stop button
- if (mouseX > 230 && mouseX < 255 && mouseY > 188 && mouseY < 210){
- startTime=-1;
- play=color(0,0,0);
- }
- //pause button
- if(mouseX > 230 && mouseX < 255 && mouseY > 160 && mouseY < 182 && startTime!=-1){
- startTime=-1;
- play=color(255,210,0);
- }
- if (mouseX > 260 && mouseX < 310 && mouseY > 10 && mouseY < 60){
- lenght=lenght+1;
- }
- if(mouseX > 260 && mouseX < 310 && mouseY > 100 && mouseY < 150){
- lenght=lenght-1;
- }
- if (lenght <= 1){
- lenght=1;
- }
- if (lenght >= 99){
- lenght=99;
- }
- }
- boolean isOver(){
- return( mouseX>=140&&mouseY>160&&mouseX<=225&&mouseY<=210);
- }
The pause button is at line 222 and the timer is from 51-65.
Thanks in advanced for any help!
1