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

2014/5/19

Problems with a grid?

Tommy99 Tommy99

2014/5/19

#
Hello. I am having trouble with both making the world (the squares) of my game green and fitting the "Wall" class into a square of the grid (my world). Here is the code for the World class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; public class OppositeWorld extends World { public final static int CELL_SIZE = 40; public OppositeWorld() { super(30, 30, 20, false); GreenfootImage img = new GreenfootImage(CELL_SIZE, CELL_SIZE); img.drawRect(0, 0, CELL_SIZE-1, CELL_SIZE-1); setBackground(img); img.setColor(Color.GREEN); img.fill(); addObject(new Wall(), 3, 3); } } Here is the code for the "Wall" class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Wall extends Actor { public Wall() { this(40);//call the other constructor } /** * Overload the constructor */ public Wall(int size) { GreenfootImage img = new GreenfootImage(size,size); img.fill(); setImage(img); } } Thank you very much.
danpost danpost

2014/5/19

#
I think your main problem is the order in which you execute the parts of the construction of the background. Obviously, you should set the background image last. And maybe you should fill the image with green and then draw the black frame around it (before setting the image to the background). Unless you are going to be creating Walls of different sizes other than the cell-size of the world, it is not necessary to overload the constructor of the Wall class. In fact, the parameter can be removed and you can use 'OppositeWorld.CELL_SIZE' to get that value (because the 'CELL_SIZE' field is 'public static', you can just use the class name followed by the field name, as shown, to get that value from anywhere in your project).
Tommy99 Tommy99

2014/5/19

#
I tried doing what you said, but now the game is just a green background with a black square in it, and there are no grids. Also, I still do not know how to fit the black square within a square in the grid. Here is the code for the World class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; public class OppositeWorld extends World { public final static int CELL_SIZE = 40; public OppositeWorld() { super(30, 30, 20, false); GreenfootImage img = new GreenfootImage(CELL_SIZE, CELL_SIZE); img.setColor(Color.GREEN); img.fill(); img.drawRect(0, 0, CELL_SIZE-1, CELL_SIZE-1); setBackground(img); addObject(new Wall(), 3, 3); } } Here is the code for the Wall class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Wall extends Actor { public Wall() { this(40);//call the other constructor } /** * Overload the constructor */ public Wall(int size) { GreenfootImage img = new GreenfootImage(size,size); img.fill(); setImage(img); } }
danpost danpost

2014/5/19

#
You have to set the drawing color back to black before drawing the rectangle.
Tommy99 Tommy99

2014/5/19

#
Oh, okay. Thanks. How do I get the "Wall" to fit inside a square in the grid though?
danpost danpost

2014/5/19

#
For one thing, I am not sure if you intended this: but, your world cell-size is 20 and yet you are using 40 for a grid size. Maybe you meant to set CELL_SIZE to 20 (but I could be mistaken). Then, your wall would also need to be sized at 20.
You need to login to post a reply.