Author: Ezra Velazquez Date: 10/08/10 Prof: Che-Wei Wang Week: Four Note: User must place the lettered bubbles in their correct spot in order to learn the word's definition Updates: - Instructsion at the beginning - More use of conditionals - Code updated with classes and functions. - Code hierarchy easier to read / // Color Scheme color c1 = #001F11; color c2 = #204709; color c3 = #0C8558; color c4 = #FFD96A; color c5 = #FF4533; Dialog dialog1; SpellBot bot; Ball ball1; Ball ball2; Ball ball3; Ball ball4; Ball ball5; Bar bar1; Bar bar2; Bar bar3; Bar bar4; Bar bar5; void setup() { size(1000, 1000); rectMode(CENTER); ellipseMode(CENTER); smooth(); background(c4); dialog1 = new Dialog(); bot = new SpellBot(500,500,c4); ball1 = new Ball((int) random(100,900),(int) random(100,750),'L', 200, 790); ball2 = new Ball((int) random(100,900),(int) random(100,750),'O', 360, 790); ball3 = new Ball((int) random(100,900),(int) random(100,750),'U', 520, 790); ball4 = new Ball((int) random(100,900),(int) random(100,750),'E', 680, 790); ball5 = new Ball((int) random(100,900),(int) random(100,750),'A', 840, 790); bar1 = new Bar(200,750,'1'); bar2 = new Bar(360,750,'2'); bar3 = new Bar(520,750,'3'); bar4 = new Bar(680,750,'4'); bar5 = new Bar(840,750,'5'); } void draw() { // Display instructions if(dialog1.clicked() == false) { dialog1.display(); } // Display the game else { bot.display(); ball1.display(); ball2.display(); ball3.display(); ball4.display(); ball5.display(); bar1.display(); bar2.display(); bar3.display(); bar4.display(); bar5.display(); //Where to move the bot eventListener(); //Where to move the bubbles ballListener(); //Present the final answer presentAnswer(); } } //Figure out bubble's x & y coordinates void ballListener() { //If bubble is near its resting place if (dist(ball1.getX(), ball1.getY()+75, bar1.getX(), bar1.getY()) < 50) { ball1.setX(bar1.getX()); ball1.setY(bar1.getY()-45); // Allow bot to gather other bubbles once this one is dropped if (bot.getLoad() == 1) { bot.setLoads(0); } } //If bubble is near bot else if (dist(ball1.getX(), ball1.getY(), bot.getX(), bot.getY()+75) < 100) { // Only pick-up bubble if carrying nothing else if(bot.getLoad() == 0 | bot.getLoad() == 1) { ball1.setX(bot.getX()); ball1.setY(bot.getY()+75); bot.setLoads(1); } } //If bubble is near its resting place if (dist(ball2.getX(), ball2.getY()+75, bar2.getX(), bar2.getY()) < 50) { ball2.setX(bar2.getX()); ball2.setY(bar2.getY()-45); // Allow bot to gather other bubbles once this one is dropped if (bot.getLoad() == 2) { bot.setLoads(0); } } //If bubble is near bot else if (dist(ball2.getX(), ball2.getY(), bot.getX(), bot.getY()+75) < 100) { // Only pick-up bubble if carrying nothing else if(bot.getLoad() == 0 | bot.getLoad() == 2) { ball2.setX(bot.getX()); ball2.setY(bot.getY()+75); bot.setLoads(2); } } //If bubble is near its resting place if (dist(ball3.getX(), ball3.getY()+75, bar3.getX(), bar3.getY()) < 50) { ball3.setX(bar3.getX()); ball3.setY(bar3.getY()-45); // Allow bot to gather other bubbles once this one is dropped if (bot.getLoad() == 3) { bot.setLoads(0); } } //If bubble is near bot else if (dist(ball3.getX(), ball3.getY(), bot.getX(), bot.getY()+75) < 100) { // Only pick-up bubble if carrying nothing else if(bot.getLoad() == 0 | bot.getLoad() == 3) { ball3.setX(bot.getX()); ball3.setY(bot.getY()+75); bot.setLoads(3); } } //If bubble is near its resting place if (dist(ball4.getX(), ball4.getY()+75, bar4.getX(), bar4.getY()) < 50) { ball4.setX(bar4.getX()); ball4.setY(bar4.getY()-45); // Allow bot to gather other bubbles once this one is dropped if (bot.getLoad() == 4) { bot.setLoads(0); } } //If bubble is near bot else if (dist(ball4.getX(), ball4.getY(), bot.getX(), bot.getY()+75) < 100) { // Only pick-up bubble if carrying nothing else if(bot.getLoad() == 0 | bot.getLoad() == 4) { ball4.setX(bot.getX()); ball4.setY(bot.getY()+75); bot.setLoads(4); } } //If bubble is near its resting place if (dist(ball5.getX(), ball5.getY()+75, bar5.getX(), bar5.getY()) < 50) { ball5.setX(bar5.getX()); ball5.setY(bar5.getY()-45); // Allow bot to gather other bubbles once this one is dropped if (bot.getLoad() == 5) { bot.setLoads(0); } } //If bubble is near bot else if (dist(ball5.getX(), ball5.getY(), bot.getX(), bot.getY()+75) < 100) { // Only pick-up bubble if carrying nothing else if(bot.getLoad() == 0 | bot.getLoad() == 5) { ball5.setX(bot.getX()); ball5.setY(bot.getY()+75); bot.setLoads(5); } } } //Where to move bot void eventListener() { //Bot moves when using the keypad if (keyPressed && (key == CODED) ) { if (keyCode == LEFT) { bot.updateX(bot.getX() - 5); } else if (keyCode == RIGHT) { bot.updateX(bot.getX() + 5); } if (keyCode == UP) { bot.updateY(bot.getY() - 5); } else if (keyCode == DOWN) { bot.updateY(bot.getY() + 5); } } } //If all bubbles are in their correct place, show answer void presentAnswer() { if(ball1.inPlace() == true && ball2.inPlace() == true && ball3.inPlace() == true && ball4.inPlace() == true && ball5.inPlace() == true) { fill(255); textSize(50); text("A Genus of Sea Sponges",500,875); } } //Listening for mouse to be pressed, only if presseded within the button and at beginning of game void mousePressed() { if(dialog1.clicked() == false && mouseY > 250 && mouseY<350 && mouseX>300 && mouseX<600) { dialog1.setClick(true); } } /*********************************** SpellBot CLass / class SpellBot { // Color Scheme color c1 = #001F11; color c2 = #204709; color c3 = #0C8558; color c4 = #FFD96A; color c5 = #FF4533; int x,y; color botColor; boolean load = false; int ball = 0; SpellBot(int xX, int yY, color col) { rectMode(CENTER); ellipseMode(CENTER); x = xX; y = yY; botColor = col; } void display() { background(c4); textAlign(LEFT); //Body & Eyelashes fill(c3); noStroke(); rect(x,y,100,100); stroke(255); strokeWeight(4); line(x-20,y-20,x-100,y-50); line(x+20,y-20,x+100,y-50); //Eyes stroke(0); //Left Eye line(x-15,y-5,x-40,y-5); //Right Eye line(x+15,y-5,x+40,y-5); stroke(0); //Left Hand line(x-50,y+25,x-100,y+75); line(x-100,y+75,x-25,y+100); //Right Hand line(x+50,y+25,x+100,y+75); line(x+100,y+75,x+25,y+100); } //Update x coordinate void updateX( int xX) { x = xX; } //Update y coordinate void updateY( int yY) { y = yY; } //Return x coordinate int getX() { return x; } //Return y coordinate int getY() { return y; } //Carrying something void setLoads(int x) { ball = x; } //Return what bot is carrying int getLoad() { return ball; } } /*********************************** Ball CLass / class Ball { // Color Scheme color c1 = #001F11; color c2 = #204709; color c3 = #0C8558; color c4 = #FFD96A; color c5 = #FF4533; int x, y; char letter; int finalX, finalY; Ball(int xX, int yY, char letterLetter, int finalXx, int finalYy) { rectMode(CENTER); ellipseMode(CENTER); x = xX; y = yY; letter = letterLetter; finalX = finalXx; finalY = finalYy; } void display() { noStroke(); fill(c5); ellipse(x,y,100,100); fill(c1); textSize(32); text(letter,x-10,y+10); } //Set x coordinate void setX( int xX) { x = xX; } //Set y coordinate void setY( int yY) { y = yY; } //Return x coordinate int getX() { return x; } //Return y coordinate int getY() { return y; } //Check if ball is in its final resting place boolean inPlace() { if(x == finalX && y == finalY - 85) { return true; } else { return false; } } } /*********************************** Bar CLass / class Bar { // Color Scheme color c1 = #001F11; color c2 = #204709; color c3 = #0C8558; color c4 = #FFD96A; color c5 = #FF4533; int x, y; char letter; int finalX, finalY; Bar(int xX, int yY, char letterLetter) { rectMode(CENTER); ellipseMode(CENTER); textAlign(CENTER); x = xX; y = yY; letter = letterLetter; } void display() { noStroke(); fill(c1); rect(x,y,160,50); textSize(24); fill(255); textAlign(CENTER); text(letter,x,y+5); } //Return x coordinate int getX() { return x; } //Return y coordinate int getY() { return y; } } /*********************************** Dialog CLass

Source code: week_4

Built with Processing