Skip to content

Instantly share code, notes, and snippets.

@davepape
Created September 24, 2013 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davepape/6684218 to your computer and use it in GitHub Desktop.
Save davepape/6684218 to your computer and use it in GitHub Desktop.
simple animated car using push/pop matrix for hierarchical transformations
# 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 = [100, 100, 0]
carDirection = 1
carbody = pyglet.graphics.vertex_list(4, ('v2f', [-75,0, 75,0, -75,50, 75,50]))
wheel = pyglet.graphics.vertex_list(4, ('v2f', [-20,-20, 20,-20, -20,20, 20,20]))
def drawCar():
glPushMatrix()
glTranslatef(carPosition[0], carPosition[1], carPosition[2])
carbody.draw(GL_TRIANGLE_STRIP)
glColor3f(1, 1, 0)
glPushMatrix()
glTranslatef(-50, 0.0, 0.0)
glRotatef(angle, 0, 0, 1)
wheel.draw(GL_TRIANGLE_STRIP)
glPopMatrix()
glColor3f(1, 0, 0)
glPushMatrix()
glTranslatef(50, 0.0, 0.0)
glRotatef(angle, 0, 0, 1)
wheel.draw(GL_TRIANGLE_STRIP)
glPopMatrix()
glPopMatrix()
@window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity()
glColor3f(0, 0, 1)
drawCar()
def update(dt):
global angle, carPosition, carDirection
angle = angle - 180 * dt * carDirection
carPosition[0] = carPosition[0] + dt * carDirection * 200
if carPosition[0] > 400:
carDirection = -1
elif carPosition[0] < 100:
carDirection = 1
pyglet.clock.schedule_interval(update,1/60.0)
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment