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

2012/12/2

2D world generation

Game/maniac Game/maniac

2012/12/2

#
How do you create an algorithm which will randomly generate hills, caves, trees and stuff like that.
Game/maniac Game/maniac

2012/12/2

#
I want it to look blocky like terraria
SPower SPower

2012/12/2

#
It completely depends on what you want. How do you want the objects to be spread? Is there a limit? Things like that is the beginning of an algorithm, it's not like waiting a little bit and the perfect algorithm just falls from the sky! :)
Game/maniac Game/maniac

2012/12/2

#
i want it to look like the generation for terraria
Game/maniac Game/maniac

2012/12/2

#
I have a basic idea of the sort of code I want:
int x=0;
int y=Greenfoot.getRandomNumber(10)*16;

public GameWorld()
{
    super(720, 560, 1);
}

public void act()
{
    generate();
}

public void generate()
{
    if(x<getWidth())
    {
        for(int i;i<getHeight;i+=16)
        {
            addObject(new Block(), x, i+y);
        }
        x+=16;
        y=Greenfoot.getRandomNumber(10)*16;
    }
}
Does that help?
Game/maniac Game/maniac

2012/12/2

#
I made som mistakes in the generate method heres a fix:
public void generate()  
{  
    if(x<getWidth())  
    {  
        for(int i=0;i<getHeight();i+=16)  
        {  
            addObject(new Block(), x, i+y);  
        }  
        x+=16;  
        y=Greenfoot.getRandomNumber(10)*16;  
    }  
}
Game/maniac Game/maniac

2012/12/2

#
I have improved the generation of the blocks so it is smoother but I still need help generating Caves heres what I have at the monument:
int x = 0;  
    int y = Greenfoot.getRandomNumber(20)*16+48;  
  
    public GameWorld()  
    {  
        super(720, 560, 1);  
    }
  
    public void act()  
    {  
        generate();  
    }  
  
    public void generate()  
    {  
        if(x<=getWidth()){  
            for(int i=0;i<getHeight();i+=16)  
            {  
                addObject(new Block(), x, i+y);  
            }  
            x+=16;  
            int upDown = Greenfoot.getRandomNumber(4);
            if(upDown == 0 && y > 48){
                y -= Greenfoot.getRandomNumber(2)*16;
            }else if(upDown == 1 && y < getHeight()){
                y += Greenfoot.getRandomNumber(2)*16;
            }else if(upDown == 2 && y > 48){
                y -= Greenfoot.getRandomNumber(3)*16;
            }else if(upDown == 3 && y < getHeight()){
                y += Greenfoot.getRandomNumber(3)*16;
            }
        }
    }
Game/maniac Game/maniac

2012/12/2

#
I still need help with generating caves in my world
You need to login to post a reply.