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

2012/6/14

Boulder Dash exam project

fisch3r fisch3r

2012/6/14

#
Hi guys! We're currently trying to create the old school Boulder Dash game in Greenfoot for our final exams. We've just run into a weird problem.. We're trying to create a world using x's and 0's in a grid, like a lot of other people in here have. It has always worked in other games, but not in this one. Here's the code:
public BoulderWorld()
    {    
        // Create a new world with 20x20 cells with a cell size of 30x30 pixels.
        super(20, 20, 30); 
        populate();
    }
    
    public void populate() {
        String worldRow;
        
        String[] world={
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx",
        "xxxxxxxxxxxxxxxxxxxx"
        };
        
        for (int y = 0; y < 20; y++) 
        {
            worldRow = world[y]; // Rækken som der indsættes i
            
            for (int x = 0; x < 20; x++) 
            {
                if(worldRow.charAt(x) == 'x') // x = indsætter sten
                {
                    Sand s = new Sand();
                    addObject(s, x*20, y*20);
                }
                
                if(worldRow.charAt(x) == '0') // x = indsætter sten
                {
                    Dude d = new Dude();
                    addObject(d, x*20, y*20);
                }
            }
        }
    }
and here's the result: Can anybody tell us what's wrong?
fisch3r fisch3r

2012/6/14

#
We just tried filling the level with sand, just to test it. We'll put in more characters as we get further along with the project.
davmac davmac

2012/6/14

#
You have a cell size of 30 (specified in line 4) so all the coordinates will be multiplied by 30 automatically. Perhaps you mean to use a cell size of 20? Even in that case you don't need to multiply x and y by 20 in lines 43 and 49.
fisch3r fisch3r

2012/6/14

#
Ahh, I get it now. I reused some code from an earlier project. Thanks a lot!
You need to login to post a reply.