Advertisement
apl-mhd

LabFinalV1

Jan 3rd, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.46 KB | None | 0 0
  1. #include "iGraphics.h"
  2.  
  3. //Variables for the Bricks
  4. int A_x = 150, A_y = 310;                                   //1st layer of Brick
  5. int B1_x = 60, B1_y = 230, B2_x = 230, B2_y = 230;      //2nd layer of Bricks
  6. int C1_x = 100, C1_y = 150, C2_x = 270, C2_y = 150;     //3rd layer of Bricks
  7. int D_x = 150, D_y = 80;                                //4th layer of Brick
  8. int E1_x = 60, E1_y = 15, E2_x = 230, E2_y = 15;        //5th layer of Bricks
  9.  
  10. int HomePageFlag = -1; //homepage
  11. int score=0; // score variable
  12. char scoreText[100]; // convert int to text
  13.  
  14.  
  15.  
  16.  
  17. /*****+++++++++Home page UI setion++++++++++++++++*******/
  18. int newGameX=0, helpX=450, elx=10; //newgame
  19. int newGameR=255,newGameg=255,newGameb=255;
  20.  
  21. int mouseHoverX, mouseHoverY;
  22.  
  23. int loadingWidth=0;
  24.  
  25.  
  26. /*++++++++++++++++++++End Home page UI+++++++++++++++++*/
  27.  
  28. //Variable for the User Ball
  29. int ball_x = 200, ball_y = 590, radious = 10;
  30.  
  31. //Variable for Scoring Squares
  32. int S1_x = 100, S1_y = 50;                              //Starts from the lower left of the display
  33. int S2_x = 300, S2_y = 300;                             //Starts from the mid right of the screen
  34. int L = 15;                                             //Length of the Squares
  35.  
  36. int det1 = 1, det2 = 1;                                 //Drection indicator for the Squares
  37.  
  38. //Variables for Enemy Ball
  39. int Enemy_x = 210, Enemy_y = 15, Enemy_R = 15;          //Enemy_R = Radious of the Enemy Ball
  40. int X = 1, Y = 1;                                       //Direction indicator variables (DX Ball)
  41. int E_speed = 4;                                        //Speed of the Enemy Ball
  42. int Enemy_Point = 0; //Points earned or scored by Enemy Ball
  43. char enemyText[100];
  44.  
  45.  
  46. //Variable for Score
  47. //int score = 0;
  48.  
  49. void game()
  50. {
  51.     int start = 590;
  52.     int temp = 585;
  53.     int rock;
  54.  
  55.     //Increment of Bricks
  56.     A_y += 4;
  57.     B1_y += 4;
  58.     B2_y += 4;
  59.     C1_y += 4;
  60.     C2_y += 4;
  61.     D_y += 4;
  62.     E1_y += 4;
  63.     E2_y += 4;
  64.  
  65.     if (A_y >= temp) {
  66.         A_y = 0;
  67.     }
  68.     if (B1_y >= temp) {
  69.         B1_y = B2_y = 0;
  70.     }
  71.     if (C1_y >= temp) {
  72.         C1_y = C2_y = 0;
  73.     }
  74.     if (D_y >= temp) {
  75.         D_y = 0;
  76.     }
  77.     if (E1_y >= temp) {
  78.         E1_y = E2_y = 0;
  79.     }
  80.     /*
  81.  
  82.     //If the Ball hits the Bricks
  83.     rock = ball_y - A_y;
  84.     if (rock <= 25 && ball_x >= 150 && ball_x <= 250) {
  85.         det = 1;
  86.     }
  87.     if (ball_y - A_y <= 25 && ball_y - A_y <= 24 && ball_x >= A_x && ball_x <= A_x + 100) {
  88.         det = 1;
  89.     }
  90.     if (det == 1) {
  91.         ball_y += 4;
  92.     }
  93.     if (ball_y >= 600) {
  94.         det = 0;
  95.         ball_y -= 4;
  96.     }*/
  97.  
  98.     if (ball_y < 0) {
  99.         ball_y = 590;
  100.     }
  101.     else
  102.     {
  103.         //For 1st layer of Brick
  104.         if ((ball_y - A_y) <= 25 && (ball_y - A_y) >= 17 && ball_x >= 150 && ball_x <= 250)
  105.             ball_y += 4;
  106.  
  107.         //2nd layer of Bricks
  108.         else if ((ball_y - B1_y) <= 25 && (ball_y - B1_y) >= 17 && ball_x >= 60 && ball_x <= 160)
  109.             ball_y += 4;
  110.  
  111.         else if ((ball_y - B2_y) <= 25 && (ball_y - B2_y) >= 17 && ball_x >= 230 && ball_x <= 330)
  112.             ball_y += 4;
  113.  
  114.         //3rd layer of Bricks
  115.         else if ((ball_y - C1_y) <= 25 && (ball_y - C1_y) >= 17 && ball_x >= 100 && ball_x <= 200)
  116.             ball_y += 4;
  117.  
  118.         else if ((ball_y - C2_y) <= 25 && (ball_y - C2_y) >= 17 && ball_x >= 270 && ball_x <= 370)
  119.             ball_y += 4;
  120.  
  121.         //4th layer of Brick
  122.         else if ((ball_y - D_y) <= 25 && (ball_y - D_y) >= 17 && ball_x >= 150 && ball_x <= 250)
  123.             ball_y += 4;
  124.  
  125.         //5th layer of Bricks
  126.         else if ((ball_y - E1_y) <= 25 && (ball_y - E1_y) >= 17 && ball_x >= 60 && ball_x <= 160)
  127.             ball_y += 4;
  128.  
  129.         else if ((ball_y - E2_y) <= 25 && (ball_y - E2_y) >= 17 && ball_x >= 230 && ball_x <= 330)
  130.             ball_y += 4;
  131.  
  132.         else
  133.         {
  134.             ball_y -= 4;
  135.         }
  136.     }
  137.  
  138.     //If the ball hits the side walls
  139.     if (ball_x <= 10) {
  140.         ball_x = 10;
  141.     }
  142.     else if (ball_x >= 410) {
  143.         ball_x = 410;
  144.     }
  145. }
  146.  
  147. void Square1()
  148. {
  149.     int U = 2;
  150.    
  151.     //U = rand() % 8;
  152.     srand(time(NULL));
  153.  
  154.     S1_y += 1;
  155.     /*
  156.     if (P1_x >= 405) {
  157.         det1 = 0;
  158.     }
  159.     else if (P1_x <= 0) {
  160.         det1 = 1;
  161.     }
  162.  
  163.     if (det1 == 0) {
  164.         P1_x -= U;
  165.     }
  166.     else
  167.     {
  168.         P1_x += U;
  169.     }*/
  170.  
  171.     if (S1_y >= 600) {
  172.         S1_x = (rand() % 160) + 50;
  173.         S1_y = 0;
  174.         det1 = 1;
  175.     }
  176.     else
  177.     {
  178.         if (S1_x >= 405) {
  179.             det1 = 0;
  180.         }
  181.         else if (S1_x <= 0) {
  182.             det1 = 1;
  183.         }
  184.  
  185.         if (det1 == 0) {
  186.             S1_x -= U;
  187.         }
  188.         else
  189.         {
  190.             S1_x += U;
  191.         }
  192.     }
  193. }
  194.  
  195. void Square2()
  196. {
  197.     int U = 2;
  198.  
  199.     //U = rand() % 8;
  200.     srand(time(NULL));
  201.  
  202.     S2_y += 1;
  203.  
  204.     if (S2_y >= 600) {
  205.         S2_x = (rand() % 170) + 250;
  206.         S2_y = 0;
  207.         det2 = 0;
  208.     }
  209.     else
  210.     {
  211.         if (S2_x >= 405) {
  212.             det2 = 0;
  213.         }
  214.         else if (S2_x <= 0) {
  215.             det2 = 1;
  216.         }
  217.  
  218.         if (det2 == 0) {
  219.             S2_x -= U;
  220.         }
  221.         else
  222.         {
  223.             S2_x += U;
  224.         }
  225.     }
  226. }
  227.  
  228. void Enemy_Ball()
  229. {
  230.     /*
  231.     If X = 1, the Enemy Ball will move Right
  232.     If x = 0, the Enemy Ball will move Left
  233.                      AND
  234.     If Y = 1, the Enemy Ball will move Up
  235.     If Y = 0, the Enemy Ball will move Dowm
  236.     */
  237.     if (X == 1) {
  238.         Enemy_x += E_speed;
  239.     }
  240.     else
  241.     {
  242.         Enemy_x -= E_speed;
  243.     }
  244.  
  245.     if (Y == 1) {
  246.         Enemy_y += E_speed;
  247.     }
  248.     else
  249.     {
  250.         Enemy_y -= E_speed;
  251.     }
  252.  
  253.     //H = 420 - Enemy_R;
  254.     if (Enemy_x >= 420 - Enemy_R) {
  255.         X = 0;
  256.     }
  257.     else if (Enemy_x <= Enemy_R) {
  258.         X = 1;
  259.     }
  260.  
  261.     if (Enemy_y >= 585) {
  262.         Y = 0;
  263.     }
  264.     else if (Enemy_y <= 15) {
  265.         Y = 1;
  266.     }
  267.  
  268.  
  269. }
  270.  
  271. void Score_Card()
  272. {
  273.     int V, H;
  274.     int M1_x, M1_y;
  275.     int M2_x, M2_y;
  276.  
  277.     //Set a Mid-point for Scoring Square 1 (S1_x & S1_y)
  278.     M1_x = S1_x + 7;
  279.     M1_y = S1_y + 7;
  280.  
  281.     //Set a Mid-point for Scoring Square 2 (S2_x & S2_y)
  282.     M2_x = S2_x + 7;
  283.     M2_y = S2_y + 7;
  284.  
  285.     srand(time(NULL));
  286.    
  287.     //Calculation for clash position between User Ball and Mid-point for Scoring Square 1 (S1_x & S1_y)
  288.     H = ball_x - M1_x;                                    //H is Hrozontal calulation in side wise (Right or Left)
  289.     V = ball_y - M1_y;                                    //V is Vertical calculation in height wise (Up or Down)
  290.  
  291.     //If the user ball and Scoring Square 1 (S1_x & S1_y) clashes
  292.     if (V >= -radious && V <= radious)
  293.     {
  294.         if (H >= -radious && H <= radious)
  295.         {  
  296.             S1_x = (rand() % 160) + 50;
  297.             S1_y = 0;
  298.             det1 = 1;
  299.             score++;
  300.             //Radious of the Enemy Ball increases
  301.             Enemy_R += 5;
  302.         }
  303.     }
  304.  
  305.     //Reuse of the same variables for calculation for clash position between User Ball and Mid-point for Scoring Square 2 (S2_x & S2_y)
  306.     H = ball_x - M2_x;
  307.     V = ball_y - M2_y;
  308.  
  309.     //If the user ball and Scoring Square 2 (S2_x & S2_y) clashes
  310.     if (V >= -radious && V <= radious)
  311.     {
  312.         if (H >= -radious && H <= radious)
  313.         {
  314.             S2_x = (rand() % 170) + 250;
  315.             S2_y = 0;
  316.             det2 = 0;
  317.             score++;
  318.             //Radious of the Enemy Ball increases
  319.             Enemy_R += 5;
  320.         }
  321.     }
  322. }
  323.  
  324. void End_Game()
  325. {
  326.     int V, H, D;
  327.    
  328.     srand(time(NULL));
  329.  
  330.     //Sumation for radious of Enemy Ball and User Ball
  331.     D = radious + Enemy_R;
  332.  
  333.     //Clash distance
  334.     V = ball_y - Enemy_y;                               //Vertcal clash distance
  335.     H = ball_x - Enemy_x;                               //Horizontal clash distance
  336.  
  337.     //Enemy Ball earns Point IF
  338.     if (V >= -D && V <= D)
  339.     {
  340.         if (H >= -D && H <= D)
  341.         {
  342.             Enemy_Point +=1;
  343.             //Game Over IF
  344.             if (Enemy_Point == 3) {
  345.                 //Game Over******** EXIT
  346.  
  347.                 HomePageFlag=2; /*for last page*/
  348.                 //score=0;
  349.                 //Enemy_Point=0;
  350.  
  351.             }
  352.        
  353.             else
  354.             {
  355.                 srand(time(NULL));
  356.                 Enemy_x = (rand() % 380) + 20;
  357.                 Enemy_y = 0;
  358.             }
  359.         }
  360.     }
  361. }
  362.  
  363.  
  364. void homePageText(){
  365.  
  366.     if(newGameX <170){
  367.         newGameX +=2;
  368.         elx = newGameX;
  369.     }
  370.    
  371.     if (helpX > 200)
  372.         helpX -=2;
  373.  
  374.     if(loadingWidth<197){
  375.    
  376.         loadingWidth +=1;
  377.     }
  378.    
  379.  
  380.  
  381.  
  382. }
  383.  
  384. void iDraw()
  385. {
  386.     iClear();
  387.  
  388.     iText(0,590, "Score:");/*Score section */
  389.  
  390.     _itoa_s(score,scoreText,15);
  391.     iText(60,590,scoreText);
  392.  
  393.     iText(350,590, "Life:"); /*Life section*/
  394.  
  395.     _itoa_s(Enemy_Point,enemyText,15);
  396.     iText(390,590,enemyText);
  397.  
  398.  
  399.  
  400.     if(HomePageFlag ==-1){ /*home page*/
  401.  
  402.         iSetColor(232,44,12);
  403.         iFilledRectangle(0,0,420,600);
  404.        
  405.        
  406.        
  407.         //iText(200, 330, score, GLUT_BITMAP_HELVETICA_18);
  408.         iSetColor(newGameR,newGameg,newGameb); // ellipes color
  409.        
  410.         iFilledEllipse(elx+40,305,60,20); //ellipes for new game text
  411.  
  412.         iSetColor(255,100,255);
  413.        
  414.         iText(newGameX,300,"New Game",GLUT_BITMAP_HELVETICA_18);
  415.  
  416.         iSetColor(newGameR,newGameg,newGameb); // ellipes color
  417.         iFilledEllipse(elx+40,260,60,20);
  418.  
  419.         iSetColor(255,100,255);
  420.  
  421.         iText(helpX,250,"Exit",GLUT_BITMAP_HELVETICA_18);
  422.  
  423.  
  424.         /*start page end*/
  425.     }
  426.  
  427.     else if(HomePageFlag==20){ /*loading page for 0*/
  428.  
  429.        
  430.             iSetColor(232,44,12);
  431.             iFilledRectangle(0,0,420,600);
  432.  
  433.             iSetColor(255,255,255);
  434.             iRectangle(110,290,200,40);
  435.             iSetColor(0,0,0);
  436.             iFilledRectangle(111,292,loadingWidth,38);
  437.    
  438.     }
  439.  
  440.     else if (HomePageFlag==2){ /*last page 2*/
  441.            
  442.             score=0;
  443.             Enemy_Point=0;
  444.  
  445.             iSetColor(232,44,12);
  446.             iFilledRectangle(0,0,420,600);
  447.  
  448.             iSetColor(255,255,255);
  449.             iFilledRectangle(135,400,150,30);
  450.             iSetColor(0,0,0);
  451.             iText(140, 410, "Score",GLUT_BITMAP_HELVETICA_18); //end score
  452.             iText(200, 410, scoreText,GLUT_BITMAP_HELVETICA_18);
  453.            
  454.            
  455.             iSetColor(255,255,255);
  456.             iFilledRectangle(135,300,150,30);
  457.             iSetColor(0,0,0);
  458.             iText(152, 310, "Newgame",GLUT_BITMAP_HELVETICA_18); //end score
  459.  
  460.             iSetColor(255,255,255);
  461.             iFilledRectangle(135,200,150,30);
  462.             iSetColor(0,0,0);
  463.             iText(195, 210, "Exit",GLUT_BITMAP_HELVETICA_18); //Exit
  464.  
  465.  
  466.        
  467.     }
  468.    
  469.  
  470.    
  471.     else if(HomePageFlag == 10 ) {
  472.     //For Bricks
  473.     iSetColor(0, 255, 0);
  474.     iFilledRectangle(A_x, A_y, 100, 15);
  475.    
  476.     iSetColor(128, 0, 128);
  477.     iFilledRectangle(B1_x, B1_y, 100, 15);
  478.     iFilledRectangle(B2_x, B2_y, 100, 15);
  479.  
  480.     iSetColor(200, 55, 113);
  481.     iFilledRectangle(C1_x, C1_y, 100, 15);
  482.     iFilledRectangle(C2_x, C2_y, 100, 15);
  483.  
  484.     iSetColor(152, 122, 103);
  485.     iFilledRectangle(D_x, D_y, 100, 15);
  486.  
  487.     iSetColor(15, 113, 83);
  488.     iFilledRectangle(E1_x, E1_y, 100, 15);
  489.     iFilledRectangle(E2_x, E2_y, 100, 15);
  490.  
  491.     //For Ball
  492.     iSetColor(255, 255, 0);
  493.     iFilledCircle(ball_x, ball_y, radious, 100);
  494.  
  495.     //iClear();
  496.     //For Point Scoring Squares
  497.     iSetColor(192, 192, 192);
  498.     iFilledRectangle(S1_x, S1_y, L, L);
  499.  
  500.     iSetColor(128, 255, 255);
  501.     iFilledRectangle(S2_x, S2_y, L, L);
  502.  
  503.     //For Enemy Ball
  504.     iSetColor(255, 0, 0);
  505.     iFilledCircle(Enemy_x, Enemy_y, Enemy_R);
  506.  
  507.     }
  508. }
  509. void iMouseMove(int mx, int my)
  510. {
  511.  
  512.    
  513.  
  514.     printf("%d\n",mx);
  515.     //place your codes here
  516. }
  517. void iMouse(int button, int state, int mx, int my)
  518. {
  519.  
  520.  
  521.     mouseHoverX = mx;
  522.     mouseHoverY = my;
  523.  
  524.  
  525.    
  526.     if(mouseHoverX> 149 && mouseHoverX<268 && mouseHoverY<325 && mouseHoverY>288){
  527.         newGameR = 232;
  528.         newGameg = 106;
  529.         newGameR = 122;
  530.         HomePageFlag =10;
  531.     }
  532.  
  533.     if(mouseHoverX> 135 && mouseHoverX<285 && mouseHoverY<430 && mouseHoverY>400){ //last page new game
  534.         newGameR = 232;
  535.         newGameg = 106;
  536.         newGameR = 122;
  537.         HomePageFlag =10;
  538.     }
  539.    
  540.         if(mouseHoverX> 135 && mouseHoverX<285 && mouseHoverY<230 && mouseHoverY>200){ //last page exit
  541.         newGameR = 232;
  542.         newGameg = 106;
  543.         newGameR = 122;
  544.         HomePageFlag =10;
  545.  
  546.         exit(0);
  547.     }
  548.  
  549.         if(mouseHoverX> 152 && mouseHoverX<268 && mouseHoverY>243 && mouseHoverY<279){ //first page exit
  550.        
  551.             //HomePageFlag =10;
  552.  
  553.         exit(0);
  554.     }
  555.  
  556.  
  557.     printf("%d\n",mouseHoverY);
  558.  
  559. }
  560. void iKeyboard(unsigned char key)
  561. {
  562.     //place your codes here
  563. }
  564.  
  565. void iSpecialKeyboard(unsigned char key)
  566. {
  567.     if (key == GLUT_KEY_LEFT) {
  568.         ball_x -= 22;
  569.     }
  570.     else if (key == GLUT_KEY_RIGHT) {
  571.         ball_x += 22;
  572.     }
  573.     else if (key == GLUT_KEY_UP) {
  574.         ball_y += 4;
  575.     }
  576.     else if (key == GLUT_KEY_DOWN) {
  577.         ball_y -= 8;
  578.     }
  579. }
  580. int main()
  581. {
  582.     iSetTimer(10, game);
  583.     iSetTimer(10, Square1);
  584.     iSetTimer(10, Square2);
  585.     iSetTimer(10, Enemy_Ball);
  586.     iSetTimer(10, Score_Card);
  587.     iSetTimer(10, End_Game);
  588.     iSetTimer(30, homePageText);
  589.     iInitialize(420, 600, "Rapid Roll");
  590.     printf("Score: %d\n", score);
  591.     return 0;
  592. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement