// The Bottles Bottle bottle1 = new Bottle(true, 7, 100, 150, 15, 10, 25, color(111,22,22), color(235,246,252), color(232,183,183)); Bottle bottle2 = new Bottle(true, 2, 50, 200, 15, 10, 25, color(37,76,142), color(235,246,252), color(254,255,18)); Bottle bottle3 = new Bottle(true, 8, 200, 75, 15, 20, 10, color(42,77,12), color(235,246,252), color(140,170,115)); Bottle bottle4 = new Bottle(true, 1, 50, 75, 15, 10, 10, color(191,185,173), color(235,246,252), color(234,175,54)); Bottle bottle5 = new Bottle(false, 5, 50, 75, 15, 10, 15, color(227,211,229), color(235,246,252), color(10,100,10,128)); Bottle bottle6 = new Bottle(true, 4, 125, 125, 20, 1, 10, color(45,14,52), color(235,246,252), color(211,179,216)); Bottle bottle7 = new Bottle(true, 6, 75, 50, 15, 10, 10, color(255), color(235,246,252), color(100,0,0)); Bottle bottle8 = new Bottle(false, 3, 50, 200, 15, 10, 15, color(165,79,28), color(235,246,252), -1); int totalBottleWidth = 700; // The bottle array Bottle[] bottles= new Bottle[8]; // Which bottle is held? Bottle currBottle = null; // The screen int windowW = 1000; int windowH = 800; // Win message String winMsg = ""; // The scale Scale theScale = new Scale(500, 50, color(200)); Bottle bottleOnLeft = null, bottleOnRight = null; // The sorting array ArrayList sortedBottles = new ArrayList(); int currDropLocationForSorted = -1; int startPosYSorted = 235;//225; // The max y-position of the sorting area int maxYForSortingArea = 250; // The current x-position of the red line in sorting area (-1 if none) int currXForRedLine = -1; // Variables for spacing the bottles int dx = 25; int startPosX = windowW/2-(totalBottleWidth+dx*(bottles.length-2))/2;//50; int startPosY = windowH-25;//975; void setup() { //size(1000, 1000); //size(screen.width, screen.height); size(windowW,windowH); background(255); // Bottles into array bottles[0] = bottle1; bottles[1] = bottle2; bottles[2] = bottle3; bottles[3] = bottle4; bottles[4] = bottle5; bottles[5] = bottle6; bottles[6] = bottle7; bottles[7] = bottle8; // Initial bottle location setup for (int i=0; i < bottles.length; i++) { if (i>0) { bottles[i].draw(bottles[i-1].currX + bottles[i-1].w + dx, startPosY); } else { bottles[i].draw(startPosX,startPosY); } bottles[i].setHomePosition(bottles[i].currX, bottles[i].currY); } // Draw the scale theScale.draw(width/2 - theScale.w/2, 475+55/*height/2 + 125*/); } void keyTyped() { // Reset the game if a key is pressed background(255); sortedBottles = new ArrayList(); currBottle = null; bottleOnLeft = bottleOnRight = null; currXForRedLine = -1; theScale.currSide = ' '; // Initial bottle location setup for (int i=0; i < bottles.length; i++) { if (i>0) { bottles[i].draw(bottles[i-1].currX + bottles[i-1].w + dx, startPosY); } else { bottles[i].draw(startPosX,startPosY); } bottles[i].setHomePosition(bottles[i].currX, bottles[i].currY); bottles[i].isOnRight = false; bottles[i].isOnLeft = false; } } void draw() { background(255); fill(230); rect(5,5,width-10,maxYForSortingArea-5); if (winMsg.length() > 0 && sortedBottles.size() == bottles.length) { textAlign(CENTER); stroke(0); fill(100,0,0); textSize(48); text(winMsg, width/2, startPosY-100); } theScale.draw(theScale.currX, theScale.currY); theScale.drawHeavier(theScale.currSide); for (int i=0; i < bottles.length; i++) { if (currBottle != bottles[i]) bottles[i].draw(bottles[i].currX, bottles[i].currY); } if (currBottle != null) { currBottle.draw(currBottle.currX, currBottle.currY); } if (currXForRedLine >= 0) { stroke(255,0,0); line(currXForRedLine, 25, currXForRedLine, startPosYSorted); stroke(0); } if (sortedBottles.isEmpty()) { stroke(0); fill(100,0,0); textAlign(CENTER); textSize(40); text("Put bottles from lightest to heaviest here.", width/2, 100); text("Use space under scale to help organize while weighing.", width/2, 150); } } void mousePressed() { //for (int i=0; i < bottles.length; i++) for (int i=bottles.length-1; i >= 0; i--) { if (bottles[i].hitTest(mouseX, mouseY)) { currBottle = bottles[i]; currBottle.setMouseClick(mouseX, mouseY); if (!sortedBottles.contains(currBottle)) { currBottle.homeX = mouseX - currBottle.clickX; currBottle.homeY = mouseY - currBottle.clickY; } currBottle.draw(mouseX - currBottle.clickX, mouseY - currBottle.clickY); if (currBottle.isOnLeft) { bottleOnLeft = null; theScale.drawHeavier(' '); } if (currBottle.isOnRight) { bottleOnRight = null; theScale.drawHeavier(' '); } currBottle.isOnLeft = currBottle.isOnRight = false; sortedBottles.remove(currBottle); break; } // Didn't hit any bottle, so currBottle is null currBottle = null; } } void mouseDragged() { if (currBottle != null) { int x = mouseX - currBottle.clickX; if (x < 0) x = 0; if (x > width - currBottle.w) x = width - currBottle.w; int y = mouseY - currBottle.clickY; if (y - currBottle.h < 0) y = currBottle.h + currBottle.hLid; if (y > height) y = height; currBottle.draw(x, y); } // Recompute the positions of the sorted bottles int x = startPosX; for (int i=0; i < sortedBottles.size(); i++) { Bottle b = (Bottle)sortedBottles.get(i); b.currX = x; b.currY = startPosYSorted; x += b.w + dx; } // If we are in the sorting area, we also want to draw a red line to show where the bottle would be dropped if (mouseY <= maxYForSortingArea) { // Find the first bottle we are not completely to the right of int i=0; int xLimit = startPosX - dx/2; for (i=0; i < sortedBottles.size(); i++) { Bottle b = (Bottle)sortedBottles.get(i); if (i==1) { Bottle bBefore = (Bottle)sortedBottles.get(i-1); xLimit = startPosX + bBefore.w + dx + b.w/2; } else if (i > 1) { Bottle bBefore = (Bottle)sortedBottles.get(i-1); xLimit += bBefore.w/2 + dx + b.w/2; } if (mouseX < xLimit) { break; } } if (i <= 0) { xLimit = startPosX - dx/2; } else if (i >= sortedBottles.size()) { Bottle b = (Bottle)sortedBottles.get(i-1); if (sortedBottles.size() == 1) { xLimit = startPosX + b.w + dx/2; } else { xLimit += b.w/2 + dx/2; } } else { Bottle b = (Bottle)sortedBottles.get(i); xLimit -= b.w/2 + dx/2; } currDropLocationForSorted = i; currXForRedLine = xLimit; } else { currXForRedLine = -1; } } void mouseReleased() { char side = theScale.whichSide(mouseX, mouseY); //println(side); currXForRedLine = -1; // Currently dragging a bottle? if (currBottle != null) { int bottleX = currBottle.clickX; int bottleY = currBottle.clickY; int bottleW = currBottle.w; int bottleH = currBottle.h; // Swap positions with the first bottle so the current one appears on top int currIdx; for (currIdx=0; currIdx < bottles.length; currIdx++) { if (currBottle == bottles[currIdx]) break; } for ( ; currIdx < bottles.length-1; currIdx++) { bottles[currIdx] = bottles[currIdx+1]; } bottles[bottles.length-1] = currBottle; // Check if we have dropped the bottle onto the scale if (theScale.hitTest(mouseX, mouseY, bottleX, bottleY, bottleW, bottleH)) { // Is there already a bottle there? If so, snap back to home if ((side=='l' && bottleOnLeft != null) || (side=='r' && bottleOnRight != null)) { currBottle.draw(currBottle.homeX, currBottle.homeY); } // If no bottle already on scale, can drop it into the scale else { currBottle.drawOnScale(theScale, side); if (side == 'l') { bottleOnLeft = currBottle; currBottle.isOnLeft = true; currBottle.isOnRight = false; } if (side == 'r') { bottleOnRight = currBottle; currBottle.isOnLeft = false; currBottle.isOnRight = true; } // Check which bottle is heavier and light that side if (bottleOnLeft != null && bottleOnRight != null) { if (bottleOnLeft.weight < bottleOnRight.weight) { theScale.drawHeavier('r'); } else { theScale.drawHeavier('l'); } } } } else // did not release bottle over scale { // Update variables about bottles on scales if needed if (currBottle.isOnLeft) { currBottle.isOnLeft = false; bottleOnLeft = null; theScale.currSide = ' '; } else if (currBottle.isOnRight) { currBottle.isOnRight = false; bottleOnRight = null; theScale.currSide = ' '; } // Check if we are trying to put the bottle in sorted order at the top if (mouseY <= maxYForSortingArea) { // Insert before the index saved for sorting if (currDropLocationForSorted < 0) { sortedBottles.add(0, currBottle); } else { sortedBottles.add(currDropLocationForSorted, currBottle); } // Check if the current sorted order is complete and correct if (sortedBottles.size() == bottles.length) { boolean correct = false; for (int i=1; i < sortedBottles.size(); i++) { Bottle b1 = (Bottle)sortedBottles.get(i-1); Bottle b2 = (Bottle)sortedBottles.get(i); if (b1.weight > b2.weight) { break; } if (i == sortedBottles.size()-1) { correct = true; } } if (correct) { winMsg = "Congratulations! It's sorted correctly!"; } else { winMsg = "Sorry, not sorted correctly yet."; } } } // Update the current locations of all the sorted bottles int x = startPosX; for (int i=0; i < sortedBottles.size(); i++) { Bottle b = (Bottle)sortedBottles.get(i); b.currX = x; b.currY = startPosYSorted; x += b.w + dx; } } //////////// AND FINALLY, make currBottle null currBottle = null; } else // currBottle is null { // Make sure the scale still shows which item is heavier if (bottleOnLeft != null && bottleOnRight != null) { if (bottleOnLeft.weight < bottleOnRight.weight) { theScale.drawHeavier('r'); } else { theScale.drawHeavier('l'); } } else { theScale.drawHeavier(' '); } } }