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

2015/4/1

Help pls ;)

moksha92 moksha92

2015/4/1

#
Hey People, i am doing a Game with a Rabbit trying to eat Mushrooms. He is chased by a Dog trying to eat him. I want to do 2 Levels, the first should end if the Rabbit has eaten 10 (or so) Mushrooms or is eaten by the Dog. I have got the Code for the end if he is eaten by the Dog but I cant get it to work if the Rabbit has eaten 10 Mushrooms. + i would like to let the Mushrooms disappear after 10 or 20 seconds, to make it harder. + i would like to let the Background change color for like 2-3 seconds if the Rabbit eats a Mushroom. Thanks already for the help! ;) And here the codes: Rabitt:
public class hase extends Actor
{
    /**
     * Act - do whatever the hase wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        {if(Greenfoot.isKeyDown("up"))
            {move(3);}}
         {if(Greenfoot.isKeyDown("down"))
             {move(-3);}}
         {if(Greenfoot.isKeyDown("left"))
             {turn(-3);}}
         {if(Greenfoot.isKeyDown("right"))
             {turn(3);}}
          eat();
    }    
    public void eat()
    {Actor pilz;
        pilz = getOneObjectAtOffset(0,0,pilz.class);
        if(pilz !=null){getWorld().removeObject(pilz);
        Greenfoot.playSound("burp.mp3");
        ((world) getWorld()).counteat();
        }
    }
}
World (Mushroom spawn inside it):
public class world extends World
{

    Counter counter = new Counter("Score: ");
    public world()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1); 
        setPaintOrder(ScoreBoard.class, hase.class, pilz.class, dog.class, Counter.class);
        prepare();
    }
    private int counterspawn;
    private int counterremove;

    private void prepare()
    {
        hase hase = new hase();
        addObject(hase, 275, 250);
        dog dog = new dog();
        addObject(dog, 603,365);
        dog.setLocation(823,515);
        hase.setLocation(252,222);
        addObject(counter, 100, 560);
    }
    public void act()
    {
        runcounterspawn();
    }
    public void counteat()
    {
        counter.add(10);    
    }
    private void runcounterspawn()
    {
        counterspawn = (counterspawn+1)%300;
        if (counterspawn == 0) spawnPilz();
    }
    private void spawnPilz()
    {
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject(new pilz(), x, y);
    }
    public void gameOver()
    {
        addObject(new ScoreBoard(counter.getValue()), getWidth()/2, getHeight()/2);
        Greenfoot.playSound("zonk.mp3");
        Greenfoot.stop();
    }
    
}
erdelf erdelf

2015/4/1

#
to your initial problem with the end... in the world class in the counteat method you just have to add a call to the gameOver method if the counter is above a certain count.
Super_Hippo Super_Hippo

2015/4/1

#
You should keep the class names in one language. Furthermore, they should start with a uppercase letter. (→ Hase, Pilz and Hund or Rabbit, Mushroom and Dog) In the Hase class, you have many unnecessary { and }. To your problems: - let the Mushroom disappear:
private int time = 0;

public void act()
{
    time++;
    if (time>550) //about 10 seconds at speed 50
    {
        getWorld().removeObject(this);
    }
}
- change background color and win the game after 10 mushrooms
private int mushroomsEaten = 0; //numbers of mushrooms which were already eaten

public void eat()
{
    Actor pilz = getOneObjectAtOffset(0,0,Pilz.class);
    if (pilz !=null)
    {
        mushroomEaten++;
        if (mushroomEaten==10)
        {
            //advance to next level
        }
        getWorld().removeObject(pilz);
        Greenfoot.playSound("burp.mp3");
        ((Welt) getWorld()).counteat(); //changed 'world' to 'Welt' because it should start with a uppercase letter aswell
        ((Welt) getWorld()).changeColor();
        }
    }
}

//Welt class
private int colorTimer = -1;

public void act()
{
    if (colorTimer>0)
    {
        colorTimer--;
        if (colorTimer==0)
        {
            setBackground(/**normal background*/);
            colorTimer--;
        }
    }
}

public void changeColor()
{
    colorTimer = 150;
    setBackground(/*other color*/);
}
moksha92 moksha92

2015/4/1

#
thanks for the fast replie..!! :)
moksha92 moksha92

2015/4/1

#
Okay now its all working thanks! ;) But one more question. I now have it working with the second level but now i want it to be like in the first level, if the Rabbit eats 10 mushrooms the Game ends and the scoreboard is displayed. Here my code for the change of level 1 to level 2... (do i have to make an int level or so?) so greenfoot knows which level it is and which method it should use?
  public void changeLevel()
    {
     Greenfoot.setWorld(new world2());
    }
    public void nextlevel()
    {
        addObject(new ScoreBoard(counter.getValue()), getWidth()/2, getHeight()/2);
        Greenfoot.playSound("clap.mp3");
        changeLevel();
        
    }
and the eat method of the rabitt
 public void eat()
    {Actor pilz;
        pilz = getOneObjectAtOffset(0,0,pilz.class);
        if(pilz !=null)
        {
            pilzEaten++;
            if (pilzEaten==2)
            {((world) getWorld()).nextlevel();
            }
            getWorld().removeObject(pilz);
            Greenfoot.playSound("burp.mp3");
            ((world) getWorld()).counteat();
            ((world) getWorld()).changeColor();
        }
    }
moksha92 moksha92

2015/4/1

#
i was thinking about adding a second if to the eat method. like if level 1 and pilzEaten==2 do nextLevel but if level2 and pilzEaten ==2 do win or so how do I add the level thing to the if method?
Super_Hippo Super_Hippo

2015/4/1

#
You could use
if (pilzEaten==2)
{
    if (getWorld() instanceof world)
    {
        //next level
    }
    else //if (getWorld() instanceof world2)
    {
        //end game
    }
}
moksha92 moksha92

2015/4/1

#
like this?
 public void eat()
    {Actor pilz;
        pilz = getOneObjectAtOffset(0,0,pilz.class);
        if(pilz !=null)
        {
            getWorld().removeObject(pilz);
            Greenfoot.playSound("burp.mp3");
            ((world) getWorld()).counteat();
            ((world) getWorld()).changeColor();
            pilzEaten++;
            if (pilzEaten==2)
            {
                if (getWorld() instanceof world)
                {((world) getWorld()).nextlevel();
                }
                else if(getWorld() instanceof world2)
                {
                    ((world) getWorld()).win();
                }
            }
       }
   }
here the win code
public void win()
    {
        addObject(new ScoreBoard(counter.getValue()), getWidth()/2, getHeight()/2);
        Greenfoot.playSound("win.mp3");
        Greenfoot.stop();
    }
i tried it out, and if i am in world2 then it only starts with world2 again? thanks allready
danpost danpost

2015/4/1

#
Line 18 above should be:
((world2) getWorld()).win();
and the 'win' method should be in your 'world2' class.
moksha92 moksha92

2015/4/1

#
it just doesnt do the win method it only does the nextlevel method... :(
danpost danpost

2015/4/1

#
moksha92 wrote...
it just doesnt do the win method it only does the nextlevel method... :(
Question 1: what class does your 'world2' class extend? I ask this because if 'world2' extends 'world' (and not 'World'), then any world created from the 'world2' class will also be a 'world' instance. Provided you never add any more levels, you can change the first if clause to:
if (getWorld() instanceof world && !getWorld() instanceof world2)
or you can just switch the order of the 'if' clauses (check for 'world2' before 'world'):
if(getWorld() instanceof world2)
{
    ((world2) getWorld()).win();
}
else if (getWorld() instanceof world)
{
    ((world) getWorld()).nextlevel();
}
Truthfully, however, you should probably have both 'world' and 'world2' subclass the same subclass of World. For example: * SubWorld extends World * world extends SubWorld * world2 extends SubWorld Then, you can place all common methods and fields in the SubWorld class and not have to try and determine which world the player is in (it will always be in a 'SubWorld' world). There is something else you could do with the SubWorld class. You can add an empty method to it like 'public void nextlevel() {}' and rename your 'win' method 'nextlevel' in the 'world2' class. It will then execute whichever method needs to run depending on the world with '((SubWorld)getWorld()).nextlevel();'.
You need to login to post a reply.