C++ Assignment 11
Tron

Background: A good game from a bad movie

Tron was a Disney movie that was released in 1982. It was one of the earliest feature films to reflect the video-game craze of the 1980s. Jeff Bridges stars as a computer programmer who becomes part of the very game that he's programming. The game is a "light cycle duel". Your "light cycle" can race at extreme speeds across the arena leaving a trail of laser light behind you. You should try and drive your opponents into a light trail, while avoiding the trails yourself. There is an example of the tron game with one human player and one computer opponent at http://www.bolland.org/lcd.html. There is another example of the tron game with two human players at http://www.geocities.com/ifaybish/javascripttrond.html.

In this assignment you will create a single player version of the tron game. This program is very similar to the previous etch-a-sketch assignment.

"Motorizing" the Etch-a-sketch

The first step in this assignment is to modify your etch-a-sketch so that it continues to make a trail even when you are not pressing a key. To do this, we will use the kbhit() function. It returns true if a key has been pressed. We will use this function to "protect" the call to getch() so that it doesn't stop the program. We will also introduce a delay() so the program doesn't run too quickly. The modifications look like this:

if(kbhit())
    cKey = getch();
delay(20);

Winning (or Losing) the game

The game is over when we cross a light cycle trail. To find out if we are touching the trail, we will use the getpixel() function from winbgim.h. We will also use outtextxy to display a message that the game is over. Make sure to place this call before you use putpixel.

if(getpixel(nX,nY) == WHITE)
          outtextxy(200,340,"Game Over");

You will want the game to end if your trail crosses another trail or goes out of bounds.

Adding a computer opponent

  1. Writing the computer opponent is difficult, so don’t make it any more so by having a big, ugly main function. Once you get the basic tron game working, break it up into functions—put the code that moves the Human player in its own function:
    
    void Border ();
    void HumanMovement();
    int nX = 320, nY = 240;
    char cKey = '4';
    int main ()
    {
       initwindow(640,480);
       Border();
       do
       {
          HumanMovement();
       }while (cKey != 'q');
       getch();
       closegraph();
       return 0;
    }
  2. Now we can concentrate on the computer. We need a function similar to HumanMovement(), let’s call it ComputerMovement(). We’ll use HumanMovement()as a basis for ComputerMovement(). What will be the same? different?
  3. The computer will need its own variables for its position and "Key Press". Except it’s not really a key press, it just stores the direction the computer travels in.
    
    int nCompX = 320, nCompY = 360;
    char cCompKey = 'u';

    To make our life easier, we should pick letters with meaning: 'u' for up, etc.
    
    void ComputerMovement()
    {
        switch (cCompKey)
        {
            case 'l':
               nCompX--;
               break;
            //and so on
        }
        if(getpixel(nCompX,nCompY) != BLACK)
        {
            outtextxy(200,340,"Game Over Computer Loses");
            cKey = 'q';
        }
        putpixel(nCompX, nCompY, YELLOW);
    }
  4. The problem now is that the computer never changes direction, We need to add code to each case statement so that if the computer is about to run into a wall, it will change direction. That means changing the variable cCompKey.
    
    void ComputerMovement()
    {
       switch (cCompKey)
       {
         case 'l':
           nCompX--;
    
           //”Look” to the left
           if(getpixel(nCompX-1,nCompY)!=BLACK)
              cCompKey = 'u';
           break;
    
         //and so on. . .

Extensions

If you have extra time you can add more features to the game to make it more interesting. You can add extra computer opponents or even another human player. You may want to keep score. The link example of outtextxy with an int shows how to display a number to the screen using outtextxy. You may want to speed the game up as it progresses. If you find the delay() function too slow, you might try writing your own DoNothing function. Here's an example:
void DoNothing()
{
    for(int nNum = 0; nNum < nTimes; nNum++);
}
I found that if I set nTimes to 200000 I got a reasonable delay. You could add obstacles or a maze to make the game more challenging. Be creative and have fun, your tron game doesn't have to look or work like any other.