naeem043

Game

Jan 2nd, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.79 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <iostream>
  3. #include <stdio.h>
  4.  
  5. //double r;
  6. //r=1+rand()%9;
  7.  
  8. double a1=0.8,a2=0.7,b1=0.5,b2=0.6,s1=0.5,s2=0.4,e1=0.2,e2=0.3;
  9. int count=0,f=0,n=0,r=0;
  10. double g=0,h=0;
  11.  
  12. double lx1=0.1, lx2=0.2, ly1=-0.18, ly2=-0.05;
  13. double levelx1=-0.2, levelx2=0.025, levely1=0.1, levely2=0.1;
  14. double levelxx1=0.98, levelxx2=1.2, levelyy1=0.1, levelyy2=0.1;
  15.  
  16.  
  17. void random(){
  18.     g=1+rand()%8;
  19.     h=1+rand()%8;
  20.     s2=(g/10);
  21.     s1=s2-0.1;
  22.     e2=(h/10);
  23.     e1=e2-0.1;
  24.     printf("%lf %lf %lf %lf %lf %lf",g,h,s2,s1,e2,e1);
  25.  
  26. }
  27.  
  28.  
  29. void level1(){
  30. glBegin(GL_QUADS);
  31.     glColor3f(0.0, 1.0, 0.0);
  32.     glVertex3f(levelx1, 0.1, 0.0);
  33.     glVertex3f(levelx2, 0.1, 0.0);     
  34.     glVertex3f(levelx2, 0.9, 0.0);
  35.     glVertex3f(levelx1, 0.9, 0.0);
  36.     glEnd();
  37.  
  38.     glBegin(GL_QUADS);
  39.     glColor3f(1.0, 0.0, 0.0);
  40.     glVertex3f(levelx1, levely1, 0.0);
  41.     glVertex3f(levelx2, levely1, 0.0);     
  42.     glVertex3f(levelx2, levely2, 0.0);
  43.     glVertex3f(levelx1, levely2, 0.0);
  44.     glEnd();
  45.  
  46. }
  47.  
  48. void level2(){
  49. glBegin(GL_QUADS);
  50.     glColor3f(0.0, 1.0, 0.0);
  51.     glVertex3f(levelxx1, 0.1, 0.0);
  52.     glVertex3f(levelxx2, 0.1, 0.0);    
  53.     glVertex3f(levelxx2, 0.9, 0.0);
  54.     glVertex3f(levelxx1,0.9, 0.0);
  55.     glEnd();
  56.  
  57.     glBegin(GL_QUADS);
  58.     glColor3f(1.0, 0.0, 0.0);
  59.     glVertex3f(levelxx1, levelyy1, 0.0);
  60.     glVertex3f(levelxx2, levelyy1, 0.0);       
  61.     glVertex3f(levelxx2, levelyy2, 0.0);
  62.     glVertex3f(levelxx1,levelyy2, 0.0);
  63.     glEnd();
  64.  
  65. }
  66.  
  67. void life(){
  68.  
  69.     glBegin(GL_QUADS);
  70.     glColor3f(0.0, 1.0, 0.0);
  71.     glVertex3f(0.1, ly1, 0.0);
  72.     glVertex3f(0.9, ly1, 0.0);     
  73.     glVertex3f(0.9, ly2, 0.0);
  74.     glVertex3f(0.1,ly2, 0.0);
  75.     glEnd();
  76.  
  77.     glBegin(GL_QUADS);
  78.     glColor3f(1.0, 0.0, 0.0);
  79.     glVertex3f(lx1, ly1, 0.0);
  80.     glVertex3f(lx2, ly1, 0.0);     
  81.     glVertex3f(lx2, ly2, 0.0);
  82.     glVertex3f(lx1,ly2, 0.0);
  83.     glEnd();
  84.  
  85. }
  86.  
  87. void frame (void) {
  88.    
  89.     glBegin(GL_QUADS);
  90.     glColor3f(1.0, 1.0, 1.0);
  91.     glVertex3f(-0.25, 1.25, 0.0);
  92.     glVertex3f(1.25, 1.25, 0.0);        //main region
  93.     glVertex3f(1.25, -0.25, 0.0);
  94.     glVertex3f(-0.25, -0.25, 0.0);
  95.     glEnd();
  96.  
  97.  
  98.     glBegin(GL_QUADS);
  99.     glColor3f(1.0, 0.0, 0.0);
  100.     glVertex3f(0.08, 0.09, 0.0);
  101.     glVertex3f(0.08, 0.91, 0.0);        //left
  102.     glVertex3f(0.09, 0.91, 0.0);
  103.     glVertex3f(0.09, 0.09, 0.0);
  104.     glEnd();
  105.  
  106.     glBegin(GL_QUADS);
  107.     glColor3f(1.0, 0.0, 0.0);
  108.     glVertex3f(0.91, 0.09, 0.0);
  109.     glVertex3f(0.91, 0.91, 0.0);        //right
  110.     glVertex3f(0.922, 0.91, 0.0);
  111.     glVertex3f(0.922, 0.09, 0.0);
  112.     glEnd();
  113.  
  114.     glBegin(GL_QUADS);
  115.     glColor3f(1.0, 0.0, 0.0);
  116.     glVertex3f(0.08, 0.91, 0.0);
  117.     glVertex3f(0.922, 0.91, 0.0);       //up
  118.     glVertex3f(0.922, 0.923, 0.0);
  119.     glVertex3f(0.08, 0.923, 0.0);
  120.     glEnd();
  121.  
  122.     glBegin(GL_QUADS);
  123.     glColor3f(1.0, 0.0, 0.0);
  124.     glVertex3f(0.08, 0.09, 0.0);
  125.     glVertex3f(0.922, 0.09, 0.0);       //down
  126.     glVertex3f(0.922, 0.082, 0.0);
  127.     glVertex3f(0.08, 0.082, 0.0);
  128.     glEnd();
  129.  
  130.    
  131.  
  132.     double x1=0.1,x2=0.9,l1=0.9,l2=0.9;
  133.     int i;
  134.     for(i=1;i<10;i++)
  135.     {      
  136.             glBegin(GL_LINES);
  137.             glColor3f(0.0, 0.0, 0.0);
  138.             glColor3f(0.0, 0.0, 0.0);
  139.             if(i==9 || i==1)
  140.             {
  141.                 glColor3f(1.0, 0.0, 0.0);
  142.             }          
  143.             glVertex3f(x1,l1,0.0);
  144.             glVertex3f(x2,l1,0.0);
  145.             glEnd();
  146.             l1=l1-0.1;         
  147.     }
  148.  
  149.     x1 = 0.1;
  150.     l1 = 0.9;
  151.     l2 = 0.1;
  152.  
  153.     for(i=1;i<10;i++)
  154.     {       glBegin(GL_LINES);
  155.             glColor3f(0.0, 0.0, 0.0);
  156.             if(i==9 || i==1)
  157.             {
  158.                 glColor3f(1.0, 0.0, 0.0);
  159.             }          
  160.             glVertex3f(x1,l1,0.0);
  161.             glVertex3f(x1,l2,0.0);
  162.             glEnd();
  163.             x1=x1+0.1;         
  164.     }  
  165.  
  166. }
  167.  
  168.  
  169. void surprise()
  170. {
  171.    
  172.         glPushMatrix();
  173.         glColor3f(0.0, 0.0, 1.0);
  174.         glTranslatef(0.1,1.1,0.0);
  175.         glutSolidTeapot(0.08); 
  176.         glPopMatrix();
  177.  
  178.         glPushMatrix();
  179.         glColor3f(0.0, 0.0, 1.0);
  180.         glTranslatef(0.35,1.1,0.0);
  181.         glutSolidTeapot(0.08); 
  182.         glPopMatrix();
  183.        
  184.         glPushMatrix();
  185.         glColor3f(0.0, 0.0, 1.0);
  186.         glTranslatef(0.6,1.1,0.0);
  187.         glutSolidTeapot(0.08); 
  188.         glPopMatrix();
  189.  
  190.         glPushMatrix();
  191.         glColor3f(0.0, 0.0, 1.0);
  192.         glTranslatef(0.85,1.1,0.0);
  193.         glutSolidTeapot(0.08); 
  194.         glPopMatrix(); 
  195.  
  196.  
  197.  
  198.  
  199. }
  200. void surprise2(){
  201.    
  202.  
  203. }
  204.  
  205. void red_point () {
  206.    
  207.     glBegin(GL_QUADS);
  208.     glColor3f(1.0, 0.0, 0.0);
  209.     glVertex3f(a1, b1, 0.0);
  210.     glVertex3f(a2, b1, 0.0);
  211.     glVertex3f(a2, b2, 0.0);
  212.     glVertex3f(a1, b2, 0.0);
  213.     glEnd();
  214. }
  215.  
  216. void green_point () {
  217.    
  218.     glBegin(GL_QUADS);
  219.     glColor3f(0.0, 1.0, 0.0);
  220.     glVertex3f(s1, e1, 0.0);
  221.     glVertex3f(s2, e1, 0.0);
  222.     glVertex3f(s2, e2, 0.0);
  223.     glVertex3f(s1, e2, 0.0);
  224.     glEnd();
  225. }
  226. void keyboard(unsigned char k, int x, int y)
  227. {
  228.  
  229.     if(k=='e')
  230.     {
  231.         e1+=0.1;
  232.         e2+=0.1;
  233.         if(e2>0.9 || e1<0.2 || s1<0.2 || s2>0.8)
  234.         {
  235.             f++;
  236.         }
  237.         if(s2==a2 && e1==b1 && e2==b2)
  238.         {
  239.            
  240.             count++;
  241.            
  242.         }
  243.        
  244.        
  245.     }
  246.     else if(k=='x')
  247.     {
  248.         e1-=0.1;
  249.         e2-=0.1;
  250.         if(e2>0.9 || e1<0.2 || s1<0.2 || s2>0.8)
  251.         {
  252.             f++;
  253.         }
  254.         if(s2==a2 && e1==b1 && e2==b2)
  255.         {
  256.            
  257.             count++;
  258.            
  259.         }
  260.         else
  261.         green_point();
  262.     }
  263.     else if(k=='f')
  264.     {
  265.         s1+=0.1;
  266.         s2+=0.1;
  267.         if(e2>0.9 || e1<0.2 || s1<0.2 || s2>0.8)
  268.         {
  269.             f++;
  270.         }
  271.         if(s2==a2 && e1==b1 && e2==b2)
  272.         {
  273.            
  274.             count++;
  275.            
  276.         }
  277.         else
  278.         green_point();
  279.     }
  280.     else if(k=='s')
  281.     {
  282.         s1-=0.1;
  283.         s2-=0.1;
  284.         if(e2>0.9 || e1<0.2 || s1<0.2 || s2>0.8)
  285.         {
  286.             f++;
  287.         }
  288.         if(s2==a2 && e1==b1 && e2==b2)
  289.         {
  290.            
  291.             count++;
  292.            
  293.         }
  294.         else
  295.         green_point();
  296.     }
  297.      
  298.     glutPostRedisplay();
  299.    
  300. }
  301.  
  302. void processSpecialKeys(int key, int x, int y)
  303. {
  304.    
  305.    
  306.      if(key==GLUT_KEY_UP)
  307.     {
  308.         b1+=0.1;
  309.         b2+=0.1;
  310.         if(s2==a2 && e1==b1 && e2==b2)
  311.         {
  312.             if(count==16)
  313.             {
  314.                 printf("Successfully Play the Game");
  315.                 exit(0);
  316.             }
  317.             else if(count==8)
  318.             {
  319.                 levelyy2+=0.1;
  320.             }
  321.             else
  322.             {
  323.                 levely2+=0.1;
  324.                 count++;
  325.             }
  326.             random();
  327.         }
  328.  
  329.        
  330.  
  331.        
  332.        
  333.     }
  334.  
  335.     else if(key==GLUT_KEY_DOWN)
  336.     {
  337.         b1-=0.1;
  338.         b2-=0.1;
  339.         if(s2==a2 && e1==b1 && e2==b2)
  340.         {
  341.             levely2+=0.1;
  342.         }
  343.     }
  344.  
  345.     else if(key==GLUT_KEY_LEFT)
  346.     {
  347.         a1-=0.1;
  348.         a2-=0.1;
  349.         if(s2==a2 && e1==b1 && e2==b2)
  350.         {
  351.             levely2+=0.1;
  352.         }
  353.     }
  354.  
  355.     else if(key==GLUT_KEY_RIGHT)
  356.     {
  357.         a1+=0.1;
  358.         a2+=0.1;
  359.         if(s2==a2 && e1==b1 && e2==b2)
  360.         {
  361.             levely2+=0.1;
  362.         }
  363.     }
  364.     glutPostRedisplay();
  365.  
  366. }
  367.  
  368.  
  369. void display (void) {
  370.    
  371.     glClearColor (0.0,0.0,0.0,1.0);
  372.     glClear (GL_COLOR_BUFFER_BIT);
  373.     glLoadIdentity();  
  374.     gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  375.     frame();
  376.     green_point();
  377.     red_point();
  378.     life();
  379.     level1();
  380.     level2();
  381.     surprise2();
  382.     surprise();
  383.        
  384.     glFlush();
  385. }
  386.  
  387. void reshape (int w, int h) {
  388.  
  389.     glViewport (0, 0, (GLsizei)w, (GLsizei)h);   
  390.     glMatrixMode (GL_PROJECTION);
  391.     glLoadIdentity ();     
  392.     gluPerspective (30, 1, 1.0, 100.0);
  393.     glMatrixMode (GL_MODELVIEW);
  394.  
  395. }
  396.  
  397. int main()
  398. {
  399.     glutInitDisplayMode (GLUT_SINGLE);
  400.     glutInitWindowSize (700,700);
  401.     glutInitWindowPosition (100, 100);
  402.     glutCreateWindow ("A basic OpenGL Window");
  403.     glutDisplayFunc (display);
  404.     glutKeyboardFunc(keyboard);
  405.     glutSpecialFunc(processSpecialKeys);
  406.     glutReshapeFunc (reshape);
  407.     glutIdleFunc(display);
  408.     glutMainLoop ();
  409.     return 0;
  410. }
Advertisement
Add Comment
Please, Sign In to add comment