I need your help please (image on/off)

Hello guys,
I need help please, The thing would be, when I press a key an image appears and when I press it again it disappears.
i’ve made it into my code and the image doesn’t 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);
  }
  }
boolean toggle = false;
void setup() { size(600,600); }
void draw() { 
  background(0);
  if(toggle) {
    rect(200,200,300,300);
  }
}
void keyPressed() {
  if(key == ' ') toggle = !toggle;
}

And I suggest you use Ctrl + t to tidy up the code. It is far easier to read that way

1 Like

Not correct

Leave out the if !!!

the assignment is enough: showImage = ! showImage;

(I had an if in your last thread but with OTHER assignments…)

Chrisir

Putting together the replies you have so far this code shows how to use the spacebar to toggle and image (I have used a green rectangle but you can change the code)

boolean  showImage = false;

void setup() {
  size(400, 200);
}

void draw() {
  background(255);
  fill(100, 200, 100);
  noStroke();
  if (showImage) {
    // replace this with your own code
    rect(50, 50, 200, 85);
  }
}

void keyTyped() {
  // Toggle 'image' using spacebar
  if (key == ' ') showImage = !showImage;
}

2 Likes