Changing between game levels

edited November 2014 in How To...

Hi everybody, this is my second topic, and it's about (again) the game I'm doing for University. This is my first post where I explained the game me and a (solved) doubt. The game is a simple bouncing ball, but as I am a beginner in Processing I get stuck sometimes.

I've done the first level: bouncing ball breaking blocks after two hits. When the ball touches them, they are moved off the screen. In the first hit the become darker, which is made by a String 'state': when it's equal to 'intact' they still red, when it's 'touched' they become darcker red.

Up to there everything is ok. When you destroy every block, you get the message 'YOU WON', and pressing a key you get to the second level. Here is the problem. After hours trying to make it work, I could make that the base, the ball, and the blocks drawing works perfectly, but when the hits them, nothing happends. I think the problem is in the conditional (in the block class) who detects if the ball touch the block, if it's true it become 'touched', and after that it goes outside the window. I know that I don't explain very well, and my english doesn't help. I just uploaded the pde's files if you want to understand better. Here you can download it (4kb).

Some useful translations (my sketch is in spanish):

-juego=game

-inicio=start

-nivel=level

-mover=move

-ancho=width

-alto=height

-dibujar=draw

-bloque=block

-estado=status

-intacto=intact

-dañado=deny/touched

-destruidos=destroyed

-ganaste=won, perdiste=loose

Thank you very much, hope you understand me.

Tagged:

Answers

  • when it works in the first level and doesn't work in the 2nd, it has to do with the transition from level one to two.

    when the conditional works in the 1st level, it works in the 2nd

    my suspicion is that you need to reset some stuff, especially set all states back to intact and the other boolean back to their initial state (very important).

    ;-)

  • Yes, that's what I'm doing. When the second level starts, the first command is block[j][i].status="intact"; , then I move them to the original position, but when the ball hit the block nothing happen. I tried to create a reset function, but it doesn't worked, I think because it appears in the draw() (60 times per second) instead of setup(). I tried lot of things, but I can't get the answer. Thank you for your answer.

  • draw() makes no sense

      game.nivel1();
      if(game.estado.equals("nivel2")){
        game.nivel2();
      }
    

    because you must call nivel1 OR nivel2

      if(game.estado.equals("nivel1")){
        game.nivel1();
      }
      else if(game.estado.equals("nivel2")){
        game.nivel2();
      }
    
  • Answer ✓

    here I doubt this works....

       // ---- Ganaste | Perdiste ---- //
          if (destruidos==true) {
            fun.ganaste(pelo);
            if(key=='a'){
              estado="nivel2";
            }
          }
    

    enter a println here

       // ---- Ganaste | Perdiste ---- //
          if (destruidos==true) {
            fun.ganaste(pelo);
            if(key=='a'){
              estado="nivel2";
              println ("going to level 2"); 
            }
          }
    

    is the println executed?

    I doubt it. Get rid of key==a here

  • inicio doesn't work

    game.inicio();

    but it is not called when going to the next level

  • Answer ✓

    apart from the key==a (I'm coming back) you need to re-init the blocks

        // ---- Ganaste | Perdiste ---- //
        if (destruidos==true) {
          fun.ganaste(pelo);
          //if (key=='a') {
          estado="nivel2";
          println ("level 2");
          blo = new Bloques [filas][cant];
    
          for (int j=0; j<filas; j++) {
            for (int i=0; i<cant; i++) {
              a1=i*tam;
              a2= j*tam/2;
              blo[j][i] = new Bloques (a1, a2, tam, tam/2);
              blo[j][i].estado="intacto";
            }
          }
          //}
        }
        if (pelo.y>height+pelo.tam) {
          fun.perdiste(pelo);
        }
      }
    
  • Answer ✓

    concerning the key==a

    you want it so that the sketch waites until the user hits 'a'

    but that's not the way to do it

    what you need is states.

    then you can have one state to splashScreen and one to Play and one state to BallLost or states to GoToNextLevel

    eval states in draw with switch(states) {

    Best, Chrisir ;-)

  • also remember you can hit ctrl-t to auto-format your code

  • Wow! Thank you very much Chrisir for those answers. I'm gonna see each one and tell you. game.inicio(); was deleted, but I forgot to remove it from the main program.

  • Thank you very much! I understand, now. I replace the void nivel1() and void nivel2() by conditionals in the new void gameStates() (as my teacher wants). Now it's working, I'm gonna see if I can finally finish the game. Thank you, again!

    P.S: yes, sorry about the format of the code, I wasn't clean because I tried lot of things and I got tired to format the text.

  • Answer ✓

    great!

Sign In or Register to comment.