Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------------------------------------------------------------------------------------------------
- //variables
- //------------------------------------------------------------------------------------------------------------
- Timer timer;
- int gamestate = 0;
- int count;
- String userName = "";
- String underScore = "_ ";
- //------------------------------------------------------------------------------------------------------------
- //Setup
- //------------------------------------------------------------------------------------------------------------
- void setup() {
- size(500, 600);
- timer = new Timer();
- }
- //------------------------------------------------------------------------------------------------------------
- //draw
- //------------------------------------------------------------------------------------------------------------
- void draw() {
- background(0);
- if (gamestate == 0) {
- //this section of code runs when the program first starts, or restarts
- beginningScreen();
- } else if (gamestate == 1) {
- //this is the actual game play screen
- gameScreen();
- } else if (gamestate == 2) {
- //this is the gameover screen, where users record their usernames
- gameoverScreen();
- } else if (gamestate == 3) {
- //this is the highscore & restart page
- highscoreScreen();
- }//end game screen conditionals
- }//end draw
- //------------------------------------------------------------------------------------------------------------
- //mousepressed
- //------------------------------------------------------------------------------------------------------------
- void mousePressed() {
- if (gamestate == 1) {
- count++;
- } //else if (gamestate == 1) {
- }
- //------------------------------------------------------------------------------------------------------------
- //keypressed
- //------------------------------------------------------------------------------------------------------------
- void keyPressed() {
- if ((gamestate == 0) && ((keyCode == ENTER) || keyCode == RETURN)) {
- gamestate = 1;
- timer.resetTimer();
- count = 0;
- } else if ((gamestate == 2) && ((keyCode == ENTER) || keyCode == RETURN)) {
- gamestate=3;
- } else if ((gamestate == 3) && ((keyCode == ENTER) || keyCode == RETURN)) {
- gamestate =0;
- }
- //this is the user typing code
- if (gamestate == 2) {
- if (userName.length()<3) {
- if (key==BACKSPACE) {
- if (userName.length()>0) {
- userName=userName.substring(0, userName.length()-1);
- } // length check
- } // not backspace
- else {
- userName+=key;
- } // add to text
- if (userName.length() == 0) {
- underScore = "_ ";
- } else if (userName.length() == 1) {
- underScore = " _";
- } else if (userName.length() == 2) {
- underScore = " _";
- } else if (userName.length() > 2) {
- underScore = "";
- } // func
- } else {
- userName ="";
- underScore = "_ ";
- }
- }
- }
- //------------------------------------------------------------------------------------------------------------
- //beginning screen
- //------------------------------------------------------------------------------------------------------------
- void beginningScreen() {
- textSize(32);
- text("Press RETURN to begin", width/2-150, height/2);
- text("click the mouse as many times as possible within the time.", 10, 10, width-10, height/2-10);
- }
- //------------------------------------------------------------------------------------------------------------
- //game screen
- //------------------------------------------------------------------------------------------------------------
- void gameScreen() {
- //runs the timer element to count to 10
- timer.checkTimer();
- //gets the current time based on variables from the timer class
- int currentTime = 10-(timer.currenttime-timer.lasttime)/1000;
- textSize(32);
- text("Time: ", width/2-125, height/2);
- textSize(100);
- text(currentTime, width/2-25, height/2);
- textSize(32);
- text("Clicks: ", width/2-125, height/2+125);
- textSize(100);
- text(count, width/2-25, height/2+125);
- }
- //------------------------------------------------------------------------------------------------------------
- //gameover screen
- //------------------------------------------------------------------------------------------------------------
- void gameoverScreen() {
- //game over high score screen
- textSize(32);
- text("gameover", 10, 50);
- text("Your score was: " + count, 10, 100);
- textSize(28);
- text("Type your three initial name:", 10, 150);
- textSize(46);
- fill(255, 255, 35);
- text(userName, 10, 250);
- fill(255);
- text(underScore, 10, 255);
- textSize(28);
- text("press return when done to save score.", 10, 300, width-10, height-10);
- }
- //------------------------------------------------------------------------------------------------------------
- //highscore screen
- //------------------------------------------------------------------------------------------------------------
- void highscoreScreen() {
- textSize(32);
- text("Highscore Table:", 10, 50);
- textSize(14);
- text("Your attempt:", 10, 75);
- fill(255, 255, 35);
- text(userName + " : " + count, 10, 100);
- fill(255);
- textSize(32);
- //there are now two variables:
- //count = the last attempts clicking score,
- //userName = the item entered on the last game screen
- //there is currently no highscore table to load from :(
- text("Press RETURN to restart game...", 10, height-10);
- }
- //------------------------------
- //-------------------------------------
- //end program
- //timer class begins here (new tab)
- class Timer {
- //we set last time to zero on first loop to init the timer correctly
- //we define three variables
- //one that is our current time, one that will hold a previous time, the third our time gap
- int lasttime;
- int currenttime;
- int check = 10000; //millis works in 1000th of seconds so 1000 = 1second
- Timer() {
- }
- void resetTimer(){
- lasttime = millis();
- }
- void checkTimer() {
- //current time = millis() is just for tidiness sake
- currenttime = millis();
- // we check to see if our current time is > last time + our check (1000)
- if (currenttime>(lasttime+check)) {
- //if it is we do something and reset last time =current time
- println("done");
- lasttime = currenttime;
- gamestate =2;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement