Advertisement
redsees

Untitled

May 5th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.93 KB | None | 0 0
  1. /*
  2.  * pac.cpp
  3.  *
  4.  *  Created on: Feb 3, 2013
  5.  *      Author: kamath
  6.  */
  7. /*
  8.  * pro.c
  9.  *
  10.  *  Created on: Apr 30, 2012
  11.  *      Author: nash
  12.  */
  13.  
  14. #include<ctype.h>
  15. #include<GL/glut.h>
  16. #include<math.h>
  17. #include<stdio.h>
  18. //#define M_PI 3.14159265358979323846264338327950288419716939937510
  19. #define false 0
  20. #define true 1
  21.  
  22. const int BOARD_X = 31;
  23. const int BOARD_Y = 28;
  24.  
  25. int board_array[BOARD_X][BOARD_Y] =
  26.                   {{8,5,5,5,5,5,5,5,5,5,5,5,5,1,1,5,5,5,5,5,5,5,5,5,5,5,5,7},
  27.         {6,0,0,0,0,0,0,0,0,0,0,0,0,2,4,0,0,0,0,0,0,0,0,0,0,0,0,6},
  28.         {6,0,8,1,1,7,0,8,1,1,1,7,0,2,4,0,8,1,1,1,7,0,8,1,1,7,0,6},
  29.         {6,0,2,11,11,4,0,2,11,11,11,4,0,2,4,0,2,11,11,11,4,0,2,11,11,4,0,6},
  30.         {6,0,9,3,3,10, 0,9,3,3,3,10,0,9,10,0,9,3,3,3,10,0,9,3,3,10,0,6},
  31.         {6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6},
  32.         {6,0,8,1,1,7,0,8,7,0,8,1,1,1,1,1,1,7,0,8,7,0,8,1,1,7,0,6},
  33.         {6,0,9,3,3,10,0,2,4,0,9,3,3,11,11,3,3,10,0,2,4,0,9,3,3,10,0,6},
  34.         {6,0,0,0,0,0,0,2,4,0,0,0,0,2,4,0,0,0,0,2,4,0,0,0,0,0,0,6},
  35.         {9,5,5,5,5,7,0,2,11,1,1,7,0,2,4,0,8,1,1,11,4,0,8,5,5,5,5,10},
  36.         {0,0,0,0,0,6,0,2,11,3,3,10,0,9,10,0,9,3,3,11,4,0,6,0,0,0,0,0},
  37.         {0,0,0,0,0,6,0,2,4,0,0,0,0,0,0,0,0,0,0,2,4,0,6,0,0,0,0,0},
  38.         {0,0,0,0,0,6,0,2,4,0,8,5,5,1,1,5,5,7,0,2,4,0,6,0,0,0,0,0},
  39.         {5,5,5,5,5,10,0,9,10,0,6,0,0,0,0,0,0,6,0,9,10,0,9,5,5,5,5,5},
  40.         {0,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0},
  41.         {5,5,5,5,5,7,0,8,7,0,6,0,0,0,0,0,0,6,0,8,7,0,8,5,5,5,5,5},
  42.         {0,0,0,0,0,6,0,2,4,0,9,5,5,5,5,5,5,10,0,2,4,0,6,0,0,0,0,0},
  43.         {0,0,0,0,0,6,0,2,4,0,0,0,0,0,0,0,0,0,0,2,4,0,6,0,0,0,0,0},
  44.         {0,0,0,0,0,6,0,2,4,0,8,1,1,1,1,1,1,7,0,2,4,0,6,0,0,0,0,0},
  45.         {8,5,5,5,5,10,0,9,10,0,9,3,3,11,11,3,3,10,0,9,10,0,9,5,5,5,5,7},
  46.         {6,0,0,0,0,0,0,0,0,0,0,0,0,2,4,0,0,0,0,0,0,0,0,0,0,0,0,6},
  47.         {6,0,8,1,1,7,0,8,1,1,1,7,0,2,4,0,8,1,1,1,7,0,8,1,1,7,0,6},
  48.         {6,0,9,3,11,4,0,9,3,3,3,10,0,9,10,0,9,3,3,3,10,0,2,11,3,10,0,6},
  49.         {6,0,0,0,2,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,4,0,0,0,6},
  50.         {2,1,7,0,2,4,0,8,7,0,8,1,1,1,1,1,1,7,0,8,7,0,2,4,0,8,1,4},
  51.                              {2,3,10,0,9,10,0,2,4,0,9,3,3,11,11,3,3,10,0,2,4,0,9,10,0,9,3,4},
  52.         {6,0,0,0,0,0,0,2,4,0,0,0,0,2,4,0,0,0,0,2,4,0,0,0,0,0,0,6},
  53.         {6,0,8,1,1,1,1,11,11,1,1,7,0,2,4,0,8,1,1,11,11,1,1,1,1,7,0,6},
  54.         {6,0,9,3,3,3,3,3,3,3,3,10,0,9,10,0,9,3,3,3,3,3,3,3,3,10,0,6},
  55.         {6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6},
  56.         {9,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,10}};
  57.  
  58.  
  59. int pebble_array[BOARD_X][BOARD_Y] =
  60.        {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  61.         {0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0},
  62.         {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
  63.         {0,3,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,3,0},
  64.         {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
  65.         {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
  66.                   {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
  67.  
  68.  
  69.         {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
  70.         {0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0},
  71.                   {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
  72.         {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
  73.         {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
  74.         {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
  75.         {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
  76.         {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
  77.         {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
  78.         {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
  79.         {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
  80.         {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
  81.         {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
  82.         {0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0},
  83.         {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
  84.         {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
  85.         {0,3,1,1,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,1,1,3,0},
  86.         {0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0},
  87.         {0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0},
  88.         {0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0},
  89.         {0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
  90.         {0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
  91.         {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
  92.                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
  93.  
  94.  
  95. GLubyte list[5];
  96.  
  97. int tp_array[31][28];
  98.  
  99. int pebbles_left;
  100.  
  101. double speed1 = 0.1;
  102.  
  103. double angle1 = 90;
  104.  
  105. double a=13.5, b=23;
  106.  
  107. bool animate = false;
  108.  
  109. int lives=3;
  110.  
  111. int points=0;
  112.  
  113.  
  114. void keys();
  115. unsigned char ckey='w';
  116. void mykey(unsigned char key,int x,int y);
  117. bool Open(int a,int b);
  118. void Move()
  119. {
  120.     a +=  speed1*cos(M_PI/180*angle1);
  121.     b +=  speed1*sin(M_PI/180*angle1);
  122.     if(animate&&ckey==GLUT_KEY_UP&& (int) a - a > -0.1 && angle1 != 270)      //w
  123.         {
  124.             if (Open(a,b-1))
  125.             {
  126.                 animate = true;
  127.  
  128.  
  129.                           angle1 = 270;
  130.                        }
  131.                     }
  132.  
  133.         else if(animate&&ckey==GLUT_KEY_DOWN&& (int) a - a > -0.1 && angle1 != 90)// s
  134.         {
  135.             if (Open(a,b+1))
  136.             {
  137.                 animate = true;
  138.                 angle1= 90;
  139.             }
  140.         }
  141.  
  142.         else if(animate&&ckey==GLUT_KEY_LEFT&& (int) b - b > -0.1 && angle1 != 180)//a
  143.         {
  144.             if (Open(a-1,b))
  145.             {
  146.                 animate = true;
  147.                 angle1 = 180;
  148.             }
  149.         }
  150.  
  151.         else if(animate&&ckey==GLUT_KEY_RIGHT&& (int) b - b > -0.1 && angle1 != 0)//d
  152.         {
  153.             if (Open(a+1,b))
  154.             {
  155.                 animate = true;
  156.                 angle1 = 0;
  157.             }
  158.         }
  159. }
  160.  
  161.  
  162. void Pac(void)
  163. {
  164.     //Draw Pacman
  165.     glColor3f(0,1,1);
  166.     glPushMatrix();
  167.  
  168.               glTranslatef(a,-b,0);
  169.     glTranslatef(0.5,0.6,0);
  170.               glTranslatef((float)BOARD_X/-2.0f,(float)BOARD_Y/2.0f,0.5);
  171.     glutSolidSphere(0.5,15,10);
  172.     glPopMatrix();
  173. }
  174.  
  175.  
  176. //Monster Drawing And Moving Begins
  177.  
  178. bool open_move[4];
  179.  
  180. bool gameover = false;
  181.  
  182. int num_ghosts = 4;
  183.  
  184. int start_timer=3;
  185.  
  186.  
  187.  
  188. class Ghost
  189. {
  190.    private:
  191.  
  192.   public:
  193.          bool edible;
  194.          int edible_max_time;
  195.          int edible_timer;
  196.          bool eaten;
  197.          bool transporting;
  198.          float color[3];
  199.          double speed;
  200.          double max_speed;
  201.          bool in_jail;
  202.          int jail_timer;
  203.          double angle;
  204.          double x, y;
  205.  
  206.          Ghost(double, double);
  207.  
  208.  
  209.                 ~Ghost(void);
  210.  
  211.          void Move(); //Move the Monster
  212.  
  213.          void Update(void);  //Update Monster State
  214.  
  215.          void Chase(double, double, bool*);  //Chase Pacman
  216.  
  217.          bool Catch(double, double);    //collision detection
  218.  
  219.          void Reinit(void);
  220.  
  221.          void Vulnerable(void);
  222.  
  223.          void Draw(void);   //Draw the Monster
  224.          void game_over(void);
  225.  
  226. };
  227.  
  228. Ghost *ghost[4];
  229.  
  230. Ghost::~Ghost(void){}
  231.  
  232. Ghost::Ghost(double tx, double ty)
  233. {
  234.     tx = x;
  235.     ty = y;
  236.     angle = 90;
  237.     speed = max_speed=1;
  238.     color[0] = 1;
  239.     color[1] = 0;
  240.     color[2] = 0;
  241.     eaten = false;
  242.     edible_max_time =300;
  243.     edible = false;
  244.     in_jail = true;
  245.  
  246.  
  247.     jail_timer = 30;
  248. }
  249.  
  250. void Ghost::Reinit(void)
  251. {
  252.     edible = false;
  253.     in_jail = true;
  254.     angle = 90;
  255. }
  256.  
  257. //Move Monster
  258. void Ghost::Move()
  259. {
  260.     x +=  speed*cos(M_PI/180*angle);
  261.     y +=  speed*sin(M_PI/180*angle);
  262. }
  263. void Ghost::game_over()
  264. {
  265.  
  266. }
  267.  
  268. void Ghost::Update(void)
  269. {
  270.  
  271.     if ((int)x == 0 && (int) y == 14 && (!(transporting)))
  272.     {
  273.         angle=180;
  274.     }
  275.  
  276.     if (x < 0.1 && (int) y == 14)
  277.     {
  278.         x = 26.9;
  279.         transporting = true;
  280.     }
  281.  
  282.     if ((int)x == 27 && (int) y == 14 && (!(transporting)))
  283.     {
  284.         angle=0;
  285.     }
  286.  
  287.     if (x > 26.9 && (int) y == 14)
  288.  
  289.     {
  290.         x = 0.1;
  291.         transporting = true;
  292.     }
  293.  
  294.     if ((int)x == 2 || (int)x == 25)
  295.         transporting = false;
  296.  
  297.     if (((int) x < 5 || (int) x > 21) && (int) y == 14 && !edible && !eaten)
  298.         speed = max_speed/2;
  299.         speed = max_speed;
  300.  
  301.     //edibility
  302.     if (edible_timer == 0 && edible && !eaten)
  303.     {
  304.  
  305.  
  306.         edible = false;
  307.         speed = max_speed;
  308.     }
  309.                 if (edible)
  310.         edible_timer--;
  311.  
  312.     //JAIL
  313.     if (in_jail && (int) (y+0.9) == 11)
  314.     {
  315.         in_jail = false;
  316.         angle = 180;
  317.     }
  318.  
  319.     if (in_jail && ((int)x == 13 || (int)x == 14))
  320.     {
  321.         angle = 270;
  322.     }
  323.  
  324.     //if time in jail is up, position for exit
  325.     if (jail_timer == 0  && in_jail)
  326.  
  327.     {
  328.         //move right to exit
  329.         if (x < 13)
  330.             angle = 0;
  331.         if (x > 14)
  332.             angle = 180;
  333.     }
  334.  
  335.     //decrement time in jail counter
  336.     if (jail_timer > 0)
  337.         jail_timer--;
  338.  
  339.     //EATEN GHOST SEND TO JAIL
  340.     if (eaten && ((int) x == 13 || (int) (x+0.9) == 14) && ((int)y > 10 && (int) y < 15))
  341.     {
  342.         in_jail = true;
  343.         angle = 90;
  344.         if((int) y == 14)
  345.         {
  346.             eaten = false;
  347.             speed = max_speed;
  348.             jail_timer = 66;
  349.             x = 11;
  350.         }
  351.     }
  352. }
  353.  
  354.  
  355. bool Ghost::Catch(double px, double py)
  356. {
  357.     // Collision Detection
  358.     if (px - x < 0.2 && px - x > -0.2 && py - y < 0.2 && py - y > -0.2)
  359.     {
  360.         return true;
  361.     }
  362.     return false;
  363.  
  364.  
  365. }
  366.  
  367. //called when pacman eats a super pebble
  368.  
  369. void Ghost::Vulnerable(void)
  370. {
  371.     if (!(edible))
  372.     {
  373.         angle = ((int)angle + 180)%360;
  374.         speed = max_speed;
  375.     }
  376.     edible = true;
  377.     edible_timer = edible_max_time;
  378.     //speed1=0.15;
  379. }
  380.  
  381.  
  382.  
  383. void Ghost::Chase(double px, double py, bool *open_move)
  384. {
  385.               int c;
  386.     if (edible)
  387.         c = -1;
  388.     else
  389.         c = 1;
  390.  
  391.     bool moved = false;
  392.  
  393.     if ((int) angle == 0 || (int) angle == 180)
  394.     {
  395.         if ((int)c*py > (int)c*y && open_move[1])
  396.             angle = 90;
  397.  
  398.         else if ((int)c*py < (int)c*y && open_move[3])
  399.             angle = 270;
  400.     }
  401.  
  402.     else if ((int) angle == 90 || (int) angle == 270)
  403.     {
  404.  
  405.         if ((int)c*px > (int)c*x && open_move[0])
  406.             angle = 0;
  407.         else if ((int)c*px < (int)c*x && open_move[2])
  408.             angle = 180;
  409.     }
  410.  
  411.     //Random Moves Of Monsters
  412.  
  413.     if ((int) angle == 0 && !open_move[0])
  414.         angle = 90;
  415.  
  416.     if ((int) angle == 90 && !open_move[1])
  417.         angle = 180;
  418.  
  419.     if ((int) angle == 180 && !open_move[2])
  420.         angle = 270;
  421.  
  422.  
  423.  
  424.                if ((int) angle == 270 && !open_move[3])
  425.         angle = 0;
  426.  
  427.     if ((int) angle == 0 && !open_move[0])
  428.         angle = 90;
  429.  
  430.  
  431. }
  432.  
  433. void Ghost::Draw(void)
  434. {
  435.  
  436.     if (!edible)
  437.         glColor3f(color[0],color[1],color[2]);
  438.  
  439.     else
  440.     {
  441.         if (edible_timer < 150)
  442.             glColor3f((edible_timer/10)%2,(edible_timer/10)%2,1);
  443.         if (edible_timer >= 150)
  444.             glColor3f(0,0,1);
  445.  
  446.     }
  447.  
  448.     if (eaten)
  449.         glColor3f(1,1,0); //When Eaten By PacMan Change Color To Yellow
  450.  
  451.     glPushMatrix();
  452.     glTranslatef(x,-y,0);
  453.     glTranslatef(0.5,0.6,0);
  454.     glTranslatef((float)BOARD_X/-2.0f, (float)BOARD_Y/2.0f,0.5);
  455.     glutSolidSphere(.5,10,10);
  456.     glPopMatrix();
  457.  
  458. }
  459.  
  460.  
  461. void tp_restore(void)
  462. {
  463.     for (int ISO = 0; ISO < BOARD_X; ISO++)
  464.     {
  465.         for (int j = 0; j < BOARD_Y; j++)
  466.         {
  467.             tp_array[ISO][j] = pebble_array[ISO][j];
  468.         }
  469.     }
  470.     pebbles_left = 244;
  471. }
  472.  
  473.  
  474. void Draw(void)
  475. {
  476.  
  477.     glColor3f(1,0,1);
  478.  
  479.     //split board drawing in half to avoid issues with depth
  480.     for (int ISO = 0; ISO < BOARD_X; ISO++)
  481.  
  482.  
  483.     {
  484.         for (int j = 0; j < BOARD_Y/2; j++)
  485.         {
  486.  
  487.             glColor3f(0,0,1);
  488.             int call_this = 0;
  489.  
  490.             glPushMatrix();
  491.             glTranslatef(-(float) BOARD_X / 2.0f,-(float) BOARD_Y / 2.0f, 0);
  492.  
  493.  
  494.                                            glTranslatef(j, BOARD_Y - ISO,0);
  495.  
  496.             glPushMatrix();
  497.             glTranslatef(0.5,0.5,0);
  498.  
  499.             switch (board_array[ISO][j])
  500.             {
  501.             case 4:
  502.                 glRotatef(90.0,0,0,1);
  503.             case 3:
  504.                 glRotatef(90.0,0,0,1);
  505.             case 2:
  506.                 glRotatef(90.0,0,0,1);
  507.             case 1:
  508.                 call_this = 1;
  509.                 break;
  510.             case 6:
  511.                 glRotatef(90.0,0,0,1);
  512.             case 5:
  513.                 call_this = 2;
  514.                 break;
  515.             case 10:
  516.                 glRotatef(90.0,0,0,1);
  517.             case 9:
  518.                 glRotatef(90.0,0,0,1);
  519.             case 8:
  520.                 glRotatef(90.0,0,0,1);
  521.             case 7:
  522.                 call_this = 3;
  523.                 break;
  524.             }
  525.  
  526.             glScalef(1,1,0.5);
  527.             glTranslatef(-0.5,-0.5,0);
  528.             glCallList(list[call_this]);
  529.             glPopMatrix();
  530.             //now put on the top of the cell
  531.             if (call_this != 0 || board_array[ISO][j] == 11)
  532.             {
  533.                 glTranslatef(0,0,-0.5);
  534.                 glCallList(list[4]);
  535.             }
  536.             glPopMatrix();
  537.  
  538.             if (tp_array[ISO][j] > 0)
  539.             {
  540.  
  541.  
  542.                             glColor3f(0,300,1/(float)tp_array[ISO][j]);
  543.                 glPushMatrix();
  544.                 glTranslatef(-(float) BOARD_X / 2.0f,-(float) BOARD_Y / 2.0f, 0);
  545.                 glTranslatef(j, BOARD_Y - ISO,0);
  546.                 glTranslatef(0.5,0.5,0.5);
  547.                 glutSolidSphere(0.1f*((float)tp_array[ISO][j]),6,6);
  548.                 glPopMatrix();
  549.             }
  550.         }
  551.     }
  552.  
  553.  
  554.               int ISO;
  555.  
  556.     for (ISO= 0; ISO< BOARD_X; ISO++)
  557.     {
  558.         for (int j = BOARD_Y-1; j >= BOARD_Y/2; j--)
  559.         {
  560.                           glColor3f(0,0,1);
  561.             int call_this = 0;
  562.  
  563.             glPushMatrix();
  564.  
  565.             glTranslatef(-(float) BOARD_X / 2.0f,-(float) BOARD_Y / 2.0f, 0);
  566.             glTranslatef(j, BOARD_Y - ISO,0);
  567.  
  568.             glPushMatrix();
  569.             glTranslatef(0.5,0.5,0);
  570.             switch (board_array[ISO][j])
  571.             {
  572.             case 4:
  573.                 glRotatef(90.0,0,0,1);
  574.             case 3:
  575.                 glRotatef(90.0,0,0,1);
  576.             case 2:
  577.                 glRotatef(90.0,0,0,1);
  578.             case 1:
  579.                 call_this = 1;
  580.                 break;
  581.             case 6:
  582.                 glRotatef(90.0,0,0,1);
  583.             case 5:
  584.                 call_this = 2;
  585.                 break;
  586.             case 10:
  587.                 glRotatef(90.0,0,0,1);
  588.             case 9:
  589.                 glRotatef(90.0,0,0,1);
  590.             case 8:
  591.                 glRotatef(90.0,0,0,1);
  592.             case 7:
  593.                 call_this = 3;
  594.                 break;
  595.             }
  596.             glScalef(1,1,0.5);
  597.             glTranslatef(-0.5,-0.5,0);
  598.             glCallList(list[call_this]);
  599.  
  600.  
  601.                                             glPopMatrix();
  602.             //now put on top
  603.             if (call_this != 0 || board_array[ISO][j] == 11)
  604.             {
  605.                 glTranslatef(0,0,-0.5);
  606.                 glCallList(list[4]);
  607.             }
  608.             glPopMatrix();
  609.  
  610.             if (tp_array[ISO][j] > 0)
  611.             {
  612.                 glColor3f(0,300,1/(float)tp_array[ISO][j]);
  613.                 glPushMatrix();
  614.  
  615.                                                glTranslatef(-(float) BOARD_X / 2.0f,-(float) BOARD_Y / 2.0f, 0);
  616.                 glTranslatef(j, BOARD_Y - ISO,0);
  617.                 glTranslatef(0.5,0.5,0.5);
  618.                 glutSolidSphere(0.1f*((float)tp_array[ISO][j]),6,6);
  619.                 glPopMatrix();
  620.             }
  621.         }
  622.     }
  623.                Pac();
  624. }
  625.  
  626.  
  627. bool Open(int a, int b)
  628. {
  629.     if (board_array[b][a] > 0)
  630.     {
  631.         return false;
  632.     }
  633.     return true;
  634. }
  635.  
  636.  
  637. void RenderScene();
  638.  
  639.  
  640. void mykey(unsigned char key,int x,int y)
  641. {
  642.  
  643.     if (start_timer > 0)
  644.     {
  645.         start_timer--;
  646.  
  647.     }
  648. }
  649. void specialDown(int key,int x,int y)
  650. {
  651.     if (start_timer > 0)
  652.             start_timer--;
  653.     ckey=key;
  654.         if(key==GLUT_KEY_UP&& (int) a - a > -0.1 && angle1 != 270)      //w
  655.         {
  656.             if (Open(a, b - 1))
  657.             {
  658.  
  659.  
  660.                 animate = true;
  661.                 angle1 = 270;
  662.  
  663.               }
  664.             }
  665.  
  666.         else if(key==GLUT_KEY_DOWN&& (int) a - a > -0.1 && angle1 != 90)// s
  667.         {
  668.             if (Open(a,b + 1))
  669.             {
  670.                 animate = true;
  671.                 angle1= 90;
  672.             }
  673.         }
  674.  
  675.  
  676.  
  677.                            else if(key==GLUT_KEY_LEFT&& (int) b - b > -0.1 && angle1 != 180)//a
  678.         {
  679.             if (Open(a-1,b))
  680.             {
  681.                 animate = true;
  682.                 angle1 = 180;
  683.  
  684.             }
  685.         }
  686.  
  687.         else if(key==GLUT_KEY_RIGHT&& (int) b - b > -0.1 && angle1 != 0)//d
  688.         {
  689.             if (Open(a+1, b))
  690.             {
  691.                 animate = true;
  692.                 angle1 = 0;
  693.             }
  694.         }
  695. }
  696.  
  697. void specialUp(int key,int x,int y)
  698. {
  699.  
  700. }
  701.  
  702. void P_Reinit()
  703. {
  704.     a = 13.5;
  705.     b = 23;
  706.     angle1 = 90;
  707.     animate = false;
  708.     Pac();
  709. }
  710.  
  711.  
  712. void G_Reinit(void)
  713. {
  714.  
  715.     start_timer = 3;
  716.  
  717.  
  718.  
  719.     //ghost initial starting positions
  720.     int start_x[4] = {11,12,15,16};
  721.     float ghost_colors[4][3] = {{255,0,0},{120,240,120},{255,200,200},{255,125,0}};
  722.  
  723.     for (int i = 0; i < num_ghosts; i++)
  724.     {
  725.  
  726.         ghost[i]->Reinit();
  727.         ghost[i]->x = start_x[i];
  728.         ghost[i]->y = 14;
  729.         ghost[i]->eaten = false;
  730.         ghost[i]->jail_timer = i*33 + 66;
  731.         ghost[i]->max_speed = 0.1 - 0.01*(float)i;
  732.         ghost[i]->speed = ghost[i]->max_speed;
  733.  
  734.         //colorize ghosts
  735.         for (int j = 0; j < 3; j++)
  736.             ghost[i]->color[j] = ghost_colors[i][j]/255.0f;
  737.     }
  738. }
  739.  
  740.  
  741.  
  742. void renderBitmapString(float x, float y, void *font, char *string)
  743. {
  744.   char *c;
  745.   glRasterPos2f(x,y);
  746.  
  747.   for (c=string; *c != '\0'; c++)
  748.       {
  749.     glutBitmapCharacter(font, *c);
  750.   }
  751. }
  752.  
  753.  
  754. void Write(char *string)
  755. {
  756.      while(*string)
  757.                glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *string++);
  758. }
  759.  
  760. void print(char *string)
  761. {
  762.                while(*string)
  763.             glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *string++);
  764. }
  765.  
  766.  
  767. //Display Function->This Function Is Registered in glutDisplayFunc
  768.  
  769. void RenderScene()
  770. {
  771.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  772.     //Through Movement->From One End To The Other
  773.  
  774.     if ((int)a == 27 && (int) b == 14 && angle1 == 0)
  775.     {
  776.  
  777.  
  778. a = 0;
  779.         animate = true;
  780.     }
  781.  
  782.     else
  783.     if ((int)(a + 0.9) == 0 && (int) b == 14 && angle1 == 180)
  784.     {
  785.         a = 27;
  786.         animate = true;
  787.     }
  788.  
  789.     //Collision Detection For PacMan
  790.     if (animate)
  791.         Move();
  792.  
  793.     if(!(Open((int)(a + cos(M_PI/180*angle1)),
  794.         (int)(b + sin(M_PI/180*angle1)))) &&
  795.         a - (int)a < 0.1 && b - (int)b < 0.1)
  796.  
  797.                 animate = false;
  798.  
  799.  
  800.     if (tp_array[(int)(b+0.5)][(int)(a+0.5)]== 1)
  801.     {
  802.         tp_array[(int)(b+0.5)][(int)(a+0.5)]= 0;
  803.         pebbles_left--;
  804.         points+=1;
  805.     }
  806.  
  807.  
  808.     //Super Pebble Eating
  809.     else if(tp_array[(int)(b+0.5)][(int)(a+0.5)] == 3)
  810.     {
  811.         tp_array[(int)(b+0.5)][(int)(a+0.5)]= 0;
  812.         pebbles_left--;
  813.         points+=5;
  814.  
  815.         for (int i = 0; i < 4; i++)
  816.         {
  817.             if (!ghost[i]->eaten)
  818.                 ghost[i]->Vulnerable(); //Calls A Function To Make Monster Weak
  819.         }
  820.     }
  821.  
  822.     //All The Pebbles Have Been Eaten
  823.     if (pebbles_left == 0)
  824.     {
  825.         G_Reinit();
  826.         P_Reinit();
  827.         tp_restore();
  828.         points=0;
  829.         lives=3;
  830.     }
  831.  
  832.  
  833.     if (!gameover)
  834.         Draw();
  835.  
  836.  
  837.     for (int d = 0; d < num_ghosts; d++)
  838.     {
  839.  
  840.         if (!gameover && start_timer == 0)
  841.             ghost[d]->Update();
  842.  
  843.         if (!ghost[d]->in_jail &&
  844.             ghost[d]->x - (int)ghost[d]->x < 0.1 && ghost[d]->y - (int)ghost[d]->y < 0.1)
  845.         {
  846.  
  847.             bool open_move[4];
  848.  
  849.             //Finding Moves
  850.             for (int ang = 0; ang < 4; ang++)
  851.             {
  852.                 open_move[ang] = Open((int)(ghost[d]->x + cos(M_PI/180*ang*90)),
  853.                     (int)(ghost[d]->y + sin(M_PI/180*ang*90)));
  854.  
  855.  
  856.             }
  857.  
  858.             //Chase Pac Man
  859.             if (!ghost[d]->eaten)
  860.             {
  861.             if(ghost[d]->x - (int)ghost[d]->x < 0.1 && ghost[d]->y - (int)ghost[d]->y < 0.1)
  862.                     ghost[d]->Chase(a, b, open_move);
  863.             }
  864.  
  865.             else
  866.             {
  867.                 if(ghost[d]->x - (int)ghost[d]->x < 0.1 && ghost[d]->y - (int)ghost[d]->y < 0.1)
  868.                     ghost[d]->Chase(13, 11, open_move);
  869.             }
  870.         }
  871.  
  872.  
  873.         if (ghost[d]->in_jail && !(Open((int)(ghost[d]->x + cos(M_PI/180*ghost[d]->angle)),
  874.             (int)(ghost[d]->y + sin(M_PI/180*ghost[d]->angle)))) && ghost[d]->jail_timer > 0   &&ghost[d]->x - (int)ghost[d]->x < 0.1 && ghost[d]->y - (int)ghost[d]->y < 0.1)
  875.                         {
  876.                  ghost[d]->angle = (double)(((int)ghost[d]->angle + 180)%360);
  877.                                      }
  878.  
  879.  
  880.         if (!gameover && start_timer == 0)
  881.             ghost[d]->Move();
  882.             ghost[d]->Draw();
  883.  
  884.         if(!(ghost[d]->eaten))
  885.         {
  886.             bool collide = ghost[d]->Catch(a,b);
  887.  
  888.             //Monster Eats PacMan
  889.             if(lives>0)
  890.             {
  891.             if (collide && !(ghost[d]->edible))
  892.             {
  893.                 lives--;
  894.  
  895.  
  896.                 if (lives == 0)
  897.                 {
  898.                     gameover = true;
  899.                     lives=0;
  900.                     ghost[d]->game_over();
  901.                 }
  902.  
  903.                 P_Reinit();
  904.                 d = 4;
  905.             }
  906.             }
  907.  
  908.             //PacMan Eats Monster And Sends It To Jail
  909.             else if (collide && ((ghost[d]->edible)))
  910.             {
  911.  
  912.                 ghost[d]->edible = false;
  913.  
  914.  
  915.                                                           ghost[d]->eaten = true;
  916.                 ghost[d]->speed = 1;
  917.             }
  918.         }
  919.     }
  920.  
  921.     if(gameover==true)
  922.     {
  923.         glColor3f(1,0,0);
  924.         renderBitmapString(-5, 0.5,GLUT_BITMAP_HELVETICA_18 ,"GAME OVER");
  925.     }
  926.  
  927.     char tmp_str[40];
  928.  
  929.     glColor3f(1, 1, 0);
  930.     glRasterPos2f(10, 18);
  931.     sprintf(tmp_str, "Points: %d", points);
  932.     Write(tmp_str);
  933.  
  934.     glColor3f(1, 0, 0);
  935.     glRasterPos2f(-5, 18);
  936.     sprintf(tmp_str, "PAC MAN");
  937.     print(tmp_str);
  938.  
  939.     glColor3f(1, 1, 0);
  940.     glRasterPos2f(-12, 18);
  941.     sprintf(tmp_str, "Lives: %d", lives);
  942.     Write(tmp_str);
  943.  
  944.     glutPostRedisplay();
  945.     glutSwapBuffers();
  946. }
  947.  
  948.  
  949.  
  950. void create_list_lib()
  951. {
  952.     //Set Up Maze Using Lists
  953.     list[1] = glGenLists(1);
  954.  
  955.  
  956.              glNewList(list[1], GL_COMPILE);
  957.  
  958.     //North Wall
  959.     glBegin(GL_QUADS);
  960.     glColor3f(0,0,1);
  961.     glNormal3f(0.0, 1.0, 0.0);
  962.         glVertex3f(1.0, 1.0, 1.0);
  963.         glVertex3f(1.0, 1.0, 0.0);
  964.         glVertex3f(0.0, 1.0, 0.0);
  965.         glVertex3f(0.0, 1.0, 1.0);
  966.     glEnd();
  967.  
  968.     glEndList();
  969.  
  970.     list[2] = glGenLists(1);
  971.     glNewList(list[2], GL_COMPILE);
  972.  
  973.  
  974.  
  975.        glBegin(GL_QUADS);
  976.     //North Wall
  977.     glColor3f(0,0,1);
  978.     glNormal3f(0.0, 1.0, 0.0);
  979.         glVertex3f(1.0, 1.0, 1.0);
  980.         glVertex3f(1.0, 1.0, 0.0);
  981.         glVertex3f(0.0, 1.0, 0.0);
  982.         glVertex3f(0.0, 1.0, 1.0);
  983.         //South Wall
  984.         glColor3f(0,0,1);
  985.         glNormal3f(0.0, -1.0, 0.0);
  986.         glVertex3f(1.0, 0.0, 0.0);
  987.         glVertex3f(1.0, 0.0, 1.0);
  988.         glVertex3f(0.0, 0.0, 1.0);
  989.         glVertex3f(0.0, 0.0, 0.0);
  990.     glEnd();
  991.     glEndList();
  992.  
  993.     list[3] = glGenLists(1);
  994.  
  995.     glNewList(list[3], GL_COMPILE);
  996.     glBegin(GL_QUADS);
  997.     //North Wall
  998.     glColor3f(0,0,1);
  999.     glNormal3f(0.0f, 1.0f, 0.0f);
  1000.         glVertex3f(1.0, 1.0, 1.0);
  1001.         glVertex3f(1.0, 1.0, 0.0);
  1002.         glVertex3f(0.0, 1.0, 0.0);
  1003.         glVertex3f(0.0, 1.0, 1.0);
  1004.         //East Wall
  1005.         glColor3f(0,0,1);
  1006.         glNormal3f(1.0, 0.0, 0.0);
  1007.         glVertex3f(1.0, 1.0, 0.0);
  1008.         glVertex3f(1.0, 1.0, 1.0);
  1009.         glVertex3f(1.0, 0.0, 1.0);
  1010.         glVertex3f(1.0, 0.0, 0.0);
  1011.     glEnd();
  1012.     glEndList();
  1013.  
  1014.  
  1015.     list[4] = glGenLists(1);
  1016.  
  1017.     glNewList(list[4], GL_COMPILE);
  1018.     glBegin(GL_QUADS);
  1019.     //Top Wall
  1020.     glColor3f(-1,0.3,0);
  1021.     glNormal3f(1.0, 0.0, 1.0);
  1022.         glVertex3f(1, 1, 1.0);
  1023.         glVertex3f(0, 1, 1.0);
  1024.         glVertex3f(0, 0, 1.0);
  1025.         glVertex3f(1, 0, 1.0);
  1026.     glEnd();
  1027.     glEndList();
  1028. }
  1029.  
  1030.  
  1031. void init()
  1032. {
  1033.  
  1034.  
  1035.              /*   float   color[4];
  1036.     Enable Lighting.
  1037.     glEnable(GL_LIGHT0);
  1038.     glEnable(GL_LIGHTING);
  1039.  
  1040.     Ambient And Diffuse Lighting
  1041.     glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  1042.     glEnable(GL_COLOR_MATERIAL);
  1043.  
  1044.     color[0] = 1.0f; color[1] = 1.0f; color[2] = 0.0f; color[3] = 0.0f;
  1045.     glLightfv(GL_LIGHT0, GL_DIFFUSE, color);
  1046.  
  1047.     color[0] = 1.0f; color[1] = 0.0f; color[2] = 1.0f; color[3] = 1.0f;
  1048.     glLightfv(GL_LIGHT0, GL_AMBIENT, color);*/
  1049.  
  1050.  
  1051.     glEnable(GL_NORMALIZE);
  1052.  
  1053.     glMatrixMode(GL_PROJECTION);
  1054.     glLoadIdentity();
  1055.  
  1056.     gluPerspective(60,1.33,0.005,100);
  1057.  
  1058.     glMatrixMode(GL_MODELVIEW);
  1059.     glLoadIdentity();
  1060.     gluLookAt(-1.5, 0, 40, -1.5, 0, 0, 0.0f,1.0f,0.0f);
  1061. }
  1062.  
  1063. void erase()
  1064. {
  1065.     glColor3f(0.1,0.0,0.0);
  1066.     glBegin(GL_POLYGON);
  1067.     glVertex2f(0,0);
  1068.     glVertex2f(0.5,0);
  1069.     glVertex2f(0.25,0.5);
  1070.     glEnd();
  1071. }
  1072.  
  1073.  
  1074. int main(int argc,char **argv)
  1075. {
  1076.     glutInit(&argc,argv);
  1077.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
  1078.     glutInitWindowSize(1200, 780);
  1079.     glutInitWindowPosition(0,0);
  1080.     glutCreateWindow("Pac GL 3D");
  1081.  
  1082.     init();
  1083.  
  1084.     glutDisplayFunc(RenderScene);
  1085.  
  1086.     create_list_lib();
  1087.  
  1088.     glutKeyboardFunc(mykey);
  1089.  
  1090.     glutSpecialFunc(specialDown);
  1091.     glutSpecialUpFunc(specialUp);
  1092.  
  1093.  
  1094.  
  1095.               glEnable(GL_DEPTH_TEST);
  1096.  
  1097.     int start_x[4] = {11,12,15,16};
  1098.  
  1099.     for (int ISO = 0; ISO < num_ghosts; ISO++)
  1100.     {
  1101.         ghost[ISO] = new Ghost(start_x[ISO],14);
  1102.     }
  1103.  
  1104.     float ghost_colors[4][3] = {{255,0,0},{120,240,120},{255,200,200},{255,125,0}};
  1105.     int ISO;
  1106.  
  1107.     for (ISO = 0; ISO < num_ghosts; ISO++)
  1108.     {
  1109.         ghost[ISO]->x = start_x[ISO];
  1110.         ghost[ISO]->y = 14;
  1111.         ghost[ISO]->eaten = false;
  1112.         ghost[ISO]->max_speed = 0.1 - 0.01*(float)ISO;
  1113.         ghost[ISO]->speed = ghost[ISO]->max_speed;
  1114.  
  1115.         //colorize ghosts
  1116.         for (int j = 0; j < 3; j++)
  1117.             ghost[ISO]->color[j] = ghost_colors[ISO][j]/255.0f;
  1118.  
  1119.  
  1120.     }
  1121.  
  1122.     for ( ISO = 0; ISO < BOARD_X; ISO++)
  1123.               {
  1124.             for (int j = 0; j < BOARD_Y; j++)
  1125.                 {
  1126.                  tp_array[ISO][j] = pebble_array[ISO][j];
  1127.  
  1128.                  }
  1129.               }
  1130.  
  1131.  
  1132.                pebbles_left = 244;
  1133.     glShadeModel(GL_SMOOTH);
  1134.     glutMainLoop();
  1135.     return 0;
  1136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement