Advertisement
Guest User

Code with Timer Function

a guest
Mar 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.43 KB | None | 0 0
  1.  
  2. //------------------------------------------------------------------------------------------------------------
  3. //variables
  4. //------------------------------------------------------------------------------------------------------------
  5. Timer timer;
  6. int gamestate = 0;
  7. int count;
  8. String userName = "";
  9. String underScore = "_  ";
  10.  
  11.  
  12. //------------------------------------------------------------------------------------------------------------
  13. //Setup
  14. //------------------------------------------------------------------------------------------------------------
  15. void setup() {
  16.   size(500, 600);
  17.   timer = new Timer();
  18. }
  19.  
  20.  
  21. //------------------------------------------------------------------------------------------------------------
  22. //draw
  23. //------------------------------------------------------------------------------------------------------------
  24. void draw() {
  25.  
  26.   background(0);
  27.  
  28.   if (gamestate == 0) {
  29.     //this section of code runs when the program first starts, or restarts
  30.     beginningScreen();
  31.   } else if (gamestate == 1) {
  32.     //this is the actual game play screen
  33.     gameScreen();
  34.   } else if (gamestate == 2) {
  35.     //this is the gameover screen, where users record their usernames
  36.     gameoverScreen();
  37.   } else if (gamestate == 3) {
  38.     //this is the highscore & restart page
  39.     highscoreScreen();
  40.   }//end game screen conditionals
  41. }//end draw
  42.  
  43.  
  44. //------------------------------------------------------------------------------------------------------------
  45. //mousepressed
  46. //------------------------------------------------------------------------------------------------------------
  47. void mousePressed() {
  48.   if (gamestate == 1) {
  49.     count++;
  50.   } //else if (gamestate == 1) {
  51. }
  52.  
  53.  
  54. //------------------------------------------------------------------------------------------------------------
  55. //keypressed
  56. //------------------------------------------------------------------------------------------------------------
  57. void keyPressed() {
  58.   if ((gamestate == 0) && ((keyCode == ENTER) || keyCode == RETURN)) {
  59.     gamestate = 1;
  60.     timer.resetTimer();
  61.     count = 0;
  62.   } else if ((gamestate == 2) && ((keyCode == ENTER) || keyCode == RETURN)) {
  63.     gamestate=3;
  64.   } else if ((gamestate == 3) && ((keyCode == ENTER) || keyCode == RETURN)) {
  65.     gamestate =0;
  66.   }
  67.  
  68.   //this is the user typing code
  69.   if (gamestate == 2) {
  70.     if (userName.length()<3) {
  71.       if (key==BACKSPACE) {
  72.         if (userName.length()>0) {
  73.           userName=userName.substring(0, userName.length()-1);
  74.         } // length check
  75.       } // not backspace
  76.       else {
  77.         userName+=key;
  78.       } // add to text
  79.  
  80.  
  81.       if (userName.length() == 0) {
  82.         underScore = "_  ";
  83.       } else if (userName.length() == 1) {
  84.         underScore = "  _";
  85.       } else if (userName.length() == 2) {
  86.         underScore = "   _";
  87.       } else if (userName.length() > 2) {
  88.         underScore = "";
  89.       } // func
  90.     } else {
  91.       userName ="";
  92.       underScore = "_  ";
  93.     }
  94.   }
  95. }
  96.  
  97. //------------------------------------------------------------------------------------------------------------
  98. //beginning screen
  99. //------------------------------------------------------------------------------------------------------------
  100. void beginningScreen() {
  101.   textSize(32);
  102.   text("Press RETURN to begin", width/2-150, height/2);
  103.  
  104.   text("click the mouse as many times as possible within the time.", 10, 10, width-10, height/2-10);
  105. }
  106.  
  107.  
  108. //------------------------------------------------------------------------------------------------------------
  109. //game screen
  110. //------------------------------------------------------------------------------------------------------------
  111. void gameScreen() {
  112.   //runs the timer element to count to 10
  113.   timer.checkTimer();
  114.   //gets the current time based on variables from the timer class
  115.   int currentTime = 10-(timer.currenttime-timer.lasttime)/1000;
  116.  
  117.   textSize(32);
  118.   text("Time: ", width/2-125, height/2);
  119.   textSize(100);
  120.   text(currentTime, width/2-25, height/2);
  121.  
  122.   textSize(32);
  123.   text("Clicks: ", width/2-125, height/2+125);
  124.   textSize(100);
  125.   text(count, width/2-25, height/2+125);
  126. }
  127.  
  128.  
  129. //------------------------------------------------------------------------------------------------------------
  130. //gameover screen
  131. //------------------------------------------------------------------------------------------------------------
  132. void gameoverScreen() {
  133.   //game over high score screen
  134.   textSize(32);
  135.   text("gameover", 10, 50);
  136.   text("Your score was: " + count, 10, 100);
  137.   textSize(28);
  138.   text("Type your three initial name:", 10, 150);
  139.   textSize(46);
  140.  
  141.   fill(255, 255, 35);
  142.   text(userName, 10, 250);
  143.   fill(255);
  144.   text(underScore, 10, 255);
  145.   textSize(28);
  146.   text("press return when done to save score.", 10, 300, width-10, height-10);
  147. }
  148.  
  149.  
  150. //------------------------------------------------------------------------------------------------------------
  151. //highscore screen
  152. //------------------------------------------------------------------------------------------------------------
  153. void highscoreScreen() {
  154.   textSize(32);
  155.   text("Highscore Table:", 10, 50);
  156.   textSize(14);
  157.   text("Your attempt:", 10, 75);
  158.   fill(255, 255, 35);
  159.   text(userName + " : " + count, 10, 100);
  160.   fill(255);
  161.  
  162.   textSize(32);
  163.  
  164.   //there are now two variables:
  165.   //count = the last attempts clicking score,
  166.   //userName = the item entered on the last game screen
  167.  
  168.   //there is currently no highscore table to load from :(
  169.  
  170.  
  171.  
  172.   text("Press RETURN to restart game...", 10, height-10);
  173. }
  174.  
  175.  
  176.  
  177. //------------------------------
  178. //-------------------------------------
  179.  
  180. //end program
  181. //timer class begins here (new tab)
  182. class Timer {
  183.   //we set last time to zero on first loop to init the timer correctly
  184.   //we define three variables
  185.   //one that is our current time, one that will hold a previous time, the third our time gap
  186.   int lasttime;
  187.   int currenttime;
  188.   int check = 10000; //millis works in 1000th of seconds so 1000 = 1second
  189.  
  190.   Timer() {
  191.    
  192.   }
  193.  
  194.   void resetTimer(){
  195.   lasttime = millis();
  196.   }
  197.  
  198.  
  199.   void checkTimer() {
  200.     //current time = millis() is just for tidiness sake
  201.     currenttime = millis();
  202.  
  203.     // we check to see if our current time is > last time + our check (1000)
  204.     if (currenttime>(lasttime+check)) {  
  205.       //if it is we do something and reset last time =current time
  206.       println("done");
  207.       lasttime = currenttime;
  208.       gamestate =2;
  209.     }
  210.   }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement