import sys, time, math from pyglet.gl import * from euclid import * window = pyglet.window.Window(512,512) quadric = gluNewQuadric() ballPos = Vector3(0,0,0) ballVel = Vector3(8,4.5,0) @window.event def on_draw(): glClear(GL_COLOR_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-10,10, -10,10, -10,10) glMatrixMode(GL_MODELVIEW) glPushMatrix() glTranslatef(ballPos[0], ballPos[1], ballPos[2]) glColor3f(1,0,0) gluSphere(quadric, 1.0, 32, 16) glPopMatrix() def checkForBounce(): if ballPos[0] > 10.0: ballVel[0] = -ballVel[0] ballPos[0] = 10.0 - (ballPos[0] - 10.0) elif ballPos[0] < -10.0: ballVel[0] = -ballVel[0] ballPos[0] = -10.0 + (-10.0 - ballPos[0]) if ballPos[1] > 10.0: ballVel[1] = -ballVel[1] ballPos[1] = 10.0 - (ballPos[1] - 10.0) elif ballPos[1] < -10.0: ballVel[1] = -ballVel[1] ballPos[1] = -10.0 + (-10.0 - ballPos[1]) def update(dt): global ballPos, ballVel ballPos += ballVel * dt checkForBounce() pyglet.clock.schedule_interval(update,1/60.0) pyglet.app.run()