# Untitled

a guest
Mar 2nd, 2019
58
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import random
2.
3. import pygame
4. from pygame.locals import *
5.
6. from OpenGL.GL import *
7. from OpenGL.GLU import *
8.
9. class Block:
10.     def __init__(self):
11.         self.types = ['GRASS', 'DIRT', 'STONE', 'AIR']
12.         self.sides = ['BOTTOM', 'TOP', 'LEFT', 'RIGHT', 'FRONT', 'BACK']
13.
14.         self.block_type = None
15.         self.block_position = None
16.         self.is_solid = None
17.
18.     def create_block(self, block_type, block_position):
19.         self.block_type = block_type
20.         self.block_position = block_position
21.
22.         if self.block_type == 'AIR':
23.             self.is_solid = False
24.         else:
25.             self.is_solid = True
26.
27.     def create_cube(self, cube_side):
28.         p0 = (-0.5, -0.5, 0.5)
29.         p1 = (0.5, -0.5,  0.5)
30.         p2 = (0.5, -0.5, -0.5)
31.         p3 = (-0.5, -0.5, -0.5)
32.         p4 = (-0.5, 0.5, 0.5)
33.         p5 = (0.5, 0.5, 0.5)
34.         p6 = (0.5, 0.5, -0.5)
35.         p7 = (-0.5, 0.5, -0.5)
36.
37.         p0 = tuple(l + r for l, r in zip(p0, self.block_position))
38.         p1 = tuple(l + r for l, r in zip(p1, self.block_position))
39.         p2 = tuple(l + r for l, r in zip(p2, self.block_position))
40.         p3 = tuple(l + r for l, r in zip(p3, self.block_position))
41.         p4 = tuple(l + r for l, r in zip(p4, self.block_position))
42.         p5 = tuple(l + r for l, r in zip(p5, self.block_position))
43.         p6 = tuple(l + r for l, r in zip(p6, self.block_position))
44.         p7 = tuple(l + r for l, r in zip(p7, self.block_position))
45.
46.         uv00 = (0, 0)
47.         uv10 = (1, 0)
48.         uv01 = (0, 1)
49.         uv11 = (1, 1)
50.
51.         if cube_side == 'BOTTOM':
53.
54.             glTexCoord2f(*uv11)
55.             glVertex3f(*p0)
56.             glTexCoord2f(*uv01)
57.             glVertex3f(*p1)
58.             glTexCoord2f(*uv00)
59.             glVertex3f(*p2)
60.             glTexCoord2f(*uv10)
61.             glVertex3f(*p3)
62.         elif cube_side == 'TOP':
64.
65.             glTexCoord2f(*uv11)
66.             glVertex3f(*p7)
67.             glTexCoord2f(*uv01)
68.             glVertex3f(*p6)
69.             glTexCoord2f(*uv00)
70.             glVertex3f(*p5)
71.             glTexCoord2f(*uv10)
72.             glVertex3f(*p4)
73.         elif cube_side == 'LEFT':
75.
76.             glTexCoord2f(*uv11)
77.             glVertex3f(*p7)
78.             glTexCoord2f(*uv01)
79.             glVertex3f(*p4)
80.             glTexCoord2f(*uv00)
81.             glVertex3f(*p0)
82.             glTexCoord2f(*uv10)
83.             glVertex3f(*p3)
84.         elif cube_side == 'RIGHT':
86.
87.             glTexCoord2f(*uv11)
88.             glVertex3f(*p5)
89.             glTexCoord2f(*uv01)
90.             glVertex3f(*p6)
91.             glTexCoord2f(*uv00)
92.             glVertex3f(*p2)
93.             glTexCoord2f(*uv10)
94.             glVertex3f(*p1)
95.         elif cube_side == 'FRONT':
97.
98.             glTexCoord2f(*uv11)
99.             glVertex3f(*p4)
100.             glTexCoord2f(*uv01)
101.             glVertex3f(*p5)
102.             glTexCoord2f(*uv00)
103.             glVertex3f(*p1)
104.             glTexCoord2f(*uv10)
105.             glVertex3f(*p0)
106.         elif cube_side == 'BACK':
108.
109.             glTexCoord2f(*uv11)
110.             glVertex3f(*p6)
111.             glTexCoord2f(*uv01)
112.             glVertex3f(*p7)
113.             glTexCoord2f(*uv00)
114.             glVertex3f(*p3)
115.             glTexCoord2f(*uv10)
116.             glVertex3f(*p2)
117.         glEnd()
118.
119.     def draw_cube(self, chunks, x, y, z):
120.         if chunks[x][y][z].block_type == 'AIR':
121.             return
122.         try:
123.             if not chunks[x][y][z+1].is_solid:
124.                 self.create_cube('FRONT')
125.         except:
126.             pass
127.         try:
128.             if not chunks[x][y][z-1].is_solid:
129.                 self.create_cube('BACK')
130.         except:
131.             pass
132.         try:
133.             if not chunks[x][y+1][z].is_solid:
134.                 self.create_cube('TOP')
135.         except:
136.             pass
137.         try:
138.             if not chunks[x][y-1][z].is_solid:
139.                 self.create_cube('BOTTOM')
140.         except:
141.             pass
142.         try:
143.             if not chunks[x+1][y][z].is_solid:
144.                 self.create_cube('RIGHT')
145.         except:
146.             pass
147.         try:
148.             if not chunks[x-1][y][z].is_solid:
149.                 self.create_cube('LEFT')
150.         except:
151.             pass
152.
153. class Chunk:
154.     def __init__(self, world_size, segments):
155.         self.segments = segments
156.         self.world_size = world_size
157.         self.create_chunk()
158.
159.     def list_3D(self, dim):
160.         return [[[None for _ in range(dim[2])] for _ in range(dim[1])] for _ in range(dim[0])]
161.
162.     def create_chunk(self):
163.         chunk_data = self.list_3D([self.world_size, self.world_size, self.world_size])
164.         for i in range(self.world_size):
165.             for j in range(self.world_size):
166.                 for k in range(self.world_size):
167.                     b = Block()
168.                     if self.segments[i+j+k] < 50:
169.                         b.create_block('AIR', (i, j, k))
170.                     else:
171.                         b.create_block('DIRT', (i, j, k))
172.                     chunk_data[i][j][k] = b
173.         for i in range(self.world_size):
174.             for j in range(self.world_size):
175.                 for k in range(self.world_size):
176.                     chunk_data[i][j][k].draw_cube(chunk_data, i, j, k)
177.
178. def main():
179.     pygame.init()
180.     display = (800,600)
181.     pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
182.
183.     gluPerspective(125, (display[0]/display[1]), 1.0, 50.0)
184.
185.     glTranslatef(0.0,0.0, -5)
186.
187.     world_size = 8
188.     segments = [random.randint(0, 100) for _ in range(world_size*3)]
189.
190.     while True:
191.         for event in pygame.event.get():
192.             if event.type == pygame.QUIT:
193.                 pygame.quit()
194.                 quit()
195.
196.         glRotatef(1, 3, 1, 1)
197.         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
198.         b = Chunk(world_size, segments)
199.         pygame.display.flip()
200.         pygame.time.wait(10)
201.
202.
203. main()
RAW Paste Data