Skip to content

Instantly share code, notes, and snippets.

@anta40
Created December 23, 2020 15:18
Show Gist options
  • Save anta40/b5db0827af2ebde6705687e642975cd5 to your computer and use it in GitHub Desktop.
Save anta40/b5db0827af2ebde6705687e642975cd5 to your computer and use it in GitHub Desktop.
// Tested only on MacOS
#include <GLUT/glut.h>
#include <stdio.h>
float xt=1.0,yt=1.0; // For interactive Keyboard
float x = 1.0,y = 1.0,z=1.0; // For Movement
float angle =0; // For Function animation
float Autorun = 300; // For Movement Autorun
void animation(void)
{
if(angle>=0 && angle<10)
angle = angle+0.5;
else angle = 0;
glutPostRedisplay();
}
void Auto(void)
{
if(Autorun<=300 && Autorun>-350)
Autorun = Autorun-0.05;
else Autorun = 300;
glutPostRedisplay();
}
void settings(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glPointSize(5.0);
glLineWidth(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,400.0,0.0,400.0);
}
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glPushMatrix();
glRotatef(angle,0.0,0.0,0.0);
glTranslatef(Autorun,0.0,0.0);
glTranslatef(xt,yt,0.0); //For Move NEWS/QDZC each Position.
glBegin(GL_POLYGON); // draw body
glColor3f(0.0,1.0,0.0);
glVertex2i(40,200);
glVertex2i(120,280);
glVertex2i(320,200);
glVertex2i(100,160);
glEnd();
glPushMatrix();
glRotatef(angle,0.0,0.0,0.0);
glBegin(GL_POLYGON); //draw tail
glColor3f(0.0,1.0,0.0);
glVertex2i(320,200);
glVertex2i(360,240);
glVertex2i(340,200);
glVertex2i(360,160);
glVertex2i(320,200);
glEnd();
glBegin(GL_POLYGON); //draw Top Key
glColor3f(1.0,0.0,0.0);
glVertex2i(120,280);
glVertex2i(140,300);
glVertex2i(280,216);
glVertex2i(120,280);
glEnd();
glBegin(GL_POLYGON); //draw Buttom Key
glColor3f(1.0,0.0,0.0);
glVertex2i(100,160);
glVertex2i(140,200);
glVertex2i(120,164);
glVertex2i(100,160);
glEnd();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
glFlush();
}
void keyboard(GLubyte key, int x, int y) // For keyboard interactive
{
switch ( key )
{
case 'd':
xt += 2.0; // Move thuea la 2.0 picel step by step
glColor3f(0.0,1.0,0.0);
glutPostRedisplay();
break;
case 'a':
xt -= 2.0;
glColor3f(1.0,0.0,0.0);
glutPostRedisplay();
break;
case 's':
yt -= 2.0;
glColor3f(0.0,0.0,0.0);
glutPostRedisplay();
break;
case 'w':
yt += 2.0;
glColor3f(0.0,0.0,1.0);
glutPostRedisplay();
break;
case 'e':
xt += 2.0;
yt += 2.0;
glColor3f(1.0,0.0,1.0);
glutPostRedisplay();
break;
case 'q':
xt -= 2.0;
yt += 2.0;
glColor3f(0.0,1.0,1.0);
glutPostRedisplay();
break;
case 'c':
xt += 2.0;
yt -= 2.0;
glColor3f(1.0,0.0,1.0);
glutPostRedisplay();
break;
case 'z':
xt -= 2.0;
yt -= 2.0;
glColor3f(0.0,0.0,1.0);
glutPostRedisplay();
break;
default:
break;
}
}
int main(int a,char ** b)
{
glutInit(&a,b);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(200,50); //200 Lelt 50 Height
glutInitWindowSize(500,500);
glutCreateWindow("Our Project");
settings();
glutDisplayFunc(Display);
glutIdleFunc(animation);
glutIdleFunc(Auto);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment