Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Need a loop for rows and columns

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need a loop for rows and columns

    hey guys i need help making a loop to create bricks in my breakout game.

    here is the code to make one brick:

    //creates a brick in breakoutWourld.
    brick = new Brick(1,"red");
    addToGame(brick, 20, 10);


    any ideas on how i can loop it to make 2 rows that have 11 bricks.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need a loop for rows and columns

    for loops are perfectly suited for this problem.

    for (int i = 0; i < 2; i++)
    {
         for (int j = 0; j < 11; j++)
         {
              brick = new Brick(1,"red");
              addToGame(brick,20+j,10+i);
         }
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Ceasar (October 9th, 2009)

  4. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need a loop for rows and columns

    Umm it creates the blocks but they seem to be stacked on each other.

    ummis it possible to use the brick height and width in the code so they dont stack on each other?

  5. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need a loop for rows and columns

    private static final int BRICK_HEIGHT = 8;
    private static final int BRICK_WIDTH = 16;

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need a loop for rows and columns

    just multiply j by BRICK_WIDTH and i by BRICK_HEIGHT

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    Ceasar (October 9th, 2009)

  8. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need a loop for rows and columns

    yup that work and i have assigned points to the colors.

  9. #7
    Junior Member
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need a loop for rows and columns

    Ok here is another question.

    If i used the code to make all the bricks of the game how can i change the colours between them?

    for (int i = 0; i < 8; i++)
    {
    for (int j = 0; j < 11; j++)
    {
    brick = new Brick(1,"red");
    addToGame(brick,20+j*BRICK_WIDTH,10+i*BRICK_HEIGHT );
    }
    }

    at the moment its making all the bricks red.

    i have four colors red, blue, green, yellow.

  10. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Need a loop for rows and columns

    if( (i*11 + j) % 4 == 0) //red
    else if( (i*11 + j) % 3 == 0) //blue
    else if( (i*11 + j) % 2 == 0) //green
    else //yellow

    Or you could use the inlining method, but it would get messy lol

    Chris

  11. #9
    Junior Member
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need a loop for rows and columns

    That doesnt seem to be working for me...not sure what i did wrong

Similar Threads

  1. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM
  2. For loop tricks
    By helloworld922 in forum Java Programming Tutorials
    Replies: 0
    Last Post: October 4th, 2009, 01:09 AM
  3. JAVA for loop
    By tazjaime in forum Loops & Control Statements
    Replies: 2
    Last Post: August 18th, 2009, 07:43 PM
  4. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM
  5. adding rows to coloumns. Netbeans
    By haygaurav in forum Java IDEs
    Replies: 1
    Last Post: April 1st, 2009, 03:16 PM