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

      {

To move the enemies we check to see if the current enemy (enemy[loop2]) needs to move in a specific direction to move towards the enemy x and y position we want. In the first line below we check to see if the enemy fine position on the x-axis is less than the desired x position times 60. (remember each grid crossing is 60 pixels apart from left to right). If the fine x position is less than the enemy x position times 60 we move the enemy to the right by steps[adjust] (the speed our game is set to play at based on the value of adjust). We also rotate the enemy clockwise to make it look like it's rolling to the right. We do this by increasing enemy[loop2].spin by steps[adjust] (the current game speed based on adjust).

We then check to see if the enemy fx value is greater than the enemy x position times 60 and if so, we move the enemy left and spin the enemy left.

We do the same when moving the enemy up and down. If the enemy y position is less than the enemy fy position times 40 (40 pixels between grid points up and down) we increase the enemy fy position, and rotate the enemy to make it look like it's rolling downwards. Lastly if the enemy y position is greater than the enemy fy position times 40 we decrease the value of fy to move the enemy upward. Again, the enemy spins to make it look like it's rolling upward.

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

      {

       enemy[loop2].fx+=steps[adjust]; // If So, Increase Fine Position On X Axis

       enemy[loop2].spin+=steps[adjust]; // Spin Enemy Clockwise

      }

      if (enemy[loop2].fx>enemy[loop2].x*60) // Is Fine Position On X Axis Higher Than Intended Position?

      {

       enemy[loop2].fx-=steps[adjust]; // If So, Decrease Fine Position On X Axis

       enemy[loop2].spin-=steps[adjust]; // Spin Enemy Counter Clockwise

      }

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

      {

       enemy[loop2].fy+=steps[adjust]; // If So, Increase Fine Position On Y Axis

       enemy[loop2].spin+=steps[adjust]; // Spin Enemy Clockwise

      }

      if (enemy[loop2].fy>enemy[loop2].y*40) // Is Fine Position On Y Axis Higher Than Intended Position?

      {

       enemy[loop2].fy-=steps[adjust]; // If So, Decrease Fine Position On Y Axis

       enemy[loop2].spin-=steps[adjust]; // Spin Enemy Counter Clockwise

      }

     }

    }

After moving the enemies we check to see if any of them have hit the player. We want accuracy so we compare the enemy fine positions with the player fine positions. If the enemy fx position equals the player fx position and the enemy fy position equals the player fy position the player is DEAD :)

If the player is dead, we decrease lives. Then we check to make sure the player isn't out of lives by checking to see if lives equals 0. If lives does equal zero, we set gameover to TRUE.

We then reset our objects by calling ResetObjects(), and play the death sound.

Sound is new in this tutorial. I've decided to use the most basic sound routine available… PlaySound(). PlaySound() takes three parameters. First we give it the name of the file we want to play. In this case we want it to play the Die .WAV file in the Data directory. The second parameter can be ignored. We'll set it to NULL. The third parameter is the flag for playing the sound. The two most common flags are: SND_SYNC which stops everything else until the sound is done playing, and SND_ASYNC, which plays the sound, but doesn't stop the program from running. We want a little delay after the player dies so we use SND_SYNC. Pretty easy!

The one thing I forgot to mention at the beginning of the program: In order for PlaySound() and the timer to work, you have to include the WINMM.LIB file under PROJECT / SETTINGS / LINK in Visual C++. Winmm.lib is the Windows Multimedia Library. If you don't include this library, you will get error messages when you try to compile the program.

    // Are Any Of The Enemies On Top Of The Player?

    if ((enemy[loop1].fx==player.fx) && (enemy[loop1].fy==player.fy)) {

     lives--; // If So, Player Loses A Life

     if (lives==0) // Are We Out Of Lives?

     {

      gameover=TRUE; // If So, gameover Becomes TRUE

     }

     ResetObjects(); // Reset Player / Enemy Positions

     PlaySound("Data/Die.wav", NULL, SND_SYNC); // Play The Death Sound

    }

   }

Now we can move the player. In the first line of code below we check to see if the right arrow is being pressed, player.x is less than 10 (don't want to go off the grid), that player.fx equals player.x times 60 (lined up with a grid crossing on the x-axis, and that player.fy equals player.y times 40 (player is lined up with a grid crossing on the y-axis).

If we didn't make sure the player was at a crossing, and we allowed the player to move anyways, the player would cut right through the middle of boxes, just like the enemies would have done if we didn't make sure they were lined up with a vertical or horizontal line. Checking this also makes sure the player is done moving before we move to a new location.

If the player is at a grid crossing (where a vertical and horizontal lines meet) and he's not to far right, we mark the current horizontal line that we are on as being traced over. We then increase the player.x value by one, causing the new player position to be one box to the right.

We do the same thing while moving left, down and up. When moving left, we make sure the player wont be going off the left side of the grid. When moving down we make sure the player wont be leaving the bottom of the grid, and when moving up we make sure the player doesn't go off the top of the grid.

When moving left and right we make the horizontal line (hline[ ] [ ]) under us TRUE meaning it's been traced. When moving up and down we make the vertical line (vline[ ] [ ]) under us TRUE meaning it has been traced.

   if (keys[VK_RIGHT] && (player.x<10) && (player.fx==player.x*60) && (player.fy==player.y*40)) {

    hline[player.x][player.y]=TRUE; // Mark The Current Horizontal Border As Filled

    player.x++; // Move The Player Right

   }

   if (keys[VK_LEFT] && (player.x>0) && (player.fx==player.x*60) && (player.fy==player.y*40)) {

    player.x--; // Move The Player Left

    hline[player.x][player.y]=TRUE; // Mark The Current Horizontal Border As Filled

   }

   if (keys[VK_DOWN] && (player.y<10) && (player.fx==player.x*60) && (player.fy==player.y*40)) {

    vline[player.x][player.y]=TRUE; // Mark The Current Verticle Border As Filled

    player.y++; // Move The Player Down

   }

   if (keys[VK_UP] && (player.y>0) && (player.fx==player.x*60) && (player.fy==player.y*40)) {

    player.y--; // Move The Player Up

    vline[player.x][player.y]=TRUE; // Mark The Current Verticle Border As Filled

   }

We increase / decrease the player fine fx and fy variables the same way we increase / decreased the enemy fine fx and fy variables.