This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kang
Kang wrote ...

2019/3/7

Need help with inheriting the traits to offspring...

Kang Kang

2019/3/7

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Predator here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Predator extends Actor
{
    int speed = Greenfoot.getRandomNumber(5) + 1;
    int width = Greenfoot.getRandomNumber(5) + 5;
    int height = Greenfoot.getRandomNumber(5) + 5;
    int turnRate = Greenfoot.getRandomNumber(10) + 20; 
    int hunger = 700;
    int heritedSpeed;
    public Predator() 
        {
            picture(0,0,width,height);
            setRotation(Greenfoot.getRandomNumber(360));
            heritedSpeed = Greenfoot.getRandomNumber(1) + speed;
        }
    public void picture(int x, int y, int width, int height)
        {
            GreenfootImage image = new GreenfootImage(width,height);
            image.setColor(Color.RED);
            image.fillRect(x,y,width,height);
            setImage(image);
        }
    public void movement()
    {
        move(speed);
        if(Greenfoot.getRandomNumber(turnRate) == 1)
           {
               setRotation(Greenfoot.getRandomNumber(360));
           }
    }
    public void act() 
        {
           movement();
           bounceOffWall();
           catchingPrey();
           death();
        }     
    public void bounceOffWall()
        {
            if (getY() == 0 || getY() == getWorld().getHeight()-1) 
            {
                setRotation(360-getRotation());
            }
            if (getX() == 0 || getX() == getWorld().getWidth()-1) 
            {
                setRotation(180-getRotation());
            }
        }
    public void catchingPrey()
    {
        int x = getX(); 
        int y = getY(); 
        if(isTouching(Prey.class))
        {
            removeTouching(Prey.class);
            hunger = 700;
            getWorld().addObject(new Predator(),x,y); 
        }
    }
    public void death()
    {
        hunger -= (0.1 + ((width + height)/15) + (speed/3));
        if(hunger < 1)
        {
            getWorld().removeObject(this);
        }
    }
    public int reproduction()
    {
        speed = Greenfoot.getRandomNumber(1) + speed;
        return speed;
    }
}


 
This is my Code right now which I am trying to make Predator - Prey simulation. However, I have no clue how would I reproduce an offspring that has the traits of its parent. How should I do it?
danpost danpost

2019/3/7

#
Kang wrote...
<< Code Omitted >> This is my Code right now which I am trying to make Predator - Prey simulation. However, I have no clue how would I reproduce an offspring that has the traits of its parent. How should I do it?
You could add a second consstructor, this new one with a Predator parameter:
public Predator(Predator parent)
{
    this();
    // copy inherited field values here
    /** example */  speed = parent.speed; /** */
}
Kang Kang

2019/3/7

#
danpost wrote...
Kang wrote...
<< Code Omitted >> This is my Code right now which I am trying to make Predator - Prey simulation. However, I have no clue how would I reproduce an offspring that has the traits of its parent. How should I do it?
You could add a second consstructor, this new one with a Predator parameter:
public Predator(Predator parent)
{
    this();
    // copy inherited field values here
    /** example */  speed = parent.speed; /** */
}
I tried what you have written exactly but it seems like it isn't working at all. The offsprings are still all different from its parent... I am not sure why ;-;
danpost danpost

2019/3/7

#
Kang wrote...
I tried what you have written exactly but it seems like it isn't working at all. The offsprings are still all different from its parent... I am not sure why ;-;
How, and where, are you creating the offsprings? Show code.
Kang Kang

2019/3/7

#
danpost wrote...
Kang wrote...
I tried what you have written exactly but it seems like it isn't working at all. The offsprings are still all different from its parent... I am not sure why ;-;
How, and where, are you creating the offsprings? Show code.
public void catchingPrey()
    {
        int x = getX(); 
        int y = getY(); 
        if(isTouching(Prey.class))
        {
            removeTouching(Prey.class);
            hunger = 700;
            getWorld().addObject(new Predator(),x,y); 
        }
    }
This is how offsprings are created. They are created when parent catches prey.
danpost danpost

2019/3/7

#
Kang wrote...
<< Code Omitted >> This is how offsprings are created. They are created when parent catches prey.
You need to make use of the new constructor. Change line 9 to:
getWorld().addObject(new Predator(this), x, y);
Kang Kang

2019/3/7

#
danpost wrote...
Kang wrote...
<< Code Omitted >> This is how offsprings are created. They are created when parent catches prey.
You need to make use of the new constructor. Change line 9 to:
getWorld().addObject(new Predator(this), x, y);
Sorry for keep disturbing but it is still not working.
int speed = Greenfoot.getRandomNumber(5) + 1;
    int width = Greenfoot.getRandomNumber(5) + 5;
    int height = Greenfoot.getRandomNumber(5) + 5;
    int turnRate = Greenfoot.getRandomNumber(10) + 20; 
    int hunger = 700;
    int heritedSpeed;
    //int gameSpeed = ((MyWorld)getWorld()).getSpeed();
    public Predator(Predator parent)
    {
        this();
        speed = parent.speed;
        width = parent.width;
        height = parent.height;
        turnRate = parent.turnRate;
    }
public void catchingPrey()
    {
        int x = getX(); 
        int y = getY(); 
        if(isTouching(Prey.class))
        {
            removeTouching(Prey.class);
            hunger = 700;
            getWorld().addObject(new Predator(this),x,y); 
        }
    }
These are the codes that I have copied. DId I make a mistake here?
danpost danpost

2019/3/7

#
Kang wrote...
<< Codes Omitted >> These are the codes that I have copied. DId I make a mistake here?
Looks good. One thing I just noticed is that the size is set before you changed the width and height. Call:
picture(0, 0, width, height);
at the end of the new constructor. If you think "still not working", explain how.
Kang Kang

2019/3/7

#
danpost wrote...
Kang wrote...
<< Codes Omitted >> These are the codes that I have copied. DId I make a mistake here?
Looks good. One thing I just noticed is that the size is set before you changed the width and height. Call:
picture(0, 0, width, height);
at the end of the new constructor. If you think "still not working", explain how.
Thank you so much : ) It is working perfectly now.
You need to login to post a reply.