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

2018/2/4

How to make a checkerboard in Greenfoot?

Zweeg Zweeg

2018/2/4

#
I'm trying to make a checkerboard in Greenfoot right now. It has to be 8*8 tiles and the world is 600*600*1. However, the checkerboard has to be made using a for loop. I have public void checkerboard() { GreenfootImage image = new GreenfootImage(50, 50); image.fillRect(0,0,50,50); } for a start just to test if I can make a rectangle, but when I do void checkerboard in the world, nothing happens. Can you help me?
danpost danpost

2018/2/4

#
So far, you have created an image and filled it in, making it a black square. You have not done anything else with it. It is not set as the background image or drawn onto the current background image. Until one of these is done, nothing visual will happen. You stated a 'for' loop is needed. Is that a requirement or is that just what you expect is needed?
Zweeg Zweeg

2018/2/4

#
That is what I expect is needed
Zweeg Zweeg

2018/2/4

#
Also, how would I set my code as the current boackground image?
danpost danpost

2018/2/4

#
All you need to create is one portion of the checkerboard pattern (4 squares of alternating light and dark) and use the 'setBackground' method to apply it to the entire background:
1
2
3
4
5
6
7
8
9
public void checkerboard()
{
    GreenfootImage image = new GreenfootImage(150, 150);
    image.fill(); // fill with black
    image.setColor(Color.WHITE);
    image.fillRect(0, 0, 75, 75); // upper-left
    image.fillRect(75, 75, 75, 75); // lower-right
    setBackground(image);
}
The image is totally filled in first to guarantee that no transparency is in the image (you get unwanted behavior if any of the background image is transparent).
You need to login to post a reply.