Created
September 24, 2013 16:04
-
-
Save davepape/6687083 to your computer and use it in GitHub Desktop.
multiple bouncing triangles, using a simple class to hold the triangle data
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
from pyglet.gl import * | |
window = pyglet.window.Window(600,500) | |
class Triangle: | |
def __init__(self,x,y,dx,dy,rot): | |
self.posX = x | |
self.posY = y | |
self.dx = dx | |
self.dy = dy | |
self.rot = rot | |
triangles = [ Triangle(10,10,1,1,0), Triangle(300,10,-1,1,45), Triangle(100,400,1,-1,70) ] | |
triangleVlist = pyglet.graphics.vertex_list(3, ('v2f', [-50,-50, 50,-50, 0,50])) | |
@window.event | |
def on_draw(): | |
global triangles | |
glClear(GL_COLOR_BUFFER_BIT) | |
glLoadIdentity() | |
for tri in triangles: | |
glLoadIdentity() | |
glTranslatef(tri.posX, tri.posY, 0) | |
glRotatef(tri.rot, 0, 0, 1) | |
glColor3f(1, 0, 0.7) | |
triangleVlist.draw(GL_TRIANGLES) | |
def update(dummy): | |
global triangles | |
for tri in triangles: | |
if tri.posX < 0: | |
tri.dx = 1 | |
elif tri.posX > 600: | |
tri.dx = -1 | |
if tri.posY < 0: | |
tri.dy = 1 | |
elif tri.posY > 500: | |
tri.dy = -1 | |
tri.posX += tri.dx * 5 | |
tri.posY += tri.dy * 5 | |
tri.rot += 2 | |
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