Beginner's corner

Top  Previous  Next

Bouncing ball

 

If you liked the many "Marbles" clones now you have the chance to create your own clone. We will write the code for a ball that can be controlled with the arrow keys and bounces all the time. I'm not going to use the physics engine at all, so the code will work ok with any Acknex edition.

 

aum41_shot2

 

function main()

{

   fps_max = 60;

   level_load (level1_wmb);

   wait (3);

   while (1)

   {

      camera.x = ball.x - 200;

      camera.y = ball.y;

      camera.z = 100;

      camera.tilt = -45;

      wait (1);

   }

}

 

Every time you see function main() in one of the Aum scripts you know that we have a standalone project, right? This means that you can add more features to the existing code whenever you need them and it will (hopefully) continue to run ok. Our function limits the frame rate to 60 fps, loads a simple level, waits until it is loaded, and then sets the proper camera position and angle. Please note that the camera has a fixed height (100 quants) and stays 200 quants behind the ball all the time.

 

action bouncing_ball

{

   ball = my;

   my.enable_block = on;

   my.enable_entity = on;

   my.event = reverse_speed;

   while (1)

   {

       if (key_cuu == on)

       {

           ball_speed.x = 5 * time;

           my.tilt -= 8 * time;

        }

       if (key_cud == on)

       {

          ball_speed.x = -5 * time;

          my.tilt += 8 * time;

       }

       if ((key_cuu == off) && (key_cud == off))

       {

          ball_speed.x = 0;

       }

       if (key_cul == on)

      {

         ball_speed.y = 5 * time;

         my.roll += 8 * time;

      }

     if (key_cur == on)

     {

        ball_speed.y = -5 * time;

        my.roll -= 8 * time;

     }

 

You can see that I have given the ball pointer an original name (ball); it is sensitive to level blocks and entities, running its reverse_speed function if it collides with something. If we press the "up" arrow key, the ball will move forward with the speed given by 5 * time and it will change its tilt angle with the speed given by -8 * time because we want to create the illusion that the ball rotates. The same things happen when the player presses the "down", "left" or "right" arrow keys; of course that we use different speeds, movement axis and rotation angles.

 

    if ((key_cuu == off) && (key_cud == off))

    {

       ball_speed.x = 0;

    }

    if ((key_cul == off) && (key_cur == off))

    {

       ball_speed.y = 0;

     }

     vec_set (temp, my.x);

     temp.z -= 10000;

     trace_mode = ignore_me + ignore_passable + use_box;

     trace_dist = trace(my.x, temp);

     if (trace_dist > max_height) // the ball has reached its maximum height?

    {

       ball_speed.z = -abs(ball_speed.z); // then give the ball a negative speed

    }

    ent_move (nullvector, ball_speed);

    wait(1);

}

}

 

If the player isn't pressing the arrow keys, ball_speed.x and / or ball_speed.y will be reset; the ball needs to stop if we don't press the keys at all. The next few lines use a "trace" instruction to detect the floor level. I have decided to use a thick tracing ray (use_box) because I didn't want the ball to get stuck close to the margin of the ramp or in a similar situation.

 

aum41_shot3

 

If trace_dist is greater than max_height (set to 70 quants at the beginning of the script file) the ball has reached its maximum height, so it must change its speed from positive to negative because we want it to fall to the ground from now on. I chose 70 quants for the maximum height above the floor level, but you can use any other value here.

 

function reverse_speed()

{

   ball_speed.z *= -1;

}

 

The event function simply changes the sign of the speed (from positive to negative, or from negative to positive) on impact.

 

Random tunes

 

Many games use a few soundtracks, playing them in a random order in every level. This snippet will show you how to do the same thing for your game.

 

starter random_tunes()

{

   var current_track = 0;

   randomize();

   while (1)

   {

      current_track = int(random(5)) + 1;

      if (current_track == 1)

      {

         media_play("track1.mid", null, 100);

      }

      if (current_track == 2)

      {

         media_play("track2.mid", null, 100);

      }

      if (current_track == 3)

      {

         media_play("track3.mid", null, 100);

      }

      if (current_track == 4)

      {

         media_play("track4.mid", null, 100);

      }

      if (current_track == 5)

      {

         media_play("track5.mid", null, 100);

      }

      while (snd_playing(media_handle) != 0) {wait (1);}

      sleep (1);

   }

}

 

We've got a starter function here, so it will start running as soon as the level is loaded. We define a local variable named current_track, and then we make sure that we will generate a different random number series every time we run the script by calling the built-in "randomize" function. The first line of code inside the while(1) loop generates a random number that ranges from 1 to 5; we will play one of the 5 sound tracks depending on the "current_track" value. The last few lines of code wait until the last song stops and create a 1 second pause between the tracks. Feel free to use any other value here, including wait (1) if you don't want to put a small pause between any 2 consecutive soundtracks.