Why won't this run? (keyPressed)

ok, forget translate

here is a version with working UP and SHIFT

color c20=color(255);
color c24=color(35, 155, 86);//GREEN
color c21=color(244, 208, 63);//YELLOW
color c22=color(230, 126, 34);//ORANGE
color c27=color(174);//gray

void setup()
{
  //frameRate(10);
  size(500, 500);
}

void draw()
{
  background(200);
  house();
}

// -------------------------------------------------------------------------------------------

void house() {

  translate (-200, -200);

  //else
  //{
  // color c8=color(231,76,60);//RED

  //fill(c8);
  //rect(mouseX+150,mouseY+200,200,180);
  // }
  //}

  int i;
  i=0;
  while (i<5)
  {
    color c34=color(255, 51, 227);
    fill(c34);
    rect(mouseX+150, mouseY+140-33*i, 20, 18);
    i = i+1;
  }

  fill(c20);
  rect(mouseX+150, mouseY+200, 200, 180);

  fill(c24);
  triangle(mouseX+130, mouseY+200, mouseX+370, mouseY+200, mouseX+250, mouseY+100);

  // color c3=color(100, 10, 0);
  fill(c27);
  rect(mouseX+234, mouseY+310, 30, 70);

  color c4=color(60, 120, 200);
  fill(c4);
  rect(mouseX+160, mouseY+240, 40, 30);

  color c5=color(60, 120, 200);
  fill(c5);
  rect(mouseX+300, mouseY+240, 40, 30);

  color c6=color(60, 120, 200);
  fill(c6);
  ellipse(mouseX+250, mouseY+255, 70, 50);

  color c7=color(249, 249, 249);
  fill(c7);
  rect(mouseX+247, mouseY+230, 5, 50);

  fill(c7);
  rect(mouseX+215, mouseY+250, 70, 5);

  fill(c7);
  rect(mouseX+160, mouseY+250, 40, 5);

  fill(c7);
  rect(mouseX+300, mouseY+250, 40, 5);

  fill(c7);
  rect(mouseX+317, mouseY+240, 5, 30);

  fill(c21);
  rect(mouseX+177, mouseY+240, 5, 30);
}

// -------------------------------------------------------------------------------------------

void keyPressed() {

  if (key == CODED)
  {
    if (keyCode == UP)
    {
      c20=color(108, 52, 131); //PURPLE
    }
  }

  // rect(mouseX+150, mouseY+200, 200, 180);

  if (keyCode == DOWN)
  {
    c24=color(35, 155, 86);//GREEN

    fill(c24);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }

  if (keyCode == LEFT)
  {
    c21=color(244, 208, 63);//YELLOW

    fill(c21);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }
  if (keyCode == RIGHT)
  {
    c22=color(230, 126, 34);//ORANGE
    fill(c22);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }
  if (keyCode == SHIFT)
  {
    println("Shift");
    c27=color(174, 214, 241);//BLUE
    fill(c27);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }
}
//
1 Like

in this version ALL 5 keys change color

you need to do a bit of house cleaning in the code (no pun intended)
so that all windows use the same color (for example)

Don’t be discouraged!

You can do it!

Chrisir

color c20=color(255);
color c24=color(222);//
color c21=color(244);//
color c22=color(34);//
color c27=color(174);//gray

void setup()
{
  //frameRate(10);
  size(500, 500);
}

void draw()
{
  background(200);
  house();
}

// -------------------------------------------------------------------------------------------

void house() {

  translate (-200, -200);

  //else
  //{
  // color c8=color(231,76,60);//RED

  //fill(c8);
  //rect(mouseX+150,mouseY+200,200,180);
  // }
  //}

  int i;
  i=0;
  while (i<5)
  {
    color c34=color(255, 51, 227);
    fill(c34);
    rect(mouseX+150, mouseY+140-33*i, 20, 18);
    i = i+1;
  }

  fill(c20);
  rect(mouseX+150, mouseY+200, 200, 180);

  fill(c24);
  triangle(mouseX+130, mouseY+200, mouseX+370, mouseY+200, mouseX+250, mouseY+100);

  // color c3=color(100, 10, 0);
  fill(c27);
  rect(mouseX+234, mouseY+310, 30, 70);

  fill(c22);
  rect(mouseX+160, mouseY+240, 40, 30);

  color c5=color(60, 120, 200);
  fill(c5);
  rect(mouseX+300, mouseY+240, 40, 30);

  color c6=color(60, 120, 200);
  fill(c6);
  ellipse(mouseX+250, mouseY+255, 70, 50);

  color c7=color(249, 249, 249);
  fill(c7);
  rect(mouseX+247, mouseY+230, 5, 50);

  fill(c7);
  rect(mouseX+215, mouseY+250, 70, 5);

  fill(c7);
  rect(mouseX+160, mouseY+250, 40, 5);

  fill(c7);
  rect(mouseX+300, mouseY+250, 40, 5);

  fill(c7);
  rect(mouseX+317, mouseY+240, 5, 30);

  fill(c21);
  rect(mouseX+177, mouseY+240, 5, 30);
}

// -------------------------------------------------------------------------------------------

void keyPressed() {

  if (key == CODED)
  {
    if (keyCode == UP)
    {
      c20=color(108, 52, 131); //PURPLE
    }
  }

  // rect(mouseX+150, mouseY+200, 200, 180);

  if (keyCode == DOWN)
  {
    c24=color(35, 155, 86);//GREEN

    fill(c24);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }

  if (keyCode == LEFT)
  {
    c21=color(244, 208, 63);//YELLOW

    fill(c21);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }
  if (keyCode == RIGHT)
  {
    c22=color(230, 126, 34);//ORANGE
    // fill(c22);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }
  if (keyCode == SHIFT)
  {
    println("Shift");
    c27=color(174, 214, 241);//BLUE
    fill(c27);
    //rect(mouseX+150, mouseY+200, 200, 180);
  }
}
//
1 Like

It was a problem that the color you changed in keyPressed were not used in house() - there were other colors like c4 used…

:wink:

1 Like

Ok thank you so much for all your help.

2 Likes

You are welcome!!

Chrisir

1 Like

I placed translate() at the start of house to move all that gets drawn afterwards up and to the left

So the house can be placed everywhere in the window; before that it was too far right and down.

See reference for translate