/* Name: Samuel Barnes Due: October 1, 2018 Title: thimblerig Description: Which cup has the ball? Pay close attention! They move quickly... */ // cup 1 (position, speed) int cup1_X = (int) random(50, 350); int cup1_Y = (int) random(50, 350); int cup1_XSpeed = (int) random(1, 11); int cup1_YSpeed = (int) random(1, 11); // cup 2 (position, speed) int cup2_X = (int) random(50, 350); int cup2_Y = (int) random(50, 350); int cup2_XSpeed = (int) random(1, 11); int cup2_YSpeed = (int) random(1, 11); // cup 3 (position, speed) int cup3_X = (int) random(50, 350); int cup3_Y = (int) random(50, 350); int cup3_XSpeed = (int) random(1, 11); int cup3_YSpeed = (int) random(1, 11); // determines stage of game (0, 1, or 2) int stage = 0; // increases by 1 for every frame that cups are shuffled int stage1_Timer = 0; // true when player finishes game boolean gameOver = false; // determines visibility of ball int ballAlpha = 255; void setup() { // 400 by 400 pixels size(400, 400); // 60 frames per second frameRate(60); // print instructions for stage 0 println("When you're ready, press z and I'll shuffle the cups. Whatch the ball carefully!"); } void draw() { // STAGE 0 if (stage == 0) { // when player presses z, advance to stage 1 if (key == 'z') { stage = 1; } } // STAGE 1 else if (stage == 1) { // update position of cups cup1_X += cup1_XSpeed; cup1_Y += cup1_YSpeed; cup2_X += cup2_XSpeed; cup2_Y += cup2_YSpeed; cup3_X += cup3_XSpeed; cup3_Y += cup3_YSpeed; // if cups move off screen, reverse speed if (xOffScreen(cup1_X)) cup1_XSpeed *= -1; if (yOffScreen(cup1_Y)) cup1_YSpeed *= -1; if (xOffScreen(cup2_X)) cup2_XSpeed *= -1; if (yOffScreen(cup2_Y)) cup2_YSpeed *= -1; if (xOffScreen(cup3_X)) cup3_XSpeed *= -1; if (yOffScreen(cup3_Y)) cup3_YSpeed *= -1; // if ball is visible, decrease visibility if (ballAlpha > 0) ballAlpha--; // after 20 seconds, advance to stage 2 stage1_Timer++; if (stage1_Timer >= 20 * (int) frameRate) { // print instructions for stage 2 println("Select a cup with the mouse. Which do you think has the ball?"); // advance to stage 2 stage = 2; } } // STAGE 2 else if (stage == 2) { // if player presses mouse, determine outcome if (mousePressed && gameOver == false) { // reveal ball's location ballAlpha = 255; // end the game gameOver = true; // print outcome for player if (mouseX > cup1_X - 20 && mouseX < cup1_X + 20 && mouseY > cup1_Y - 20 && mouseY < cup1_Y + 20) println("You guessed correctly! Press x to play again."); else println("That's incorrect. Press x to play again."); } // if the player presses x, restart the game if (key == 'x' && gameOver) { // reset variables for next game cup1_X = (int) random(50, 350); cup1_Y = (int) random(50, 350); cup1_XSpeed = (int) random(1, 11); cup1_YSpeed = (int) random(1, 11); cup2_X = (int) random(50, 350); cup2_Y = (int) random(50, 350); cup2_XSpeed = (int) random(1, 11); cup2_YSpeed = (int) random(1, 11); cup3_X = (int) random(50, 350); cup3_Y = (int) random(50, 350); cup3_XSpeed = (int) random(1, 11); cup3_YSpeed = (int) random(1, 11); stage1_Timer = 0; gameOver = false; // print instructions for stage 0 println(""); println("- - - - - - - - - -"); println(""); println("When you're ready, press z and I'll shuffle the cups. Whatch the ball carefully!"); // return to stage 0 stage = 0; } } // draw background background(0); drawTiles(); // draw cups drawCup(cup1_X, cup1_Y); drawCup(cup2_X, cup2_Y); drawCup(cup3_X, cup3_Y); // draw ball drawBall(cup1_X, cup1_Y, ballAlpha); } // return true if x coordinate off screen boolean xOffScreen(int x) { if (x > width - 20 || x < 0 + 20) return true; else return false; } // return true if y coordinate off screen boolean yOffScreen(int y) { if (y > height - 20 || y < 0 + 20) return true; else return false; } void drawTiles() { boolean toggleTile = false; background(64, 66, 141); rectMode(CORNER); noStroke(); fill(48, 50, 110); for (int x = 0; x < width; x += 10) { toggleTile = !toggleTile; for (int y = 0; y < height; y += 10) { toggleTile = !toggleTile; if (toggleTile == true) rect(x, y, 10, 10); } } } void drawCup(int x, int y) { rectMode(CENTER); noStroke(); fill(220, 20, 60); rect(x, y, 40, 40); } void drawBall(int x, int y, int alpha) { ellipseMode(CENTER); noStroke(); fill(50, 205, 50, alpha); ellipse(x, y, 10, 10); }