Created
October 15, 2013 15:14
-
-
Save davepape/6993187 to your computer and use it in GitHub Desktop.
a bouncing ball, with gravitational acceleration pulling it downward
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(80, 0) | |
ballAcc = Vector2(0, 0) | |
gravity = Vector2(0, -98) | |
@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.0, 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, ballXform | |
ballAcc = gravity | |
ballVel += ballAcc * dt | |
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