Making Objective Interactive After Resetting the Screen

I'm trying to have three bottles on my screen with a cork on top of each, and every time someone clicks the cork with a sword, it pops off in a different way, and then once a key is pressed, the screen is reset, and the user can pop the cork off again. What I'm having trouble with is that when the key has been pressed and the screen is reset, the corks become static. If you click them, nothing happens. Why is this? What can I do to fix it?

As well, I'd eventually like to make my bottles move around on the screen in random spots every time the screen is reset or the program is played. I know how I can do that, but how do I prevent the bottles from overlapping each other, or from going off the screen?

I'm a complete beginner, so please use very basic functions. Thank you!

//Project 2
//Amal Abdullah
//301313742

PImage bottleOne;
PImage bottleTwo;
PImage bottleThree;
PImage corkOne;
PImage corkTwo;
PImage corkThree;
float bottleX;
float bottleY;
float bottleX2;
float bottleY2;
float bottleX3;
float bottleY3;
float corkX;
float corkY;
float cork2X;
float cork2Y;
float cork3X;
float cork3Y;
float dx=2;
float dy=-3;
boolean clicked1 = false;
boolean clicked2 = false;
boolean clicked3 = false;
boolean pressed = false;

void setup()
{
  size(500, 500);
  bottleOne = loadImage("bottle 1.jpg");
  bottleOne.resize(50, 150);
  bottleTwo = loadImage("bottle 2.jpg");
  bottleTwo.resize(100, 150);
  bottleThree = loadImage("bottle 3.jpg");
  bottleThree.resize(100, 150);
  corkOne = loadImage("cork.jpg");
  corkOne.resize(40, 40);
  corkTwo = loadImage("cork.jpg");
  corkTwo.resize(40, 40);
  corkThree = loadImage("cork.jpg");
  corkThree.resize(40, 40);
  corkX=90;
  corkY=300;
  cork2X=190;
  cork2Y=300;
  cork3X=320;
  cork3Y=300;
  bottleX=80;
  bottleY=300;
  bottleX2=160;
  bottleY2=300;
  bottleX3=290;
  bottleY3=300;
}

void draw()
{
  background(194, 229, 240);
  //sword knife-part
  beginShape();
  strokeWeight(1);
  stroke(0);
  fill(163, 178, 183, 200);
  vertex(mouseX, mouseY+10);
  vertex(mouseX+10, mouseY+40);
  vertex(mouseX+10, mouseY-25);
  vertex(mouseX-10, mouseY-30);
  endShape(CLOSE);
  //sword mid part
  strokeWeight(5);
  fill(139, 69, 19);
  stroke(139, 69, 19);
  beginShape();
  //bottom right point of sword
  vertex(mouseX-20, mouseY-27);
  vertex(mouseX-20, mouseY-32);
  //top right hand corner of handle
  vertex(mouseX+25, mouseY-29);
  vertex(mouseX+25, mouseY-23);
  endShape(CLOSE);
  //sword handle
  stroke(232, 205, 49);
  fill(232, 205, 49);
  beginShape();
  vertex(mouseX-5, mouseY-35);
  vertex(mouseX+10, mouseY-33);
  vertex(mouseX+10, mouseY-60);
  vertex(mouseX-5, mouseY-60);
  endShape(CLOSE);
  //drawing bottles
  image(bottleOne, bottleX, bottleY);
  image(bottleTwo, bottleX2, bottleY2);
  image(bottleThree, bottleX3, bottleY3);
  //drawing corks
  image(corkOne, corkX, corkY);
  image(corkTwo, cork2X, cork2Y);
  image(corkThree, cork3X, cork3Y);

  if (clicked1) {
    corkX+=dx;
    corkY+=dy;
  }
  if (clicked2) {
    cork2X=dx;
    cork2Y=450;
    //if (cork2X==dx)
    //{cork2Y=450;}
  }
  if (clicked3) {
    cork3X+=-dx;
    cork3Y+=-dy;
  }

  if (pressed) {
    background(194, 229, 240);
    corkX=90;
    corkY=300;
    cork2X=190;
    cork2Y=300;
    cork3X=320;
    cork3Y=300;
    bottleX=80;
    bottleY=300;
    bottleX2=160;
    bottleY2=300;
    bottleX3=290;
    bottleY3=300;
    ////drawing bottles
    image(bottleOne, bottleX, bottleY);
    image(bottleTwo, bottleX2, bottleY2);
    image(bottleThree, bottleX3, bottleY3);
    //drawing corks
    image(corkOne, corkX, corkY);
    image(corkTwo, cork2X, cork2Y);
    image(corkThree, cork3X, cork3Y);
  }
  }
  //animating corks
  void mousePressed() {
    if (dist(mouseX, mouseY, corkX+40, corkY+40) < 40) {
      clicked1 = true;
    } 
    if (dist(mouseX, mouseY, cork2X+40, cork2Y+40) < 40) {
      clicked2 = true;
    }
    if (dist(mouseX, mouseY, cork3X+40, cork3Y+40) < 40) {
      clicked3 = true;
    }
  }

  void keyPressed() {
    pressed = true;
  }

Answers

Sign In or Register to comment.