You are here: JGT Tutorial > 12. Levels

Levels

When the player succeeds in killing all of the enemies in a level, we want to reward him with a new, different level.

The first step is to detect when the player wins. We modify playLevel() so that it returns a boolean: true if you won, false if you lost.

   /** main game loop * */
   public boolean playLevel() {
      (...)
         if (null != collision && !collision._a.isEmpty()) {
            System.out.println("Boom! You died");
            // done with the level
            _destroyed.play();
            return false;
         }
         if (_enemies.isEmpty()) return true; // next level
      }
   }
The next step is to modify the main program so that, when playLevel returns, it checks whether it should move on to the next level or start over. We use the variable level to keep track of the current level number.
   /** game entry point * */
   public void run() {
      try {
         // sound by "bliss" (on Freesounds.iua.upf.edu)
         _boom = new SimpleClip("gamedata/boom.wav");
         _fire = new SimpleClip("gamedata/wuw1.wav");
         // sound by "meatball4u" (on Freesounds.iua.upf.edu)
         _destroyed = new SimpleClip("gamedata/17226_meatball4u_explode4.wav");
         while (_gameRunning) {
            // initialize game for first level
            int level=0;
            _ship = new Ship(this, getKeyboardStatus());
            _shipList = new ArrayList();
            _shipList.add(_ship);
            _enemies = new ArrayList();
            _bullets = new ArrayList();
            _enemyBullets = new ArrayList();
            _animations = new ArrayList();
            createImageAndGraphics();
            boolean passed;
            do {
               // play the level until win or lose
               passed = playLevel(level);
               if (passed) {
                  level++;
               }
            } while (passed);
         }
      } catch (Exception x) {
         x.printStackTrace();
         throw new RuntimeException("Sorry, there's some sort of bug", x);
      }
   }
I copied the whole function, but most of it is unchanged. Finally, we add a parameter to playLevel and buildLevel so that they can act according to the correct level. The new buildLevel shown here has three different levels, but of course you can add more.
   public void buildLevel(int no) throws Exception {
      _enemies.clear();
      _animations.clear();
      Formation f = new Formation(0, getWidth());
      f.setSpeed(2);
      if (0 == no) {
         for (int x = 0; x < 4; x++) {
            for (int y = 0; y < 3; y++) {
               Enemy baddie = new Enemy(this, 10 + x * 50, 10 + y * 40);
               f.add(baddie);
               _enemies.add(baddie);
            }
         }
      } else if (1 == no) {
         for (int x = 0; x < 5; x++) {
            for (int y = 0; y < 3; y++) {
               Enemy baddie = new Enemy(this, 10 + x * 50, 10 + y * 40);
               f.add(baddie);
               _enemies.add(baddie);
            }
         }
      } else if (no > 1) {
         for (int x = 0; x < 6; x++) {
            for (int y = 0; y < 1; y++) {
               Enemy baddie = new Enemy(this, 10 + x * 40, 10 + y * 40);
               f.add(baddie);
               _enemies.add(baddie);
            }
         }
         Formation f2 = new Formation(0, getWidth());
         f2.setSpeed(2);
         for (int x = 0; x < 3; x++) {
            for (int y = 0; y < 1; y++) {
               Enemy baddie = new Enemy(this, 10 + x * 50, 90 + 10 + y * 40 +(x%2)*20);
               f2.add(baddie);
               _enemies.add(baddie);
            }
         }
         _animations.add(f2);
      }
      _animations.add(f);
   }
The third level has two formations that moves independently. And that's it! Now we have three levels with different formations of bad guys.

   

Next: (later)