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

    // Play Freeze Enemy Sound

    PlaySound("Data/freeze.wav", NULL, SND_ASYNC | SND_LOOP);

    hourglass.fx=2; // Set The hourglass fx Variable To Two

    hourglass.fy=0; // Set The hourglass fy Variable To Zero

   }

This bit of code increases the player spin value by half the speed that the game runs at. If player.spin is greater than 360.0f we subtract 360.0f from player.spin. Keeps the value of player.spin from getting to high.

   player.spin+=0.5f*steps[adjust]; // Spin The Player Clockwise

   if (player.spin>360.0f) // Is The spin Value Greater Than 360?

   {

    player.spin-=360; // If So, Subtract 360

   }

The code below decreases the hourglass spin value by 1/4 the speed that the game is running at. If hourglass.spin is less than 0.0f we add 360.0f. We don't want hourglass.spin to become a negative number.

   hourglass.spin-=0.25f*steps[adjust]; // Spin The Hourglass Counter Clockwise

   if (hourglass.spin<0.0f) // Is The spin Value Less Than 0?

   {

    hourglass.spin+=360.0f; // If So, Add 360

   }

The first line below increased the hourglass counter that I was talking about. hourglass.fy is increased by the game speed (game speed is the steps value based on the value of adjust).

The second line checks to see if hourglass.fx is equal to 0 (non visible) and the hourglass counter (hourglass.fy) is greater than 6000 divided by the current internal level (level).

If the fx value is 0 and the counter is greater than 6000 divided by the internal level we play the hourglass .WAV file in the DATA directory. We don't want the action to stop so we use SND_ASYNC. We won't loop the sound this time though, so once the sound has played, it wont play again.

After we've played the sound we give the hourglass a random value on the x-axis. We add one to the random value so that the hourglass doesn't appear at the players starting position at the top left of the grid. We also give the hourglass a random value on the y-axis. We set hourglass.fx to 1 this makes the hourglass appear on the screen at it's new location. We also set hourglass.fy back to zero so it can start counting again.

This causes the hourglass to appear on the screen after a fixed amount of time.

   hourglass.fy+=steps[adjust]; // Increase The hourglass fy Variable

   if ((hourglass.fx==0) && (hourglass.fy>6000/level)) // Is The hourglass fx Variable Equal To 0 And The fy

   {

    // Variable Greater Than 6000 Divided By The Current Level?

    PlaySound("Data/hourglass.wav", NULL, SND_ASYNC); // If So, Play The Hourglass Appears Sound

    hourglass.x=rand()%10+1; // Give The Hourglass A Random X Value

    hourglass.y=rand()%11; // Give The Hourglass A Random Y Value

    hourglass.fx=1; // Set hourglass fx Variable To One (Hourglass Stage)

    hourglass.fy=0; // Set hourglass fy Variable To Zero (Counter)

   }

If hourglass.fx is equal to zero and hourglass.fy is greater than 6000 divided by the current internal level (level) we set hourglass.fx back to 0, causing the hourglass to disappear. We also set hourglass.fy to 0 so it can start counting once again.

This causes the hourglass to disappear if you don't get it after a certain amount of time.

   if ((hourglass.fx==1) && (hourglass.fy>6000/level)) // Is The hourglass fx Variable Equal To 1 And The fy

   {

    // Variable Greater Than 6000 Divided By The Current Level?

    hourglass.fx=0; // If So, Set fx To Zero (Hourglass Will Vanish)

    hourglass.fy=0; // Set fy to Zero (Counter Is Reset)

   }

Now we check to see if the 'freeze enemy' timer has run out after the player has touched the hourglass.

If hourglass.fx equal 2 and hourglass.fy is greater than 500 plus 500 times the current internal level we kill the timer sound that we started playing endlessly. We kill the sound with the command PlaySound(NULL, NULL, 0). We set hourglass.fx back to 0, and set hourglass.fy to 0. Setting fx and fy to 0 starts the hourglass cycle from the beginning. fy will have to hit 6000 divided by the current internal level before the hourglass appears again.

   if ((hourglass.fx==2) && (hourglass.fy>500+(500*level)))// Is The hourglass fx Variable Equal To 2 And The fy

   {

    // Variable Greater Than 500 Plus 500 Times The Current Level?

    PlaySound(NULL, NULL, 0); // If So, Kill The Freeze Sound

    hourglass.fx=0; // Set hourglass fx Variable To Zero

    hourglass.fy=0; // Set hourglass fy Variable To Zero

   }

The last thing to do is increase the variable delay. If you remember, delay is used to update the player movement and animation. If our program has finished, we kill the window and return to the desktop.

   delay++; // Increase The Enemy Delay Counter

  }

 }

 // Shutdown

 KillGLWindow(); // Kill The Window

 return (msg.wParam); // Exit The Program

}

I spent a long time writing this tutorial. It started out as a simple line tutorial, and flourished into an entertaining mini game. Hopefully you can use what you have learned in this tutorial in GL projects of your own. I know alot of you have been asking about TILE based games. Well you can't get more tiled than this :) I've also gotten alot of emails asking how to do exact pixel plotting. I think I've got it covered :) Most importantly, this tutorial not only teaches you new things about OpenGL, it also teaches you how to use simple sounds to add excitement to your visual works of art! I hope you've enjoyed this tutorial. If you feel I have incorrectly commented something or that the code could be done better in some sections, please let me know. I want to make the best OpenGL tutorials I can and I'm interested in hearing your feedback.

Please note, this was an extremely large projects. I tried to comment everything as clearly as possible, but putting what things into words isn't as easy as it may seem. I know how everything works off by heart, but trying to explain is a different story :) If you've read through the tutorial and have a better way to word things, or if you feel diagrams might help out, please send me suggestions. I want this tutorial to be easy to follow through. Also note that this is not a beginner tutorial. If you haven't read through the previous tutorials please don't email me with questions until you have. Thanks.

Jeff Molofee (NeHe)

* DOWNLOAD Visual C++ Code For This Lesson.

* DOWNLOAD Borland C++ Code For This Lesson. (Conversion by Patrick Salmons)

* DOWNLOAD Delphi Code For This Lesson. (Conversion by Marc Aarts)

* DOWNLOAD Irix Code For This Lesson. (Conversion by Dimi)

* DOWNLOAD Java Code For This Lesson. (Conversion by Jeff Kirby)