我想创建一个3D立方体rubiks不过尺寸都没有设定尺寸对齐。 当我设置的尺寸为3,创建一个3x3x3的立方体,我结束了创建型4x4x4立方体。 然而,当我设置尺寸为2,它创建一个3x3x3的立方体,并且当尺寸被设置为1,则创建一个1x1x1立方体。 有人能帮助呢? 谢谢!
代码立方体:
#imports
dim = 3
cube = [[['#' for c in range(dim)] for c in range(dim)] for r in range(dim)]
for i in range(dim):
for j in range(dim):
for k in range(dim):
print(cube[i][j][k], end='')
print()
print()
for i in range(dim):
for j in range(dim):
for k in range(dim):
scale = 0.5
cube[i][j][k] = Piece(i * scale, j * scale, k * scale, scale)
def Cube():
for i in range(dim):
for j in range(dim):
for k in range(dim):
p = cube[i][j][k]
p.draw()
def main():
pygame.init()
display = (1200, 1000)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
gluPerspective(45, (display[0] / display[1]), 0.1, 50)
glClearColor(0.6, 0.6, 0.6, 0)
glTranslatef(0.0, 0.0, -5)
glRotatef(45, 1, 1, 0)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
Cube()
pygame.display.flip()
pygame.time.wait(10)
main()
代码海贼王:
from OpenGL.GL import *
colors = (
# white
(1, 1, 1),
# blue
(1, 1, 0),
# orange
(1, 0, 0),
# red
(1, 0.5, 0.1),
# green
(0, 1, 0),
# yellow
(0, 0, 1),
)
edges = [(0, 1), (1, 2), (2, 3), (3, 0), (4, 5), (5, 6),
(6, 7), (7, 4), (0, 4), (1, 5), (2, 6), (3, 7)]
surfaces = [(0, 1, 2, 3), (5, 4, 7, 6), (4, 0, 3, 7), (1, 5, 6, 2),
(4, 5, 1, 0), (3, 2, 6, 7)]
class Piece:
def __init__(self, x, y, z, length):
self.x = x
self.y = y
self.z = z
self.len = length
self.v = [
(self.x - self.len, self.y - self.len, self.z - self.len),
(self.x + self.len, self.y - self.len, self.z - self.len),
(self.x + self.len, self.y + self.len, self.z - self.len),
(self.x - self.len, self.y + self.len, self.z - self.len),
(self.x - self.len, self.y - self.len, self.z + self.len),
(self.x + self.len, self.y - self.len, self.z + self.len),
(self.x + self.len, self.y + self.len, self.z + self.len),
(self.x - self.len, self.y + self.len, self.z + self.len),
]
def getVertices(self):
return self.x, self.y, self.z
def draw(self):
glEnable(GL_DEPTH_TEST)
glLineWidth(5)
glColor3fv((0, 0, 0))
glBegin(GL_LINES)
for edge in edges:
glVertex3fv(self.v[edge[0]])
glVertex3fv(self.v[edge[1]])
glEnd()
glEnable(GL_POLYGON_OFFSET_FILL)
glPolygonOffset(1.0, 1.0)
glBegin(GL_QUADS)
for i, quad in enumerate(surfaces):
glColor3fv(colors[i])
for iv in quad:
glVertex3fv(self.v[iv])
glEnd()
glDisable(GL_POLYGON_OFFSET_FILL)