Fix text that is misplaced

edited January 2017 in Library Questions

Every time I fail and press R to restart the startup text is very off coordinate to where it is when it first starts up.

/* How to Play : Use arrow-keys and press the key shoutcasted by the window (e.g. Press left if it says "! Left !").
 You win once you've reached a win streak of 20. You lose if you reach 0 HP.*/
import ddf.minim.*;
AudioPlayer spl;
Minim song;
PImage imgChrome, imgChrome2;

String[] symDanceKeys = {"←", "→", "↑", "↓"};
String[] ltrDanceKeys = {"Left", "Right", "Up", "Down"};

String cLTRSkey = "A";
String currentSYMkey = "←";
String cSk = currentSYMkey;
String currentLTRkey = "Left";
String cLk = currentLTRkey;

int aSec; 
int emoHP = 0;
int emoExpression = 0;
int danceStreak = 0;
int displayKeyX = 50;
int displayKeyY = 60;

boolean paused = false;
boolean tMin=false; //Timer minutes
boolean ending = false;
boolean displayKeyPressed=true;
boolean startScreen=false;
boolean winGame=false;
boolean endScreen=false;
boolean easy=false;
boolean medium=false;
boolean hard=false;
boolean expert=false;

float danceDiff = 2;
float timer = 0; // in seconds.
float sTimer; //Screen timer
float suTimer; //Startup Text timer
float dTimer; //Difficulty timer
float splTimer; // Song timer

PFont ar;

void setup() {
  song = new Minim(this);
  spl = song.loadFile("Tet.mp3");
  imgChrome2 = loadImage("chrome2.jpg");
  imgChrome = loadImage("chrome.jpg");
  background(#C6ba32);
  ar = createFont("ArialMT-48", 14, true);
  size(800, 500);
  frameRate(60);
}

void draw() {
  //if (splTimer>=4) {
  //  spl.play();
    //spl.loop(-0);
  //}
  splTimer += 0.0166666667;
  suTimer += 0.0166666667;
  dTimer += 0.0166666667;
  if (suTimer<=4) {
    startScreen=true;
  }
  if (startScreen) {
    textSize(14);
    fill(#00E8FF);
    noStroke();
    quad (30, 15, 500, 15, 480, 130, 10, 130);
    fill(#000000);
    text("To play, press the keys you're asked to press", 30, 30);
    text("as they are scrolling down the screen.", 30, 45);
    text("The more you press the correct key, the fast it gets.", 30, 60);
    text("The more you press the wrong key, your HP goes down.", 30, 75);
    text("If your HP hits 0, game over, but if your streak hits 20, you win!", 30, 90);
    text("Press the keys 1-4 on the startup screen to change the difficulty.", 30, 105);
    text("Press nothing for Super Easy mode!", 30, 120);
    if (suTimer>=4) {
      startScreen=false;
    }
  }

  textFont(ar, 14);
  if (ending) {
    endScreen=true;
    background(#C6ba32);
    displayText("Press R to restart the game!", (310));
    displayText("The game has ended..", (230));
    String endingText = "";
    if (winGame) {
      endScreen=true;
      endingText = "You have won the game, Congrats! ( ͡° ͜ʖ ͡°)";
    } else {
      endingText = "You have lost the game, You suck! ( ͡° ʖ̯ ͡°)";
    }
    displayText(endingText, (270));
    delay(500);
  } else {
    // Since the frameRate is 60, to calculate 1 second, add by 1/60 everytime draw() runs.
    //vv|LOOK HERE FOR TIMER|vv\\  
    timer += 0.0166666667;
    if (timer >=4 ) {
      println(mouseX+"X", mouseY+"Y");
      println( width+"W", height+"H");
      timer = 0;

      //^^|LOOK HERE FOR TIMER|^^\\

      if (displayKeyPressed) {
        // Selects a random number between 0 and the length of the danceKeys array/table.
        //vv|LOOK HERE FOR RANDOMIZER|vv\\
        int randomKey = int(random(ltrDanceKeys.length));
        cLk = ltrDanceKeys[randomKey];
        cSk = symDanceKeys[randomKey];
        //^^|LOOK HERE FOR RANDOMIZER|^^\\
        displayKeyY = 60;
        displayKeyX = int(random(50, width - 50));
        displayKeyPressed = false;
        text(cSk + cSk + cLk + cSk + cSk, displayKeyX, displayKeyY);
      } else {
        displayKeyPressed = true;
      }
    }
    // If four seconds hasn't passed yet..
    else if (!displayKeyPressed) {
      //Resets the background so the moving text effect is more pleasing.
      background(#C6ba32);
      drawEmoticon(emoExpression);

      displayKeyY += danceDiff;

      // If it reaches the bottom, you can't press the assigned key anymore.
      if (displayKeyY >= (height - 30)) {
        displayKeyPressed = true;
      } else {
        textSize(14);

        // Creates a wind effect after dance streak reaches 16.
        if (danceStreak >= 16) {
          for (int i = 1; i <= (danceStreak - 15); i++) {
            fill(0, ((4 - i) * 30));
            text(cSk + cSk + cLk + cSk + cSk, int(random(displayKeyX - 30, displayKeyX + 30)), displayKeyY - (i * 20));
          }
        }
        fill(0);
        textSize(20);
        text(cSk + cSk + cLk + cSk + cSk, displayKeyX, displayKeyY);
      }

      displayBars();
      displayHealth();
    }
    fill(#FFFFFF);
    //quad(1 x, 1 y, 2 x, 2 y, 3 x, 3 y, 4 x, 4 y);
    quad(676, 443, 737, 443, 737, 458, 676, 458);
    quad(676, 430, 737, 430, 737, 444, 676, 444);
    textSize(10);
    fill(#000000);
    text("Mins | Sec", 707, 440);
    textSize(14); 
    sTimer +=0.0166666667;
    //If sTimer is 0
    if (sTimer >=0) {
      //Display timer
      fill(#000000);
      text(sTimer, 711, 457);
    }
    if (sTimer>=60.000) {
      tMin = true;
      sTimer=0;
    }
    //If tMin is true
    if (tMin) {
      tMin=false;
      //Add a minute to the timer
      fill(#000000);
      text(aSec+".", 685, 457);
      aSec = aSec + 1;
    }
    if (sTimer>=0.000) {
      fill(#000000);
      text(aSec+".", 683, 457);
    }
  }
}

void keyPressed() {
  if (key=='r'||key=='R') {
    if (endScreen==true) {
      restart();
    }
  }
  if (key=='1') {
    if (dTimer<=4) {
      easy=true;
      medium=false;
      hard=false;
      expert=false;
      fill(#ffffff);
      quad(50, 420, 128, 420, 128, 440, 50, 440);
      fill(#000000);
      text("EASY", 70, 435);
    } else {
      if (dTimer>=4) {
        println("Cannot change difficulty at this time.");
      }
    }
  }
  if (key=='2') {
    if (dTimer<=4) {
      easy=false;
      medium=true;
      hard=false;
      expert=false;
      fill(#ffffff);
      quad(50, 420, 128, 420, 128, 440, 50, 440);
      fill(#000000);
      text("MEDIUM", 60, 435);
      emoHP=8;
      danceDiff=3;
    } else {
      if (dTimer>=4) {
        println("Cannot change difficulty at this time.");
      }
    }
  }
  if (key=='3') {
    if (dTimer<=4) {
      easy=false;
      medium=false;
      hard=true;
      expert=false;
      fill(#ffffff);
      quad(50, 420, 128, 420, 128, 440, 50, 440);
      fill(#000000);
      text("HARD", 70, 435);
      emoHP=4;
      danceDiff=6;
    } else {
      if (dTimer>=4);
      println("Cannot change difficulty at this time.");
    }
  }
  if (key=='4') {
    if (dTimer<=4) {
      easy=false;
      medium=false;
      hard=false;
      expert=true;
      fill(#ffffff);
      quad(50, 420, 128, 420, 128, 440, 50, 440);
      fill(#000000);
      text("EXPERT", 60, 435);
      emoHP=1;
      danceDiff=10;
    } else {
      if (dTimer>=4) {
        println("Cannot change difficulty at this time");
      }
    }
  }
  if (key=='P'||key=='p') {
    paused=true;
    noLoop();
    textSize(24);
    fill(#00E8FF);
    rect(335, 190, 160, 100);
    fill(#000000);
    text("Paused", 415, 245);
    textSize(12);
    text("Press ENTER to continue", 415, 280);
  } else {
    if (keyCode==ENTER&&paused==true) {
      paused=false;
      background(#C6ba32);
      drawHead();
      displayBars();
      displayHealth();
      draw();
      clear();
      loop();
    }
    // Problem #1; How2 detect WASD keys?
    if (!displayKeyPressed) {
      if (keyCode == LEFT) {
        if (cLk == ltrDanceKeys[0]) { // Selects the first (0) value from the danceKeys array.
          //println(danceKeys[0]) == String "Left"
          changeHealth(1);
        } else {
          paused=false;
          changeHealth(-1);
        }
      } else if (keyCode == RIGHT) {
        if (cLk == ltrDanceKeys[1]) {
          changeHealth(1);
        } else {
          paused=false;
          changeHealth(-1);
        }
      } else if (keyCode == UP) {
        if (cLk == ltrDanceKeys[2]) {
          changeHealth(1);
        } else {
          changeHealth(-1);
        }
      } else if (keyCode == DOWN) {
        if (cLk == ltrDanceKeys[3]) {
          changeHealth(1);
        } else {
          changeHealth(-1);
        }
      }
    }
  }
}

void changeHealth(int num) {
  displayKeyPressed = true;
  emoHP = emoHP + num;

  // Check if win or lose.
  //VV||MAX HP||VV\\
  if (emoHP > 10) {
    emoHP = 10;
    //^^||MAX HP||^^\\
  } else if (emoHP <= 0) {
    println("Game over!");
    ending = true;
  }
  if (danceStreak >= 20) {
    println("You have won the game!");
    ending = true;
    winGame = true;
    endScreen = true;
  }

  // Checking win streaks for Difficulty Management and Emoticon Control.
  if (num >= 0) {
    danceStreak ++;
    emoExpression = 0;
  } else {
    danceStreak --;
    emoExpression = 1;
    if (danceStreak<=0) {
      danceStreak=0;
    }
  }
  if (danceStreak<=1 && easy==true) {
    danceDiff = 2;
  } else {
    if (danceStreak >2&& easy==true) {
      danceDiff = danceStreak;
    }
  }
  if (danceStreak <= 1 && medium==true) {
    danceDiff = 3;
  } else {
    if (danceStreak >=3 && medium==true) {
      danceDiff = danceStreak;
    }
  }
  if (danceStreak <= 1 && hard==true) {
    danceDiff = 6;
  } else {
    if (danceStreak >=6 && hard==true) {
      danceDiff = danceStreak;
    }
  }
  if (danceStreak<=1 && expert==true) {
    danceDiff = 10;
    if (danceDiff > 10) {
      danceDiff = 10;
    }
  }
  // This is to prevent that "freezing" effect that occurs when a key is pressed (due to timer use).
  background(#C6ba32);

  drawEmoticon(emoExpression);
  displayBars();
  displayHealth();
}

void displayBars() {
  noStroke();
  fill(150);
  rect(0, 0, width, 30);
}

void displayHealth() {
  noStroke();
  fill(150);
  rect(0, 0, width, 30);

  fill(250, 250, ((10 - emoHP) * 10) + 100);

  // Using algebra to determine how long the HP bar should be.
  int totalTopBarLen = width;
  int healthBarLen = (totalTopBarLen / 10) * emoHP;
  rect(0, 0, healthBarLen, 30);
  displayText("(" + emoHP + " HP) (" + danceStreak + " Streak) (" + danceDiff + " Diff)", 20);
}
void displayText(String str, int y) {
  fill(0);
  textSize(16);
  textAlign(CENTER);
  text(str, width / 2, y);
}

//This uses math to make the faces
void drawEmoticon(int faceNum) {
  drawHead();

  // Mouth
  if (faceNum == 0) {
    if (emoHP <= 6) {
      line((width / 2) - 20, (height / 2) + 50, (width / 2) + 20, (height / 2) + 50);
    } else {
      // Arithmetic sequences formulae used to calculate the mouth-thingys' lengths.
      int mouthLenX = ((-5) + (emoHP - 1) * 5);
      int mouthLenY = ((75) + (emoHP - 1) * -5);
      line(width / 2, (height / 2) + 60, (width / 2) - mouthLenX, (height / 2) + mouthLenY);
      line(width / 2, (height / 2) + 60, (width / 2) + mouthLenX, (height / 2) + mouthLenY);
    }
  } else if (faceNum == 1) {
    line((width / 2) - 20, (height / 2) + 50, (width / 2) + 20, (height / 2) + 50);
    if (emoHP <= 6) {
      int mouthLenX = (50 + ((emoHP - 1) * -4));
      int mouthLenY = (80 + ((emoHP - 1) * -4));
      line((width / 2) - 20, (height / 2) + 50, (width / 2) - mouthLenX, (height / 2) + mouthLenY);
      line((width / 2) + 20, (height / 2) + 50, (width / 2) + mouthLenX, (height / 2) + mouthLenY);
    }
  }
}

void drawHead() {
  // Head
  noStroke();
  fill(250, 250, ((10 - emoHP) * 10) + 100); // Changes face-paleness according to health.
  ellipse(width / 2, height / 2, width / 3, width / 3);

  // Eyes
  stroke(50);
  strokeWeight(5);
  ellipse((width / 2) - 40, (height / 2) - 30, 20, 20);
  ellipse((width / 2) + 40, (height / 2) - 30, 20, 20);
}
void restart() {
  background(#C6ba32);
  danceDiff = 2;
  timer = 0; // in seconds.
  sTimer = 0; //Screen timer
  suTimer = 0; //Startup Text timer
  dTimer = 0; //Difficulty timer
  splTimer = 0; // Song timer
  paused = false;
  tMin=false; //Timer minutes
  ending = false;
  displayKeyPressed=true;
  startScreen=false;
  winGame=false;
  endScreen=false;
  easy=false;
  medium=false;
  hard=false;
  expert=false;
  aSec = 0; 
  emoHP = 10;
  emoExpression = 0;
  danceStreak = 0;
  endScreen=false;
  redraw();
}
Tagged:

Answers

Sign In or Register to comment.