//Interactive Toy // Jackson Lanaus // Target Practice // 9/26/2017 // creating the varibles, but not statign them as that is needed elsewhere for the reset key to work float circleLocX ; float circleLocY ; float circlelocX2 ; float circleLocY2 ; float speedX ; float speedY ; float speedX2 ; float speedY2 ; int score ; int shots ; float gridX ; float gridY ; float spacing ; void setup() { size(400, 400); noCursor(); // stating the variables (need to be stated in setup to make the reset key work) circleLocX = 56; circleLocY = 240; circlelocX2 = 183; circleLocY2 = 256; speedX = -2.5; speedY = 3; speedX2 = random(3-8); speedY2 = random(3-8); score = 0; shots = 15; gridX = 0; gridY = 0; spacing = 25; } void draw() { background(0); stroke(0); fill(100); grid(); displayStats(); target1(); target2(); reticle(); constrainTarget(); displayStats(); endScreen(); restart(); } void mousePressed() { firing(); } void endScreen() { // entering an end screen when all of "shots" are taken if (shots <= 0) { background(188, 30, 30); fill(255); textSize(40); text("Final score is: "+score, 60, height/2); text("please EXIT or hit R", 15, (height/2)+50); } } ///////////////////// // Function Def's // //////////////////// // display the stats at the top left void displayStats() { fill(255); textSize(20); text("Score: "+score, 10, 20); text("Shots: "+shots, 10, 50); } // keep the targets on screen void constrainTarget() { if ((circleLocX>width)||(circleLocX < 0)) { // moving the target along the X axis, until it reaches the full width speedX = speedX * -1; } if ((circleLocY>height)||(circleLocY < 0)) { // moving the target along the Y axis, until it reaches the full width speedY = speedY * -1; } if ((circlelocX2>width)||(circlelocX2 < 0)) { // moving the target 2 along the X axis, until it reaches the full width speedX2 = speedX2 * -1; } if ((circleLocY2>height)||(circleLocY2 < 0)) { // moving the target 2 along the Y axis, until it reaches the full width speedY2 = speedY2 * -1; } } // firing sequence void firing() { if (mousePressed == true) { shots = shots-1; // the "dist" means if the distance of mouseX and mouseY (mouse location) is less than 32 pixles of "circleLocX" and "circleLocY" (target location) then run this code if (dist(mouseX, mouseY, circleLocX, circleLocY)<=32) { score = score+1; // same as above but with the second target } else if (dist(mouseX, mouseY, circlelocX2, circleLocY2)<=16) { score = score+2; } } } // draw the reticle on the mouse void reticle() { //reticle noStroke(); fill(252, 0, 0); ellipse(mouseX, mouseY, 5, 5); } // draw the first target, and move it void target1() { //target fill(255, 180, 4); stroke(0); ellipse(circleLocX, circleLocY, 32, 32); circleLocX = circleLocX + speedX; circleLocY = circleLocY + speedY; } // draw the second target, and move it void target2() { //target 2 stroke(0); fill(random(255), random(255), random(255)); ellipse(circlelocX2, circleLocY2, 16, 16); circlelocX2 = circlelocX2 + speedX2; circleLocY2 = circleLocY2 + speedY2; } // background grid void grid() { stroke(27, 203, 40); strokeWeight(2); gridX = 0; while (gridX < width) { line(gridX, 0, gridX, height); gridX = gridX + spacing; } gridY = 0; while (gridY < height) { line(0, gridY, width, gridY); gridY = gridY + spacing; } } // if R is hit rest (run setup) // thanks to shirley tong's monsters attack for an excelent example: http://www-acad.sheridanc.on.ca/PROG14998/2015/interactive-toy/interactive_toy_22/index.html# void restart() { if (keyPressed) { if (key=='r' || key == 'R') { setup(); } } }