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

2017/5/23

Condense code

1
2
Trystar360 Trystar360

2017/5/23

#
I am making a maze type game and the way i have it now is i have one of these
public Position	p1=new Position(1,1, new Wall(0), this);
    
for every block. and right now i will have to have hundreds of these. so is there a way to condense it but change still have the location different? here are the first 10
public Position	p1=new Position(1,1, new Wall(0), this);
    public Position	p2=new Position(2,1, new Wall(0), this);
    public Position	p3=new Position(3,1, new Wall(0), this);
    public Position	p4=new Position(4,1, new Wall(0), this);
    public Position	p5=new Position(20,1, new Wall(0), this);
    public Position	p6=new Position(21,1, new Wall(0), this);
    public Position	p7=new Position(22,1, new Wall(0), this);
    public Position	p8=new Position(23,1, new Wall(0), this);
    public Position	p9=new Position(1,2, new Wall(0), this);
    public Position	p10=new Position(23,2, new Wall(0), this);
Super_Hippo Super_Hippo

2017/5/23

#
You could have an array of all these Position objects. However, I am not sure what those Position objects are for and if they are only there to add something (a wall in this case) to the world, then an easier way is to have an array where each different number represents a different class.
private int map[][] = //0=nothing, 1=Wall
{
    {1 1 1 1 1 1 1},
    {1 0 0 0 0 0 1},
    {1 0 0 0 0 0 1},
    {1 0 0 0 0 0 1},
    {1 0 0 0 0 0 1},
    {1 0 0 0 0 0 1},
    {1 1 1 1 1 1 1}
};


//when creating the world
for (int y=0; y=map.length; y++) for (int x=0; x=map[y].length; x++)
{
    Actor actor = null;
    switch (map[y][x])
    {
        case 1: actor = new Wall(); break;
        //case 2: actor = new Tree(); break;
        //....
    }
    addObject(actor, x, y);
}
Trystar360 Trystar360

2017/5/23

#
Super_Hippo wrote...
You could have an array of all these Position objects. However, I am not sure what those Position objects are for and if they are only there to add something (a wall in this case) to the world, then an easier way is to have an array where each different number represents a different class.
private int map[][] = //0=nothing, 1=Wall
{
    {1 1 1 1 1 1 1},
    {1 0 0 0 0 0 1},
    {1 0 0 0 0 0 1},
    {1 0 0 0 0 0 1},
    {1 0 0 0 0 0 1},
    {1 0 0 0 0 0 1},
    {1 1 1 1 1 1 1}
};


//when creating the world
for (int y=0; y=map.length; y++) for (int x=0; x=map[y].length; x++)
{
    Actor actor = null;
    switch (map[y][x])
    {
        case 1: actor = new Wall(); break;
        //case 2: actor = new Tree(); break;
        //....
    }
    addObject(actor, x, y);
}
its not quite just adding them. i have a class that uses that info and kind of keeps track of the overall position of the wall while they are not in the world. would you like me to show that code ?
Trystar360 Trystar360

2017/5/23

#
This is my position class
import greenfoot.*;
public class Position  
{
    // instance variables - replace the example below with your own
    private int x1, y1;
    private double midx1,midy1;
    private greenfoot.Actor actor1;
    private Location loc = new Location();
    private greenfoot.World wrld1;
    /**
     * Constructor for objects of class Position
     */
    public Position(int x, int y, greenfoot.Actor actor, greenfoot.World wrld)
    {
        x1 = x;
        y1 = y;
        actor1 = actor;
        wrld1 = wrld;
    }
    
    public void act()
    {
        loc.act();
        midx1 = loc.midx();
        midy1 = loc.midy();
       
    }

    
    public void s(int x)
    {
        
    }
    
    public double xfar()
    {
        return x1 - midx1;
    }
    
    public double yfar()
    {
        return y1 - midy1;
    }
    
    public int xpos()
    {   double xx = xfar();
        int x = 0;
        if(xx == 1)
        x = 10;
        if(xx == -1)
        x = 4;
        if(xx == -2.5)
        x = 0;
        if(xx == -2)
        x = 1;
        if(xx == -1.5)
        x = 2;
        if(xx == -.5)
        x = 5;
        if(xx == 0)
        x = 7;
        if(xx == .5)
        x = 8;
        if(xx == 1.5)
        x = 11;
        if(xx == 2)
        x = 13;
        if(xx == 2.5)
        x = 14;
        return x;
    }
        
        
    public int ypos()
    {
        double yy = yfar();
        int y = 0;
        
        if(yy == -1)
        y = 1;
        if(yy == 1)
        y = 7;
        if(yy == -1.5)
        y = 0;
        if(yy == -.5)
        y = 2;
        if(yy == 0)
        y = 4;
        if(yy == .5)
        y = 5;
        if(yy == 1.5)
        y = 8;
        
        return y;
    }

    public void position()
    {
        if(xfar() >= -2.5 &&  xfar() <= 2.5 && yfar() >= -1.5 && yfar() <= 1.5)
        {
            if(actor1.getWorld() == null)
            {
                Actor ref  = (Actor) new MyWorld().getObjects(Refrance.class).get(0);
                //MyWorld world = (MyWorld) new MyWorld();
                wrld1.addObject(actor1,xpos(),ypos());
            }
            if(actor1.getWorld() != null)
            {
                actor1.setLocation(xpos(), ypos());
            }
        }
        else 
        wrld1.removeObject(actor1);
    }
}
Super_Hippo Super_Hippo

2017/5/23

#
Okay, now I am a bit lost.
...keeps track of the overall position of the wall while they are not in the world
Why are you doing that? I don't really see a system in this class (especially regarding the Location class, the xpos and ypos methods. Well, and line 103 obviously.
danpost danpost

2017/5/23

#
I would like to see the wall class codes!
Trystar360 Trystar360

2017/5/23

#
the wall class is
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
/**
 * Write a description of class Wall here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Wall extends Actor
{
    public GreenfootImage img;
    public Wall(int vh)
    {
        if(vh == 0)
        img = new GreenfootImage(201, 201);
        if(vh == 1)
        img = new GreenfootImage(201, 201);
        img.drawRect(0,0,0,0);
        img.setColor(Color.BLACK);
        img.fill();
        setImage(img);
    }
    
    public void act() 
    {
        MyWorld world = (MyWorld)getWorld();
        world.wall = true;
    }    
}
and the maze is a 25 by 25 grid but you only see 4 by 3 of that. the world is 15 by 9 and each one is 67. so when i press the up arrow it doesn't actually move the main actor up it moves the over all coordinates. so then the position class ads, removes, and moves the walls as needed
Trystar360 Trystar360

2017/5/23

#
Super_Hippo wrote...
Okay, now I am a bit lost.
...keeps track of the overall position of the wall while they are not in the world
Why are you doing that? I don't really see a system in this class (especially regarding the Location class, the xpos and ypos methods. Well, and line 103 obviously.
the way this class works is it takes 2 ints(the x and the y) which is the over all location of that wall. then it gets the middle coordinates (where the main actor is) and figures out when to have the wall in the world, who not to and where to put it
Trystar360 Trystar360

2017/5/23

#
Heres the game so you can understand what I'm saying .
danpost danpost

2017/5/23

#
Trystar360 wrote...
Heres the game so you can understand what I'm saying .
I do not think I need to look at it. You should probably just make the world one without bounds (see the constructors in the World class API documentation). That way, you do not have to add and remove anything -- the actors, when re-positioned, just move in and out of the view of the scenario window pane. No trying to keep track of the coordinates of any actors, either.
Trystar360 Trystar360

2017/5/23

#
ok thanks
Trystar360 Trystar360

2017/5/24

#
danpost wrote...
Trystar360 wrote...
Heres the game so you can understand what I'm saying .
I do not think I need to look at it. You should probably just make the world one without bounds (see the constructors in the World class API documentation). That way, you do not have to add and remove anything -- the actors, when re-positioned, just move in and out of the view of the scenario window pane. No trying to keep track of the coordinates of any actors, either.
I made the world without bounds but I'm still exactly sure how i would make that work k
danpost danpost

2017/5/24

#
Trystar360 wrote...
I made the world without bounds but I'm still exactly sure how i would make that work
Remove the position class. Have the main character more normally. Then, have the world adjust the position of all objects in it the same distance and direction as needed to place the main character back in the middle. This is basic scrolling technique. You could add a scrolling support class to assist you with this.
Trystar360 Trystar360

2017/5/24

#
Super_Hippo wrote...
You could have an array of all these Position objects. However, I am not sure what those Position objects are for and if they are only there to add something (a wall in this case) to the world, then an easier way is to have an array where each different number represents a different class.
private int map[][] = //0=nothing, 1=Wall
{
    {1 1 1 1 1 1 1},
    {1 0 0 0 0 0 1},
    {1 0 0 0 0 0 1},
    {1 0 0 0 0 0 1},
    {1 0 0 0 0 0 1},
    {1 0 0 0 0 0 1},
    {1 1 1 1 1 1 1}
};


//when creating the world
for (int y=0; y=map.length; y++) for (int x=0; x=map[y].length; x++)
{
    Actor actor = null;
    switch (map[y][x])
    {
        case 1: actor = new Wall(); break;
        //case 2: actor = new Tree(); break;
        //....
    }
    addObject(actor, x, y);
}
could you explain this a little more ?
danpost danpost

2017/5/24

#
Basically, the map (lines 1 through 10) represents the initial layout of your world. A different value is used for each different type object (wall, player, enemy, etc), with a zero representing an empty grid square. I prefer to use a one-dimensional String array for one main reason -- the visual representation can be maintain to a higher degree as 9 is the highest int value before using 2 characters to represent an object, spaces more aptly represent an empty grid square as opposed to a '0' and the characters used can hint better as to what object they represent. So, the map above can be represented as follows with a player and enemy added:
private String[] map =
{
    "wwwwwww",
    "w     w",
    "w     w",
    "w     w",
    "w     w",
    "wp   ew",
    "wwwwwww"
};
The characters used can be used in the switch like as given above to build the world:
Actor actor = null;
switch (map[y].charAt(x))
{
    case 'w': actor = new Wall(); break;
    case 'p': actor = new Player(); break;
    case 'e': actor = new Enemy(); break;
}
if (actor !- null) addObject(actor, x, y);
There are more replies on the next page.
1
2