Problem with timer in game

Hello,

I am trying to make really simple click and point game. It would have 4 windows:

  1. Title

  2. Rules

  3. Game

  4. Scoreboard

I have programmed code that counts time and shows it at left upper corner. My problem is that i want it to start counting time from 3rd window, not since the program is launched. I am begginer and I tried to find something at forums but I failed :( Also I have wrtitten code that allows you to play again without restarting progam (changing window from 4th to 2nd and then to 3rd again). I have tried to reset(?) time while its on 2nd window, it would do job for this two problems, but it didnt worked. This code is quite long so i leave it on my dropbox.

Thanks in advance!

https://dropbox.com/sh/7t4p14l7z80divc/AADAweYCWBiA7XIhGefsoL_Fa?dl=0

Answers

  • edited April 2018 Answer ✓

    I modified your Time tab like this:

    by the usage if timer startTime we don't measure the total time but only the time since last reset

    since that the last line in setup() is now

     time.reset();
    }
    

    you also need time.reset(); wherever you want to reset the timer

    Chrisir ;-)

    class Czas {
    
      int startTime=millis();   // !!!!!!!!!!!!!!!!!! unnecessary since we do it in setup()
    
      void diplay() {
        fill(0);
        textSize(14);
        text(nf(m, 2) + ":" + nf(s, 2), 70, 18);
        textSize(14);
        text("Czas:", 24.5, 17.5);
      }
    
      void zegar() {
    
        m = minute();
        s = second();
      }
    
      void reset() {
        startTime=millis();  // !!!!!!!!!!!!!!!!!!
      }
    
    
      int minute() {
        return ((millis()-startTime) / (1000*60)) % 60 ; // !!!!!!!!!!!!!!!!!!
      }
    
      int second() {
        return ((millis()-startTime) / 1000) % 60 ; // !!!!!!!!!!!!!!!!!!
      }
    }
    
  • edited April 2018 Answer ✓

    modified tabs rules

    variable ms not needed here and before setup()

    class Zasady {
    
      void zasady() {
        background(250);
        textSize(13);
        text("Rules are very simple. You have 2 minutes to crack as many blocks as you can!", width/2, height/2);
        text("Good Luck!", width/2, height/2 + 17);
        text("(Press left mouse button to continue)", width/2, height - 15);
      }
      void mouseLeft() {
        if (mouseButton == LEFT) {
          mode = 2;
          l = 0;
          //ms = millis() - millis();
          time.reset();   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        }
      }
    }
    
  • Answer ✓

    my version for main tab

    //     CZAS W TRZECIM OKNIE( zrobić osobny czas dla tego okna i dla programu?)
    
    pStart p;                                  //game
    Czas time;                                 //time
    Licznik licz;                              //points
    Startowy start;                            //starting window
    End koniec;                                //scoreboard
    Zasady zasady;                             //rules
    
    float x = random(100, 500);                //rect pos / size
    float y = random(60, 280);
    
    float w = 25;
    float h = w;
    
    int mode;                                  // ekrany 0 - starting; 1 - rules; 2 - game; 3 - scoreboard
    
    int l = 0;                                 // points
    
    int s = 0;                                 //fuckin time
    int m = 0;
    // int ms = 0;
    //int mx = 0;
    
    void setup() {
      size(600, 360);
    
      p = new pStart();
      time = new Czas();
      licz = new Licznik();
      start = new Startowy();
      koniec = new End();
      zasady = new Zasady();
    
      colorMode(RGB);
    
      mode = 0;
    
      time.reset();
    }
    
    void draw() {
    
      switch (mode) {
    
      case 0: 
        start.Start();
        start.mouseRight();
        break; 
    
      case 1:
        zasady.zasady();
        zasady.mouseLeft();
        break; 
    
      case 2:
        background(250);
        time.zegar();
        println(millis());
        println(nf(m, 2) + ":" + nf(s, 2));
        time.diplay();
        licz.punkty();
        p.display();
        p.wybuch();
        p.nad(x, y, w, w);
        p.click();
        break; 
    
      case 3:
        koniec.Wynik();
        koniec.mouseRight();
        break; 
        //
      }//switch
      //
    }//func
    //
    
  • Everything works perfect, thanks!!!

Sign In or Register to comment.