title: Python: How do I generate minecraft style terrain with pyglet?
body: <p>I'm currently trying to make a Minecraft clone using pyglet and python for fun, and I found a nice tutorial which I used to create a player class with movement, and a 3D block that generates in the scene. </p>
  
  <p>Now I want to create some sort of a terrain, I read about terrain generating and I stumbled upon a function called "noise function" which seems to fit nicely with one I'm trying to do. sadly I don't really know how to implement it. :(</p>
  
  <p>At first I tried to generate a flat terrain by creating a function in the Model class which contains the code that creates a cube, and then I create a loop that generates all the numbers between 1 and 20 for example, and use them as values to the cube function. but it didn't work so i had to remove it :( </p>
  
  <p>I do think that it might be too soon to implement a terrain with the noise function seeing the stage I'm currently in. so creating an endless flat terrain that do work will be good as well :D</p>
  
  <p>If you want to check the program, You'll need to press 'E' when you run the code to enable a mouse lock, which will let you to move the mouse and the player in the scene.</p>
  
  <p>Here is the code I have:</p>
  
  <pre><code>from pyglet.gl import *
  from pyglet.window import key
  import math
  import random
  from random import *
  
  
  class Model:
      def get_tex(self, file):
          tex = pyglet.image.load(file).texture
          glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
          glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
          return pyglet.graphics.TextureGroup(tex)
  
      def __init__(self):
  
          self.top = self.get_tex('grass_top.png')
          self.side = self.get_tex('grass_side.png')
          self.bottom = self.get_tex('dirt.png')
  
          self.batch = pyglet.graphics.Batch()
  
          tex_coords = ('t2f', (0, 0, 1, 0, 1, 1, 0, 1,))
  
          x, y, z = randint(0, 5), randint(0, 5), -1
          X, Y, Z = x+1, y+1, z+1
  
          self.batch.add(4, GL_QUADS, self.side, ('v3f', (x, y, z, x, y, Z, x, Y, Z, x, Y, z,)), tex_coords)
          self.batch.add(4, GL_QUADS, self.side, ('v3f', (X, y, Z, X, y, z, X, Y, z, X, Y, Z,)), tex_coords)
          self.batch.add(4, GL_QUADS, self.bottom, ('v3f', (x, y, z, X, y, z, X, y, Z, x, y, Z,)), tex_coords)
          self.batch.add(4, GL_QUADS, self.top, ('v3f', (x, Y, Z, X, Y, Z, X, Y, z, x, Y, z, )), tex_coords)
          self.batch.add(4, GL_QUADS, self.side, ('v3f', (X, y, z, x, y, z, x, Y, z, X, Y, z, )), tex_coords)
          self.batch.add(4, GL_QUADS, self.side, ('v3f', (x, y, Z, X, y, Z, X, Y, Z, x, Y, Z, )), tex_coords)
  
      def draw(self):
          self.batch.draw()
  
  
  class Player:
      def __init__(self, pos=(0, 0, 0), rot=(0, 0)):
          self.pos = list(pos)
          self.rot = list(rot)
  
      def mouse_motion(self, dx, dy):
          dx /= 8
          dy /= 8
          self.rot[0] += dy
          self.rot[1] -= dx
          if self.rot[0] &gt; 90:
              self.rot[0] = 90
          elif self.rot[0] &lt; -90:
              self.rot[0] = -90
  
      def update(self, dt, keys):
          s = dt*10
          rotation_y = -self.rot[1]/180*math.pi
          dx, dz = s*math.sin(rotation_y), s*math.cos(rotation_y)
          if keys[key.W]:
              self.pos[0] += dx
              self.pos[2] -= dz
          if keys[key.S]:
              self.pos[0] -= dx
              self.pos[2] += dz
          if keys[key.A]:
              self.pos[0] -= dz
              self.pos[2] -= dx
          if keys[key.D]:
              self.pos[0] += dz
              self.pos[2] += dx
  
          if keys[key.SPACE]:
              self.pos[1] += s
          if keys[key.LSHIFT]:
              self.pos[1] -= s
  
  
  class Window(pyglet.window.Window):
      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],)
  
      def Projection(self):
          glMatrixMode(GL_PROJECTION)
          glLoadIdentity()
  
      def Model(self):
          glMatrixMode(GL_MODELVIEW)
          glLoadIdentity()
  
      def set2d(self):
          self.Projection()
          gluOrtho2D(0, self.width, 0, self.height)
          self.Model()
  
      def set3d(self):
          self.Projection()
          gluPerspective(70, self.width / self.height, 0.05, 1000)
          self.Model()
  
      def setLock(self, state): self.lock = state; self.set_exclusive_mouse(state)
      lock = False; mouse_lock = property(lambda self: self.lock, setLock)
  
      def __init__(self, *args, **kwargs):
          super().__init__(*args, **kwargs)
          self.set_minimum_size(300, 200)
          self.keys = key.KeyStateHandler()
          self.push_handlers(self.keys)
          pyglet.clock.schedule(self.update)
  
          self.model = Model()
          self.player = Player((0.5, 1.5, 1.5), (-30, 0))
  
      def on_mouse_motion(self, x, y, dx, dy):
          if self.mouse_lock:
              self.player.mouse_motion(dx, dy)
  
      def on_key_press(self, KEY, MOD):
          if KEY == key.ESCAPE:
              self.close()
          elif KEY == key.E:
              self.mouse_lock = not self.mouse_lock
  
      def update(self, dt):
          self.player.update(dt, self.keys)
  
      def on_draw(self):
          self.clear()
          self.set3d()
          self.push(self.player.pos, self.player.rot)
          self.model.draw()
          glPopMatrix()
  
  
  if __name__ == '__main__':
      window = Window(width=854, height=480, caption='Minecraft', resizable=True)
      glClearColor(0.5, 0.7, 1, 1)
      glEnable(GL_DEPTH_TEST)
      # glEnable(GL_CULL_FACE)
      pyglet.app.run()
  
  </code></pre>
  
  <p>My project use's some images to create the dirt texture. so here is a link to a website which has the images and the main program which you can see above you: 
  <a href="http://www.mediafire.com/file/7iolhmh1hqj9516/Basic+Pyglet+Cube.rar" rel="nofollow noreferrer">http://www.mediafire.com/file/7iolhmh1hqj9516/Basic+Pyglet+Cube.rar</a></p>
  
  <p>I already tried to generate more then one block using a list and recalling a function</p>