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

It is VERY important to note that you can have a maximum of 64 objects on the screen (0-63). If you try to render 65 or more objects, picking becomes confused, and odd things start to happen. Everything from objects randomly exploding to your computer crashing. It's a physical limit in OpenGL (just like the 8 lights limit).

If by some chance you are a god, and you finish level 30, the level will no longer increase, but your score will. Your morale will also reset to 10 every time you finish the 30th level.

  if (kills>level*5) // New Level Yet?

  {

   miss=0; // Misses Reset Back To Zero

   kills=0; // Reset Level Kills

   level+=1; // Increase Level

   if (level>30) // Higher Than 30?

    level=30; // Set Level To 30 (Are You A God?)

   }

  }

 }

}

Update() is where I check for key presses, and update object movement. One of the nice things about Update() is the milliseconds timer. You can use the milliseconds timer to move objects based on the amount of time that has passed since Update() was last called. It's important to note that moving object based on time keeps the objects moving at the same speed on any processor… BUT there are drawbacks! Lets say you have an object moving 5 units in 10 seconds. On a fast system, the computer will move the object half a unit every second. On a slow system, it could be 2 seconds before the update procedure is even called. So when the object moves, it will appear to skip a spot. The animation will not be as smooth on a slower system. (Note: this is just an exaggerated example… computers update ALOT faster than once every two seconds).

Anyways… with that out of the way… on to the code. The code below checks to see if the escape key is being pressed. If it is, we quit the application by calling TerminateApplication(). g_window holds the information about our window.

void Update(DWORD milliseconds) // Perform Motion Updates Here

{

 if (g_keys->keyDown[VK_ESCAPE]) // Is ESC Being Pressed?

 {

  TerminateApplication (g_window); // Terminate The Program

 }

The code below checks to see if the space bar is pressed and the game is over. If both conditions are true, we initialize all 30 object (give them new directions, textures, etc). We set game to FALSE, telling the program the game is no longer over. We set the score back to 0, the level back to 1, the player kills to 0 and finally we set the miss variable back to zero. This restarts the game on the first level with full morale and a score of 0.

 if (g_keys->keyDown[' '] && game) // Space Bar Being Pressed After Game Has Ended?

 {

  for (int loop=0; loop<30; loop++) // Loop Through 30 Objects

   InitObject(loop); // Initialize Each Object

  game=FALSE; // Set game (Game Over) To False

  score=0; // Set score To 0

  level=1; // Set level Back To 1

  kills=0; // Zero Player Kills

  miss=0; // Set miss (Missed Shots) To 0

 }

The code below checks to see if the F1 key has been pressed. If F1 is being pressed, ToggleFullscreen will switch from windowed to fullscreen mode or fullscreen mode to windowed mode.

 if (g_keys->keyDown[VK_F1]) // Is F1 Being Pressed?

 {

  ToggleFullscreen (g_window); // Toggle Fullscreen Mode

 }

To create the illusion of rolling clouds and moving ground, we decrease roll by .00005f multiplied by the number of milliseconds that have passed. This keeps the clouds moving at the same speed on all systems (fast or slow).

We then set up a loop to loop through all of the objects on the screen. Level 1 has one object, level 10 has 10 objects, etc.

 roll-=milliseconds*0.00005f; // Roll The Clouds

 for (int loop=0; loop<level; loop++) // Loop Through The Objects

 {

We need to find out which way the object should be spinning. We do this by checking the value of rot. If rot equals 1, we need to spin the object clockwise. To do this, we decrease the value of spin. We decrease spin by 0.2f multiplied by value of loop plus the number of milliseconds that have passed. By using milliseconds the objects will rotate the same speed on all systems. Adding loop makes each NEW object spin a little faster than the last object. So object 2 will spin faster than object 1 and object 3 will spin faster than object 2.

  if (object[loop].rot==1) // If Rotation Is Clockwise

   object[loop].spin-=0.2f*(float(loop+milliseconds)); // Spin Clockwise

Next we check to see if rot equals 2. If rot equals 2, we need to spin counter clockwise. The only difference from the code above is that we are increasing the value of spin instead of decreasing it. This causes the object to spin in the opposite direction.

  if (object[loop].rot==2) // If Rotation Is Counter Clockwise

   object[loop].spin+=0.2f*(float(loop+milliseconds)); // Spin Counter Clockwise

Now for the movement code. We check the value of dir if it's equal to 1, we increase the objects x value based on the milliseconds passed multiplied by 0.012f. This moves the object right. Because we use milliseconds the objects should move the same speed on all systems.

  if (object[loop].dir==1) // If Direction Is Right

   object[loop].x+=0.012f*float(milliseconds); // Move Right

If dir equals 0, the object is moving left. We move the object left by decreasing the objects x value. Again we decrease x based on the amount of time that has passed in milliseconds multiplied by our fixed value of 0.012f.

  if (object[loop].dir==0) // If Direction Is Left

   object[loop].x-=0.012f*float(milliseconds); // Move Left

Only two more directions to watch for. This time we check to see if dir equals 2. If so, we increase the objects y value. This causes the object to move UP the screen. Keep in mind the positive y axis is at the top of the screen and the negative y axis is at the bottom. So increasing y moves from the bottom to the top. Again movement is based on time passed.

  if (object[loop].dir==2) // If Direction Is Up

   object[loop].y+=0.012f*float(milliseconds); // Move Up

The last direction our object can travel is down. If dir equals three, we want to move the object down the screen. We do this by increasing the objects y value based on the amount of time that has passed. Notice we move down slower than we move up. When an object is falling, our fixed falling rate is 0.0025f. When we move up, the fixed rate is 0.012f.

  if (object[loop].dir==3) // If Direction Is Down

   object[loop].y-=0.0025f*float(milliseconds); // Move Down

After moving our objects we have to check if they are still in view. The code below first checks to see where our object is on the screen. We can roughly calculate how far left an object can travel by taking the objects distance into the screen minus 15.0f (to make sure it's a little past the screen) and dividing it by 2. For those of you that don't already know… If you are 20 units into the screen, depending on the way you set up the perspective, you have roughly 10 units from the left of the screen to the center and 10 from the center to the right. so –20.0f(distance)-15.0f(extra padding)=-35.0f… divide that by 2 and you get –17.5f. That's roughly 7.5 units off the left side of the screen. Meaning our object is completely out of view.