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

2011/6/11

On fixing 'Infect'

1
2
3
4
5
6
danpost danpost

2011/6/15

#
Let me know when I can upload another version to show you. I have some image issues that I am working on. OK! Got the image problem taken care of!
kiarocks kiarocks

2011/6/16

#
ok
kiarocks kiarocks

2011/6/16

#
you can now
danpost danpost

2011/6/16

#
kiarocks, it has been uploaded for you. Also, it is being looked at to determine why a compiler message is popping up after compiling the program.
kiarocks kiarocks

2011/6/16

#
awesome!!!
danpost danpost

2011/6/16

#
Do you want me to take it off? or leave it on? I have no need for it to be on anymore.
kiarocks kiarocks

2011/6/17

#
you can take it off
danpost danpost

2011/6/17

#
I took it off. Yes, one survived! It was probably hiding under a rock and avoided getting infected!
kiarocks kiarocks

2011/6/18

#
no idea what you said in comments but ill try
kiarocks kiarocks

2011/6/25

#
how would you do what mik suggested?
danpost danpost

2011/6/25

#
First, in the Person.class, add the variables 'private int age = 18 * InfectWorld.ageRate;' and 'private int procreateChance = 20;' and 'private int infectChance = 10;' and 'private int deathChance = 5;' and, of course, 'private int sex;'. In '(World) InfectWorld' you could add another variable 'public static final int ageRate = 20;' to regulate how fast they age. '20' is probably not what you want (it can be changed -- higher, probably, or lower, I doubt). It will be the number of Person.act()s cycled through to complete one year of life.
public void act() // In 'Person.class'
{
    age++;
    int actualAge = (age - (age % InfectWorld.ageRate) ) / InfectWorld.ageRate; // Computes actual age
    if (age % InfectWorld.ageRate == 0 && actualAge % 13 == 0) // On every 13th Birthday
    // The first part asks if it is a birthday, the second part asks if actualAge is a multiple of 13
    {
        infectChance += 10;
        deathChance += 5;
    }
    if (actualAge< 15) { procreateChance = 0; }
    if (actualAge > 14) { procreateChance = 10; }
    if (actualAge > 20) { procreateChance = 30; }
    if (actualAge > 35) { procreateChance = 10; }
    if (actualAge > 40) { procreateChange = 5; }
    if (actualAge > 50) { procreateChange = 1; }
    // This next part adjust the image size for growth
    GreenfootImage myImage = getImage(); // full size = 43 x 51
    int myWidth = (actualAge + 1) * 43 / 18;    
    if (myWidth > 43) { myWidth = 43; }
    int myHeight = (actualAge + 1) * 51 / 18;
    if (myHeight > 51) { myHeight = 51; }
    myImage.scale(myWidth, myHeight);
    setImage(myImage);
    // Check for death
    if (Greenfoot.getRandomNumber(100) < deathChance)
    {
        die(); // Need to create method 'private void die() { ... }' to do what needs done when death occurs
    }
    ...  //  Check for near infecteds, and if so:
    if (Greenfoot.getRandomNumber(100) < infectChance)
    {
        infect();
    }
    // Check for couterpart Person adjacent, and if so:
    if (Greenfoot.getRandomNumber(100) < procreateChance)
    {
        procreate(); // add Person: age, procreateChance, infectChance = 0; sex = Greenfoot.getRandomNumber(2);  etc.
    }
    ... // other code you MIGHT already have
}
This would be the basics for setting up what he was referring to.
danpost danpost

2011/6/26

#
Actually, it might be better for lines 5 through 16 above to incorporate the use of a 'switch' like so:
if (age % InfectWorld.ageRate == 0) // asking if is an actual birthday
{
    switch (actualAge)
    {
        case 13:
            infectChance = 10;
            deathChance = 1;
            procreateChance = 2;
            break;
        case 18:
            infectChance = 15;
            deathChance = 5;
            procreateChance = 10;
            break;
        case 21:
            deathChance = 3;
            procreateChance = 15 - 5 * infectionLevel;
            break;
        case 26:
            procreateChance = 20 - 5 * infectionLevel;
        case 35:
            procreateChance = 5 - infectionLevel;
        case 50:
            procreateChance = 0;
            infectChance = 20;
            deathChance = 5;
        case 60:
            infectChance = 25;
            deathChance = 8;
        case 75:
            infectChance = 30;
            deathChance = 12;
    }
 }
kiarocks kiarocks

2011/6/26

#
how do i add variable age?
danpost danpost

2011/6/26

#
You could even add to Person.class the variables 'private boolean isPregnant = false;' and 'private int howLongTillDue = 0;' so if procreating, change 'isPregnant' to 'true' and 'howLongTillDue' to 'InfectWorld.ageRate * 3 / 4' (equivalent to 9 months). In act() for Person:
if (isPregnant)
{
    howLongTillDue--;
    if (howLongTillDue == 0)
    {
        getWorld().addObject(new Person(0, 0), getX(), getY());
        isPregnant = false;
    }
}
And add methods: (find out which of the two encountering Persons is the female)
private void procreate(Person female)
{
    isPregnant = true;
    howLongTillDue = InfectWorld.ageRate * 3 / 4;
}
Add a new constructor:
public void Person(int myAge, int stageOfInfection)
{
    age = myAge;
    infectionStage = stageOfInfection;
    sex = Greenfoot.getRandomNumber(2);
    direction = Greenfoot.getRandomNumber(4);
    setRotation(90 * direction);
    // set image and set other variables here
}
danpost danpost

2011/6/26

#
Where the other variables for Person are declared:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;

public class Person extends Actor
{
    private int direction;
    private int infectionStage;
    private int actCycle = 0;
    // Each instance of Person has these variables and the values are independent from each other
    // The new variables go right along with those above
    private int age;
    private int sex;
    private int infectChance;
    private int procreateChance;
    private int deathChance;
    private boolean isPregnant = false;
    private int howLongTillDue = 0;
    
    public Person()
    {
        direction = Greenfoot.getRandomNumber(4);
        setRotation(90 * direction);
        age = Greenfoot.getRandomNumber(100);
        sex = Greenfoot.getRandomNumber(2);
        infectionStage = 0;
        // set other variables and image dependent on age/infectionStage
    }

    public Person(int myAge, int stageOfInfection)
    {
        age = myAge;
        infectionStage = stageOfInfection;
        sex = Greenfoot.getRandomNumber(2);
        // set other variables and image
    }

    public void act() 
    {
         // act code
    }
}
There are more replies on the next page.
1
2
3
4
5
6