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

2013/6/10

Conditions for new level

1
2
3
how do you set conditions for a new level The DoctorProfessor is out!
Zamoht Zamoht

2013/6/10

#
Can you be more specific? What is it exactly you want to do?
Well, I'm making a game in which you play as a teddy bear who has to gather honey (i was stuck for inspiration and smene had a winnie the pooh shirt on!) and i need to change levels when yu collect all the honey. how do i do this? The DoctorProfessor is out!
Zamoht Zamoht

2013/6/10

#
Okay so here is one way to go around it. Code for a world that constantly checks for the amount of collectable honey left. When there is no honey left it sets up a new level.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class HoneyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HoneyWorld extends World
{
    int level;

    /**
     * Constructor for objects of class HoneyWorld.
     * 
     */
    public HoneyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        
        setupLevel(1);
    }
    
    public void setupLevel(int lvl)
    {
        removeObjects(getObjects(null));
        
        level = lvl;
        
        if (level == 1)
        {
            //Add objects for the first level.
        }
        else if (level == 2)
        {
            //Add objects for level 2.
        }
    }
    
    public void act()
    {
        //Change Honey.class to whatever name you use.
        List honey = getObjects(Honey.class);
        if (honey.isEmpty())
        {
            setupLevel(level+1);
        }
    }
}
great! only now the world doesn't appear at all... It's just a blank screen. It compiled all right but there is nothing. can anyone help with this? The DoctorProfessor is out!
Zamoht Zamoht

2013/6/11

#
Did you add any code to where I commented "Add objects for the first level." ? Because if there is no Honey class in the world, it will change to the next level.
tollpatch tollpatch

2013/6/11

#
Hi, DoctorProfessorYoshi, I just wrote a similar game program. It's a modification of Wombat, who has to push a loaf into a house to complete a level. In this moment I write only an abstract, if its not clear enough, ask more. Each level is a World, for example WombatWorld, IceWorld, WaterWorld, NightWorld...... The Actor classes are Wombat, House, Rock, Barrel and Loaf. Wombat is moved by arrow keys, Rocks and the House are fixed, the Loaf and the Barrels can be pushed by Wombat. These the only classes and are used in all levels. Within the Wombat class there is:
private boolean atGoal = false;

public setAtGoal ( boolean worldsend)
 { atGoal = worldsend;}
public getAtGoal ()
 { return atGoal;}
Within the constructor of the XxxxWorld is:
 wombat.setAtGoal (false); 
// otherwise ALL worlds will be run thru Further on in the code, when the loaf reaches the house:
 wombat.setAtGoal(true); 
Within the act methode of the World :
if (wombat.atGoal)
         {
           Greenfoot.setWorld(new IceWorld()); //Only one world has control in a time
         }
The only thing I change from level to level is the image of the characters in the different worlds and within the creator how many rocks and barrels are there to create the difficulty of the level. I hope this helps greetings tollpatch
Zamoht Zamoht

2013/6/11

#
@tollpatch your code will work for sure, but if you have like 100 levels, you have to create 100 world classes which is a lot of work (and it will fill a lot in your Greenfoot project and make it hard to read). If you on the other hand wants to create a game with less than 10 levels this is a good way to work around the problem.
Still nothing. I even added a new freakin' world but nothing, just a gray screen where the world should be. Everything compiles fine, there is honey in the world, but nothing happens, just the endless gray... The Dctorrofessor is out :-(
Zamoht Zamoht

2013/6/12

#
Could you please send some code or upload the scenario because I tested my code and it works fine.
this is the code for the world
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Background here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{
int level;

    /**
     * Constructor for objects of class Background.
     * 
     */
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(850, 850, 1); 

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        
        setuLevel(1);
    }
    
    public void setuLevel(int lv1)
    {
        removeObjects(getObjects(null));
        
        level = lv1;
        
        if (level == 1)
        {
            //Add objects for the first leve
            honey honey = new honey();
        addObject(honey, 311, 338);
        honey honey2 = new honey();
        addObject(honey2, 649, 60);
        honey honey3 = new honey();
        addObject(honey3, 649, 60);
        honey honey4 = new honey();
        addObject(honey4, 531, 549);
        honey honey5 = new honey();
        addObject(honey5, 91, 176);
        honey honey6 = new honey();
        addObject(honey6, 138, 429);
        honey honey7 = new honey();
        addObject(honey7, 328, 97);
        honey honey8 = new honey();
        addObject(honey8, 785, 475);
        honey honey9 = new honey();
        addObject(honey9, 164, 780);
        honey honey10 = new honey();
        addObject(honey10, 607, 308);
        honey honey11 = new honey();
        addObject(honey11, 414, 240);
        honey honey12 = new honey();
        addObject(honey12, 210, 164);
        honey honey13 = new honey();
        addObject(honey13, 234, 504);
        honey honey14 = new honey();
        addObject(honey14, 403, 389);
        honey honey15 = new honey();
        addObject(honey15, 578, 148);
        honey honey16 = new honey();
        addObject(honey16, 460, 74);
        honey honey17 = new honey();
        addObject(honey17, 741, 266);
        honey honey18 = new honey();
        addObject(honey18, 598, 699);
        honey honey19 = new honey();
        addObject(honey19, 400, 623);
        honey honey20 = new honey();
        addObject(honey20, 542, 416);
        honey honey21 = new honey();
        addObject(honey21, 37, 489);
        honey honey22 = new honey();
        addObject(honey22, 82, 236);
        honey honey23 = new honey();
        addObject(honey23, 464, 774);
        honey honey24 = new honey();
        addObject(honey24, 766, 671);
        honey honey25 = new honey();
        addObject(honey25, 688, 522);
        honey honey26 = new honey();
        addObject(honey26, 384, 478);
        honey honey27 = new honey();
        addObject(honey27, 193, 627);
        honey honey28 = new honey();
        addObject(honey28, 190, 295);
        honey honey29 = new honey();
        addObject(honey29, 160, 42);
        honey honey30 = new honey();
        addObject(honey30, 762, 134);
        honey honey31 = new honey();
        addObject(honey31, 484, 281);
        honey honey32 = new honey();
        addObject(honey32, 335, 198);
        honey honey33 = new honey();
        addObject(honey33, 51, 61);
        honey honey34 = new honey();
        addObject(honey34, 57, 681);
        honey honey35 = new honey();
        addObject(honey35, 322, 737);
        Hero hero = new Hero();
        addObject(hero, 67, 345);
        Blob blob = new Blob();
        addObject(blob, 690, 358);
        honey33.setLocation(42, 82);
        honey29.setLocation(204, 43);
        Blob blob2 = new Blob();
        addObject(blob2, 641, 788);
        Blob blob3 = new Blob();
        addObject(blob3, 474, 155);
        setuLevel(1);
    }
    else if (level == 2)
    {
        //Add objects for the second level
      honey honey = new honey();
        addObject(honey, 311, 338);
        honey honey2 = new honey();
        addObject(honey2, 649, 60);
        honey honey3 = new honey();
        addObject(honey3, 649, 60);
        honey honey4 = new honey();
        addObject(honey4, 531, 549);
        honey honey5 = new honey();
        addObject(honey5, 91, 176);
        honey honey6 = new honey();
        addObject(honey6, 138, 429);
        honey honey7 = new honey();
        addObject(honey7, 328, 97);
        honey honey8 = new honey();
        addObject(honey8, 785, 475);
        honey honey9 = new honey();
        addObject(honey9, 164, 780);
        honey honey10 = new honey();
        addObject(honey10, 607, 308);
        honey honey11 = new honey();
        addObject(honey11, 414, 240);
        honey honey12 = new honey();
        addObject(honey12, 210, 164);
        honey honey13 = new honey();
        addObject(honey13, 234, 504);
        honey honey14 = new honey();
        addObject(honey14, 403, 389);
        honey honey15 = new honey();
        addObject(honey15, 578, 148);
        honey honey16 = new honey();
        addObject(honey16, 460, 74);
        honey honey17 = new honey();
        addObject(honey17, 741, 266);
        honey honey18 = new honey();
        addObject(honey18, 598, 699);
        honey honey19 = new honey();
        addObject(honey19, 400, 623);
        honey honey20 = new honey();
        addObject(honey20, 542, 416);
        honey honey21 = new honey();
        addObject(honey21, 37, 489);
        honey honey22 = new honey();
        addObject(honey22, 82, 236);
        honey honey23 = new honey();
        addObject(honey23, 464, 774);
        honey honey24 = new honey();
        addObject(honey24, 766, 671);
        honey honey25 = new honey();
        addObject(honey25, 688, 522);
        honey honey26 = new honey();
        addObject(honey26, 384, 478);
        honey honey27 = new honey();
        addObject(honey27, 193, 627);
        honey honey28 = new honey();
        addObject(honey28, 190, 295);
        honey honey29 = new honey();
        addObject(honey29, 160, 42);
        honey honey30 = new honey();
        addObject(honey30, 762, 134);
        honey honey31 = new honey();
        addObject(honey31, 484, 281);
        honey honey32 = new honey();
        addObject(honey32, 335, 198);
        honey honey33 = new honey();
        addObject(honey33, 51, 61);
        honey honey34 = new honey();
        addObject(honey34, 57, 681);
        honey honey35 = new honey();
        addObject(honey35, 322, 737);
        Hero hero = new Hero();
        addObject(hero, 67, 345);
         honey33.setLocation(42, 82);
        honey29.setLocation(204, 43);
    }
}
public void act()
{
List honey = getObjects(honey.class);
if (honey.isEmpty())
{
    setuLevel(level=1);
}
}
}
The DoctorProfessor is out!
Zamoht Zamoht

2013/6/12

#
Okay two things I don't get. First of all you put setuLevel(1); in the bottom of your level 1 code, which means that it will just run in an endless loop. The other thing is that in your act method you should change setuLevel(level=1); to setuLevel(level+1); This should fix it.
Thanks! The DoctorProfessor is out!
Zamoht Zamoht

2013/6/12

#
So it works now?
Dang it! still nothing, Just gray, gray, gray... The DoctorProfessor is out...
There are more replies on the next page.
1
2
3