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

2023/6/30

these should now become faster and more

helpmeplease helpmeplease

2023/6/30

#
I have to do a project for the school and a bear eats objects that fall from the sky, these should now become faster and more in the course of time and unfortunately I don't know how to start. It would be very nice if someone could help me:)
danpost danpost

2023/6/30

#
helpmeplease wrote...
I have to do a project for the school and a bear eats objects that fall from the sky, these should now become faster and more in the course of time and unfortunately I don't know how to start. It would be very nice if someone could help me:)
Add a field to your world class to track the passage of time by counting act cycles. Use its value to set the speed of the falling objects as well as to control the rate at which they spawn.
helpmeplease helpmeplease

2023/6/30

#
können sie dies in einem programmtext erläutern
rdFx rdFx

2023/7/1

#
public class MyWorld 
{
  int zaehler = 0; // oder in Konstruktor setzen!
  
  public void act() 
  {
    // wird immer wieder aufgerufen (Schleife)
    zaehler += 1;
    // hier kann der zaehler genutzt werden..
    // nur angedeutet..
    if( (zaehler % 5) == 0)
    {
      // wird ausgeführt falls 5 ein Teiler von 
      // zaehler ist, also wenn zaehler == 5,
      // zaehler == 10, zaehler == 15, usw. gilt.
      
      // man kann dann z. B. ein neues Objekt 
      // in der Welt erstellen
      // addObject(new IssMich(), 50, 0);
    }
  }
}
Der Code deutet nur die Idee an, die hier von danpost beschrieben wurde. Die Klasse für die essbaren Objekte muss du natürlich selbst erstellen (im Beispiel "IssMich"). Auch die Dynamik wird hier nicht abgebildet, d. h. das Interval in welchem die Objekte fallen würden bliebe gleich (bei jedem fünften Schritt). Da die essbaren Objekte wahrscheinlich nicht immer an der gleichen Stelle herunterfallen sollen, müsste die x-Koordinate zufällig gesetzt werden (im Beispiel 50).
helpmeplease helpmeplease

2023/7/1

#
i dont undersand how this would help me to get my objects faster and more trough time
helpmeplease helpmeplease

2023/7/1

#
I dont understand how this could help me to get my objects that fly from the top to go faster and more as time goes by
danpost danpost

2023/7/1

#
helpmeplease wrote...
I dont understand how this could help me to get my objects that fly from the top to go faster and more as time goes by
With a field like:
private int acts;
in your world class to count act steps (frames), you could use in your world class:
public void act() {
    acts++;
    int spawnChance = 144000/acts;
    if (Greenfoot.getRandomNumber(spawnChance) == 0) {
        Food food = new Food();
        int speed = 1 + acts/900;
        if (speed > 8) speed = 8; // limiting speed to no more than 8
        food.speed = speed;
        int x = Greenfoot.getRandomNumber(getWidth());
        addObjectfood, x, 0);
    }
    // other actions for world
}
This is not tested. The values 144000 and 900 can be tweaked to suit your needs. Also, you could initialize the value of acts or add/subtract some from it in the setting of speed and spawnChance (ensuring that the values remain positive at all times). You could also put int fields for the score and health of the player in the world class (instead of in the class of the player) and add a method to remove food from the world. For example:
private int health = 100;
private int score;

public void removeFood(Food food, boolean ate) {
    removeObject(food);
    if (ate) {
        score += 10;
        showText("Score:  "+score, 100, 20);
    }
    else {
        health -= 10;
        showText("Health:  "+health, getWidth()-100, 20);
        if (health == 0) {
            Greenfoot.stop();
        }
    }
}
Just call the method when food is ate or reaches the bottom of the world, using something like the following in your food class:
((MyWorld)getWorld()).removeFood(this, true); // when ate
/**  ************  and  ************* */
((MyWorld).getWorld()).removeFood(this, false); // when reaching bottom edge of world
Replace "MyWorld" with "Pouwelt", if that is the name of your world.
You need to login to post a reply.