How would I change the default background?

edited November 2014 in Questions about Code
float x,y,w,h;
float witchX = 70, witchY = 70;
float sunX=100, sunY=90;
float horizon;
int speed = 2 , speedY=2;

color colorbackground = 22;


void setup() {
  size(600, 600);
  smooth();
  background( 255 );
  textAlign(CENTER);
  witch();
  x= width/2;
  y= height/2;
  w= 60;
  h= 20;
  horizon = height/5;
}

void draw() {

  background( colorbackground  );

  text ("Night", 120, 110);
  text ("Day", 500, 110);
  text ("Quit", 500,400);
  text ("Witch", 110,400);

  sun();
  bird();
  witch();
  witchX = witchX + speed;
  witchY = witchY + speedY;

    if ((witchX > width-22) || (witchX < 0+22)) {
    speed = speed * -1;
  }

  if ((witchY > height) || (witchY < 0)) {
    speedY = speedY * -1;
  }
}

void sun() {
   // Draw sun
  fill(255,255,0);     // Yellow
  ellipse(sunX, sunY,40,40);

    // Sun's movement
    if (sunX > width) {
    sunX=0;
    sunY=  random( 10, horizon-20);
}
  sunX=  sunX+2;
}

void bird(){
  //draw bird and make bird move
  float birdY= horizon-50;
  fill(145,40,130);
  triangle(x,birdY, x-w,birdY-h, x-w,birdY+h);
  triangle(x-25,birdY, x-w,birdY-h-25, x-w,birdY+35);
  //move bird
  x= (x+3);
  if(x>width*1){
    x=0;
  }
}

void witch(){
  //draw witch
  fill(28,2,43);
  rect(witchX,witchY, 25,50);
  fill(255,0,0);
  ellipse(witchX+10,witchY-10, 25,25);
  ellipse(witchX+10,witchY-10, 10,10); 
  fill(150,50,50);
  strokeWeight(5);
  stroke(150,50,50);
  line(witchX-25, witchY+50, witchX+35,witchY+50);   
}  


void mousePressed () {
  if (dist(120, 110, mouseX, mouseY) < 60) 
    colorbackground=22;
  else if (dist(500, 110, mouseX, mouseY) < 60)
    colorbackground=222;
    else if (dist(500, 400, mouseX, mouseY) < 60)
    exit();
    else if (dist(110, 400, mouseX, mouseY) < 60)
    witch();

}

--

--

I need to put two different colors in each theme. In day, I want to put two day colors and the same thing with the night? How would I make the witch disappear so it only appears when the button is pressed? How would I put the moon so when I press sun or moon, it appears and my sun has brown outline, how would I get rid of it?

Tagged:

Answers

  • edited November 2014

    I need to put two different colors in each theme. In day, I want to put two day colors and the same thing with the night?

    ok, you probably want one rect sky and one rect earth for day and for night

    so you had colorbackground=22; and colorbackground=222;

    but now you need

     colorbackgroundSky=color(222,2,2);
     colorbackgroundEarth=color(222,2,222);
    

    change both vars in lines 89 and 91 make a { around }

    void mousePressed () {
      if (dist(120, 110, mouseX, mouseY) < 60) {
        colorbackgroundSky=color(222,2,2);
        colorbackgroundEarth=color(222,2,222);
      }
      else if (dist(500, 110, mouseX, mouseY) < 60) {
         colorbackgroundSky=color(222,222,222);
         colorbackgroundEarth=color(222,111,222);
        }
        else if (dist(500, 400, mouseX, mouseY) < 60)
        exit();
        else if (dist(110, 400, mouseX, mouseY) < 60)
        witch();
    
    }
    

    (there is a color selector in the menu to choose good colors)

    in draw use

    noStroke();
    fill(colorbackgroundSky);
    rect(0,0,width, 200);
    
    noStroke();
    fill(colorbackgroundEarth);
    rect(0,200,width, height);
    

    How would I make the witch disappear so it only appears when the button is pressed?

    you need a flag that indicates if witch is to be drawn or not.

    now in draw() you say witch();

    thus witch is always drawn

    instead say

    if (witchDrawFlag)
      witch(); 
    

    in line 95 say

    witchDrawFlag=!witchDrawFlag;
    

    declare witchDrawFlag before setup as

    boolean witchDrawFlag = true; 
    

    How would I put the moon so when I press sun or moon, it appears

    similar to the witch thing

    sunDrawFlag = true;

    when the sun is clicked sunDrawFlag = !sunDrawFlag;

    so have another line in mousePressed () where you use dist to check the pos of the sun please

    and my sun has brown outline, how would I get rid of it?

    noStroke();

    before drawing the sun

    Best, Chrisir ;-)

  • edited November 2014
    -float x,y,w,h;
    float witchX = 70, witchY = 70;
    float sunX=100, sunY=90;
    float horizon;
    int speed = 2 , speedY=2;
    
    boolean witchDrawFlag = true;
    
    color colorbackground = 22;
    
    
    void setup() {
      size(600, 600);
      smooth();
      background( 255 );
      textAlign(CENTER);
      witch();
      x= width/2;
      y= height/2;
      w= 60;
      h= 20;
      horizon = height/5;
    }
    
    void draw() {
    
      background( colorbackground  );
    
      noStroke();
      fill(colorbackgroundSky);
      rect(0,0,width, 200);
    
      noStroke();
      fill(colorbackgroundEarth);
      rect(0,200,width, height);
    
    
      text ("Night", 120, 110);
      text ("Day", 500, 110);
      text ("Quit", 500,400);
      text ("Witch", 110,400);
    
      sun();
      bird();
    
      if (witchDrawFlag)
      witch();
      witchX = witchX + speed;
      witchY = witchY + speedY;
    
        if ((witchX > width-22) || (witchX < 0+22)) {
        speed = speed * -1;
      }
    
      if ((witchY > height) || (witchY < 0)) {
        speedY = speedY * -1;
      }
    }
    
    void sun() {
       // Draw sun
       noStroke();
      fill(255,255,0);     // Yellow
      ellipse(sunX, sunY,40,40);
    
        // Sun's movement
        if (sunX > width) {
        sunX=0;
        sunY=  random( 10, horizon-20);
    }
      sunX=  sunX+2;
    }
    
    void bird(){
      //draw bird and make bird move
      float birdY= horizon-50;
      fill(145,40,130);
      triangle(x,birdY, x-w,birdY-h, x-w,birdY+h);
      triangle(x-25,birdY, x-w,birdY-h-25, x-w,birdY+35);
      //move bird
      x= (x+3);
      if(x>width*1){
        x=0;
      }
    }
    
    void witch(){
      //draw witch
      fill(28,2,43);
      rect(witchX,witchY, 25,50);
      fill(255,0,0);
      ellipse(witchX+10,witchY-10, 25,25);
      ellipse(witchX+10,witchY-10, 10,10); 
      fill(150,50,50);
      strokeWeight(5);
      stroke(150,50,50);
      line(witchX-25, witchY+50, witchX+35,witchY+50);   
    }  
    
      void mousePressed () {
      if (dist(120, 110, mouseX, mouseY) < 60) {
        colorbackgroundSky=color(222,2,2);
        colorbackgroundEarth=color(222,2,222);
      }
      else if (dist(500, 110, mouseX, mouseY) < 60) {
         colorbackgroundSky=color(222,222,222);
         colorbackgroundEarth=color(222,111,222);
        }
        else if (dist(500, 400, mouseX, mouseY) < 60)
        exit();    
        else if (dist(110, 400, mouseX, mouseY) < 60)
        witchDrawFlag=!witchDrawFlag;    
    }
    
    
    
    ---
    

    I've tried what you've mentioned me here, but I think I did something wrong and I can't figure it out

  • what happens?

    define both vars before setup()

  • edited November 2014
    float x,y,w,h;
    float witchX = 70, witchY = 70;
    float sunX=100, sunY=90;
    float moonX=100, moonY=90;
    float horizon;
    int speed = 2 , speedY=2;
    
    boolean witchDrawFlag = true;
    
    color backgroundSky=color(222,2,2);
    color backgroundEarth=color(222,2,222); 
    color colorbackground = 22;
    
    
    void setup() {
      size(600, 600);
      smooth();
      background( 255 );
      textAlign(CENTER);
      witch();
      x= width/2;
      y= height/2;
      w= 60;
      h= 20;
      horizon = height/5;
    }
    
    void draw() {
    
      background( colorbackground  );
    
      noStroke();
      fill(backgroundSky);
      rect(0,0,width, 200);
    
      noStroke();
      fill(backgroundEarth);
      rect(0,200,width, height);
    
    
      text ("Night", 120, 110);
      text ("Day", 500, 110);
      text ("Quit", 500,400);
      text ("Witch", 110,400);
    
      sun();
      bird();
      moon();
      border();
    
      if (witchDrawFlag)
      witch();
      witchX = witchX + speed;
      witchY = witchY + speedY;
    
        if ((witchX > width-22) || (witchX < 0+22)) {
        speed = speed * -1;
      }
    
      if ((witchY > height) || (witchY < 0)) {
        speedY = speedY * -1;
      }
    }
    
    void sun() {
       // Draw sun
       noStroke();
      fill(255,255,0);     // Yellow
      ellipse(sunX, sunY,40,40);
    
        // Sun's movement
        if (sunX > width) {
        sunX=0;
        sunY=  random( 10, horizon-20);
    }
      sunX=  sunX+2;
    } 
    
    void moon();
      fill(235,236,234);     // Yellow
      ellipse(moonX, moonY,40,40);
    
        // Sun's movement
        if (moonX > width) {
        moonX=0;
        moonY=  random( 10, horizon-20);
    }
      moonX=  moonX+2;
    }
    
    void bird(){
      //draw bird and make bird move
      float birdY= horizon-50;
      fill(145,40,130);
      triangle(x,birdY, x-w,birdY-h, x-w,birdY+h);
      triangle(x-25,birdY, x-w,birdY-h-25, x-w,birdY+35);
      //move bird
      x= (x+3);
      if(x>width*1){
        x=0;
      }
    }
    
    void border(){
      ////
      float tilt = 20;
      for(float x=0; x<width; x+=30) {
        stroke(245,17,45);
        line( x, height, x+tilt, height-30);
      }
    }
    
    void witch(){
      //draw witch
      fill(28,2,43);
      rect(witchX,witchY, 25,50);
      fill(255,0,0);
      ellipse(witchX+10,witchY-10, 25,25);
      ellipse(witchX+10,witchY-10, 10,10); 
      fill(150,50,50);
      strokeWeight(5);
      stroke(150,50,50);
      line(witchX-25, witchY+50, witchX+35,witchY+50);   
    }  
    
      void mousePressed () {
      if (dist(120, 110, mouseX, mouseY) < 60) {
        backgroundSky=color(222,2,2);
        backgroundEarth=color(222,2,222);
      }
      else if (dist(500, 110, mouseX, mouseY) < 60) {
         backgroundSky=color(222,222,222);
         backgroundEarth=color(222,111,222);
         moon();
        }
        else if (dist(500, 400, mouseX, mouseY) < 60)
        exit();    
        else if (dist(110, 400, mouseX, mouseY) < 60)
        witchDrawFlag=!witchDrawFlag;    
    }
    

    It's working now, but "witch" and "exit" aren't showing up. I tried to make the moon so I basically copy the sun's code, but its not working. I put up the border, but I don't know how to put on every direction.--

  • you should show moon only at night and sun only at day

    the words witch and Quit didn't show, because they had the same color as the earth background

    float x, y, w, h;
    float witchX = 70, witchY = 70;
    float sunX=100, sunY=90;
    float moonX=100, moonY=90;
    float horizon;
    float speed = 2, speedY=2.4;
    
    boolean witchDrawFlag = true;
    
    color backgroundSky=color(222, 2, 2);
    color backgroundEarth=color(222, 2, 222); 
    
    color colorbackground = 22;
    
    
    void setup() {
      size(600, 600);
      smooth();
      background( 255 );
      textAlign(CENTER);
      witch();
      x= width/2;
      y= height/2;
      w= 60;
      h= 20;
      horizon = height/5;
    }
    
    void draw() {
    
      background( colorbackground  );
    
      noStroke();
      fill(backgroundSky);
      rect(0, 0, width, 200);
    
      noStroke();
      fill(backgroundEarth);
      rect(0, 200, width, height);
    
    
      fill(255);
      text ("Night", 120, 110);
      text ("Day", 500, 110);
      text ("Quit", 500, 400);
      text ("Witch", 110, 400);
    
      sun();
      bird();
      moon();
      border();
    
      if (witchDrawFlag)
        witch();
    
      witchX = witchX + speed;
      witchY = witchY + speedY;
    
      if ((witchX > width-22) || (witchX < 0+22)) {
        speed = speed * -1;
        speed = speed + random (-0.1, 0.1);
      }
    
      if ((witchY > height-22) || (witchY < 22)) {
        speedY = speedY * -1;
        speedY = speedY + random (-0.1, 0.1);
      }
    }
    
    void sun() {
      // Draw sun
      noStroke();
      fill(255, 255, 0);     // Yellow
      ellipse(sunX, sunY, 40, 40);
    
      // Sun's movement
      if (sunX > width) {
        sunX=0;
        sunY=  random( 10, horizon-20);
      }
      sunX=  sunX+2;
    } 
    
    void moon() { 
      fill(235, 236, 234);     // color
      ellipse(moonX, moonY, 40, 40);
    
      // moon's movement
      if (moonX > width) {
        moonX=0;
        moonY=  random( 10, horizon-20);
      }
      moonX=  moonX+1.8;
    }
    
    void bird() {
      //draw bird and make bird move
      float birdY= horizon-50;
      fill(145, 40, 130);
      triangle(x, birdY, x-w, birdY-h, x-w, birdY+h);
      triangle(x-25, birdY, x-w, birdY-h-25, x-w, birdY+35);
      //move bird
      x= (x+3);
      if (x>width*1) {
        x=0;
      }
    }
    
    void border() {
      ////
      float tilt = 20;
      for (float x=0; x<width; x+=30) {
        stroke(245, 17, 45);
        line( x, height, x+tilt, height-30);
      }
    
      for (float x=0; x<width; x+=30) {
        stroke(245, 17, 45);
        line( x, 0, x+tilt, 30);
      }
    
      for (float y=0; y<height; y+=30) {
        stroke(245, 17, 45);
        line( 0, y, 30, y+tilt);
      }
    
      for (float y=0; y<height; y+=30) {
        stroke(245, 17, 45);
        line( width, y, width-30, y+tilt);
      }
    }
    
    void witch() {
      //draw witch
      fill(28, 2, 43);
      rect(witchX, witchY, 25, 50);
      fill(255, 0, 0);
      ellipse(witchX+10, witchY-10, 25, 25);
      ellipse(witchX+10, witchY-10, 10, 10); 
      fill(150, 50, 50);
      strokeWeight(5);
      stroke(150, 50, 50);
      line(witchX-25, witchY+50, witchX+35, witchY+50);
    }  
    
    void mousePressed () {
      if (dist(120, 110, mouseX, mouseY) < 60) {
        backgroundSky=color(222, 2, 2);
        backgroundEarth=color(222, 2, 222);
      }
      else if (dist(500, 110, mouseX, mouseY) < 60) {
        backgroundSky=color(222, 222, 222);
        backgroundEarth=color(222, 111, 222);
        moon();
      }
      else if (dist(500, 400, mouseX, mouseY) < 60)
        exit();    
      else if (dist(110, 400, mouseX, mouseY) < 60)
        witchDrawFlag=!witchDrawFlag;
    }
    //
    
  • I got it, but my moon is coming out with the sun. I need the moon when I hit the night button otherwise I don't want to see the moon. Same thing with the witch. So should I do what you did with the witch?

  • I did that, but both sun and moon are showing up when I run the code, I don't want that I want only moon in the night when I run it.

  • in these lines

    void mousePressed () {
      if (dist(120, 110, mouseX, mouseY) < 60) {
        backgroundSky=color(222, 2, 2);
        backgroundEarth=color(222, 2, 222);
      }
      else if (dist(500, 110, mouseX, mouseY) < 60) {
        backgroundSky=color(222, 222, 222);
        backgroundEarth=color(222, 111, 222);
        moon();
      }
      else if (dist(500, 400, mouseX, mouseY) < 60)
        exit();   
      else if (dist(110, 400, mouseX, mouseY) < 60)
        witchDrawFlag=!witchDrawFlag;
    }
    //
    

    where the colors are turned to day colors

    say moonFlag = false; sunFlag = true;

    within the { }

    same for night colors

    Good luck!

  • edited November 2014

    don't ever say moon(); in mousePressed(), say moonFlag = false; or moonFlag = true;

    in draw() say

      if (moonFlag) { moon(); }
    

    like you did with witch, you know?

  • edited November 2014
    // Global
    String author = "FK - takehome: 11/5/14";
    
    float x, y, w, h;
    float witchX = 70, witchY = 70;
    float sunX=100, sunY=90;
    float moonX=100, moonY=90;
    float horizon;
    float speed = 2, speedY=2.4;
    
    boolean witchDrawFlag = true;
    boolean sunDrawFlag = true;
    boolean moonDrawFlag = false;
    
    color backgroundSky=color(222, 2, 2);
    color backgroundEarth=color(222, 2, 222);  
    color colorbackground = 22;
    
    void setup() {
      // Setup
      size(600, 600);
      smooth();
      background( 255 );
      textAlign(CENTER);
      witch();
      x= width/2;
      y= height/2;
      w= 60;
      h= 20;
      horizon = height/5;
    }
    
    void draw() {
      // Next Frame
       background( colorbackground  );
    
      // Two colors  
      noStroke();
      fill(backgroundSky);
      rect(0, 0, width, 200);
    
      noStroke();
      fill(backgroundEarth);
      rect(0, 200, width, height);
    
      // Button's Text
      fill(255);
      text ("Night", 120, 110);
      text ("Day", 500, 110);
      text ("Quit", 500, 400);
      text ("Witch", 110, 400);
      // Author and title
      text (author, 300, height - 500);
      moon();
      bird();
      moon();
      border();
    
      if (moonDrawFlag) { moon(); }
    
      if (moonDrawFlag)
      moon();
    
      if (sunDrawFlag)
      sun();
    
      if (witchDrawFlag)
        witch();
    
     // witch's movement
      witchX = witchX + speed;
      witchY = witchY + speedY;
    
      if ((witchX > width-22) || (witchX < 0+22)) {
        speed = speed * -1;
        speed = speed + random (-0.1, 0.1);
      } 
      if ((witchY > height-22) || (witchY < 22)) {
        speedY = speedY * -1;
        speedY = speedY + random (-0.1, 0.1);
      }
    }
    
    void sun() {
      // Draw sun
      noStroke();
      fill(255, 255, 0);     // Yellow
      ellipse(sunX, sunY, 40, 40);
    
      // Sun's movement
      if (sunX > width) {
        sunX=0;
        sunY=  random( 10, horizon-20);
      }
      sunX=  sunX+2;
    } 
    
    void moon() { 
      //Draw moon
      fill(235, 236, 234);     // color
      ellipse(moonX, moonY, 40, 40);
    
      // moon's movement
      if (moonX > width) {
        moonX=0;
        moonY=  random( 10, horizon-20);
      }
      moonX=  moonX+1.8;
    }
    
    void bird() {
      //draw bird and make bird move
      float birdY= horizon-50;
      fill(145, 40, 130);
      triangle(x, birdY, x-w, birdY-h, x-w, birdY+h);
      triangle(x-25, birdY, x-w, birdY-h-25, x-w, birdY+35);
      //move bird
      x= (x+3);
      if (x>width*1) {
        x=0;
      }
    }
    
    void border() {
      // Draw border 
      float tilt = 20;
      // Lower border
      for (float x=0; x<width; x+=30) {
        stroke(245, 17, 45);
        line( x, height, x+tilt, height-30);
      }
      // Upperside border
      for (float x=0; x<width; x+=30) {
        stroke(245, 17, 45);
        line( x, 0, x+tilt, 30);
      }
      // Left handside border
      for (float y=0; y<height; y+=30) {
        stroke(245, 17, 45);
        line( 0, y, 30, y+tilt);
      }
      // Right handside border
      for (float y=0; y<height; y+=30) {
        stroke(245, 17, 45);
        line( width, y, width-30, y+tilt);
      }
    }
    
    void witch() {
      //draw witch
      fill(28, 2, 43);
      rect(witchX, witchY, 25, 50);
      fill(255, 0, 0);
      ellipse(witchX+10, witchY-10, 25, 25);
      ellipse(witchX+10, witchY-10, 10, 10); 
      fill(150, 50, 50);
      strokeWeight(5);
      stroke(150, 50, 50);
      line(witchX-25, witchY+50, witchX+35, witchY+50);
    }  
    
    void mousePressed () {
      // Day button
      if (dist(120, 110, mouseX, mouseY) < 60) {
        backgroundSky=color(222, 2, 2);
        backgroundEarth=color(222, 2, 222);
        moonDrawFlag = true;
      }
      // Night button
      else if (dist(500, 110, mouseX, mouseY) < 60) {
        backgroundSky=color(222, 222, 222);
        backgroundEarth=color(222, 111, 222);
        sunDrawFlag =!sunDrawFlag;
        sunDrawFlag = false;
      }
      // Exit button
      else if (dist(500, 400, mouseX, mouseY) < 60)
        exit();    
        // Witch button
      else if (dist(110, 400, mouseX, mouseY) < 60)
        witchDrawFlag=!witchDrawFlag;
    }
    

    What am I doing wrong now, most of the time my moon get outlined or it goes faster.

  • // Global
    String author = "FK - takehome: 11/5/14";
    
    float x, y, w, h;
    float witchX = 70, witchY = 70;
    float sunX=100, sunY=90;
    float moonX=100, moonY=90;
    float octX, octY;
    float octDX = 1;
    float horizon;
    float speed = 2, speedY=2.4;
    
    boolean witchDrawFlag = true;
    boolean sunDrawFlag = true;
    boolean moonDrawFlag = false;
    
    color backgroundSky=color(222, 2, 2);
    color backgroundEarth=color(222, 2, 222);  
    color colorbackground = 22;
    
    void setup() {
      // Setup
      size(600, 600);
      smooth();
      background( 255 );
      textAlign(CENTER);
      witch();
      x= width/2;
      y= height/2;
      w= 60;
      h= 20;
      octX = width/2;
      octY = horizon + 200;
      horizon = height/5;
    }
    
    void draw() {
      // Next Frame
       background( colorbackground  );
    
      // Two colors  
      noStroke();
      fill(backgroundSky);
      rect(0, 0, width, 200);
    
      noStroke();
      fill(backgroundEarth);
      rect(0, 200, width, height);
    
      // Button's Text
      fill(255);
      text ("Night", 120, 110);
      text ("Day", 500, 110);
      text ("Quit", 500, 400);
      text ("Witch", 110, 400);
      // Author and title
      text (author, 300, height - 500);
    
      octopus();
      moon();
      bird();
      moon();
      border();
    
      if (moonDrawFlag) { moon(); }
    
      if (moonDrawFlag)
      moon();
    
      if (sunDrawFlag)
      sun();
    
      if (witchDrawFlag)
        witch();
    
     // witch's movement
      witchX = witchX + speed;
      witchY = witchY + speedY;
    
      if ((witchX > width-22) || (witchX < 0+22)) {
        speed = speed * -1;
        speed = speed + random (-0.1, 0.1);
      } 
      if ((witchY > height-22) || (witchY < 22)) {
        speedY = speedY * -1;
        speedY = speedY + random (-0.1, 0.1);
      }
    }
    
    void sun() {
      // Draw sun
      noStroke();
      fill(255, 255, 0);     // Yellow
      ellipse(sunX, sunY, 40, 40);
    
      // Sun's movement
      if (sunX > width) {
        sunX=0;
        sunY=  random( 10, horizon-20);
      }
      sunX=  sunX+2;
    } 
    
    void moon() { 
      //Draw moon
      fill(235, 236, 234);     // color
      ellipse(moonX, moonY, 40, 40);
    
      // moon's movement
      if (moonX > width) {
        moonX=0;
        moonY=  random( 10, horizon-20);
      }
      moonX=  moonX+1.8;
    }
    
    void bird() {
      //draw bird and make bird move
      float birdY= horizon-50;
      fill(145, 40, 130);
      triangle(x, birdY, x-w, birdY-h, x-w, birdY+h);
      triangle(x-25, birdY, x-w, birdY-h-25, x-w, birdY+35);
      //move bird
      x= (x+3);
      if (x>width*1) {
        x=0;
      }
    }
    
    void border() {
      // Draw border 
      float tilt = 20;
      // Lower border
      for (float x=0; x<width; x+=30) {
        stroke(245, 17, 45);
        line( x, height, x+tilt, height-30);
      }
      // Upperside border
      for (float x=0; x<width; x+=30) {
        stroke(245, 17, 45);
        line( x, 0, x+tilt, 30);
      }
      // Left handside border
      for (float y=0; y<height; y+=30) {
        stroke(245, 17, 45);
        line( 0, y, 30, y+tilt);
      }
      // Right handside border
      for (float y=0; y<height; y+=30) {
        stroke(245, 17, 45);
        line( width, y, width-30, y+tilt);
      }
    }
    
    void octopus(){
      float octoDY= 1;
    
        //// Octopus:  up & down
      octY += octDX;
      if (octY < horizon) {
        octDX = +1;    // Sink slowly.
      }
      else if (octY > height-50) {
        octDX = -3;
      }
    
      //// Draw body & slanty legs;
      fill( 255,0,255 );
      rect( octX-20,octY-25, 40, 50 );
      ellipse( octX,octY-30, 40, 40 );
    
      // Legs
      stroke( 255,0,255 );
      strokeWeight(2);
      float slant=0;
      if (octDX<0) {
        // Going up.
        slant = (frameCount % 10 < 5) ? -3 : +3;
      }
      for( float x=octX-20+2; x<octX+20; x+=5 )
      {
        line(x,octY+20, x+slant,octY+50);
      }
    }
    
    
    
    void witch() {
      //draw witch
      fill(28, 2, 43);
      rect(witchX, witchY, 25, 50);
      fill(255, 0, 0);
      ellipse(witchX+10, witchY-10, 25, 25);
      ellipse(witchX+10, witchY-10, 10, 10); 
      fill(150, 50, 50);
      strokeWeight(5);
      stroke(150, 50, 50);
      line(witchX-25, witchY+50, witchX+35, witchY+50);
    }  
    
    void mousePressed () {
      // Day button
      if (dist(120, 110, mouseX, mouseY) < 60) {
        backgroundSky=color(222, 2, 2);
        backgroundEarth=color(222, 2, 222);
        moonDrawFlag = true;
      }
      // Night button
      else if (dist(500, 110, mouseX, mouseY) < 60) {
        backgroundSky=color(222, 222, 222);
        backgroundEarth=color(222, 111, 222);
        sunDrawFlag =!sunDrawFlag;
        sunDrawFlag = false;
      }
      // Exit button
      else if (dist(500, 400, mouseX, mouseY) < 60)
        exit();    
        // Witch button
      else if (dist(110, 400, mouseX, mouseY) < 60)
        witchDrawFlag=!witchDrawFlag;
    }
    

    I added one more object, but my sun isn't showing up

  • after line 206 switch sun on

  • But its not working the way I want it

  •     sunDrawFlag = false;

        sunDrawFlag = true;

    etc.

  • I want the sun or the moon appears when I hit the button, but instead both are appearing

  • edited November 2014

    read draw() : one moon is enough

  • just read mousepressed careful and draw as well

  • // Global
    String author = "FK - takehome: 11/5/14";
    
    float x, y, w, h;
    float witchX = 70, witchY = 70;
    float sunX=100, sunY=90;
    float moonX=100, moonY=90;
    float octX, octY;
    float octDX = 1;
    float horizon;
    float speed = 2, speedY=2.4;
    
    boolean witchDrawFlag = true;
    boolean sunDrawFlag = true;
    boolean moonDrawFlag = false;
    
    color backgroundSky=color(222, 2, 2);
    color backgroundEarth=color(222, 2, 222);  
    color colorbackground = 22;
    
    void setup() {
      // Setup
      size(600, 600);
      smooth();
      background( 255 );
      textAlign(CENTER);
      witch();
      x= width/2;
      y= height/2;
      w= 60;
      h= 20;
      octX = width/2;
      octY = horizon + 200;
      horizon = height/5;
    }
    
    void draw() {
      // Next Frame
       background( colorbackground  );
    
      // Two colors  
      noStroke();
      fill(backgroundSky);
      rect(0, 0, width, 200);
    
      noStroke();
      fill(backgroundEarth);
      rect(0, 200, width, height);
    
      // Button's Text
      fill(255);
      text ("Night", 120, 110);
      text ("Day", 500, 110);
      text ("Quit", 500, 400);
      text ("Witch", 110, 400);
      // Author and title
      text (author, 300, height - 500);
    
      octopus();
      moon();
      bird();
      moon();
      border();
    
      if (moonDrawFlag) { moon(); }
    
      if (sunDrawFlag)
      sun();
    
      if (witchDrawFlag)
        witch();
    
     // witch's movement
      witchX = witchX + speed;
      witchY = witchY + speedY;
    
      if ((witchX > width-22) || (witchX < 0+22)) {
        speed = speed * -1;
        speed = speed + random (-0.1, 0.1);
      } 
      if ((witchY > height-22) || (witchY < 22)) {
        speedY = speedY * -1;
        speedY = speedY + random (-0.1, 0.1);
      }
    }
    
    void sun() {
      // Draw sun
      noStroke();
      fill(255, 255, 0);     // Yellow
      ellipse(sunX, sunY, 40, 40);
    
      // Sun's movement
      if (sunX > width) {
        sunX=0;
        sunY=  random( 10, horizon-20);
      }
      sunX=  sunX+2;
    } 
    
    void moon() { 
      //Draw moon
      noStroke();
      fill(235, 236, 234);     // color
      ellipse(moonX, moonY, 40, 40);
    
      // moon's movement
      if (moonX > width) {
        moonX=0;
        moonY=  random( 10, horizon-20);
      }
      moonX=  moonX+1.8;
    }
    
    void bird() {
      //draw bird and make bird move
      float birdY= horizon-50;
      fill(145, 40, 130);
      triangle(x, birdY, x-w, birdY-h, x-w, birdY+h);
      triangle(x-25, birdY, x-w, birdY-h-25, x-w, birdY+35);
      //move bird
      x= (x+3);
      if (x>width*1) {
        x=0;
      }
    }
    
    void border() {
      // Draw border 
      float tilt = 20;
      // Lower border
      for (float x=0; x<width; x+=30) {
        stroke(245, 17, 45);
        line( x, height, x+tilt, height-30);
      }
      // Upperside border
      for (float x=0; x<width; x+=30) {
        stroke(245, 17, 45);
        line( x, 0, x+tilt, 30);
      }
      // Left handside border
      for (float y=0; y<height; y+=30) {
        stroke(245, 17, 45);
        line( 0, y, 30, y+tilt);
      }
      // Right handside border
      for (float y=0; y<height; y+=30) {
        stroke(245, 17, 45);
        line( width, y, width-30, y+tilt);
      }
    }
    
    void octopus(){
      float octoDY= 1;  
        //// Octopus:  up & down
      octY += octDX;
      if (octY < horizon) {
        octDX = +1;    // Sink slowly.
      }
      else if (octY > height-50) {
        octDX = -3;
      }
    
      //// Draw body & slanty legs;
      fill( 255,0,255 );
      rect( octX-20,octY-25, 40, 50 );
      ellipse( octX,octY-30, 40, 40 );
    
      // Legs
      stroke( 255,0,255 );
      strokeWeight(2);
      float slant=0;
      if (octDX<0) {
        // Going up.
        slant = (frameCount % 10 < 5) ? -3 : +3;
      }
      for( float x=octX-20+2; x<octX+20; x+=5 )
      {
        line(x,octY+20, x+slant,octY+50);
      }
    }
    
    
    
    void witch() {
      //draw witch
      fill(28, 2, 43);
      rect(witchX, witchY, 25, 50);
      fill(255, 0, 0);
      ellipse(witchX+10, witchY-10, 25, 25);
      ellipse(witchX+10, witchY-10, 10, 10); 
      fill(150, 50, 50);
      strokeWeight(5);
      stroke(150, 50, 50);
      line(witchX-25, witchY+50, witchX+35, witchY+50);
    }  
    
    void mousePressed () {
      // Night button
      if (dist(120, 110, mouseX, mouseY) < 60) {
        backgroundSky=color(222, 2, 2);
        backgroundEarth=color(222, 2, 222);
        moonDrawFlag =!moonDrawFlag;
        moonDrawFlag = false;
        sunDrawFlag = false;
      }
      // Day button
      else if (dist(500, 110, mouseX, mouseY) < 60) {
        backgroundSky=color(222, 222, 222);
        backgroundEarth=color(222, 111, 222);
        sunDrawFlag =!sunDrawFlag;
        sunDrawFlag = true;
        moonDrawFlag = true;    
      }
      // Exit button
      else if (dist(500, 400, mouseX, mouseY) < 60)
        exit();    
        // Witch button
      else if (dist(110, 400, mouseX, mouseY) < 60)
        witchDrawFlag=!witchDrawFlag;
    }
    

    I figured out the Draw and I looked the mousepressed and did some changes, but still no result.

  • edited November 2014

    read your mousepressed() code please,

    what if you hit "day"-button?

    What happens, what want you to happen?

  • Is it because I have moonflag = true in day?

  • there are more errors

  • Same problem with sunflag

  • edited November 2014

    yes....

    read careful your own code...

    think it through...

    as if you had to excute your program yourself...

  • it's all in your code

    read mousePressed()

  • I turned both off now does it has to do something with sundrawflag=?

  • So I should get rid of sunDrawFlag =!sunDrawFlag

  • edited November 2014

    yes. This line is meaningless when you set the flag right afterwards. Get rid of it.

    oh boy.....

  • those are switches....

    just turn them on and off how you need it

  • I tried in any possible with on and off, but didn't work

  • edited November 2014

    why?

  • // Global
    String author = "FK - takehome: 11/5/14";
    
    float x, y, w, h;
    float witchX = 70, witchY = 70;
    float sunX=100, sunY=90;
    float moonX=100, moonY=90;
    float octX, octY;
    float octDX = 1;
    float horizon;
    float speed = 2, speedY=2.4;
    
    boolean witchDrawFlag = true;
    boolean sunDrawFlag = true;
    boolean moonDrawFlag = true;
    
    color backgroundSky=color(222, 2, 2);
    color backgroundEarth=color(222, 2, 222);  
    color colorbackground = 22;
    
    void setup() {
      // Setup
      size(600, 600);
      smooth();
      background( 255 );
      textAlign(CENTER);
      witch();
      x= width/2;
      y= height/2;
      w= 60;
      h= 20;
      octX = width/2;
      octY = horizon + 200;
      horizon = height/5;
    }
    
    void draw() {
      // Next Frame
       background( colorbackground  );
    
      // Two colors  
      noStroke();
      fill(backgroundSky);
      rect(0, 0, width, 200);
    
      noStroke();
      fill(backgroundEarth);
      rect(0, 200, width, height);
    
      // Button's Text
      fill(255);
      text ("Night", 120, 110);
      text ("Day", 500, 110);
      text ("Quit", 500, 400);
      text ("Witch", 110, 400);
      // Author and title
      text (author, 300, height - 500);
    
      octopus();
      moon();
      bird();
      moon();
      border();
    
      // For buttons
      if (moonDrawFlag) { moon(); }
    
      if (sunDrawFlag)
      sun();
    
      if (witchDrawFlag)
        witch();
    
     // witch's movement
      witchX = witchX + speed;
      witchY = witchY + speedY;
    
      if ((witchX > width-22) || (witchX < 0+22)) {
        speed = speed * -1;
        speed = speed + random (-0.1, 0.1);
      } 
      if ((witchY > height-22) || (witchY < 22)) {
        speedY = speedY * -1;
        speedY = speedY + random (-0.1, 0.1);
      }
    }
    
    void sun() {
      // Draw sun
      noStroke();
      fill(255, 255, 0);     // Yellow
      ellipse(sunX, sunY, 40, 40);
    
      // Sun's movement
      if (sunX > width) {
        sunX=0;
        sunY=  random( 10, horizon-20);
      }
      sunX=  sunX+2;
    } 
    
    void moon() { 
      //Draw moon
      noStroke();
      fill(235, 236, 234);     // color
      ellipse(moonX, moonY, 40, 40);
    
      // moon's movement
      if (moonX > width) {
        moonX=0;
        moonY=  random( 10, horizon-20);
      }
      moonX=  moonX+1.8;
    }
    
    void bird() {
      //draw bird and make bird move
      float birdY= horizon-50;
      fill(145, 40, 130);
      triangle(x, birdY, x-w, birdY-h, x-w, birdY+h);
      triangle(x-25, birdY, x-w, birdY-h-25, x-w, birdY+35);
      //move bird
      x= (x+3);
      if (x>width*1) {
        x=0;
      }
    }
    
    void border() {
      // Draw border 
      float tilt = 20;
      // Lower border
      for (float x=0; x<width; x+=30) {
        stroke(245, 17, 45);
        line( x, height, x+tilt, height-30);
      }
      // Upperside border
      for (float x=0; x<width; x+=30) {
        stroke(245, 17, 45);
        line( x, 0, x+tilt, 30);
      }
      // Left handside border
      for (float y=0; y<height; y+=30) {
        stroke(245, 17, 45);
        line( 0, y, 30, y+tilt);
      }
      // Right handside border
      for (float y=0; y<height; y+=30) {
        stroke(245, 17, 45);
        line( width, y, width-30, y+tilt);
      }
    }
    
    void octopus(){
      float octoDY= 1;  
        //// Octopus:  up & down
      octY += octDX;
      if (octY < horizon) {
        octDX = +1;    // Sink slowly.
      }
      else if (octY > height-50) {
        octDX = -3;
      }
    
      //// Draw body & slanty legs;
      fill( 255,0,255 );
      rect( octX-20,octY-25, 40, 50 );
      ellipse( octX,octY-30, 40, 40 );
    
      // Legs
      stroke( 255,0,255 );
      strokeWeight(2);
      float slant=0;
      if (octDX<0) {
        // Going up.
        slant = (frameCount % 10 < 5) ? -3 : +3;
      }
      for( float x=octX-20+2; x<octX+20; x+=5 )
      {
        line(x,octY+20, x+slant,octY+50);
      }
    }
    
    
    
    void witch() {
      //draw witch
      fill(28, 2, 43);
      rect(witchX, witchY, 25, 50);
    
      //Head
      fill(255, 0, 0);
      ellipse(witchX+10, witchY-10, 25, 25);
      ellipse(witchX+10, witchY-10, 10, 10); 
    
      //Broom
      fill(150, 50, 50);
      strokeWeight(5);
      stroke(150, 50, 50);
      line(witchX-25, witchY+50, witchX+35, witchY+50);  
    }  
    
    void mousePressed () {
      // Night button
      if (dist(120, 110, mouseX, mouseY) < 60) {
        backgroundSky=color(222, 2, 2);
        backgroundEarth=color(222, 2, 222);
        moonDrawFlag =!moonDrawFlag;
        moonDrawFlag = true;
        sunDrawFlag = false;
      }
      // Day button
      else if (dist(500, 110, mouseX, mouseY) < 60) {
        backgroundSky=color(222, 222, 222);
        backgroundEarth=color(222, 111, 222);
        sunDrawFlag =!sunDrawFlag;
        sunDrawFlag = true;
        moonDrawFlag = false;    
      }
      // Exit button
      else if (dist(500, 400, mouseX, mouseY) < 60)
        exit();    
        // Witch button
      else if (dist(110, 400, mouseX, mouseY) < 60)
        witchDrawFlag=!witchDrawFlag;
    }--
    
  • edited November 2014

    you still have 3moons in draw()!!! Bad.

    and witch in setup()

  • Thank you so much, sorry for giving you a hard time.

  • edited November 2014

    you haven't given me a hard time, it's alright

    here is what I made of it - only one moon in draw() now.

    At the beginning, it is day, so sun is on, moon is off, witch is off (before setup()).

    I also defined 4 fix colors that we use for the 4 backgrounds.

    You still need to do some cleaning up the code.

    ;-)

    // Global
    String author = "FK - takehome: 11/5/14";
    
    float x, y, w, h;
    float witchX = 70, witchY = 70;
    float sunX=100, sunY=90;
    float moonX=100, moonY=90;
    float octX, octY;
    float octDX = 1;
    float horizon;
    float speed = 2, speedY=2.4;
    
    // these don't change
    color backgroundSkyAtDaytime   = color(11, 2, 233); 
    color backgroundEarthAtDaytime = color(115, 234, 123);
    color backgroundSkyAtNight     = color(11); 
    color backgroundEarthAtNight   = color(35);
    
    // in the beginning, we have day : 
    boolean witchDrawFlag = false;
    boolean sunDrawFlag = true;              
    boolean moonDrawFlag = false;     
    
    // and we use day colors (these to change and take the values from above)
    color backgroundSky   = backgroundSkyAtDaytime;
    color backgroundEarth = backgroundEarthAtDaytime;   
    
    // color colorbackground = 22;
    
    void setup() {
      // Setup
      size(600, 600);
      smooth();
      background( 255 );
      textAlign(CENTER);
      // witch();
      x= width/2;
      y= height/2;
      w= 60;
      h= 20;
      octX = width/2;
      octY = horizon + 200;
      horizon = height/5;
    }
    
    void draw() {
      // Next Frame
      // background( colorbackground  );
    
      // Two colors  
      noStroke();
      fill(backgroundSky);
      rect(0, 0, width, 200);
    
      noStroke();
      fill(backgroundEarth);
      rect(0, 200, width, height);
    
      // Button's Text
      fill(255);
      text ("Night", 120, 110);
      text ("Day", 500, 110);
      text ("Quit", 500, 400);
      text ("Witch", 110, 400);
    
      // Author and title
      text (author, 300, height - 500);
    
      octopus();
      bird();
      border();
    
      if (moonDrawFlag)  
        moon();
    
      if (sunDrawFlag)
        sun();
    
      if (witchDrawFlag)
        witch();
    
      // witch's movement
      witchX = witchX + speed;
      witchY = witchY + speedY;
    
      if ((witchX > width-22) || (witchX < 0+22)) {
        speed = speed * -1;
        speed = speed + random (-0.1, 0.1);
      } 
      if ((witchY > height-22) || (witchY < 22)) {
        speedY = speedY * -1;
        speedY = speedY + random (-0.1, 0.1);
      }
    }
    
    void sun() {
      // Draw sun
      noStroke();
      fill(255, 255, 0);     // Yellow
      ellipse(sunX, sunY, 40, 40);
    
      // Sun's movement
      if (sunX > width) {
        sunX=0;
        sunY=  random( 10, horizon-20);
      }
      sunX=  sunX+2;
    } 
    
    void moon() { 
      //Draw moon
      noStroke();
      fill(235, 236, 234);     // color
      ellipse(moonX, moonY, 40, 40);
    
      // moon's movement
      if (moonX > width) {
        moonX=0;
        moonY=  random( 10, horizon-20);
      }
      moonX=  moonX+1.8;
    }
    
    void bird() {
      //draw bird and make bird move
      float birdY= horizon-50;
      fill(145, 40, 130);
      triangle(x, birdY, x-w, birdY-h, x-w, birdY+h);
      triangle(x-25, birdY, x-w, birdY-h-25, x-w, birdY+35);
      //move bird
      x= (x+3);
      if (x>width*1) {
        x=0;
      }
    }
    
    void border() {
      // Draw border 
      float tilt = 20;
      // Lower border
      for (float x=0; x<width; x+=30) {
        stroke(245, 17, 45);
        line( x, height, x+tilt, height-30);
      }
      // Upperside border
      for (float x=0; x<width; x+=30) {
        stroke(245, 17, 45);
        line( x, 0, x+tilt, 30);
      }
      // Left handside border
      for (float y=0; y<height; y+=30) {
        stroke(245, 17, 45);
        line( 0, y, 30, y+tilt);
      }
      // Right handside border
      for (float y=0; y<height; y+=30) {
        stroke(245, 17, 45);
        line( width, y, width-30, y+tilt);
      }
    }
    
    void octopus() {
      float octoDY= 1;  
      //// Octopus:  up & down
      octY += octDX;
      if (octY < horizon) {
        octDX = +1;    // Sink slowly.
      }
      else if (octY > height-50) {
        octDX = -3;
      }
    
      //// Draw body & slanty legs;
      fill( 255, 0, 255 );
      rect( octX-20, octY-25, 40, 50 );
      ellipse( octX, octY-30, 40, 40 );
    
      // Legs
      stroke( 255, 0, 255 );
      strokeWeight(2);
      float slant=0;
      if (octDX<0) {
        // Going up.
        slant = (frameCount % 10 < 5) ? -3 : +3;
      }
      for ( float x=octX-20+2; x<octX+20; x+=5 )
      {
        line(x, octY+20, x+slant, octY+50);
      }
    }
    
    
    
    void witch() {
      //draw witch
      fill(28, 2, 43);
      rect(witchX, witchY, 25, 50);
    
      //Head
      fill(255, 0, 0);
      ellipse(witchX+10, witchY-10, 25, 25);
      ellipse(witchX+10, witchY-10, 10, 10); 
    
      //Broom
      fill(150, 50, 50);
      strokeWeight(5);
      stroke(150, 50, 50);
      line(witchX-25, witchY+50, witchX+35, witchY+50);
    }  
    
    void mousePressed () {
      // Night button
      if (dist(120, 110, mouseX, mouseY) < 60) {
        //    backgroundSky=color(222, 2, 2);
        //    backgroundEarth=color(222, 2, 222);
        backgroundSky= backgroundSkyAtNight;
        backgroundEarth = backgroundEarthAtNight;
        //moonDrawFlag =!moonDrawFlag;
        moonDrawFlag = true;
        sunDrawFlag = false;
      }
      // Day button
      else if (dist(500, 110, mouseX, mouseY) < 60) {
        backgroundSky=  backgroundSkyAtDaytime;
        backgroundEarth = backgroundEarthAtDaytime;
        //    backgroundSky=color(222, 222, 222);
        //    backgroundEarth=color(222, 111, 222);
        //sunDrawFlag =!sunDrawFlag;
        sunDrawFlag = true;
        moonDrawFlag = false;
      }
      // Exit button
      else if (dist(500, 400, mouseX, mouseY) < 60) {
        exit();
      }
      // Witch button
      else if (dist(110, 400, mouseX, mouseY) < 60) {
        witchDrawFlag=!witchDrawFlag;
      }
    }
    //
    
Sign In or Register to comment.