Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.91 KB | None | 0 0
  1. from pyglet.gl import *
  2. from pyglet.window import key
  3. import math
  4.  
  5.  
  6. class Model:
  7.  
  8.     def get_tex(self,file):
  9.         tex = pyglet.image.load(file).texture
  10.         glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
  11.         glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
  12.         return pyglet.graphics.TextureGroup(tex)
  13.  
  14.     def __init__(self, argx, argy, argz):
  15.  
  16.         self.top = self.get_tex('grass_top.png')
  17.         self.side = self.get_tex('grass_side.png')
  18.         self.bottom = self.get_tex('dirt.png')
  19.  
  20.         self.batch = pyglet.graphics.Batch()
  21.  
  22.         tex_coords = ('t2f',(0,0, 1,0, 1,1, 0,1, ))
  23.  
  24.         x,y,z = argx,argy,-argz
  25.         X,Y,Z = x+1,y+1,z+1
  26.  
  27.         self.batch.add(4,GL_QUADS,self.side,('v3f', (x, y, z, x, y, Z, x, Y, Z, x, Y, z, )), tex_coords)
  28.         self.batch.add(4,GL_QUADS,self.side,('v3f', (X, y, Z, X, y, z, X, Y, z, X, Y, Z, )), tex_coords)
  29.         self.batch.add(4,GL_QUADS,self.bottom,('v3f', (x, y, z, X, y, z, X, y, Z, x, y, Z, )), tex_coords)
  30.         self.batch.add(4,GL_QUADS,self.top,('v3f', (x, Y, Z, X, Y, Z, X, Y, z, x, Y, z, )), tex_coords)
  31.         self.batch.add(4,GL_QUADS,self.side,('v3f', (X, y, z, x, y, z, x, Y, z, X, Y, z, )), tex_coords)
  32.         self.batch.add(4,GL_QUADS,self.side,('v3f', (x, y, Z, X, y, Z, X, Y, Z, x, Y, Z, )), tex_coords)
  33.  
  34.  
  35.     def draw(self):
  36.         self.batch.draw()
  37.  
  38.  
  39.  
  40. class Player:
  41.     def __init__(self,pos=(0,0,0),rot=(0,0)):
  42.         self.pos = list(pos)
  43.         self.rot = list(rot)
  44.  
  45.     def mouse_motion(self,dx,dy):
  46.         dx/=8; dy/=8; self.rot[0]+=dy; self.rot[1]-=dx
  47.         if self.rot[0]>90: self.rot[0] = 90
  48.         elif self.rot[0]<-90: self.rot[0] = -90
  49.  
  50.     def update(self,dt,keys):
  51.         s = dt*10
  52.         rotY = -self.rot[1]/180*math.pi
  53.         dx,dz = s*math.sin(rotY),s*math.cos(rotY)
  54.         if keys[key.W]: self.pos[0]+=dx; self.pos[2]-=dz
  55.         if keys[key.S]: self.pos[0]-=dx; self.pos[2]+=dz
  56.         if keys[key.A]: self.pos[0]-=dz; self.pos[2]-=dx
  57.         if keys[key.D]: self.pos[0]+=dz; self.pos[2]+=dx
  58.  
  59.         if keys[key.SPACE]: self.pos[1]+=s
  60.         if keys[key.LSHIFT]: self.pos[1]-=s
  61.  
  62.  
  63. class Window(pyglet.window.Window):
  64.  
  65.     def push(self,pos,rot): glPushMatrix(); glRotatef(-rot[0],1,0,0); glRotatef(-rot[1],0,1,0); glTranslatef(-pos[0],-pos[1],-pos[2],)
  66.     def Projection(self): glMatrixMode(GL_PROJECTION); glLoadIdentity()
  67.     def Model(self): glMatrixMode(GL_MODELVIEW); glLoadIdentity()
  68.     def set2d(self): self.Projection(); gluOrtho2D(0,self.width,0,self.height); self.Model()
  69.     def set3d(self): self.Projection(); gluPerspective(70,self.width/self.height,0.05,1000); self.Model()
  70.  
  71.     def setLock(self,state): self.lock = state; self.set_exclusive_mouse(state)
  72.     lock = False; mouse_lock = property(lambda self:self.lock,setLock)
  73.  
  74.     def __init__(self,*args,**kwargs):
  75.         super().__init__(*args,**kwargs)
  76.         self.set_minimum_size(300,200)
  77.         self.keys = key.KeyStateHandler()
  78.         self.push_handlers(self.keys)
  79.         pyglet.clock.schedule(self.update)
  80.  
  81.         self.model = Model(0,0,0)
  82.         self.model2 = Model(2,0,0)
  83.         self.model3 = Model(1,1,0)
  84.         self.player = Player((0.5,1.5,1.5),(-30,0))
  85.  
  86.     def on_mouse_motion(self,x,y,dx,dy):
  87.         if self.mouse_lock: self.player.mouse_motion(dx,dy)
  88.  
  89.     def on_key_press(self,KEY,MOD):
  90.         if KEY == key.ESCAPE: self.close()
  91.         elif KEY == key.E: self.mouse_lock = not self.mouse_lock
  92.  
  93.     def update(self,dt):
  94.         self.player.update(dt,self.keys)
  95.  
  96.     def on_draw(self):
  97.         self.clear()
  98.         self.set3d()
  99.         self.push(self.player.pos,self.player.rot)
  100.         self.model.draw()
  101.         glPopMatrix()
  102.  
  103.  
  104. if __name__ == '__main__':
  105.     window = Window(width=854,height=480,caption='Minecraft',resizable=True)
  106.     glClearColor(0.5,0.7,1,1)
  107.     glEnable(GL_DEPTH_TEST)
  108.     #glEnable(GL_CULL_FACE)
  109.     pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement