''' 9/29/15 Rubik's cube Peter Farrell It's controlled by keystrokes ''' from visual import * import msvcrt SIDE = 10 DEPTH = 0.25 CUBESIDE = 3*SIDE + 3*DEPTH scene = display(height = 600, width = 600) scene.ambient = color.gray(0.7) faces = [] #list to store all 54 faces class Face: def __init__(self,length,height, width,pos,color,axis): self.pos = pos self.color = color self.axis = axis self.length = length self.height = height self.width = width self.face_obj = box(axis = self.axis, length = self.length, height = self.height, width = self.width, pos = self.pos, color = self.color) #define the rotations. #horizontal: def horiz_rot(self): rate(1200) self.face_obj.rotate(angle=-pi/200, axis=(0,1,0), origin=(0,0,0)) #vertical: def vert_rot(self): rate(1200) self.face_obj.rotate(angle=-pi/200, axis=(1,0,0), origin=(0,0,0)) #lateral?: def lateral_rot(self): rate(1200) self.face_obj.rotate(angle=-pi/200, axis=(0,0,1), origin=(0,0,0)) def top(self,faceList): if self.face_obj.y > SIDE/2: #if you're on the "top" faceList.append(self) #add yourself to the list def middle_horiz(self,faceList): if abs(self.face_obj.y) < 2: faceList.append(self) def bottom(self,faceList): if self.face_obj.y < -SIDE: faceList.append(self) def right(self,faceList): if self.face_obj.x > SIDE/2: faceList.append(self) def middle_vert(self,faceList): if abs(self.face_obj.pos.x) < 2: faceList.append(self) def left(self,faceList): if self.face_obj.x < -SIDE: faceList.append(self) def front(self,faceList): if self.face_obj.z > SIDE/2: faceList.append(self) def middle_lat(self,faceList): if abs(self.face_obj.pos.z) < 2: faceList.append(self) def back(self,faceList): if self.face_obj.z < -SIDE: faceList.append(self) #front face for i in range(3): for j in range(3): face1 = Face(SIDE, SIDE, DEPTH, (-CUBESIDE/2 + SIDE/2 + (SIDE + DEPTH)*i, CUBESIDE/2 - SIDE/2 - (SIDE + DEPTH)*j, -CUBESIDE/2 + 3*SIDE + 3*DEPTH), color.red, (1,0,0)) faces.append(face1) #back face for i in range(3): for j in range(3): face1 = Face(SIDE, SIDE, DEPTH, (-CUBESIDE/2 + SIDE/2 + (SIDE + DEPTH)*i, CUBESIDE/2 - SIDE/2 - (SIDE + DEPTH)*j, -CUBESIDE/2), color.orange, axis = (1,0,0)) faces.append(face1) #top face for i in range(3): for j in range(3): face1 = Face(SIDE, DEPTH, SIDE, (-CUBESIDE/2 + SIDE/2 + (SIDE + DEPTH)*j, -CUBESIDE/2 + 3*SIDE + 3*DEPTH, -CUBESIDE/2 + SIDE/2 + (SIDE + DEPTH)*i), color.green, axis = (0,0,1)) faces.append(face1) #bottom face for i in range(3): for j in range(3): face1 = Face(SIDE, DEPTH, SIDE, (-CUBESIDE/2 + SIDE/2 + (SIDE + DEPTH)*j, -CUBESIDE/2, -CUBESIDE/2 + SIDE/2 + (SIDE + DEPTH)*i), color.blue, (0,0,1)) faces.append(face1) #left face for i in range(3): for j in range(3): face1 = Face(DEPTH, SIDE, SIDE, (-CUBESIDE/2 + 0, CUBESIDE/2 - SIDE/2 - (SIDE + DEPTH)*j, CUBESIDE/2 - SIDE/2 - (SIDE + DEPTH)*i), color.yellow, (1,0,0)) faces.append(face1) #right face for i in range(3): for j in range(3): face1 = Face(DEPTH, SIDE, SIDE, (-CUBESIDE/2 + 3*SIDE + 3*DEPTH, CUBESIDE/2 - SIDE/2 - (SIDE + DEPTH)*j, -CUBESIDE/2 + SIDE/2 + (SIDE + DEPTH)*i), color.white, (1,0,0)) faces.append(face1) while True: #check for keystrokes s = scene.kb.getkey() if s == '1': #if 1 is pressed faceList = [] #check if you're a candidate for the "left" rotation for face in faces: face.left(faceList) #if so, join the faceList for i in range(100): #then rotate slowly for face in faceList: face.vert_rot() if s == '2': #if 2 is pressed faceList = [] #check if you're a candidate for the rotation for face in faces: face.middle_vert(faceList) #if so, join the faceList for i in range(100): #then rotate slowly for face in faceList: face.vert_rot() if s == '3': #if 3 is pressed faceList = [] #check if you're a candidate for the rotation for face in faces: face.right(faceList) #if so, join the faceList for i in range(100): #then rotate slowly for face in faceList: face.vert_rot() if s == '4': #if 4 is pressed faceList = [] #check if you're a candidate for the rotation for face in faces: face.top(faceList) #if so, join the faceList for i in range(100): #then rotate slowly for face in faceList: face.horiz_rot() if s == '5': #if 5 is pressed faceList = [] #check if you're a candidate for the rotation for face in faces: face.middle_horiz(faceList) #if so, join the faceList for i in range(100): #then rotate slowly for face in faceList: face.horiz_rot() if s == '6': #if 6 is pressed faceList = [] #check if you're a candidate for the rotation for face in faces: face.bottom(faceList) #if so, join the faceList for i in range(100): #then rotate slowly for face in faceList: face.horiz_rot() if s == '7': #if 7 is pressed faceList = [] #check if you're a candidate for the rotation for face in faces: face.front(faceList) #if so, join the faceList for i in range(100): #then rotate slowly for face in faceList: face.lateral_rot() if s == '8': #if 8 is pressed faceList = [] #check if you're a candidate for the rotation for face in faces: face.middle_lat(faceList) #if so, join the faceList for i in range(100): #then rotate slowly for face in faceList: face.lateral_rot() if s == '9': #if 9 is pressed faceList = [] #check if you're a candidate for the rotation for face in faces: face.back(faceList) #if so, join the faceList for i in range(100): #then rotate slowly for face in faceList: face.lateral_rot()