Random Custom Classes

I’m trying to create a program where different types of fish are defined into different classes, and the user would be able to spawn them randomly by pressing the ‘f’ key. I can’t seem to figure out how to define the random function to a class if that makes sense. So, for instance, if I pressed the ‘f’ key, in theory, a random fish would spawn, but I can’t figure out how to make that happen. I have the classes seperated into different tabs.

Here’s the main tab.

PImage betta, gourami, kelp, vine;

int i;

int numberBetta, numberGourami;
int numberKelp, numberVine;

Fish[] Betta, Gourami;
Plant[] kelps, vines;

void setup() {
  size(500, 500);
  background(#CBF3D2);
  imageMode(CENTER);
  betta = loadImage("fish.png");
  gourami = loadImage("gourami.png");
  kelp = loadImage("kelp.png");
  vine = loadImage("vine.png");

  start();
}

void start() {

  numberBetta = 2;
  numberGourami = 2;
  numberKelp = 2;
  numberVine = 2;

  Betta = new Fish[numberBetta];
  Gourami = new Fish[numberGourami];
  kelps = new Kelp[numberKelp];
  vines = new Vine[numberVine];

  for ( i=0; i<numberBetta; i ++)
  {
    Betta[i] = new Betta(betta, "Sally", 250, 250, 2);
  }
  for ( i=0; i<numberGourami; i ++)
  {
    Gourami[i] = new Gourami(gourami, "George", 250, 250, 4);
  }
  for ( i=0; i<numberKelp; i ++)
  {
    kelps[i] = new Kelp(kelp, "Sea", 175, 375, 9);
  }
  for ( i=0; i<numberVine; i ++)
  {
    vines[i] = new Vine(vine, "Jungle", 325, 350, 9);
  }
}

void fishBowl(float x, float y) {
  fill(#8CBFFF);
  beginShape();
  vertex(150.58+x, 93.09+y);
  vertex(150.58+x, 119.48+y);
  vertex(161.32+x, 119.48+y);
  vertex(161.32+x, 126.151+y);
  vertex(132.23+x, 126.151+y);
  vertex(132.23+x, 137.826+y);
  vertex(122.26+x, 137.826+y);
  vertex(122.405+x, 150.058+y);
  vertex(107.69+x, 150.058+y);
  vertex(107.69+x, 164.514+y);
  vertex(97.46+x, 164.514+y);
  vertex(97.46+x, 179.525+y);
  vertex(86.49+x, 179.525+y);
  vertex(86.49+x, 197.873+y);
  vertex(73.62+x, 197.873+y);
  vertex(73.62+x, 209.548+y);
  vertex(63.16+x, 209.548+y);
  vertex(63.16+x, 356.82+y);
  vertex(73.66+x, 356.82+y);
  vertex(73.66+x, 374.29+y);
  vertex(86.55+x, 374.29+y);
  vertex(86.55+x, 392.54+y);
  vertex(97.54+x, 392.54+y);
  vertex(97.54+x, 408.59+y);
  vertex(107.79+x, 408.59+y);
  vertex(107.79+x, 422.53+y);
  vertex(122.37+x, 422.53+y);
  vertex(122.37+x, 434.45+y);
  vertex(138.14+x, 434.45+y);
  vertex(138.14+x, 449.8+y);
  vertex(166.88+x, 449.8+y);
  vertex(166.88+x, 467.09+y);
  vertex(250+x, 467.09+y);
  vertex(250+x, 93.09+y);
  vertex(363.86+x, 93.09+y);
  vertex(363.86+x, 119.48+y);
  vertex(354.29+x, 119.48+y);
  vertex(354.29+x, 126.151+y);
  vertex(381.97+x, 126.151+y);
  vertex(381.97+x, 137.826+y);
  vertex(391.98+x, 137.826+y);
  vertex(391.98+x, 150.058+y);
  vertex(406.57+x, 150.058+y);
  vertex(406.57+x, 164.514+y);
  vertex(416.83+x, 164.514+y);
  vertex(416.83+x, 179.525+y);
  vertex(427.83+x, 179.525+y);
  vertex(427.83+x, 197.873+y);
  vertex(440.74+x, 197.873+y);
  vertex(440.74+x, 209.548+y);
  vertex(451.29+x, 209.548+y);
  vertex(451.29+x, 356.82+y);
  vertex(440.74+x, 356.82+y);
  vertex(440.74+x, 374.29+y);
  vertex(427.89+x, 374.29+y);
  vertex(427.89+x, 392.54+y);
  vertex(416.91+x, 392.54+y);
  vertex(416.91+x, 408.59+y);
  vertex(406.66+x, 408.59+y);
  vertex(406.66+x, 422.53+y);
  vertex(392.08+x, 422.53+y);
  vertex(392.08+x, 434.45+y);
  vertex(376.1+x, 434.45+y);
  vertex(376.1+x, 450.5+y);
  vertex(348.3+x, 450.5+y);
  vertex(348.3+x, 467.09+y);
  vertex(250+x, 467.09+y);
  vertex(250+x, 93.09+y);
  endShape(CLOSE);
}

void sand(float x, float y) {
  fill(#476A6F);
  beginShape();
  vertex(123+x, 423.232+y);
  vertex(138.568+x, 423.232+y);
  vertex(138.14+x, 408.59+y);
  vertex(166.923+x, 409.332+y);
  vertex(166.88+x, 398.73+y);
  vertex(202.88+x, 398.73+y);
  vertex(202.88+x, 404.884+y);
  vertex(230.89+x, 404.92+y);
  vertex(230.89+x, 410.79+y);
  vertex(263.664+x, 410.79+y);
  vertex(263.9+x, 401.92+y);
  vertex(287.571+x, 402.92+y);
  vertex(287.97+x, 396.73+y);
  vertex(323.97+x, 396.73+y);
  vertex(323.97+x, 402.104+y);
  vertex(344.54+x, 401.92+y);
  vertex(343.726+x, 412.112+y);
  vertex(366.52+x, 412.67+y);
  vertex(365.965+x, 423.232+y);
  vertex(391.541+x, 422.676+y);
  vertex(392.08+x, 434.45+y);
  vertex(376.1+x, 434.45+y);
  vertex(376.1+x, 450.5);
  vertex(348.3+x, 450.5+y);
  vertex(348.3+x, 467.09+y);
  vertex(166.88+x, 467.09+y);
  vertex(166.88+x, 449.8+y);
  vertex(138.14+x, 449.8+y);
  vertex(138.14+x, 434.45+y);
  vertex(122.73+x, 434.45+y);
  endShape(CLOSE);
}

void sandShade(float x, float y) {
  fill(#2d4244);
  beginShape();
  vertex(176.26+x, 422.78+y);
  vertex(184.88+x, 422.78+y);
  vertex(184.88+x, 427.124+y);
  vertex(189.19+x, 427.09+y);
  vertex(189.19+x, 435.7+y);
  vertex(180.57+x, 435.7+y);
  vertex(180.57+x, 431+y);
  vertex(176.26+x, 431+y);
  endShape(CLOSE);

  beginShape();
  vertex(235.2+x, 447.69+y);
  vertex(243.82+x, 447.69+y);
  vertex(243.82+x, 456.31+y);
  vertex(239.757+x, 456.31+y);
  vertex(239.51+x, 458.42+y);
  vertex(230.89+x, 458.42+y);
  vertex(230.89+x, 449.8+y);
  vertex(235.2+x, 449.8+y);
  endShape(CLOSE);

  beginShape();
  vertex(317.38+x, 421.45+y);
  vertex(322.04+x, 421.45+y);
  vertex(321.94+x, 417.22+y);
  vertex(330.56+x, 417.22+y);
  vertex(330.56+x, 425.83+y);
  vertex(326.07+x, 425.83+y);
  vertex(326+x, 430.07+y);
  vertex(317.38+x, 430.07+y);
  endShape(CLOSE);
}

void draw() {
  noStroke();
  fill(#A55F2D);
  rect(0, height/2, width, height/2);

  fishBowl(0, 0);

  fill(#ADD4F1);
  quad(163.64, 100.17, 350.5, 99.97, 350.5, 110.67, 163.64, 110.67);

  sand(0, 0);

  sandShade(0, 0); 

  for (i=0; i<numberBetta; i++)
  {
    Betta[i].display();
    Betta[i].move();
    Betta[i].grow();
  }
  for (i=0; i<numberGourami; i++)
  {
    Gourami[i].display();
    Gourami[i].move();
    Gourami[i].grow();
  }
  for (i=0; i<numberKelp; i++)
  {
    kelps[i].display();
    kelps[i].grow();
  }
  for (i=0; i<numberVine; i++)
  {
    vines[i].display();
    vines[i].grow();
  }
}

The Fish Parent Class

abstract class Fish {
  PImage fishImage;
  String fishName;
  PVector position;
  int speed;
  int growSpeed;
  float randomFish;

  Fish(PImage _image, String _name, int _x, int _y, int _s) {
    fishImage = _image;
    fishName = _name;
    position = new PVector(_x, _y);
    speed = _s;
  }

  abstract void move();
  abstract void grow();

  void display() {
    image (fishImage, position.x, position.y);
  }
}

Here’s the Betta Class

class Betta extends Fish {
  Betta(PImage _image, String _name, int _x, int _y, int _s) {
    super(_image, _name, _x, _y, _s);
  }

  void move() {
    if (position.x < width-150) {
      position.x = position.x+speed;
    }
  }
  void grow() {
    if (mousePressed) {
      betta.resize(150, 150);
    }
  }
}

Here’s the Gourami Class

class Gourami extends Fish {
  Gourami(PImage _image, String _name, int _x, int _y, int _s) {
    super(_image, _name, _x, _y, _s);
  }
  
  void move(){
    if (position.x < width-250) {
      position.x = position.x+speed;
    }
  }
  void grow() {
    if(mousePressed){
    gourami.resize(100,80);
    }
  }
}
1 Like

ArrayList<Fish> fishes = new ArrayList();

for (Fish f : fishes) {

1 Like

I’m a little confused on how to implement this code since you didn’t provide any context to your reply. Are you saying I should simply add these lines, or replace my code with yours?

1 Like

True.

and then

void keyPressed() {
if(key=='f') {
    makeNewFish();
}
}

void makeNewFish() {
int kind=int(random(4)); 
Fish newFish; 
switch(kind){
case 0:
newFish=new Betta(betta, "Sally", 250, 250, 2);
break; 

case 1: 
newFish= new Gourami(gourami, "George", 250, 250, 4);
break; 

//....

}//switch 

fishes.add(newFish); 
}

use more random numbers for size, position and speed please

1 Like