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

   glColor3f(1.0f,1.0f,1.0f); // Bright White Color

   glBindTexture(GL_TEXTURE_2D, texture[1]); // Select The Tile Image

   if ((loop1<10) && (loop2<10)) // If In Bounds, Fill In Traced Boxes

   {

    // Are All Sides Of The Box Traced?

    if (hline[loop1][loop2] && hline[loop1][loop2+1] && vline[loop1][loop2] && vline[loop1+1][loop2]) {

     glBegin(GL_QUADS); // Draw A Textured Quad

      glTexCoord2f(float(loop1/10.0f)+0.1f, 1.0f-(float(loop2/10.0f)));

      glVertex2d(20+(loop1*60)+59,(70+loop2*40+1)); // Top Right

      glTexCoord2f(float(loop1/10.0f),1.0f-(float(loop2/10.0f)));

      glVertex2d(20+(loop1*60)+1,(70+loop2*40+1)); // Top Left

      glTexCoord2f(float(loop1/10.0f),1.0f-(float(loop2/10.0f)+0.1f));

      glVertex2d(20+(loop1*60)+1,(70+loop2*40)+39); // Bottom Left

      glTexCoord2f(float(loop1/10.0f)+0.1f,1.0f-(float(loop2/10.0f)+0.1f));

      glVertex2d(20+(loop1*60)+59,(70+loop2*40)+39); // Bottom Right

     glEnd(); // Done Texturing The Box

    }

   }

   glDisable(GL_TEXTURE_2D); // Disable Texture Mapping

  }

 }

 glLineWidth(1.0f); // Set The Line Width To 1.0f

The code below checks to see if anti is TRUE. If it is, we enable line smoothing (anti-aliasing).

 if (anti) // Is Anti TRUE?

 {

  glEnable(GL_LINE_SMOOTH); // If So, Enable Antialiasing

 }

To make the game a little easier I've added a special item. The item is an hourglass. When you touch the hourglass, the enemies are frozen for a specific amount of time. The following section of code is resposible for drawing the hourglass.

For the hourglass we use x and y to position the timer, but unlike our player and enemies we don't use fx and fy for fine positioning. Instead we'll use fx to keep track of whether or not the timer is being displayed. fx will equal 0 if the timer is not visible. 1 if it is visible, and 2 if the player has touched the timer. fy will be used as a counter to keep track of how long the timer should be visible or invisible.

So we start off by checking to see if the timer is visible. If not, we skip over the code without drawing the timer. If the timer is visible, we reset the modelview matrix, and position the timer. Because our first grid point from left to right starts at 20, we will add hourglass.x times 60 to 20. We multiply hourglass.x by 60 because the points on our grid from left to right are spaced 60 pixels apart. We then position the hourglass on the y axis. We add hourglass.y times 40 to 70.0f because we want to start drawing 70 pixels down from the top of the screen. Each point on our grid from top to bottom is spaced 40 pixels apart.

After we have positioned the hourglass, we can rotate it on the z-axis. hourglass.spin is used to keep track of the rotation, the same way player.spin keeps track of the player rotation. Before we start to draw the hourglass we select a random color.

 if (hourglass.fx==1) // If fx=1 Draw The Hourglass

 {

  glLoadIdentity(); // Reset The Modelview Matrix

  glTranslatef(20.0f+(hourglass.x*60), 70.0f+(hourglass.y*40),0.0f); // Move To The Fine Hourglass Position

  glRotatef(hourglass.spin, 0.0f, 0.0f, 1.0f); // Rotate Clockwise

  glColor3ub(rand()%255, rand()%255, rand()%255); // Set Hourglass Color To Random Color

glBegin(GL_LINES) tells OpenGL we want to draw using lines. We start off by moving left and up 5 pixels from our current location. This gives us the top left point of our hourglass. OpenGL will start drawing the line from this location. The end of the line will be 5 pixels right and down from our original location. This gives us a line running from the top left to the bottom right. Immediately after that we draw a second line running from the top right to the bottom left. This gives us an 'X'. We finish off by connecting the bottom two points together, and then the top two points to create an hourglass type object :)

  glBegin(GL_LINES); // Start Drawing Our Hourglass Using Lines

   glVertex2d(-5,-5); // Top Left Of Hourglass

   glVertex2d( 5, 5); // Bottom Right Of Hourglass

   glVertex2d( 5,-5); // Top Right Of Hourglass

   glVertex2d(-5, 5); // Bottom Left Of Hourglass

   glVertex2d(-5, 5); // Bottom Left Of Hourglass

   glVertex2d( 5, 5); // Bottom Right Of Hourglass

   glVertex2d(-5,-5); // Top Left Of Hourglass

   glVertex2d( 5,-5); // Top Right Of Hourglass

  glEnd(); // Done Drawing The Hourglass

 }

Now we draw our player. We reset the modelview matrix, and position the player on the screen. Notice we position the player using fx and fy. We want the player to move smoothly so we use fine positioning. After positioning the player, we rotate the player on it's z-axis using player.spin. We set the color to light green and begin drawing. Just like the code we used to draw the hourglass, we draw an 'X'. Starting at the top left to the bottom right, then from the top right to the bottom left.

 glLoadIdentity(); // Reset The Modelview Matrix

 glTranslatef(player.fx+20.0f,player.fy+70.0f,0.0f); // Move To The Fine Player Position

 glRotatef(player.spin,0.0f,0.0f,1.0f); // Rotate Clockwise

 glColor3f(0.0f,1.0f,0.0f); // Set Player Color To Light Green

 glBegin(GL_LINES); // Start Drawing Our Player Using Lines

  glVertex2d(-5,-5); // Top Left Of Player

  glVertex2d( 5, 5); // Bottom Right Of Player

  glVertex2d( 5,-5); // Top Right Of Player

  glVertex2d(-5, 5); // Bottom Left Of Player

 glEnd(); // Done Drawing The Player

Drawing low detail objects with lines can be a little frustrating. I didn't want the player to look boring so I added the next section of code to create a larger and quicker spinning blade on top of the player that we drew above. We rotate on the z-axis by player.spin times 0.5f. Because we are rotating again, it will appear as if this piece of the player is moving a little quicker than the first piece of the player.

After doing the new rotation, we set the color to a darker shade of green. So that it actually looks like the player is made up of different colors / pieces. We then draw a large '+' on top of the first piece of the player. It's larger because we're using –7 and +7 instead of –5 and +5. Also notice that instead of drawing from one corner to another, I'm drawing this piece of the player from left to right and top to bottom.

 glRotatef(player.spin*0.5f,0.0f,0.0f,1.0f); // Rotate Clockwise

 glColor3f(0.0f,0.75f,0.0f); // Set Player Color To Dark Green

 glBegin(GL_LINES); // Start Drawing Our Player Using Lines

  glVertex2d(-7, 0); // Left Center Of Player

  glVertex2d( 7, 0); // Right Center Of Player

  glVertex2d( 0,-7); // Top Center Of Player

  glVertex2d( 0, 7); // Bottom Center Of Player

 glEnd(); // Done Drawing The Player

All we have to do now is draw the enemies, and we're done drawing :) We start off by creating a loop that will loop through all the enemies visible on the current level. We calculate how many enemies to draw by multiplying our current game stage by the games internal level. Remember that each level has 3 stages, and the maximum value of the internal level is 3. So we can have a maximum of 9 enemies.