Sunday, February 2, 2014

Adjusting the World in Platformer

The top of the MyWorld code looks like this:


import greenfoot.*;

public class MyWorld extends World
{
    /** MAP LEGEND **/
    // b = block        m = monster
    // p = platform     k = key
    // w = wall         c = character (player)
    // d = door         
    
    String[] map = { "                         ",
                     "d m           c    m     ",
                     "ppp                      ",
                     "                         ",
                     "                         ",
                     "     pp                  ",
                     "                     k   ",
                     "             ppppppppp   ",
                     "                         ",
                     "        pp               ",
                     "               m         ",
                     "    m                    ",
                     "             pppp     m  ",
                     "  ppppp                  ",
                     "                     ppp ",
                     "                         ",
                     "    m                m   ",
                     "         w        w      ",
                     "bbbbbbbbbbbbbbbbbbbbbbbbb" };
                     
    public MyWorld()
    {    
        super(800, 600, 1, false); // Set world size
        prepare();
    }
private void prepare()
    {
The next piece looks like this (I can't seem to paste it as code:

        for (int i=0; i<map.length; i++)
        for (int j=0; j<map[i].length(); j++)

And then this piece:

        {
            int kind = "cbpwmdk".indexOf(""+map[i].charAt(j)); // All "kinds" in order
            if (kind < 0) continue;
            Actor actor = null;
            if (kind == 0) actor = new Player();
            if (kind == 1) actor = new Block();
            if (kind == 2) actor = new Platform2();
            if (kind == 3) actor = new Wall();
            if (kind == 4) actor = new Monster();
            if (kind == 5) actor = new Door();
            if (kind == 6) actor = new Key();
            addObject(actor, 16+j*32, 16+i*32); // Start at pixel 16 and add objects at 32 pixel increments
        }
    }
}

No comments:

Post a Comment