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

2012/6/17

Easier way to add objects.

Zamoht Zamoht

2012/6/17

#
I've created the game Wombat Puzzles, and after 7 levels my world code has become a little too long. Right now i add object to the level like this:
if (level == 1)
        {
            addObject(new Player(), 5, 5);
            addObject(new Orange(), 0, 3);
            addObject(new Orange(), 1, 1);
            addObject(new Orange(), 1, 2);
            addObject(new Orange(), 1, 3);
            addObject(new Orange(), 2, 3);
            addObject(new Orange(), 3, 1);
            addObject(new Orange(), 3, 3);
            addObject(new Orange(), 4, 4);
            addObject(new Rock(), 0, 0);
            addObject(new Rock(), 1, 4);
            addObject(new Rock(), 4, 0);
            addObject(new Rock(), 4, 2);
            addObject(new Rock(), 4, 3);
            addObject(new Rock(), 4, 5);
            addObject(new Rock(), 5, 2);
            addObject(new Grass(), 5, 0);
        }
As you can see the world is 6x6 fields, and I want to know if it's possible to create levels by "drawing" them. So you tell the program that: O = Orange P = Player R = Rock N = nothing Then level 1: O, O , N, N, N, P R, N, R, O, O, O and so on. The level above doesn't make sence, but I hope you get what i mean. So i want to know is this possible? Is this worth it? Is it hard to do? If this is possible, worth it and not that hard, I would like to know how. I have no clue at all, so please help me getting started. Thanks for your time.
danpost danpost

2012/6/18

#
You could probably create an array of characters as you show above, but if you have more than one object starting at the same location (possibly the player and a turtle), then that method would probably need discarded. Easy enough, would be to condense your 'addObject' statements to 3 charaters: the first character in the object type the x coordinate and the y coordinate; and concatenate them into one String per level. Showing only the first level here, it would look like this:
// your world variables
String[] layout = { "p55o03o11o12o13o23o31o33o44r00r14r40r42r43r45r52g50" };
int level = 0;

// adding the objects into the world dependent on the level
String setup = layout[level];
while (setup.length > 0)
{
    int objNum = "porgt".indexOf(setup.charAt(0));
    int x = "012345".indexOf(setup.charAt(1));
    int y = "012345".indexOf(setup.charAt(2));
    setup = setup.substring(3);
    switch (objNum)
    {
        case 0: addObject(new Player(), x, y); break;
        case 1: addObject(new Orange(), x, y); break;
        case 2: addObject(new Rock(), x, y); break;
        case 3: addObject(new Grass(), x, y); break;
        case 4: addObject(new Turtle(), x, y); break;
    }
}
Zamoht Zamoht

2012/6/18

#
Thank you!
You need to login to post a reply.