# This program draws a moving "car" with rotating wheels # It demonstrates the use of the glPushMatrix and glPopMatrix to # create a hierarchy of transformations. from pyglet.gl import * window = pyglet.window.Window(500,500) angle = 0 carPosition = [-0.5, 0, 0] carDirection = 1 def drawWheel(): glBegin(GL_TRIANGLE_STRIP) glVertex2f(-0.08, -0.08) glVertex2f(0.08, -0.08) glVertex2f(-0.08, 0.08) glVertex2f(0.08, 0.08) glEnd() def drawCar(): glPushMatrix() glTranslatef(carPosition[0], carPosition[1], carPosition[2]) glBegin(GL_TRIANGLE_STRIP) glVertex2f(-0.3, 0.0) glVertex2f(0.3, 0.0) glVertex2f(-0.3, 0.2) glVertex2f(0.3, 0.2) glEnd() glColor3f(1, 1, 0) glPushMatrix() glTranslatef(-0.2, 0.0, 0.0) glRotatef(angle, 0, 0, 1) drawWheel() glPopMatrix() glColor3f(1, 0, 0) glPushMatrix() glTranslatef(0.2, 0.0, 0.0) glRotatef(angle, 0, 0, 1) drawWheel() glPopMatrix() glPopMatrix() @window.event def on_draw(): glClear(GL_COLOR_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() glMatrixMode(GL_MODELVIEW) glLoadIdentity() glColor3f(0, 0, 1) drawCar() def update(dt): global angle, carPosition, carDirection angle = angle - 180 * dt * carDirection carPosition[0] = carPosition[0] + dt * carDirection if carPosition[0] * carDirection > 1: carDirection = -carDirection pyglet.clock.schedule_interval(update,1/60.0) pyglet.app.run()