Time reset
in
Programming Questions
•
1 month ago
Okay I have been searching the forums for a while (a few days). I have tried some of the things that I had read with no luck. So basically I am using a switch to run my menu>game>end screen. At the end screen it has a replay button. So I want the game to reset the time to start at 0:0:0, but it starts where I had left off.
here is my code for the.
- Ball b1, b2;
- SplashScreen start, end;
- Time t;
- int i = 0;
- void setup()
- {
- size(400, 400);
- smooth();
- start = new SplashScreen("play_Button.gif", width/2, -160, 0, 20);
- end = new SplashScreen("endScreen.gif", width/2, 600, 0, -20);
- b2 = new Ball(int(random(40, 50)), int(random(100, 200)), 20);
- b1 = new Ball(width/2, height/2, 20);
- b1.velocity.x = 0;
- b1.velocity.y = 0;
- t = new Time(width-90, 50);
- }
- void draw()
- {
- switch(i)
- {
- case 0:
- background(255);
- start.draw();
- break;
- case 1:
- background(150);
- fill(255, 0, 0);//red
- b1.draw();
- fill(0, 0, 255);
- b2.draw();
- b2.randomDirection();
- b2.zeroNoSpeed();
- t.draw();
- if (ballCollision(b1.location.x, b1.location.y,
- 20, b2.location.x, b2.location.y, 20) == true)
- {
- i = 2;
- }
- break;
- case 2:
- background(255);
- end.draw();
- break;
- default:
- break;
- }
- }
- void keyPressed()
- {
- if (key == CODED)
- if (keyCode == RIGHT)
- {
- println("right");
- b1.velocity.x = 1;
- }
- else if (keyCode == LEFT)
- {
- println("left");
- b1.velocity.x = -1;
- }
- else if (keyCode == UP)
- {
- println("up");
- b1.velocity.y = -1;
- }
- else if (keyCode == DOWN)
- {
- println("down");
- b1.velocity.y = 1;
- }
- }
- void keyReleased()
- {
- if (key == CODED)
- if (keyCode == RIGHT)
- {
- b1.velocity.x = 0;
- }
- else if (keyCode == LEFT)
- {
- println("a");
- b1.velocity.x = 0;
- }
- else if (keyCode == UP)
- {
- println("w");
- b1.velocity.y = 0;
- }
- else if (keyCode == DOWN)
- {
- println("s");
- b1.velocity.y = 0;
- }
- }
- void mousePressed() {
- if (i==0) {
- if (mouseX > width/2 - start.button.width/2 &&
- mouseX < width/2 + start.button.width/2 &&
- mouseY > height/2 - start.button.height/2 &&
- mouseY < height/2 + start.button.height/2)
- i = 1;
- }
- if (i == 2) {
- if (mouseX > width/2 - end.button.width/2 +49 &&
- mouseX < width/2 + end.button.width/2 -54 &&
- mouseY > height/2 - end.button.height/2 +93 &&
- mouseY < height/2 + end.button.height/2 -23)
- {
- b1.location.x = width/2;
- b1.location.y = width/2;
- b2.location.x = int(random(40, 50));
- b2.location.y = int(random(100, 200));
- i = 1;
- }
- }
- }
- boolean ballCollision(float x1, float y1, int d1, float x2, float y2, int d2)
- {
- float totalTime;
- float distances= dist(x1, y1, x2, y2);
- println("dist: " + distances);
- if (d1/2+d2/2 > distances)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- class Time
- {
- PVector tLocation;
- int mi;
- int s;
- int m;
- public Time(int _x, int _y)
- {
- tLocation = new PVector(_x, _y);
- }
- void draw()
- {
- clock();
- text(m+" : "+s+" : "+mi, tLocation.x, tLocation.y);
- }
- void clock()
- {
- mi = millis();
- m = (mi/(60*1000));
- s = (mi/(1000) %60);
- mi = mi % 1000;
- }
- }
1