Need help on a final project

Hello, I am currently doing a final for one of my beginner classes and we are doing custom classes. My project is to have fish swimming around and when your mouse comes into screen, there is a hook following the mouse and the fish swim off screen.

Here is where my problems arise, I have been able to make the fish swim around the screen with its own child class, however, I cannot get the hook image to appear at all following the mouse. I made the “hook” its own child class but it will not appear. I also don’t know where to begin when it comes to wrapping my head around how to make the fish swim off the screen when the mouse moves on screen.

here is my code:

Parent Class:

PImage water, fish1, fish2, hook;

Fish f1, f2;

Hook h1;


void setup() {
  size(500,500);
  water = loadImage("water.JPG");
  fish1 = loadImage("fish 1.png");
  fish2 = loadImage("fish 2.png");
  hook = loadImage("hook.png");
  
  f1 = new Fish(fish1);
  f2= new Fish(fish2);
h1 = new Hook(hook);
}

void draw(){
  image(water,0 ,0);
  
  f1.move();
  f1.display();
  f2.move();
  f2.display();
  
}

Child Class Fish:

class Fish{
  int x, y;
  int xTarget, yTarget;
  
  int easeSpeed;
  PImage Image;
  
  Fish(PImage fishImage){
    x = int(random(-width/2, width/2));
    y = int(random(-height/2, height/2));
    Image = fishImage;
    makeTarget();
  }
  
  void move(){
    
    int distanceX = x - xTarget;
    int distanceY = y - yTarget;
    
    x = x-distanceX/easeSpeed;
    y = y-distanceY/easeSpeed;
    
    if (abs(distanceX) < easeSpeed && abs(distanceY) < easeSpeed)
    {
      makeTarget();
    }
    
  }
  
  void makeTarget(){
    
    xTarget = int(random(-width/2, width/2));
    yTarget = int(random(-height/2, height/2));
    easeSpeed = int(random(20, 90));
  }
  
  void display(){ 
    pushMatrix();
    translate(width/2, height/2);
    image(Image, x, y);
    popMatrix();
  }
}

Child Class Hook:

class Hook{
  PImage image;
  
  Hook(PImage hookImage) {
    image = hookImage;
  }
  
  void display(){
    imageMode(CENTER);
    image(image, mouseX, mouseY);

  }
}

any help would be greatly appreciated

1 Like

Hi,

it seems that you are never asking to display your hook inside the draw() function.

Try adding this line at the end of the draw() function:
h1.display();

1 Like

EDIT:

I figured out my display issue on the hook.

Now I only don’t know how to get the fish to swim off screen when the mouse is at a certain location

To do that, you need to get, for every fish in your tank, the distance between that fish and the screen. Even better, you can figure out the direction from your fish to the hook.

You can do that by using PVector objects.

It means that you will also need to edit your fish class to add a new function that takes the x and y position of your mouse and gives you a PVector pointing to the mouse position:

PVector getDeltaFromPoint(x, y) {
  ...
}

Then with that vector, you can get the magnitude (=distance) with the mag() function and if the distance is lower than a certain value you make your fish go the opposite direction.

1 Like