Generating random images in an array one at a time

Hello! I’m fairly new to this so please bear with me. I’ve been looking all over the place but I couldn’t find anything on this in particular.

So I made a little game where you have different types of food moving from the top left and right areas of the screen towards the middle and pressing A W and D would make the food from the corresponding sides dissapear. Pressing S is supposed to reset the screen and put the food back on the screen in their starting position, except a randomly different type of food on each side this time.

**So herein lies the problem… I dumped all the different food images into an array, then I set the draw to show a random food from the array… except it constantly rapidfire cycles through all the images. Now I have 3 constant cycling objects moving to the center of the screen. I want the food to be set randomly ONCE on each side and then when I press S, it resets and changes to a different food. What am I doing wrong here? Thanks!

//array1 
PImage[] myImageArray = new PImage[10];
PImage potato, grape, wood, title, slice, grapecut;
///Place food here
int foodleft=0;
int foodtop=0;
int foodright=800;
//booleans here//
boolean attack=true;
boolean fromtop=true;
boolean fromleft=true;
boolean fromright=true;
boolean life=true;

//setup start
void setup() { 
//array2
myImageArray [0] = loadImage( "potato.png");
   myImageArray [1] = loadImage( "grape.png");
   myImageArray [2] = loadImage( "banana.png");
   myImageArray [3] = loadImage( "carrot.png");
   myImageArray [4] = loadImage( "cucumber.png");
   myImageArray [5] = loadImage( "eggplant.png");
   myImageArray [6] = loadImage( "onion.png");
   myImageArray [7] = loadImage( "pear.png");
   myImageArray [8] = loadImage( "radish.png");
   myImageArray [9] = loadImage( "strawberry.png");
//loadimage   
  potato=loadImage("potato.png");
  grape=loadImage("grape.png");
  wood=loadImage("wood.png");
  title=loadImage("title.png");
  slice=loadImage("slice.gif");
  grapecut=loadImage("grapecut.png");
//screen settings//  
size(800,800);
}
//setup end


//draw start  
void draw() {
  background(0);  
  if (key=='w') {
  foodtop=0;
  fromtop=false;
}

  if (key=='a') {
  foodleft=0;
  fromleft=false;
}

  if (key=='d') {
  foodright=800;
  fromright=false;
}

  if (key=='s'){
  attack=true;
  fromleft=true;
  fromright=true;
  fromtop=true;
  }

  if (fromleft==true){
  image(myImageArray[(int)random(9)],foodleft, 400, 90, 120);
  foodleft += 2;
  }
  if (foodleft>400){
  life=false;
  }
  
/////////////////////////////////////////////////

  if (fromtop==true){
  image(myImageArray[(int)random(9)],400, foodtop, 90, 120);
  foodtop += 2;
 }
  if (foodtop>400){
  life=false;
  }
  
/////////////////////////////////////////////////

   if (fromright==true){
   image(myImageArray[(int)random(9)],foodright, 400, 90, 120);
   foodright -= 2;
   }
   if (foodright<400){
   life=false;
  }
  
/////////////////////////////////////////////////  


}//draw end




1 Like

draw() is called continuously, and key stores the value of the last key pressed. Importantly, the value of key is not changed when draw() finishes! So key will have the same value the next (and any subsequent!) times draw() runs!

What you need to do is move your “pick a random thing” code into the keyPressed() FUNCTION, so that it only changes ONCE when a key is pressed - not over and over again in draw.

In short, add this function to your code:

void keyPressed(){
  // Put any conditional checks against the value of 'key' in here,
  // And also move the blocks of code that run conditionally into here too.
  // Like these:
   if (key=='w') {
  foodtop=0;
  fromtop=false;
}

  if (key=='a') {
  foodleft=0;
  fromleft=false;
}

  if (key=='d') {
  foodright=800;
  fromright=false;
}

  if (key=='s'){
  attack=true;
  fromleft=true;
  fromright=true;
  fromtop=true;
  }
}
2 Likes

Thank you so much! Keypressed was the key to everything I should’ve thought of that. I also had to put an int to designate what random even is, so that each side can be differently random from eachother. Now my code looks like this. Nice little random image generator moving from 3 sides. I appreciate your help!
Also I checked out your website I love the little games you made! That green red square thing is pretty addicting.

//array1 
PImage[] myImageArray = new PImage[10];
PImage potato, grape, wood, title, slice, grapecut;
///Place food here
int foodleft=0;
int foodtop=0;
int foodright=800;
int rand1 = (int)random(myImageArray.length);
int rand2 = (int)random(myImageArray.length);
int rand3 = (int)random(myImageArray.length);
//booleans here//
boolean attack=true;
boolean fromtop=true;
boolean fromleft=true;
boolean fromright=true;
boolean life=true;

//setup start
void setup() { 
//array2
myImageArray [0] = loadImage( "potato.png");
   myImageArray [1] = loadImage( "grape.png");
   myImageArray [2] = loadImage( "banana.png");
   myImageArray [3] = loadImage( "carrot.png");
   myImageArray [4] = loadImage( "cucumber.png");
   myImageArray [5] = loadImage( "eggplant.png");
   myImageArray [6] = loadImage( "onion.png");
   myImageArray [7] = loadImage( "pear.png");
   myImageArray [8] = loadImage( "radish.png");
   myImageArray [9] = loadImage( "strawberry.png");
//loadimage   
  potato=loadImage("potato.png");
  grape=loadImage("grape.png");
  wood=loadImage("wood.png");
  title=loadImage("title.png");
  slice=loadImage("slice.gif");
  grapecut=loadImage("grapecut.png");
//screen settings//  
size(800,800);
}
//setup end


//draw start  
void draw() {
  background(0);  
    if (fromleft==true){
  image(myImageArray[rand1],foodleft, 400, 90, 120);
  foodleft += 2;
  }
  if (foodleft>400){
  life=false;
  }
  
/////////////////////////////////////////////////

  if (fromtop==true){
  image(myImageArray[rand2],400, foodtop, 90, 120);
  foodtop += 2;
 }
  if (foodtop>400){
  life=false;
  }
  
/////////////////////////////////////////////////

   if (fromright==true){
image(myImageArray[rand3],foodright, 400, 90, 120);
   foodright -= 2;
   }
   if (foodright<400){
   life=false;
  }
  
  
  
/////////////////////////////////////////////////  


}//draw end

void cycle(){
background(0);  
    if (fromleft==true){
  image(myImageArray[rand1],foodleft, 400, 90, 120);
  foodleft += 2;
  }
  if (foodleft>400){
  life=false;
  }
  
/////////////////////////////////////////////////

  if (fromtop==true){
  image(myImageArray[rand2],400, foodtop, 90, 120);
  foodtop += 2;
 }
  if (foodtop>400){
  life=false;
  }
  
/////////////////////////////////////////////////

   if (fromright==true){
image(myImageArray[rand3],foodright, 400, 90, 120);
   foodright -= 2;
   }
   if (foodright<400){
   life=false;
  }
}

void keyPressed(){ 
 if (key=='w') {
   image(myImageArray[rand2],400, foodtop, 90, 120);
  foodtop=0;
  fromtop=false;
}

  if (key=='a') {
      image(myImageArray[rand1],foodleft, 400, 90, 120);
  foodleft=0;
  fromleft=false;
}

  if (key=='d') {
    image(myImageArray[rand3],foodright, 400, 90, 120);
  foodright=800;
  fromright=false;
}

  if (key=='s'){
  attack=true;
  fromleft=true;
  fromright=true;
  fromtop=true;
  cycle();
  rand1 = (int)random(myImageArray.length);
  rand2 = (int)random(myImageArray.length);
  rand3 = (int)random(myImageArray.length);

  }
}

1 Like