I am trying to create a 3d rubiks cube, however the dimensions are not aligning with the dimensions set. When I set the dimensions to 3 and create a 3x3x3 cube, I end up creating a 4x4x4 cube. However, when I set the dimension to 2, it creates a 3x3x3 cube and when the dimension is set to 1, a 1x1x1 cube is created. Can someone help with this? Thanks!
Code for cube:
#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()
Code for Piece:
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)