If every array is equal to...

edited November 2014 in How To...

Hi everybody, I'm doing my final work for a subject in my university, here in Argentina. It's quite simple, but I have lot of problems. The work consists in make a game, i'm programming the classic Bounce Ball. I'm recently starting. I'm using bisimensional arrays for each block. When the ball makes contact with a block, it moves to width+blockSize (it's a bit tricky, but that's what my teacher told me to use, because we couldn't learn ArrayList at all). My problem, right now, is to make that when every block is in position=width+blockSize, appears a text saying you won. I'm doing this:

in the main class (blo is a block object)

       for (int i=0;i<cant;i++){
         blo[i].draw();
         blo[i].deny(ball);
         if(blo[i].x>=width){
         noLoop();}
       }

in the block class

void dibujar() {
    if(status.equals("intact")){
       fill(150);
     } 
     if(status.equals("deny")){
       x=width+blockSize;
       y=height+blockSize;
     }
    rect(x, y, sx, sy);
  }

I want to noLoop() the program when every block is in that position, but it stop the programs when it only hits one block. I thought that the for would do that. It works if I write:

for (int i=0;i<cant;i++){
         blo[i].draw();
         blo[i].deny(ball);
         if(blo[0].x>=width && blo[1].x>=width && blo[2].x>=width && blo[3].x>=width, etc, etc, etc){
         noLoop();}
       }

Hope you uderstand me, sorry for my english. Thank you. P.S: I have very basic knowledge about processing.

Tagged:

Answers

  • edited November 2014 Answer ✓

    I wouldn't use noLoop() in the 1st place, because you want to write You Won on the screen and then be able to restart the game... but anyway...

    regarding your for-loop: have a boolean before the for-loop allBlocksAreDead=true; When one block is not dead, set allBlocksAreDead=false; after (!) the entire for-loop check allBlocksAreDead and if it is stell true, say noLoop();

    appears a text saying you won

    to achieve this, you can get rid of noLoop and instead have a state variable (when you have one already, good) and set it to

    state=stateYouWon

    or have global boolean var playerHasWon which is initially false

    When he wins, set playerHasWon to true.

    in draw have one big if

    void draw() {
    if (playerHasWon) { 
    text (you won....  
    } 
    else {  
    // normal game as you have it now
    } 
    

    this is all pseudo-code and not tested...

    Chrisir ;-)

  • Thank you for the answer, I will try it and share the result. I was using noLoop only for testing, I won't use it finally.

  • It works great! Thank you very much.

  • Answer ✓

    btw.

    when you have

    When one block is not dead, set allBlocksAreDead=false;

    you can say break; to leave the for-loop, saves a little time for the cpu...

Sign In or Register to comment.