Skip to content

Instantly share code, notes, and snippets.

@davepape
Created October 15, 2013 15:13
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/6993177 to your computer and use it in GitHub Desktop.
Save davepape/6993177 to your computer and use it in GitHub Desktop.
a bouncing ball, using a vector for movement
import sys, time, math
from pyglet.gl import *
from euclid import *
window = pyglet.window.Window(512,512)
quadric = gluNewQuadric()
ballPos = Vector2(256,256)
ballVel = Vector2(200,145)
@window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT)
glPushMatrix()
glTranslatef(ballPos[0], ballPos[1], 0)
glColor3f(1,0,0)
gluDisk(quadric, 0, 20, 32, 1)
glPopMatrix()
def checkForBounce():
if ballPos[0] > 512.0:
ballVel[0] = -ballVel[0]
ballPos[0] = 512.0 - (ballPos[0] - 512.0)
elif ballPos[0] < 0.0:
ballVel[0] = -ballVel[0]
ballPos[0] = -ballPos[0]
if ballPos[1] > 512.0:
ballVel[1] = -ballVel[1]
ballPos[1] = 512.0 - (ballPos[1] - 512.0)
elif ballPos[1] < 0.0:
ballVel[1] = -ballVel[1]
ballPos[1] = -ballPos[1]
def update(dt):
global ballPos, ballVel
ballPos += ballVel * dt
checkForBounce()
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