# Show image if WASD is not pressed

**URL:** https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168
**Category:** Coding Questions
**Created:** [September 9, 2021, 7:51pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168 "2021-09-09T19:51:40Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [September 9, 2021, 7:51pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/1 "2021-09-09T19:51:40Z")

</div>

when either w, a, s or d is pressed my code moves my image and cycles through different ones. i want it to show image "midas[1]’ if none of these buttons is pressed. i have a line of code

> ```auto
> if (key != 'w' && key!= 'a' && key!='s' && key != 'd') {
> age(midas[1], midasx, midasy, 50, 80);
> }
> 
> ```

that i thought would do this but it only does this at the beginning and not when i stop pressing a button.

here’s the rest of my code

```auto
PImage[] midas= new PImage[8];

float midasx= 1;
float midasy= 1;

int i=1;

void setup() {
  size(600, 600);
  midas[1] = loadImage("1.png");
  midas[2] = loadImage("2.png");
  midas[3] = loadImage("3.png");
  midas[4] = loadImage("4.png");
  midas[5] = loadImage("5.png");
  midas[6] = loadImage("6.png");
  midas[7] = loadImage("7.png");
}
void draw() {
  background(0, 0, 0);
  tint(255, 255);
  if (key != 'w' && key!= 'a' && key!='s' && key != 'd') {
      image(midas[1], midasx, midasy, 50, 80);
    }

  
  if (keyPressed) {
    if (key == 'w') {
      midasy=midasy-10;
      image(midas[i], midasx, midasy, 50, 80);
    }
  }
  
    if (keyPressed) {
    if (key == 's') {
      midasy=midasy+10;
      image(midas[i], midasx, midasy, 50, 80);
    }
  }
    if (keyPressed) {
    if (key == 'a') {
      midasx=midasx-10;
      image(midas[i], midasx, midasy, 50, 80);
    }
  }
    if (keyPressed) {
    if (key == 'd') {
      midasx=midasx + 10;
      image(midas[i], midasx, midasy, 50, 80);
    }
  }
  

  i++;
  delay(100);
  if (i==7) {
    i=1;}}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 9, 2021, 7:56pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/2 "2021-09-09T19:56:24Z")

</div>

you can set variable boolean showMidas1 = true; before setup

In keyPressed Wasd set it to false

make a new function

void keyReleased() {  
showMidas1 = true;  
}

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [September 9, 2021, 8:03pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/3 "2021-09-09T20:03:20Z")

</div>

the image shows up twice now

```auto
PImage[] midas= new PImage[8];

boolean showMidas1= true;

float midasx= 1;
float midasy= 1;

int i=1;

void setup() {
  size(600, 600);
  midas[1] = loadImage("1.png");
  midas[2] = loadImage("2.png");
  midas[3] = loadImage("3.png");
  midas[4] = loadImage("4.png");
  midas[5] = loadImage("5.png");
  midas[6] = loadImage("6.png");
  midas[7] = loadImage("7.png");
}
void draw() {
  background(0, 0, 0);
  tint(255, 255);

  if(showMidas1=true){
    image(midas[1], midasx, midasy, 50, 80);}
  
  if (keyPressed) {
    if (key == 'w') {
      midasy=midasy-10;
      image(midas[i], midasx, midasy, 50, 80);
      showMidas1= false;
    }
  }
  
    if (keyPressed) {
    if (key == 's') {
      midasy=midasy+10;
      image(midas[i], midasx, midasy, 50, 80);
      showMidas1= false;
    }
  }
    if (keyPressed) {
    if (key == 'a') {
      midasx=midasx-10;
      image(midas[i], midasx, midasy, 50, 80);
      showMidas1= false;
    }
  }
    if (keyPressed) {
    if (key == 'd') {
      midasx=midasx + 10;
      image(midas[i], midasx, midasy, 50, 80);
      showMidas1= false;
    }
  }
  

  i++;
  delay(100);
  if (i==7) {
    i=1;}}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 9, 2021, 8:07pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/4 "2021-09-09T20:07:53Z")

</div>

> [@MoonAlien822](#):
>
> `if(showMidas1=true){`

Please == double ==

and also place else {  
after this line

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 9, 2021, 8:08pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/5 "2021-09-09T20:08:51Z")

</div>

And } after the entire keyPressed section

Remember to use ctrl-t in processing to get automatically format

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [September 9, 2021, 8:11pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/6 "2021-09-09T20:11:47Z")

</div>

it has now stopped doing anything

```auto

PImage[] midas= new PImage[8];

boolean showMidas1= true;

float midasx= 1;
float midasy= 1;

int i=1;

void setup() {
  size(600, 600);
  midas[1] = loadImage("1.png");
  midas[2] = loadImage("2.png");
  midas[3] = loadImage("3.png");
  midas[4] = loadImage("4.png");
  midas[5] = loadImage("5.png");
  midas[6] = loadImage("6.png");
  midas[7] = loadImage("7.png");
}
void draw() {
  background(0, 0, 0);
  tint(255, 255);

  if (showMidas1==true) {
    image(midas[1], midasx, midasy, 50, 80);
  } else {

    if (keyPressed) {
      if (key == 'w') {
        midasy=midasy-10;
        image(midas[i], midasx, midasy, 50, 80);
        showMidas1= false;
      }
    }

    if (keyPressed) {
      if (key == 's') {
        midasy=midasy+10;
        image(midas[i], midasx, midasy, 50, 80);
        showMidas1= false;
      }
    }
    if (keyPressed) {
      if (key == 'a') {
        midasx=midasx-10;
        image(midas[i], midasx, midasy, 50, 80);
        showMidas1= false;
      }
    }
    if (keyPressed) {
      if (key == 'd') {
        midasx=midasx + 10;
        image(midas[i], midasx, midasy, 50, 80);
        showMidas1= false;
      }
    }
  }

  i++;
  delay(100);
  if (i==7) {
    i=1;
  }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 9, 2021, 8:17pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/7 "2021-09-09T20:17:45Z")

</div>

Right, delete the else

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 9, 2021, 8:18pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/8 "2021-09-09T20:18:56Z")

</div>

> [@Chrisir](#):
>
> make a new function
> 
> void keyReleased() {  
> showMidas1 = true;  
> }

Did you do that?

Chrisir

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [September 9, 2021, 8:19pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/9 "2021-09-09T20:19:21Z")

</div>

> [@Chrisir](#):
>
> void keyReleased() {  
> showMidas1 = true;  
> }

oh your right i forgot

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [September 9, 2021, 8:20pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/10 "2021-09-09T20:20:38Z")

</div>

still not working

```auto
PImage[] midas= new PImage[8];

boolean showMidas1= true;

float midasx= 1;
float midasy= 1;

int i=1;

void setup() {
  size(600, 600);
  midas[1] = loadImage("1.png");
  midas[2] = loadImage("2.png");
  midas[3] = loadImage("3.png");
  midas[4] = loadImage("4.png");
  midas[5] = loadImage("5.png");
  midas[6] = loadImage("6.png");
  midas[7] = loadImage("7.png");
}
void draw() {
  background(0, 0, 0);
  tint(255, 255);

  if (showMidas1==true) {
    image(midas[1], midasx, midasy, 50, 80);
  } 

    if (keyPressed) {
      if (key == 'w') {
        midasy=midasy-10;
        image(midas[i], midasx, midasy, 50, 80);
        showMidas1= false;
      }
    }

    if (keyPressed) {
      if (key == 's') {
        midasy=midasy+10;
        image(midas[i], midasx, midasy, 50, 80);
        showMidas1= false;
      }
    }
    if (keyPressed) {
      if (key == 'a') {
        midasx=midasx-10;
        image(midas[i], midasx, midasy, 50, 80);
        showMidas1= false;
      }
    }
    if (keyPressed) {
      if (key == 'd') {
        midasx=midasx + 10;
        image(midas[i], midasx, midasy, 50, 80);
        showMidas1= false;
      }
    }
  

  i++;
  delay(100);
  if (i==7) {
    i=1;
  }}
  
  void keyReleased() {
showMidas1 = true;

}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 9, 2021, 8:23pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/11 "2021-09-09T20:23:07Z")

</div>

What happens falsely

Get rid of delay

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [September 9, 2021, 8:23pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/12 "2021-09-09T20:23:47Z")

</div>

it still shows two images at the same time sometimes

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 9, 2021, 8:25pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/13 "2021-09-09T20:25:15Z")

</div>

> [@MoonAlien822](#):
>
> if (showMidas1==true) {

add && ! keyPressed

Chrisir

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [September 9, 2021, 8:25pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/14 "2021-09-09T20:25:58Z")

</div>

yup it works, thank you

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 9, 2021, 8:29pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/15 "2021-09-09T20:29:31Z")

</div>

Actually you can move the Display of the image out of the if-clauses so there is only one line at the end

Use the displaying with an if (showMidas1==false){…

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [September 10, 2021, 3:41pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/16 "2021-09-10T15:41:37Z")

</div>

hey back with another question, i added another object “daughter” i would like “daughter” to change to a different image when “midas” touches it

```auto

PImage[] midas= new PImage[8];
PImage[] daughter= new PImage[3];

boolean showMidas1= true;

float midasx= 1;
float midasy= 1;

float daughterx= width;
float daughtery= height;

int i=1;
int i2= 1;

void setup() {
  size(600, 600);
  midas[1] = loadImage("1.png");
  midas[2] = loadImage("2.png");
  midas[3] = loadImage("3.png");
  midas[4] = loadImage("4.png");
  midas[5] = loadImage("5.png");
  midas[6] = loadImage("6.png");
  midas[7] = loadImage("7.png");
  daughter[1] = loadImage("daughter.png");
  daughter[2] = loadImage("daughter gold.png");
}
void draw() {
  background(0, 0, 0);
  tint(255, 255);
  if(midas and daughter are touching){
    i2=2;}
  
 image(daughter[i2], daughterx, daughtery, 40, 70);
 

  if (showMidas1==true&& ! keyPressed) {
    image(midas[1], midasx, midasy, 50, 80);
  } 

    if (keyPressed) {
      if (key == 'w') {
        midasy=midasy-10;
        image(midas[i], midasx, midasy, 50, 80);
        showMidas1= false;
      }
    }

    if (keyPressed) {
      if (key == 's') {
        midasy=midasy+10;
        image(midas[i], midasx, midasy, 50, 80);
        showMidas1= false;
      }
    }
    if (keyPressed) {
      if (key == 'a') {
        midasx=midasx-10;
        image(midas[i], midasx, midasy, 50, 80);
        showMidas1= false;
      }
    }
    if (keyPressed) {
      if (key == 'd') {
        midasx=midasx + 10;
        image(midas[i], midasx, midasy, 50, 80);
        showMidas1= false;
      }
    }
  

  i++;
  delay(100);
  if (i==7) {
    i=1;
  }}
  
  void keyReleased() {
showMidas1 = true;

}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 11, 2021, 5:20pm UTC](https://discourse.processing.org/t/show-image-if-wasd-is-not-pressed/32168/17 "2021-09-11T17:20:14Z")

</div>

> [@MoonAlien822](#):
>
> midas[1] = loadImage(“1.png”);  
> midas[2] = loadImage(“2.png”);  
> midas[3] = loadImage(“3.png”);  
> midas[4] = loadImage(“4.png”);  
> midas[5] = loadImage(“5.png”);

first allow me to add a comment to this:  
arrays start with 0, so first entry should be midas[0] = loadImage(“1.png”);

* * *

**Your question**

> [@MoonAlien822](#):
>
> i added another object “daughter” i would like “daughter” to change to a different image when “midas” touches it

> [@MoonAlien822](#):
>
> ` if(midas and daughter are touching){`

Yeah, use dist() command here - see reference

something like  
`if(dist(midasX,midasy, daughterx,daughtery) < 45) {`

please note that this works best, when you use the center of both images (and not upper left corner for example; so maybe say `midasx+midaswidth/2`) (and take into account the sum of their radii to know what value is best where I said 45)

(There are more advanced formulas for rect/rect collision but that should be enough)

Chrisir
