I need your help please (image on / off)

Hello guys,
I would like to do something but I don’t know how :
The thing would be, when I press a key an image appears and when I press it again it disappears.
I know that it must be something whit keyPressed, but i don’t know how to combine keypressed and PImage…
So if someone understood what I am trying to do and can help me I would be very grateful

use the function keyPressed() please


set a global variable showImage initially to false. So before setup()

boolean showImage = false;


in the function keyPressed() say something like

void keyPressed() {
  //toggle
  if (showImage) 
    showImage=false;
  else showImage=true;
}

In draw(): When you display the image say if(showImage) image(…);

Use background(0); in draw()

1 Like

Well i have no doubt about your answer but i’ve made it into my code and the image does’nt shows up
I put my code here and if someone sees something wrong about it tell me pls ^^

PImage chaines;
PImage dvd;
float b;
float d = 1;
int speedX = 3;
int speedY = 3;
float x = random(width);
float y = random(height);
bug[] bugs = new bug[3000];
boolean freeze = false;
boolean slow = false;
boolean showImage = false;

color white = (255);
void setup (){
  fullScreen();
  chaines = loadImage ("chaine2.png");

  dvd = loadImage ("dvd.png");

  for (int i = 0; i < bugs.length; i++) {
    bugs[i] = new bug();
  }
}
void draw () { 
background (white);
if (showImage) {
  image (chaines,width/2,height/2); }
if (freeze) { 
  for (int i = 0; i < bugs.length; i++) {
    bugs[i].fall();
    bugs[i].show();
}
    if (slow) {   
  frameRate(30); }
}
     
bouger(); 
couleurs();
}

void couleurs() {
  
b = b + d;
tint (b, 255-b, b);

if (b > 255 || b < 0) { d = d * -1;}
}

void bouger() {

  x = x + speedX;
 y = y + speedY;

if (x < 0 || x > width - 339) { speedX = speedX * -1;} 
if (y < 0 || y > height - 149) { speedY = speedY * -1;}

image (dvd,x,y);
}

void keyPressed() { 

if (key == 'b' || key == 'B') {
  if (freeze) { 
  freeze = false;}
  else { freeze = true;}
 if (slow) {
  slow = !slow; }
}
if (key == 'v') {
  if (showImage) {
    showImage = !showImage; }
}
}

And on another outlet

class bug {
  float x = random(width);
  float y = random (-100,100);
  float speedY = random(5,10);
  float taille = random (50, 70);
  void fall() {
    y = y+speedY;
    
    if (y > height) {
      y = random (-100,-100);
    }
  }
  void show() {
    fill(0);
    line(x,y,x,y+taille);
  }
  }

continued in his next thread