import sys, time, math, os, random from pyglet.gl import * window = pyglet.window.Window() objects = [] class Square: def __init__(self, width, height, xpos, ypos, color, texturefile): self.xpos = xpos self.ypos = ypos self.color = color if texturefile: self.texture = pyglet.image.load(texturefile).get_texture() else: self.texture = None x = width/2.0 y = height/2.0 self.vlist = pyglet.graphics.vertex_list(4, ('v2f', [-x,-y, x,-y, -x,y, x,y]), ('t2f', [0,0, 1,0, 0,1, 1,1])) def draw(self): glPushMatrix() glTranslatef(self.xpos, self.ypos, 0) glColor3f(self.color[0],self.color[1],self.color[2]) if self.texture: glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self.texture.id) self.vlist.draw(GL_TRIANGLE_STRIP) if self.texture: glDisable(GL_TEXTURE_2D) glPopMatrix() @window.event def on_draw(): glClearColor(0, 0.3, 0.5, 0) glClear(GL_COLOR_BUFFER_BIT) for o in objects: o.draw() @window.event def on_key_press(key,modifiers): global objects if key == pyglet.window.key.LEFT: objects[0].xpos -= 5 elif key == pyglet.window.key.RIGHT: objects[0].xpos += 5 elif key == pyglet.window.key.UP: objects[0].ypos += 5 elif key == pyglet.window.key.DOWN: objects[0].ypos -= 5 starttime = time.time() def update(dummy): global objects objects[1].xpos += random.uniform(-1, 1) objects[1].ypos += random.uniform(-1, 1) square1 = Square(120, 120, 300, 200, [1,1,1], "texture.jpg") objects.append(square1) square2 = Square(70, 70, 300, 400, [1,0,0], None) objects.append(square2) pyglet.clock.schedule_interval(update,1/60.0) pyglet.app.run()