Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import pygame
- from pygame.locals import *
- from OpenGL.GL import *
- from OpenGL.GLU import *
- class Block:
- def __init__(self):
- self.types = ['GRASS', 'DIRT', 'STONE', 'AIR']
- self.sides = ['BOTTOM', 'TOP', 'LEFT', 'RIGHT', 'FRONT', 'BACK']
- self.block_type = None
- self.block_position = None
- self.is_solid = None
- def create_block(self, block_type, block_position):
- self.block_type = block_type
- self.block_position = block_position
- if self.block_type == 'AIR':
- self.is_solid = False
- else:
- self.is_solid = True
- def create_cube(self, cube_side):
- p0 = (-0.5, -0.5, 0.5)
- p1 = (0.5, -0.5, 0.5)
- p2 = (0.5, -0.5, -0.5)
- p3 = (-0.5, -0.5, -0.5)
- p4 = (-0.5, 0.5, 0.5)
- p5 = (0.5, 0.5, 0.5)
- p6 = (0.5, 0.5, -0.5)
- p7 = (-0.5, 0.5, -0.5)
- p0 = tuple(l + r for l, r in zip(p0, self.block_position))
- p1 = tuple(l + r for l, r in zip(p1, self.block_position))
- p2 = tuple(l + r for l, r in zip(p2, self.block_position))
- p3 = tuple(l + r for l, r in zip(p3, self.block_position))
- p4 = tuple(l + r for l, r in zip(p4, self.block_position))
- p5 = tuple(l + r for l, r in zip(p5, self.block_position))
- p6 = tuple(l + r for l, r in zip(p6, self.block_position))
- p7 = tuple(l + r for l, r in zip(p7, self.block_position))
- uv00 = (0, 0)
- uv10 = (1, 0)
- uv01 = (0, 1)
- uv11 = (1, 1)
- if cube_side == 'BOTTOM':
- glBegin(GL_QUADS)
- glTexCoord2f(*uv11)
- glVertex3f(*p0)
- glTexCoord2f(*uv01)
- glVertex3f(*p1)
- glTexCoord2f(*uv00)
- glVertex3f(*p2)
- glTexCoord2f(*uv10)
- glVertex3f(*p3)
- elif cube_side == 'TOP':
- glBegin(GL_QUADS)
- glTexCoord2f(*uv11)
- glVertex3f(*p7)
- glTexCoord2f(*uv01)
- glVertex3f(*p6)
- glTexCoord2f(*uv00)
- glVertex3f(*p5)
- glTexCoord2f(*uv10)
- glVertex3f(*p4)
- elif cube_side == 'LEFT':
- glBegin(GL_QUADS)
- glTexCoord2f(*uv11)
- glVertex3f(*p7)
- glTexCoord2f(*uv01)
- glVertex3f(*p4)
- glTexCoord2f(*uv00)
- glVertex3f(*p0)
- glTexCoord2f(*uv10)
- glVertex3f(*p3)
- elif cube_side == 'RIGHT':
- glBegin(GL_QUADS)
- glTexCoord2f(*uv11)
- glVertex3f(*p5)
- glTexCoord2f(*uv01)
- glVertex3f(*p6)
- glTexCoord2f(*uv00)
- glVertex3f(*p2)
- glTexCoord2f(*uv10)
- glVertex3f(*p1)
- elif cube_side == 'FRONT':
- glBegin(GL_QUADS)
- glTexCoord2f(*uv11)
- glVertex3f(*p4)
- glTexCoord2f(*uv01)
- glVertex3f(*p5)
- glTexCoord2f(*uv00)
- glVertex3f(*p1)
- glTexCoord2f(*uv10)
- glVertex3f(*p0)
- elif cube_side == 'BACK':
- glBegin(GL_QUADS)
- glTexCoord2f(*uv11)
- glVertex3f(*p6)
- glTexCoord2f(*uv01)
- glVertex3f(*p7)
- glTexCoord2f(*uv00)
- glVertex3f(*p3)
- glTexCoord2f(*uv10)
- glVertex3f(*p2)
- glEnd()
- def draw_cube(self, chunks, x, y, z):
- if chunks[x][y][z].block_type == 'AIR':
- return
- try:
- if not chunks[x][y][z+1].is_solid:
- self.create_cube('FRONT')
- except:
- pass
- try:
- if not chunks[x][y][z-1].is_solid:
- self.create_cube('BACK')
- except:
- pass
- try:
- if not chunks[x][y+1][z].is_solid:
- self.create_cube('TOP')
- except:
- pass
- try:
- if not chunks[x][y-1][z].is_solid:
- self.create_cube('BOTTOM')
- except:
- pass
- try:
- if not chunks[x+1][y][z].is_solid:
- self.create_cube('RIGHT')
- except:
- pass
- try:
- if not chunks[x-1][y][z].is_solid:
- self.create_cube('LEFT')
- except:
- pass
- class Chunk:
- def __init__(self, world_size, segments):
- self.segments = segments
- self.world_size = world_size
- self.create_chunk()
- def list_3D(self, dim):
- return [[[None for _ in range(dim[2])] for _ in range(dim[1])] for _ in range(dim[0])]
- def create_chunk(self):
- chunk_data = self.list_3D([self.world_size, self.world_size, self.world_size])
- for i in range(self.world_size):
- for j in range(self.world_size):
- for k in range(self.world_size):
- b = Block()
- if self.segments[i+j+k] < 50:
- b.create_block('AIR', (i, j, k))
- else:
- b.create_block('DIRT', (i, j, k))
- chunk_data[i][j][k] = b
- for i in range(self.world_size):
- for j in range(self.world_size):
- for k in range(self.world_size):
- chunk_data[i][j][k].draw_cube(chunk_data, i, j, k)
- def main():
- pygame.init()
- display = (800,600)
- pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
- gluPerspective(125, (display[0]/display[1]), 1.0, 50.0)
- glTranslatef(0.0,0.0, -5)
- world_size = 8
- segments = [random.randint(0, 100) for _ in range(world_size*3)]
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- glRotatef(1, 3, 1, 1)
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
- b = Chunk(world_size, segments)
- pygame.display.flip()
- pygame.time.wait(10)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement