Выбрать главу

If the player fx value is less than the player x value times 60 we increase the player fx position by the step speed our game is running at based on the value of adjust.

If the player fx value is greater than the player x value times 60 we decrease the player fx position by the step speed our game is running at based on the value of adjust.

If the player fy value is less than the player y value times 40 we increase the player fy position by the step speed our game is running at based on the value of adjust.

If the player fy value is greater than the player y value times 40 we decrease the player fy position by the step speed our game is running at based on the value of adjust.

   if (player.fx<player.x*60) // Is Fine Position On X Axis Lower Than Intended Position?

   {

    player.fx+=steps[adjust]; // If So, Increase The Fine X Position

   }

   if (player.fx>player.x*60) // Is Fine Position On X Axis Greater Than Intended Position?

   {

    player.fx-=steps[adjust]; // If So, Decrease The Fine X Position

   }

   if (player.fy<player.y*40) // Is Fine Position On Y Axis Lower Than Intended Position?

   {

    player.fy+=steps[adjust]; // If So, Increase The Fine Y Position

   }

   if (player.fy>player.y*40) // Is Fine Position On Y Axis Lower Than Intended Position?

   {

    player.fy-=steps[adjust]; // If So, Decrease The Fine Y Position

   }

  }

If the game is over the following bit of code will run. We check to see if the spacebar is being pressed. If it is we set gameover to FALSE (starting the game over). We set filled to TRUE. This causes the game to think we've finished a stage, causing the player to be reset, along with the enemies.

We set the starting level to 1, along with the actual displayed level (level2). We set stage to 0. The reason we do this is because after the computer sees that the grid has been filled in, it will think you finished a stage, and will increase stage by 1. Because we set stage to 0, when the stage increases it will become 1 (exactly what we want). Lastly we set lives back to 5.

  else // Otherwise

  {

   if (keys[' ']) // If Spacebar Is Being Pressed

   {

    gameover=FALSE; // gameover Becomes FALSE

    filled=TRUE; // filled Becomes TRUE

    level=1; // Starting Level Is Set Back To One

    level2=1; // Displayed Level Is Also Set To One

    stage=0; // Game Stage Is Set To Zero

    lives=5; // Lives Is Set To Five

   }

  }

The code below checks to see if the filled flag is TRUE (meaning the grid has been filled in). filled can be set to TRUE one of two ways. Either the grid is filled in completely and filled becomes TRUE or the game has ended but the spacebar was pressed to restart it (code above).

If filled is TRUE, the first thing we do is play the cool level complete tune. I've already explained how PlaySound() works. This time we'll be playing the Complete .WAV file in the DATA directory. Again, we use SND_SYNC so that there is a delay before the game starts on the next stage.

After the sound has played, we increase stage by one, and check to make sure stage isn't greater than 3. If stage is greater than 3 we set stage to 1, and increase the internal level and visible level by one.

If the internal level is greater than 3 we set the internal leve (level) to 3, and increase lives by 1. If you're amazing enough to get past level 3 you deserve a free life :). After increasing lives we check to make sure the player doesn't have more than 5 lives. If lives is greater than 5 we set lives back to 5.

  if (filled) // Is The Grid Filled In?

  {

   PlaySound("Data/Complete.wav", NULL, SND_SYNC); // If So, Play The Level Complete Sound

   stage++; // Increase The Stage

   if (stage>3) // Is The Stage Higher Than 3?

   {

    stage=1; // If So, Set The Stage To One

    level++; // Increase The Level

    level2++; // Increase The Displayed Level

    if (level>3) // Is The Level Greater Than 3?

    {

     level=3; // If So, Set The Level To 3

     lives++; // Give The Player A Free Life

     if (lives>5) // Does The Player Have More Than 5 Lives?

     {

      lives=5; // If So, Set Lives To Five

     }

    }

   }

We then reset all the objects (such as the player and enemies). This places the player back at the top left corner of the grid, and gives the enemies random locations on the grid.

We create two loops (loop1 and loop2) to loop through the grid. We set all the vertical and horizontal lines to FALSE. If we didn't do this, the next stage would start, and the game would think the grid was still filled in.

Notice the routine we use to clear the grid is similar to the routine we use to draw the grid. We have to make sure the lines are not being drawn to far right or down. That's why we check to make sure that loop1 is less than 10 before we reset the horizontal lines, and we check to make sure that loop2 is less than 10 before we reset the vertical lines.

   ResetObjects(); // Reset Player / Enemy Positions

   for (loop1=0; loop1<11; loop1++) // Loop Through The Grid X Coordinates

   {

    for (loop2=0; loop2<11; loop2++) // Loop Through The Grid Y Coordinates

    {

     if (loop1<10) // If X Coordinate Is Less Than 10

     {

      hline[loop1][loop2]=FALSE; // Set The Current Horizontal Value To FALSE

     }

     if (loop2<10) // If Y Coordinate Is Less Than 10

     {

      vline[loop1][loop2]=FALSE; // Set The Current Vertical Value To FALSE

     }

    }

   }

  }

Now we check to see if the player has hit the hourglass. If the fine player fx value is equal to the hourglass x value times 60 and the fine player fy value is equal to the hourglass y value times 40 AND hourglass.fx is equal to 1 (meaning the hourglass is displayed on the screen), the code below runs.

The first line of code is PlaySound("Data/freeze.wav",NULL, SND_ASYNC | SND_LOOP). This line plays the freeze .WAV file in the DATA directory. Notice we are using SND_ASYNC this time. We want the freeze sound to play without the game stopping. SND_LOOP keeps the sound playing endlessly until we tell it to stop playing, or until another sound is played.

After we have started the sound playing, we set hourglass.fx to 2. When hourglass.fx equals 2 the hourglass will no longer be drawn, the enemies will stop moving, and the sound will loop endlessly.

We also set hourglass.fy to 0. hourglass.fy is a counter. When it hits a certain value, the value of hourglass.fx will change.

   // If The Player Hits The Hourglass While It's Being Displayed On The Screen

   if ((player.fx==hourglass.x*60) && (player.fy==hourglass.y*40) && (hourglass.fx==1)) {