import sys from OpenGL.GLUT import * from OpenGL.GL import * from OpenGL.GLU import * triPosX = 0 triPosY = 0 triDX = 1 triDY = 1 triRot = 0 def draw(): glClear(GL_COLOR_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(-15, 15, -10, 10) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glTranslatef(triPosX, triPosY, 0) glRotatef(triRot, 0, 0, 1) glColor3f(1, 0, 0.7) glBegin(GL_TRIANGLES) glVertex2f(-1.0, -1.0) glVertex2f(1.0, -1.0) glVertex2f(0.0, 1.0) glEnd() glutSwapBuffers() def keyboard(key, x, y): if key == chr(27): sys.exit(0) def update(dummy): global triPosX, triPosY, triDX, triDY, triRot glutTimerFunc(33, update, 0) if triPosX < -15: triDX = 1 elif triPosX > 15: triDX = -1 if triPosY < -10: triDY = 1 elif triPosY > 10: triDY = -1 triPosX += triDX * 0.2 triPosY += triDY * 0.2 triRot += 2 glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) glutInitWindowSize(600, 400) glutInitWindowPosition(0,0) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutTimerFunc(33, update, 0) glutMainLoop()