import sys, random, time, math from OpenGL.GLUT import * from OpenGL.GL import * from OpenGL.GLU import * pos1 = [0,0] pos2 = [0, 0] pos3 = [0, 0] pos4 = [0, 0] heading1 = 0 heading2 = 0 heading3 = 0 heading4 = 0 def draw(): glClear(GL_COLOR_BUFFER_BIT) glLoadIdentity() drawBackground() # First triangle glColor3f(1,0,0) glTranslatef(pos1[0], pos1[1], 0) glRotatef(heading1, 0, 0, 1) drawTriangle() # Second triangle glColor3f(0,1,0) glTranslatef(pos2[0], pos2[1], 0) glRotatef(heading2, 0, 0, 1) drawTriangle() # First orbiter glColor3f(1,1,1) glTranslatef(pos3[0], pos3[1], 0) glRotatef(heading3, 0, 0, 1) glScalef(0.3, 0.3, 0.3) drawSquare() # Second orbiter glColor3f(1,1,0) glTranslatef(pos4[0], pos4[1], 0) glRotatef(heading4, 0, 0, 1) glScalef(0.3, 0.3, 0.3) drawSquare() glFlush() def drawBackground(): random.seed(1) glColor3f(1,1,1) glBegin(GL_POINTS) for i in range(0,200): glVertex2f(random.uniform(-1,1), random.uniform(-1,1)) glEnd() def drawTriangle(): glBegin(GL_TRIANGLES) glVertex2f(-0.05, 0.0) glVertex2f(0.05, 0.0) glVertex2f(0.0, 0.15) glEnd() def drawSquare(): glBegin(GL_QUADS) glVertex2f(-0.05, -0.05) glVertex2f(0.05, -0.05) glVertex2f(0.05, 0.05) glVertex2f(-0.05, 0.05) glEnd() def keyboard(key, x, y): if key == chr(27): sys.exit(0) starttime = time.time() def timer(x): glutTimerFunc(16, timer, 0) glutPostRedisplay() global pos1, pos2, pos3, pos4, heading1, heading2, heading3, heading4 t = time.time() - starttime pos1 = [math.sin(t), math.cos(t)] heading1 = -math.degrees(t) - 90 pos2 = [-math.sin(t*1.5)*0.75, math.cos(t*1.5)*0.5] heading2 = math.degrees(t*1.5) + 90 pos3 = [math.sin(t*5)*0.2, math.cos(t*5)*0.2] pos4 = [-math.sin(t*5+1)*0.2, math.cos(t*5+1)*0.2] heading3 = math.degrees(t*7) heading4 = math.degrees(t*8) glutInit([]) glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) glutInitWindowSize(400, 400) glutInitWindowPosition(0,0) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutTimerFunc(0,timer,0) glutMainLoop()