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

2021/2/14

Problem with counters

Nachtari Nachtari

2021/2/14

#
I am new to Greenfoot and am trying something out right now. It's not really a game but something playful to test stuff out. So I have 25x25 world of gras. The gras can grow. And I have sheeps. The sheep can move, eat, mate, die. So I wanted to make 2 counters. One should keep track about how many sheeps are on the field right now and one counter should keep track how many sheep died so far. But my counters somehow don't work. The death counter works only in the beginning but than the deaths rise unproportionally high and the sheep counter does not work at all. Also my code is partly written in german. The normal commands are english but methods made by me are named and described in german.
public class Insel extends World
{
    /**
     * Schafft eine Welt mit 25x25 Zellen von jeweils 20x20 Pixel
     */
    public Insel() {
        super(25, 25, 20);
        pflanzeGras();
        setzeSchafe();
        lebendeSchafe = this.getObjects(Schaf.class).size();
        gestorbeneSchafe = 0;
    }
    
    public void act(){
        this.showText("Schafe:"+lebendeSchafe,2,0);
        this.showText("Leichen:"+gestorbeneSchafe, 2,1);
    }
    
    public static int lebendeSchafe;//Anzahl der lebenden Schafe 
    public static int gestorbeneSchafe;//Anzahl der gestorbenen Schafe
    
    /**
    * Setzt auf alle Zellen der Welt frisches Gras
     */
    public void pflanzeGras()
    {
        for(int i=0; i<25; i++) {
            for(int k=0; k<25; k++) {
                Gras gras = new Gras();
                addObject(gras, i, k);
            }
        }
    }

    /**
     * Setzt eine Anzahl von Schafen in die Welt
     */
    public void setzeSchafe() {
        for(int m=0; m<20; m++) {
            Schaf schaf = new Schaf();
            addObject(schaf, Greenfoot.getRandomNumber(25), Greenfoot.getRandomNumber(25));
        }
    }
}
Does anybody has an idea why that is?
public class Schaf extends Actor{
    public int gewicht;//Gewicht in kg
    public int alter;//Alter in Monaten
    public int eizellen;//Anzahl der Eizellen
    private int stepCount;//Anzahl der Schritte

    public void act() {
        laufen();
        fressen();
        paarung();
        tod();
        if(alter>=8){
            setImage("schaf.png");
        }
    }

    /**
     * Lässt ein Schaf sterben, wenn es entweder zu alt oder zu abgemagert ist
     */
    public void tod() {
        if(alter>= 100 + Greenfoot.getRandomNumber(20) || gewicht<=45) {
            getWorld().removeObject(this);
            Insel.gestorbeneSchafe = Insel.gestorbeneSchafe+1;
            Insel.lebendeSchafe = Insel.lebendeSchafe-1;
        }
    }

    /**
     * Wenn sich 2 Schafe, die min. 80kg schwer und zwischen 8 und 72 Monaten alt sind, in einem Radius
     * von einem Feld aufhalten, paaren sie sich und es ensteht ein neues Schaf irgendwo auf dem Feld.
     */
    public void paarung() {
        if(getObjectsInRange(1, Schaf.class)!=null && gewicht>=80 && alter>=8 && alter <=72){
            gewicht = 10;
            getWorld().addObject(new Schaf(),Greenfoot.getRandomNumber(25),Greenfoot.getRandomNumber(25));
            eizellen = eizellen-1;
            Insel.lebendeSchafe = Insel.lebendeSchafe+1;
        }
    }

    /** 
     * Die Eigenschaften jedes Schafs.
     */
    public Schaf() {
        gewicht = 64;
        alter = 1;
        eizellen = 5;
        setImage("lamm1.jpg");
    }

    /**
     * Lässt ein Schaf ein Gras auffressen.
     */
    public void fressen() {
        Gras diesGras = (Gras) getOneObjectAtOffset(0,0, Gras.class);
        if (getOneObjectAtOffset(0,0, Gras.class)!=null && diesGras.zustand == 10){
            diesGras.zustand = 0;
            diesGras.setImage("gras0.png");
            gewicht = gewicht+1;
        }
    }

    /**
     * Lässt sich ein Schaf zufällig übers Feld bewegen
     */
    public void laufen() {
        setLocation(Greenfoot.getRandomNumber(25),Greenfoot.getRandomNumber(25));
        alter = alter+1;
        stepCount = stepCount+1;
        if(stepCount == 10) {
            gewicht = gewicht-1;
            stepCount = 0;
        }
    }
}
Super_Hippo Super_Hippo

2021/2/14

#
Whenever a sheep mates, its weight is reduced to 10, so it will die immediately. That’s why the number of sheeps stays at 20.
RcCookie RcCookie

2021/2/14

#
An easier way to measure the current amount of sheep would be:
showText("Anzahl an Schafen: " + getObjects(Sheep.class).size());
RcCookie RcCookie

2021/2/14

#
Super_Hippo wrote...
Whenever a sheep mates, its weight is reduced to 10, so it will die immediately. That’s why the number of sheeps stays at 20.
And that should also be reason why the death counter rises faster than you think it should.
Nachtari Nachtari

2021/2/14

#
Super_Hippo wrote...
Whenever a sheep mates, its weight is reduced to 10, so it will die immediately. That’s why the number of sheeps stays at 20.
wow thanks a lot I didn't notice that, that actually makes sense. What a damn stupid mistake.
Nachtari Nachtari

2021/2/14

#
RcCookie wrote...
An easier way to measure the current amount of sheep would be:
showText("Anzahl an Schafen: " + getObjects(Sheep.class).size());
I had a similiar code but it was in my sheep actor so I needed the getWorld(). before using it and that just broke everything. So I was frustrated and just did it another way, but still thank you for the hint.
You need to login to post a reply.