import sys, time, math from pyglet.gl import * from random import * window = pyglet.window.Window(768,768) startTime = time.time() class Square: def __init__(self, width=1.0, height=1.0, xpos=0.0, ypos=0.0, color=[1,1,1], texturefile=None): self.width = width self.height = height self.xpos = xpos self.ypos = ypos self.color = color if texturefile: self.texture = pyglet.image.load(texturefile).get_texture() else: self.texture = None 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) glBegin(GL_QUADS) glTexCoord2f(0,0) glVertex2f(-self.width/2.0, -self.height/2.0) glTexCoord2f(1,0) glVertex2f(self.width/2.0, -self.height/2.0) glTexCoord2f(1,1) glVertex2f(self.width/2.0, self.height/2.0) glTexCoord2f(0,1) glVertex2f(-self.width/2.0, self.height/2.0) glEnd() glDisable(GL_TEXTURE_2D) else: glBegin(GL_QUADS) glVertex2f(-self.width/2.0, -self.height/2.0) glVertex2f(self.width/2.0, -self.height/2.0) glVertex2f(self.width/2.0, self.height/2.0) glVertex2f(-self.width/2.0, self.height/2.0) glEnd() glPopMatrix() ground = Square(30,30,texturefile='cobblestones.jpg') square1 = Square(2,2,texturefile='flowertex.jpg') def drawWorld(): # draw ground glPushMatrix() glTranslatef(0,-5,0) glRotatef(-90,1,0,0) ground.draw() glPopMatrix() # draw near square square1.draw() # draw moving square glPushMatrix() glTranslatef(-3, -4, 50*math.sin(time.time())-45) square1.draw() glPopMatrix() # draw spinning square glPushMatrix() glRotatef((time.time() - startTime)*45, 0, 1, 0) glTranslatef(4, 2, 0) square1.draw() glPopMatrix() @window.event def on_draw(): glClear(GL_ACCUM_BUFFER_BIT) for j in range(-2,3,1): for i in range(-2,3,1): ex = i / 20.0 + uniform(-0.03,0.03) ey = j / 20.0 + uniform(-0.03,0.03) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() glFrustum((-7.5-ex)*1/15, (7.5-ex)*1/15, (-7.5-ey)*1/15, (7.5-ey)*1/15, 1, 100) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glTranslatef(-ex, -ey, -15.0) drawWorld() glAccum(GL_ACCUM, 1.0/25) glAccum(GL_RETURN, 1.0) def update(dt): pass pyglet.clock.schedule_interval(update,1/60.0) glEnable(GL_DEPTH_TEST) glLineWidth(2) pyglet.app.run()