//QUICKTIMED// //Benjamin Mora-Davison// ///GAME LOGIC VARIABLES/// float timer; float numRounds; float round; float timerRoundModifier; boolean gameOver; boolean gameWon; boolean resetMssgShown; boolean gameOverMssgShown; ///SQUARE GENERATION VARIABLES/// //Number of squares int numSquares = 5; //Width and Height of the squares int squareSize = 50; //Gap Between squares int squareGap = 25; //Squares Y Pos float squareYPos; //Offset of Letters inside Keys float letterOffsetX = 10; float letterOffsetY = 5; ///SQUARE KEY DETECTION VARIABLES/// //Current square being checked for key presses int targetSquare; //Key State of each square/button //1 = d //2 = a //3 = w float square1State; float square2State; float square3State; float square4State; float square5State; void setup() { size(400, 400); background(0); println("If keys are not responding, restart the program."); startGame(); } //Initializes variables with their default values //Running this function also resets the game void startGame() { //Redraws the Background background(0); //Mssg Bools are set to false resetMssgShown = false; gameOverMssgShown = false; gameWon = false; timerRoundModifier = 0.3; squareYPos = height/2.5; generateRandomSquares(numSquares); timer = width; numRounds = 10; round = 1; targetSquare = 1; } //Generates number of squares void generateRandomSquares(int numSquares) { for (int i = 1; i<= numSquares; i++) { //Generates number bewteen 1-3 int tempRandom = (int)random(1, 4); //Draw key based off random number generated if (tempRandom == 1) { drawDKeyButton(i); } else if (tempRandom == 2) { drawAKeyButton(i); } else if (tempRandom == 3) { drawWKeyButton(i); } } } void draw() { drawScoreboard(); checkTimer(); } void drawScoreboard() { //Scoreboard Floats float roundRectWidth = width/numRounds; float roundRectHeight = height/numRounds; float roundRectPosY = height - roundRectHeight; //Draws White Squares (Rounds Remaining) for (int i = 0; i < numRounds; i++) { stroke(0); strokeWeight(1); fill(255); rect(i*roundRectWidth, roundRectPosY, roundRectWidth, roundRectHeight); } //Draws Grey Squares (Current Round) for (int i =0; i <= (round - 1); i++) { stroke(0); strokeWeight(1); fill(100); rect(i*roundRectWidth, roundRectPosY, roundRectWidth, roundRectHeight); } } //Changes applied based off check the timer void checkTimer() { if (timer > 0 && !gameWon) { gameOver = false; fill(0); stroke(0); rect(0, 1, width, 20); //Every frame the timer decreases based off of current round and modifiers timer -= 1 + (round*timerRoundModifier ); drawTimer(); } else { gameOver = true; drawResetKey(175, 250); if (gameWon) { drawGameWinKey(); if (!gameOverMssgShown) { println("You Win!"); //Prevents constant console spam gameOverMssgShown = true; } } } } //Draws visual representation of timer based off of current timer value void drawTimer() { fill(255); stroke(0); rect(0, 1, timer, 20); } void keyPressed() { detectInput(key, 'd', 1); detectInput(key, 'a', 2); detectInput(key, 'w', 3); //Resets the game if (key == ENTER && gameOver == true) { startGame(); } } //Takes current key being pressed and compares it to target key void detectInput(char currentKey, char targetKey, float triggerState) { //Ensures game isn't over if (timer > 0 && !gameOver) { if (currentKey == targetKey) { //Compares state of target square to trigger state provided if (targetSquare == 1 && square1State == triggerState) { //Changes state of key to 0 and draws checkmark on top to indicate proper button has been pressed square1State = 0; drawCheckmark(targetSquare); //Shifts target square forward by 1 targetSquare += 1; //Duplicated because no arrays allowed in this assignment } else if (targetSquare == 2 && square2State == triggerState) { square2State = 0; drawCheckmark(targetSquare); targetSquare += 1; } else if (targetSquare == 3 && square3State == triggerState) { square3State = 0; drawCheckmark(targetSquare); targetSquare += 1; } else if (targetSquare == 4 && square4State == triggerState) { square4State = 0; drawCheckmark(targetSquare); targetSquare += 1; } else if (targetSquare == 5 && square5State == triggerState) { square5State = 0; drawCheckmark(targetSquare); targetSquare += 1; checkRoundOver(); } } } } void newRound() { //If user has completed the game if (round == numRounds) { //The game is won gameWon = true; } else { //Start a new round background(0); //Generate a new set of keys to press generateRandomSquares(numSquares); //Reset target square, timer and increase the round targetSquare = 1; timer = width; round += 1; } } void checkRoundOver() { if (square5State == 0) { newRound(); } } void drawGameWinKey() { float posX = 175; float posY = 75; float letterOriginX = posX+letterOffsetX; drawKey(posX, posY); //Draws Checkmark fill(27, 173, 32); quad(letterOriginX, posY + 25, letterOriginX, posY + 30, letterOriginX + 10, posY+(squareSize*0.9), letterOriginX + 10, posY+(squareSize*0.8) ); quad(letterOriginX + 10, posY+(squareSize*0.9), letterOriginX + 10, posY+(squareSize*0.8), letterOriginX + 30, posY+(squareSize*0.1), letterOriginX + 30, posY+(squareSize*0.2) ); } void drawResetKey(float posX, float posY) { //Instructions on resetting the game if (!resetMssgShown) { println("Press ENTER to reset"); resetMssgShown = true; } drawKey(posX, posY); //Draw Reset Swirl stroke(0); noFill(); strokeWeight(5); arc(posX + squareSize/2, posY+ squareSize/2, 30, 30, -HALF_PI, PI); fill(0); strokeWeight(1); triangle(posX + squareSize/3, posY+ squareSize/2, posX + squareSize/3 - 12, posY+ squareSize/2, posX + squareSize/3 - 5, posY+ squareSize/4); } void drawCheckmark(float _targetSquare) { float tempRectPosX = (squareSize*_targetSquare)+(squareGap*_targetSquare)-(squareSize); drawKey(tempRectPosX, squareYPos); //Imaginary Origin where Letter inside rect begins float letterOriginX = tempRectPosX+letterOffsetX; //Draws Checkmark fill(0); quad(letterOriginX, squareYPos + 25, letterOriginX, squareYPos + 30, letterOriginX + 10, squareYPos+(squareSize*0.9), letterOriginX + 10, squareYPos+(squareSize*0.8) ); quad(letterOriginX + 10, squareYPos+(squareSize*0.9), letterOriginX + 10, squareYPos+(squareSize*0.8), letterOriginX + 30, squareYPos+(squareSize*0.1), letterOriginX + 30, squareYPos+(squareSize*0.2) ); } void drawDKeyButton(float num) { //Set button key state to D (1) int squareState = 1; setSquareState(num, squareState); //Pos X of Rect about to be drawn float tempRectPosX = (squareSize*num)+(squareGap*num)-(squareSize); drawKey(tempRectPosX, squareYPos); //Imaginary Origin where Letter inside rect begins float tempLetterOriginX = tempRectPosX+letterOffsetX; //Draws Letter D fill(255); stroke(0); strokeWeight(5); arc(tempLetterOriginX+5, squareYPos+25, 40, 36, -HALF_PI, HALF_PI); strokeWeight(1); fill(0); rect(tempLetterOriginX+3, squareYPos+5, squareSize/10, squareSize-10); } void drawAKeyButton(float num) { //Set button key state to A (2) int squareState = 2; setSquareState(num, squareState); //Pos X of Rect about to be placed float tempRectPosX = (squareSize*num)+(squareGap*num)-(squareSize); drawKey(tempRectPosX, squareYPos); //Imaginary Origin where Letter inside rect begins float letterOriginX = tempRectPosX+letterOffsetX; //Draws Letter A fill(0); quad(letterOriginX, squareYPos+(squareSize*0.9), letterOriginX+(squareSize/10), squareYPos+(squareSize*0.9), letterOriginX+17, squareYPos +(squareSize/10), letterOriginX+12, squareYPos+(squareSize/10)); quad(letterOriginX+25, squareYPos+(squareSize*0.9), letterOriginX+30, squareYPos+(squareSize*0.9), letterOriginX+17, squareYPos+(squareSize/10), letterOriginX+12, squareYPos+(squareSize/10)); quad(letterOriginX+6, squareYPos + 30, letterOriginX+6, squareYPos + (squareSize/2), letterOriginX+24, squareYPos + (squareSize/2), letterOriginX+24, squareYPos + 30); } void drawWKeyButton(float num) { //Sets square state to 3 (W key) int squareState = 3; setSquareState(num, squareState); ////Pos X of Rect about to be placed float tempRectPosX = (squareSize*num)+(squareGap*num)-(squareSize); drawKey(tempRectPosX, squareYPos); //Imaginary Origin where Letter inside rect begins float letterOriginX = tempRectPosX+letterOffsetX; //Draws W fill(0); quad(letterOriginX+5, squareYPos+(45), letterOriginX+10, squareYPos+(45), letterOriginX+5, squareYPos + 5, letterOriginX, squareYPos+ 5); quad(letterOriginX+5, squareYPos+(45), letterOriginX+10, squareYPos+(45), letterOriginX+17, squareYPos+(23), letterOriginX+12, squareYPos+(23)); quad(letterOriginX+17, squareYPos+(23), letterOriginX+12, squareYPos+(23), letterOriginX+20, squareYPos+(45), letterOriginX+25, squareYPos+(45)); quad(letterOriginX+20, squareYPos+(45), letterOriginX+25, squareYPos+(45), letterOriginX+30, squareYPos + 5, letterOriginX+25, squareYPos+ 5); } //Sets square state of squareNum provided to squareState provided void setSquareState(float squareNum, float squareState) { if (squareNum == 1) { square1State = squareState; } else if (squareNum == 2) { square2State = squareState; } else if (squareNum == 3) { square3State = squareState; } else if (squareNum == 4) { square4State = squareState; } else if (squareNum == 5) { square5State = squareState; } } //Draws a key at posX and posY provided void drawKey(float posX, float posY) { //Size of border surrounding each key (Shadows) float keyBorderSize = 7; noStroke(); fill(255); //Key rect(posX, posY, squareSize, squareSize); //Right fill(100); quad(posX + squareSize, posY, posX + squareSize, posY + squareSize, posX + squareSize + keyBorderSize, posY + squareSize + keyBorderSize, posX + squareSize + keyBorderSize, posY - keyBorderSize); //Bottom fill(150); quad(posX, posY + squareSize, posX - keyBorderSize, posY + squareSize + keyBorderSize, posX + keyBorderSize + squareSize, posY + squareSize + keyBorderSize, posX + squareSize, posY + squareSize); //Left fill(200); quad(posX, posY, posX, posY + squareSize, posX - keyBorderSize, posY + squareSize + keyBorderSize, posX - keyBorderSize, posY - keyBorderSize); //Top fill(250); quad(posX, posY, posX - keyBorderSize, posY - keyBorderSize, posX + keyBorderSize + squareSize, posY - keyBorderSize, posX + squareSize, posY); }