import time, math from pyglet.gl import * window = pyglet.window.Window() class TexAlphaSquare: def __init__(self, width, height, xpos, ypos, texturefile, color): self.xpos = xpos self.ypos = ypos self.heading = 0 self.color = color if texturefile: img = pyglet.image.load(texturefile) self.texture = img.get_texture() else: self.texture = None verts = [-width/2.0, -height/2.0, width/2.0, -height/2.0, -width/2.0, height/2.0, width/2.0, height/2.0] texcoords = [0,0, 1,0, 0,1, 1,1] self.vlist = pyglet.graphics.vertex_list(4, ('v2f', verts), ('t2f', texcoords)) def draw(self): glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) if self.texture: glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self.texture.id) glColor4f(self.color[0], self.color[1], self.color[2], self.color[3]) glPushMatrix() glTranslatef(self.xpos, self.ypos, 0) glRotatef(self.heading, 0, 0, 1) self.vlist.draw(GL_TRIANGLE_STRIP) glPopMatrix() if self.texture: glBindTexture(GL_TEXTURE_2D, 0) glDisable(GL_TEXTURE_2D) glDisable(GL_BLEND) objects = [ TexAlphaSquare(640, 480, 0, 0, 'hst9904a.jpg', [1,1,1,1]), TexAlphaSquare(192, 192, 0, 0, 'rocket.tif', [1,1,1,1]) ] @window.event def on_draw(): glClearColor(0, 0, 0, 0) glClear(GL_COLOR_BUFFER_BIT) glLoadIdentity() glTranslatef(320, 240, 0) for o in objects: o.draw() def update(dt): global objects angle = (time.time() % 10) * 36 objects[1].xpos = 192 * math.sin(angle * math.pi/180.0) objects[1].ypos = 192 * math.cos(angle * math.pi/180.0) objects[1].heading = 270 - angle pyglet.clock.schedule_interval(update,1/60.0) pyglet.app.run()