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

2013/5/15

Randomly generated dungeon

1
2
3
4
5
henrYoda henrYoda

2013/5/16

#
@Entity1037 Ok, thank you
danpost danpost

2013/5/16

#
Entity1037 wrote...
If you tell it to get a random number using 360 to 0, then all of the numbers will be multiples of 45.
What makes you think that!
Entity1037 Entity1037

2013/5/17

#
The XExplosion class in my Megaman EX scenario gets a random number between 0 and 360 to determine which direction to travel. However, its only turning so it moves in the 8 cardinal directions. I'm serious, look at the bubbles that appear when you die in my scenario: http://www.greenfoot.org/scenarios/8355
danpost danpost

2013/5/17

#
Chances are your movement is being limited to 2 pixels at a time, meaning that the only pixels that your actor can stop at are at multiples of 45 degrees from where it starts. If you make your actor move at a greater speed, you will see that more angles will be possible without changing the random number part of the code. My Radial Graphs scenario demonstrates the angle of movement at different speeds using different methods of movement.
davmac davmac

2013/5/17

#
Entity1037, see this discussion.
henrYoda henrYoda

2013/5/17

#
I am getting an error when compiling the classes. Why? My mainworld class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class mainWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class mainWorld extends World
{  
    private String[][] world;  

    public mainWorld(String[][] array)  
    {  
        world = array;  
    }  

    public void setItemsFromArray(String[] world)  
    {  
        for (int i = 0; i < world.length; i++)  
            for (int j = 0; j < world[i].length; j++)  
            {  
                if (world[i][j].equals("x"))  
                    addObject(new Player(), i * 5, j * 5);  
                if (world[i][j].equals("y"))  
                    addObject(new Enemy(), i * 4, j * 4);  
        }  
    }  
}  
My game subclass:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class world here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class game extends mainWorld
{
    int[][] myTwoDArray = {  
                                          {1, 2, 3, 4, 5},  
                                          {6, 7, 8, 9, 10},  
                                          {11, 12, 13, 14, 15}  
                                        };  
      
    //gets height of a 2D array (it equals 3)  
    int height = myTwoDArray.length;  
      
    //gets width of a 2D array (it equals 5)  
    int width = myTwoDArray[0].length;  
    /**
     * Constructor for objects of class world.
     * 
     */
    public game()
    {    
       
        super(myTwoDArray);  
    }  
    
    
   
}
danpost danpost

2013/5/17

#
There are several things wrong with these codes. (1) line 18 of the mainWorld class has only a one-dimensional array coming in as the argument (it should be a two-dimensional array) (2) lines 11 throught 15 of the game class has an array declared, but an ArrayList object being set to it (change all curly brackets to square brackets) (3) you have int values in your two-d array, but are checking for Strings in the mainWorld to populate the world (lines 23 through 26).
davmac davmac

2013/5/17

#
(2) lines 11 throught 15 of the game class has an array declared, but an ArrayList object being set to it (change all curly brackets to square brackets)
This isn't correct. The list of numbers enclosed in curly brackets is a valid array literal. Changing the curly brackets to square brackets would cause an error here.
danpost danpost

2013/5/17

#
Sorry about that. Thanks, davmac -- I guess I do not know what language I am "speaking".
henrYoda henrYoda

2013/5/17

#
Can you show me the code I need to get it working please?
danpost danpost

2013/5/17

#
danpost wrote...
There are several things wrong with these codes. (1) line 18 of the mainWorld class has only a one-dimensional array coming in as the argument (it should be a two-dimensional array) (2) lines 11 throught 15 of the game class has an array declared, but an ArrayList object being set to it (change all curly brackets to square brackets) (3) you have int values in your two-d array, but are checking for Strings in the mainWorld to populate the world (lines 23 through 26).
You will need to make the changes listed above (those without a strike through them). Then you will need to assign a numeric value to each object you want listed in your 2-d array and place the appropriate values in the array to represent the layout of the world as you want it to start. Finally, in your mainWorld class, you need to fix the 'setItemsFromArray' method; first to accept a 2-d array of 'int' values; then to add the proper actors at the locations represented in the array (a 'switch' statement might be best in this method).
public void setItemsFromArray(int[][] world)  
{
    int gap = 30; // see note below
    for (int i = 0; i < world.length; i++) for (int j = 0; j < world[i].length; j++)  
    {
        Actor actor = null; // to hold any actor created
        switch (world[I][j]) {
            case 0: break; // no actor here
            case 1: actor = new Player(); break;
            case 2: actor = new Enemy(); break;
            case 3: actor = new Wall(); break;
            case 4: actor = new  Food(); break;
        }
        if (actor != null) addObject(actor, i*gap, j*gap);
    }
}
In the sample method above, 'gap' is some constant value that represents the distance between each item in the layout array (in world cells).
henrYoda henrYoda

2013/5/19

#
Its still not working :( Could you please look at my code and fix it, so that it works? The error that I get is that "Cannot find symbol- constructor World()" Thanks :) MainWorldClass:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class mainWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MainWorldClass extends World/*Or whatever you want to call it*/  
{  
    private int[][] world;  

    public MainWorldClass(int[][] array)  
    {  
        world = array;  
        super(800, 400);
    }  

    public void setItemsFromArray(int[][] world)    
    {  
        int gap = 30; // see note below  
        for (int i = 0; i < world.length; i++) for (int j = 0; j < world[i].length; j++)    
            {  
                Actor actor = null; // to hold any actor created  
                switch (world[I][j]) {  
                    case 0: break; // no actor here  
                    case 1: actor = new Player(); break;  
                    case 2: actor = new Enemy(); break;  
                    case 3: actor = new Wall(); break;  
                    case 4: actor = new  Item(); break;  
                }  
                if (actor != null) addObject(actor, i*gap, j*gap);  
        }  
    }  
}  
my game class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class game here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class game extends MainWorldClass
{

    /**
     * Constructor for objects of class game.
     * 
     */
    public game()
    {    
        int[][] myTwoDArray = {  
                {0, 0, 0, 0, 0},  
                {2, 0, 1, 0, 2},  
                {0, 0, 0, 0, 0}  
            };  

        //gets height of a 2D array (it equals 3)  
        int height = myTwoDArray.length;  

        //gets width of a 2D array (it equals 5)  
        int width = myTwoDArray[0].length;  
        
        super(myTwoDArray);
    }
}
thssfrnssssmnt thssfrnssssmnt

2013/5/19

#
world = array; goes after super(800, 400);
thssfrnssssmnt thssfrnssssmnt

2013/5/19

#
can someone go to http://www.greenfoot.org/scenarios/8396 and set a new score
thssfrnssssmnt thssfrnssssmnt

2013/5/19

#
it should be public MainWorldClass(int array) { super(800, 400); world = array; }
There are more replies on the next page.
1
2
3
4
5