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

2018/11/13

For Loop - Creating Bricks

Sangate Sangate

2018/11/13

#
Hello, I'm trying to create rows like the ones in the Picture With the code below i'm able to create Level 1 (The 1st picture 9x9 Bricks).. But i'm really struggling with the other ones. Could someone help me out? Thanks
1
2
3
4
5
6
7
8
9
10
11
12
public void Populate(int pRows)
   {
        for(int i=9; i>0; i--)
        {
            for(int j=0; j<9; j++)
            {
                 
                    this.addObject(new SpaceInvader(),
                    this.getWidth()/2-7*SpaceInvader.WIDTH/2+SpaceInvader.WIDTH*j,
                    75+i*SpaceInvader.HEIGHT);
            }
        }
danpost danpost

2018/11/13

#
Sangate wrote...
Hello, I'm trying to create rows like the ones in the Picture << Image Omitted >> With the code below i'm able to create Level 1 (The 1st picture 9x9 Bricks).. But i'm really struggling with the other ones. Could someone help me out? << Code Omitted >>
For the first one, the only things that may be missing are color control, proper horizontal centering and the use of the parameter, pRows, within the method. Moving on to Level 2 arrangement, without concern for colors, you just need to restrict the adding of bricks when some condition is true. With proper ordering of which loop comes first, you can control the initial value or limiting value of the inner loop by having it depend on the current value of the outer loop. For Level 3, which, like Level 2, has a condition to spawn, there is also a change in how they are placed. Level 4 is a continuation of 3 with additional control for the number of bricks in a row. All the conditions (or expressions) will be based on loop counter value(s).
You need to login to post a reply.