import sys, time, math from pyglet.gl import * window = pyglet.window.Window(512,512) keys = pyglet.window.key.KeyStateHandler() window.push_handlers(keys) globalTex = pyglet.image.load('building.jpg').get_texture() glEnable(GL_DEPTH_TEST) class Building: def __init__(self, pos, size, texturefile): self.pos = pos self.size = size self.texture = pyglet.image.load(texturefile).get_texture() self.vlist = pyglet.graphics.vertex_list(8, ('v3f', [-.3,0, 0, .3, 0, 0, .3, 5, 0, -.3, 5, 0, .3,0,0, .3,0,-.6, .3,5,-.6, .3, 5, 0]), ('t2f', [0,0, 0.25,0, 0.25,1, 0,1, 0.25,0, 0.5,0, 0.5,1, 0.25,1]) ) def draw(self): glColor3f(1,1,1) glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, globalTex.id) glPushMatrix() glTranslatef(self.pos[0],self.pos[1],self.pos[2]) glScalef(self.size, self.size, self.size) self.vlist.draw(GL_QUADS) glPopMatrix() class Ground: def __init__(self, pos, size, texturefile): self.pos = pos self.size = size self.texture = pyglet.image.load(texturefile).get_mipmapped_texture() def draw(self): glColor3f(1,1,1) glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self.texture.id) glPushMatrix() glScalef(self.size, self.size, self.size) glBegin(GL_QUADS) glTexCoord2f(0,10) glVertex3f(-1, 0, -1) glTexCoord2f(10,10) glVertex3f(1, 0, -1) glTexCoord2f(10,0) glVertex3f(1, 0, 1) glTexCoord2f(0,0) glVertex3f(-1, 0, 1) glEnd() glPopMatrix() objects = [ Ground([0,0,0], 100, 'cobblestones.jpg'), Building([0,0,-20],10,'building.jpg'), Building([10,0,-20],10,'building.jpg'), Building([-10,0,-20],10,'building.jpg'), Building([-10,0,0],10,'building.jpg'), Building([0,0,0],10,'building.jpg'), Building([10,0,0],10,'building.jpg') ] position = [0, 10, 50] heading = 0 tilt = 0 color_buffer = pyglet.image.get_buffer_manager().get_color_buffer() texangle = 0 def drawTexture(): global globalTex, texangle glClearColor(0,0,1,0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0,10, 0,10) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glRotatef(texangle,0,0,1) glDisable(GL_TEXTURE_2D) glColor3f(1,0,0) glBegin(GL_TRIANGLES) glVertex2f(0,0) glVertex2f(10,0) glVertex2f(5,10) glEnd() glBindTexture(GL_TEXTURE_2D,globalTex.id) color_buffer.blit_to_texture(GL_TEXTURE_2D,0,0,0,0) @window.event def on_draw(): drawTexture() global position, heading glClearColor(0,0,0,0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60.0, float(window.width) / window.height, 0.1, 1000) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glRotatef(-tilt, 1, 0, 0) glRotatef(-heading, 0, 1, 0) glTranslatef(-position[0], -position[1], -position[2]) for o in objects: o.draw() def update(dt): global position, heading, tilt if keys[pyglet.window.key.LEFT]: position[0] += -10 * dt * math.sin(math.radians(heading+90)) position[2] += -10 * dt * math.cos(math.radians(heading+90)) elif keys[pyglet.window.key.RIGHT]: position[0] += -10 * dt * math.sin(math.radians(heading-90)) position[2] += -10 * dt * math.cos(math.radians(heading-90)) elif keys[pyglet.window.key.UP]: position[1] += 10 * dt elif keys[pyglet.window.key.DOWN]: position[1] -= 10 * dt elif keys[pyglet.window.key.W]: position[0] += -10 * dt * math.sin(math.radians(heading)) * math.cos(tilt) position[1] += 10 * dt * math.sin(math.radians(tilt)) position[2] += -10 * dt * math.cos(math.radians(heading)) * math.cos(tilt) elif keys[pyglet.window.key.S]: position[0] += 10 * dt * math.sin(math.radians(heading)) position[2] += 10 * dt * math.cos(math.radians(heading)) elif keys[pyglet.window.key.A]: heading += 90 * dt elif keys[pyglet.window.key.D]: heading -= 90 * dt elif keys[pyglet.window.key.Z]: tilt += 90 * dt elif keys[pyglet.window.key.C]: tilt -= 90 * dt global texangle texangle += 10 pyglet.clock.schedule_interval(update,1/60.0) pyglet.app.run()