Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <iostream>
- #include <stdio.h>
- //double r;
- //r=1+rand()%9;
- 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;
- int count=0,f=0,n=0,r=0;
- double g=0,h=0;
- double lx1=0.1, lx2=0.2, ly1=-0.18, ly2=-0.05;
- double levelx1=-0.2, levelx2=0.025, levely1=0.1, levely2=0.1;
- double levelxx1=0.98, levelxx2=1.2, levelyy1=0.1, levelyy2=0.1;
- void random(){
- g=1+rand()%8;
- h=1+rand()%8;
- s2=(g/10);
- s1=s2-0.1;
- e2=(h/10);
- e1=e2-0.1;
- printf("%lf %lf %lf %lf %lf %lf",g,h,s2,s1,e2,e1);
- }
- void level1(){
- glBegin(GL_QUADS);
- glColor3f(0.0, 1.0, 0.0);
- glVertex3f(levelx1, 0.1, 0.0);
- glVertex3f(levelx2, 0.1, 0.0);
- glVertex3f(levelx2, 0.9, 0.0);
- glVertex3f(levelx1, 0.9, 0.0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(1.0, 0.0, 0.0);
- glVertex3f(levelx1, levely1, 0.0);
- glVertex3f(levelx2, levely1, 0.0);
- glVertex3f(levelx2, levely2, 0.0);
- glVertex3f(levelx1, levely2, 0.0);
- glEnd();
- }
- void level2(){
- glBegin(GL_QUADS);
- glColor3f(0.0, 1.0, 0.0);
- glVertex3f(levelxx1, 0.1, 0.0);
- glVertex3f(levelxx2, 0.1, 0.0);
- glVertex3f(levelxx2, 0.9, 0.0);
- glVertex3f(levelxx1,0.9, 0.0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(1.0, 0.0, 0.0);
- glVertex3f(levelxx1, levelyy1, 0.0);
- glVertex3f(levelxx2, levelyy1, 0.0);
- glVertex3f(levelxx2, levelyy2, 0.0);
- glVertex3f(levelxx1,levelyy2, 0.0);
- glEnd();
- }
- void life(){
- glBegin(GL_QUADS);
- glColor3f(0.0, 1.0, 0.0);
- glVertex3f(0.1, ly1, 0.0);
- glVertex3f(0.9, ly1, 0.0);
- glVertex3f(0.9, ly2, 0.0);
- glVertex3f(0.1,ly2, 0.0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(1.0, 0.0, 0.0);
- glVertex3f(lx1, ly1, 0.0);
- glVertex3f(lx2, ly1, 0.0);
- glVertex3f(lx2, ly2, 0.0);
- glVertex3f(lx1,ly2, 0.0);
- glEnd();
- }
- void frame (void) {
- glBegin(GL_QUADS);
- glColor3f(1.0, 1.0, 1.0);
- glVertex3f(-0.25, 1.25, 0.0);
- glVertex3f(1.25, 1.25, 0.0); //main region
- glVertex3f(1.25, -0.25, 0.0);
- glVertex3f(-0.25, -0.25, 0.0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(1.0, 0.0, 0.0);
- glVertex3f(0.08, 0.09, 0.0);
- glVertex3f(0.08, 0.91, 0.0); //left
- glVertex3f(0.09, 0.91, 0.0);
- glVertex3f(0.09, 0.09, 0.0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(1.0, 0.0, 0.0);
- glVertex3f(0.91, 0.09, 0.0);
- glVertex3f(0.91, 0.91, 0.0); //right
- glVertex3f(0.922, 0.91, 0.0);
- glVertex3f(0.922, 0.09, 0.0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(1.0, 0.0, 0.0);
- glVertex3f(0.08, 0.91, 0.0);
- glVertex3f(0.922, 0.91, 0.0); //up
- glVertex3f(0.922, 0.923, 0.0);
- glVertex3f(0.08, 0.923, 0.0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3f(1.0, 0.0, 0.0);
- glVertex3f(0.08, 0.09, 0.0);
- glVertex3f(0.922, 0.09, 0.0); //down
- glVertex3f(0.922, 0.082, 0.0);
- glVertex3f(0.08, 0.082, 0.0);
- glEnd();
- double x1=0.1,x2=0.9,l1=0.9,l2=0.9;
- int i;
- for(i=1;i<10;i++)
- {
- glBegin(GL_LINES);
- glColor3f(0.0, 0.0, 0.0);
- glColor3f(0.0, 0.0, 0.0);
- if(i==9 || i==1)
- {
- glColor3f(1.0, 0.0, 0.0);
- }
- glVertex3f(x1,l1,0.0);
- glVertex3f(x2,l1,0.0);
- glEnd();
- l1=l1-0.1;
- }
- x1 = 0.1;
- l1 = 0.9;
- l2 = 0.1;
- for(i=1;i<10;i++)
- { glBegin(GL_LINES);
- glColor3f(0.0, 0.0, 0.0);
- if(i==9 || i==1)
- {
- glColor3f(1.0, 0.0, 0.0);
- }
- glVertex3f(x1,l1,0.0);
- glVertex3f(x1,l2,0.0);
- glEnd();
- x1=x1+0.1;
- }
- }
- void surprise()
- {
- glPushMatrix();
- glColor3f(0.0, 0.0, 1.0);
- glTranslatef(0.1,1.1,0.0);
- glutSolidTeapot(0.08);
- glPopMatrix();
- glPushMatrix();
- glColor3f(0.0, 0.0, 1.0);
- glTranslatef(0.35,1.1,0.0);
- glutSolidTeapot(0.08);
- glPopMatrix();
- glPushMatrix();
- glColor3f(0.0, 0.0, 1.0);
- glTranslatef(0.6,1.1,0.0);
- glutSolidTeapot(0.08);
- glPopMatrix();
- glPushMatrix();
- glColor3f(0.0, 0.0, 1.0);
- glTranslatef(0.85,1.1,0.0);
- glutSolidTeapot(0.08);
- glPopMatrix();
- }
- void surprise2(){
- }
- void red_point () {
- glBegin(GL_QUADS);
- glColor3f(1.0, 0.0, 0.0);
- glVertex3f(a1, b1, 0.0);
- glVertex3f(a2, b1, 0.0);
- glVertex3f(a2, b2, 0.0);
- glVertex3f(a1, b2, 0.0);
- glEnd();
- }
- void green_point () {
- glBegin(GL_QUADS);
- glColor3f(0.0, 1.0, 0.0);
- glVertex3f(s1, e1, 0.0);
- glVertex3f(s2, e1, 0.0);
- glVertex3f(s2, e2, 0.0);
- glVertex3f(s1, e2, 0.0);
- glEnd();
- }
- void keyboard(unsigned char k, int x, int y)
- {
- if(k=='e')
- {
- e1+=0.1;
- e2+=0.1;
- if(e2>0.9 || e1<0.2 || s1<0.2 || s2>0.8)
- {
- f++;
- }
- if(s2==a2 && e1==b1 && e2==b2)
- {
- count++;
- }
- }
- else if(k=='x')
- {
- e1-=0.1;
- e2-=0.1;
- if(e2>0.9 || e1<0.2 || s1<0.2 || s2>0.8)
- {
- f++;
- }
- if(s2==a2 && e1==b1 && e2==b2)
- {
- count++;
- }
- else
- green_point();
- }
- else if(k=='f')
- {
- s1+=0.1;
- s2+=0.1;
- if(e2>0.9 || e1<0.2 || s1<0.2 || s2>0.8)
- {
- f++;
- }
- if(s2==a2 && e1==b1 && e2==b2)
- {
- count++;
- }
- else
- green_point();
- }
- else if(k=='s')
- {
- s1-=0.1;
- s2-=0.1;
- if(e2>0.9 || e1<0.2 || s1<0.2 || s2>0.8)
- {
- f++;
- }
- if(s2==a2 && e1==b1 && e2==b2)
- {
- count++;
- }
- else
- green_point();
- }
- glutPostRedisplay();
- }
- void processSpecialKeys(int key, int x, int y)
- {
- if(key==GLUT_KEY_UP)
- {
- b1+=0.1;
- b2+=0.1;
- if(s2==a2 && e1==b1 && e2==b2)
- {
- if(count==16)
- {
- printf("Successfully Play the Game");
- exit(0);
- }
- else if(count==8)
- {
- levelyy2+=0.1;
- }
- else
- {
- levely2+=0.1;
- count++;
- }
- random();
- }
- }
- else if(key==GLUT_KEY_DOWN)
- {
- b1-=0.1;
- b2-=0.1;
- if(s2==a2 && e1==b1 && e2==b2)
- {
- levely2+=0.1;
- }
- }
- else if(key==GLUT_KEY_LEFT)
- {
- a1-=0.1;
- a2-=0.1;
- if(s2==a2 && e1==b1 && e2==b2)
- {
- levely2+=0.1;
- }
- }
- else if(key==GLUT_KEY_RIGHT)
- {
- a1+=0.1;
- a2+=0.1;
- if(s2==a2 && e1==b1 && e2==b2)
- {
- levely2+=0.1;
- }
- }
- glutPostRedisplay();
- }
- void display (void) {
- glClearColor (0.0,0.0,0.0,1.0);
- glClear (GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- frame();
- green_point();
- red_point();
- life();
- level1();
- level2();
- surprise2();
- surprise();
- glFlush();
- }
- void reshape (int w, int h) {
- glViewport (0, 0, (GLsizei)w, (GLsizei)h);
- glMatrixMode (GL_PROJECTION);
- glLoadIdentity ();
- gluPerspective (30, 1, 1.0, 100.0);
- glMatrixMode (GL_MODELVIEW);
- }
- int main()
- {
- glutInitDisplayMode (GLUT_SINGLE);
- glutInitWindowSize (700,700);
- glutInitWindowPosition (100, 100);
- glutCreateWindow ("A basic OpenGL Window");
- glutDisplayFunc (display);
- glutKeyboardFunc(keyboard);
- glutSpecialFunc(processSpecialKeys);
- glutReshapeFunc (reshape);
- glutIdleFunc(display);
- glutMainLoop ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment