Using the Random function correctly?

Hi,

I have created a game where a diver swims up and down the screen collecting clams. The clams appear from the right hand side and slowly drift left towards the diver. I have used the random function to set the y coordinates of the clams that appear. It seems to be working kind of... the clams keep appearing randomly BUT they seem to be getting re drawn every millisecond! haha How can I get it so that the random Y coordinate given to it remains that until It drifts out of view or collected by the diver. I have tried using booleans to do this but it hasn't worked or more likely I didn't use them in the right way.

Here is the section of code I am referring to:

    void Clams(){

      image(LClam,LargeX,random(60,330));
      image(MClam,MedX,random(60,330));
      image(SClam,SmallX,random(60,330));

        LargeX-=2;  
        MedX-=2;
        SmallX-=2; 

      if(LargeX<1){
        LargeX=int(random(725,900)); //resets background images
      }
      if (MedX<1){
        MedX=int(random(725,900));  //resets background images
      }
        if(SmallX<1){
        SmallX=int(random(725,900)); //resets background images
      }
    }

Here is the full game code so far:

int xDir = 3;
int BG1xPos = 0;
int BG2xPos = 700;
int SB1xPos = 0;
int SB2xPos = 700;
int DyPos = 190;
int LargeX = 750;
int LargeY = 220;
int MedX = 800;
int MedY = 250;
int SmallX = 850;
int SmallY = 270;
PImage BG1; //Background Image(s)
PImage BG2;
PImage SB1; //Seabed Image(s)
PImage SB2;
PImage DiverUP; //Diver Image(s)
PImage DiverDOWN;
PImage DiverNORMAL;
PImage LClam; //Razor Clam Image(s)
PImage MClam;
PImage SClam;
Boolean UpPressed = false;
Boolean DownPressed = false;


void setup(){

 size(700,400); //size of the screen
 textSize(25);  //text size set for all text
 BG1 = loadImage("mainBG1.png");
 BG2 = loadImage("mainBG1.png");
 SB1 = loadImage("seabed.png");
 SB2 = loadImage("seabed.png");
 DiverUP = loadImage("DiverUP.png");
 DiverDOWN = loadImage("Descending.png");
 DiverNORMAL = loadImage("Neutral.png");
 LClam = loadImage("LargeClam.png");
 MClam = loadImage("MediumClam.png");
 SClam = loadImage("SmallClam.png"); 
}

void draw(){


  BackgroundScreen();
  Clams();
  if(UpPressed == false && DownPressed == false){
  DiverN();
  }
  if(UpPressed == true && DownPressed == false){
  DiverU();
  }
  if(UpPressed == false && DownPressed == true){
  DiverD();
  }
} 

void keyReleased(){
    UpPressed = false;
    DownPressed = false;
}

void keyPressed(){
  if(key == CODED){
  if(keyCode ==UP){
    UpPressed = true;
    DownPressed = false;
  DyPos= DyPos -12;
  DiverU();
  }
  if(keyCode ==DOWN){
    UpPressed = false;
    DownPressed = true;
  DyPos= DyPos +12;
  DiverD();
  }
  if(DyPos<60){
  DyPos=60;
  }
  if(DyPos>330){
  DyPos=330;
  }
  }
}

void Clams(){

  image(LClam,LargeX,random(60,330));
  image(MClam,MedX,random(60,330));
  image(SClam,SmallX,random(60,330));

    LargeX-=2;  
    MedX-=2;
    SmallX-=2; 

  if(LargeX<1){
    LargeX=int(random(725,900)); //resets background images
  }
  if (MedX<1){
    MedX=int(random(725,900));  //resets background images
  }
    if(SmallX<1){
    SmallX=int(random(725,900)); //resets background images
  }
}


//All Diver Images
void DiverN(){
  image(DiverNORMAL,23,DyPos); 
}
void DiverU(){
  image(DiverUP,23,DyPos); 
}
void DiverD(){
  image(DiverDOWN,23,DyPos); 
}

//All Background Images
void BackgroundScreen(){

    image(BG1,BG1xPos,0); //Background Images called
    image(BG2,BG2xPos,0);
    image(SB1,SB1xPos,357);
    image(SB2,SB2xPos,357);

    BG1xPos-=2;  //Background Images move to the left (animation)
    BG2xPos-=2;
    SB1xPos-=1; //Seabed animation to the left (slower speed)
    SB2xPos-=1;

  if(BG2xPos<1){
    BG1xPos=0; //resets background images
  }
  if (BG2xPos<1){
    BG2xPos=700;  //resets background images
  }
    if(SB2xPos<1){
    SB1xPos=0; //resets background images
  }
  if (SB2xPos<1){
    SB2xPos=700;  //resets background images
  }
}
Tagged:

Answers

  • Your error is that random() generates a new random number every time it's called, so every time you call Clams(), you get a new random y value. You need to call random() just once, when resetting the clams, and store it, like you stored their x values.

    By the way, by convention, method names start with a lowercase letter, the first letter of the next words with an uppercase letter, like backgroundScreen() instead of BackgroundScreen(). It doesn't affect functionality of course.

  • Hi colouredmirrorball,

    How would i go about calling random() just once?

    Thanks

  • You already have the largeY, medY and smallY variables. You also assigned some values to them when initializing the sketch (which get ignored as you used random() instead for the y position of the image). Try using the random function there instead of those values, and use these y variables for the y position.

  • And as usually advised on this forum, have a look at OOP tutorial. It is really more easy to manage your clams as classes instead of functions.

  • That works, however, now because it has set that random value at the start it cannot set it to a new random number for the next time it appears on the screen.

    I want it so that

    if(largeX<1){
     largeX=int(random(750,900));
     largeY=//give it a new random number
    }
    

    do you know what I mean?

    thanks, glen

  • Okay Ater, Ill have a look at that as well.

    Cheers

  • its all good I think I figured it out! woop woop!

    cheers Guys

Sign In or Register to comment.