import pygame
import sys
pygame.init()
class moFang:
""" 1: Yellow, 2: White, 3: Red, 4: Orange, 5: Blue, 6: Green """
def __init__(self, shang: str, xia: str, qian: str, hou: str, zuo: str, you: str):
self.shang = [list(map(int, i.split(" "))) for i in shang.split("\n")]
self.xia = [list(map(int, i.split(" "))) for i in xia.split("\n")]
self.qian = [list(map(int, i.split(" "))) for i in qian.split("\n")]
self.hou = [list(map(int, i.split(" "))) for i in hou.split("\n")]
self.you = [list(map(int, i.split(" "))) for i in you.split("\n")]
self.zuo = [list(map(int, i.split(" "))) for i in zuo.split("\n")]
self.caoZuoList = [self.R, self.U, self.F, self.L, self.D, self.B, self.M, self.E, self.x, self.y, self.z, self.u, self.r, self.f, self.l, self.d, self.b, self.s]
self.zhiLing = ["R", "R'", "R2", "U", "U'", "U2", "F", "F'", "F2", "L", "L'", "L2", "D", "D'", "D2", "B", "B'", "B2", "M", "M'", "M2", "E", "E'", "E2", "x", "x'", "x2", "y", "y'", "y2", "z", "z'", "z2", "u", "u'", "u2", "r", "r'", "r2", "f", "f'", "f2", "l", "l'", "l2", "d", "d'", "d2", "b", "b'", "b2", "s", "s'", "s2"]
def xiaZhuan(self, List):
List[0][1], List[1][2], List[2][1], List[1][0], List[0][0], List[0][2], List[2][2], List[2][0] = List[1][0], List[0][1], List[1][2], List[2][1], List[2][0], List[0][0], List[0][2], List[2][2]
return List
def R(self):
tmp1, tmp2, tmp3 = self.shang[0][2], self.shang[1][2], self.shang[2][2]
self.shang[0][2], self.shang[1][2], self.shang[2][2] = self.qian[0][2], self.qian[1][2], self.qian[2][2]
self.qian[0][2], self.qian[1][2], self.qian[2][2] = self.xia[0][2], self.xia[1][2], self.xia[2][2]
self.xia[0][2], self.xia[1][2], self.xia[2][2] = self.hou[0][2], self.hou[1][2], self.hou[2][2]
self.hou[0][2], self.hou[1][2], self.hou[2][2] = tmp1, tmp2, tmp3
self.you = self.xiaZhuan(self.you)
def U(self):
tmp = self.qian[0].copy()
self.qian[0] = self.you[0].copy()
self.you[0] = self.hou[2][::-1]
self.hou[2] = self.zuo[0][::-1]
self.zuo[0] = tmp
self.shang = self.xiaZhuan(self.shang)
def F(self):
self.qian = self.xiaZhuan(self.qian)
tmp = self.shang[2]
self.shang[2] = [self.zuo[2][2], self.zuo[1][2], self.zuo[0][2]]
self.zuo[2][2], self.zuo[1][2], self.zuo[0][2] = self.xia[0][::-1]
self.xia[0] = [self.you[2][0], self.you[1][0], self.you[0][0]]
self.you[0][0], self.you[1][0], self.you[2][0] = tmp
def D(self):
self.xia = self.xiaZhuan(self.xia)
tmp = self.qian[2].copy()
self.qian[2] = self.zuo[2].copy()
self.zuo[2] = self.hou[0].copy()
self.hou[0] = self.you[2].copy()
self.you[2] = tmp
def L(self):
self.zuo = self.xiaZhuan(self.zuo)
tmp1, tmp2, tmp3 = self.shang[0][0], self.shang[1][0], self.shang[2][0]
self.shang[0][0], self.shang[1][0], self.shang[2][0] = self.hou[0][0], self.hou[1][0], self.hou[2][0]
self.hou[0][0], self.hou[1][0], self.hou[2][0] = self.xia[0][0], self.xia[1][0], self.xia[2][0]
self.xia[0][0], self.xia[1][0], self.xia[2][0] = self.qian[0][0], self.qian[1][0], self.qian[2][0]
self.qian[0][0], self.qian[1][0], self.qian[2][0] = tmp1, tmp2, tmp3
def B(self):
self.hou = self.xiaZhuan(self.hou)
tmp = self.shang[0]
self.shang[0] = [self.you[0][2], self.you[1][2], self.you[2][2]]
self.you[0][2], self.you[1][2], self.you[2][2] = self.xia[2]
self.xia[2] = [self.zuo[0][0], self.zuo[1][0], self.zuo[2][0]]
self.zuo[0][0], self.zuo[1][0], self.zuo[2][0] = tmp
def M(self):
tmp1, tmp2, tmp3 = self.shang[0][1], self.shang[1][1], self.shang[2][1]
self.shang[0][1], self.shang[1][1], self.shang[2][1] = self.hou[0][1], self.hou[1][1], self.hou[2][1]
self.hou[0][1], self.hou[1][1], self.hou[2][1] = self.xia[0][1], self.xia[1][1], self.xia[2][1]
self.xia[0][1], self.xia[1][1], self.xia[2][1] = self.qian[0][1], self.qian[1][1], self.qian[2][1]
self.qian[0][1], self.qian[1][1], self.qian[2][1] = tmp1, tmp2, tmp3
def E(self):
tmp = self.qian[1].copy()
self.qian[1] = self.zuo[1].copy()
self.zuo[1] = self.hou[1].copy()
self.hou[1] = self.you[1].copy()
self.you[1] = tmp
def x(self):
self.mingLing("R M' L'")
def y(self):
self.mingLing("U E' D'")
def z(self):
self.mingLing("F y M y' B'")
def r(self):
self.mingLing("R M'")
def u(self):
self.mingLing("U E'")
def f(self):
self.mingLing("z D")
def d(self):
self.mingLing("D E")
def l(self):
self.mingLing("x' R")
def b(self):
self.mingLing("z' F")
def s(self):
self.mingLing("b' B")
def mingLing(self, mingling: str):
minglingList = mingling.split(" ")
for i in minglingList:
if i in self.zhiLing:
xiabiao = self.zhiLing.index(i) + 1
xiabiao = xiabiao // 3 - 1 if xiabiao % 3 == 0 else xiabiao // 3
if "'" in i:
for i in range(3):
self.caoZuoList[xiabiao]()
elif "2" in i:
self.caoZuoList[xiabiao]()
self.caoZuoList[xiabiao]()
else:
self.caoZuoList[xiabiao]()
width, height = 600, 600
color = [(255, 255, 0), (255, 255, 255), (255, 0, 0), (255, 140, 0), (0, 0, 255), (0, 255, 0)]
icon = pygame.image.load('imgs/icon.png')
mofang = moFang(
"""\
1 1 1
1 1 1
1 1 1\
""",
"""\
2 2 2
2 2 2
2 2 2\
""",
"""\
3 3 3
3 3 3
3 3 3\
""",
"""\
4 4 4
4 4 4
4 4 4\
""",
"""\
5 5 5
5 5 5
5 5 5\
""",
"""\
6 6 6
6 6 6
6 6 6\
"""
)
mianPos = [(0, 0, 90, 90), (0, 0, 90, 30), (0, 60, 90, 30), (0, 0, 30, 90), (60, 0, 30, 90)]
jianPan = [
pygame.K_r, pygame.K_u, pygame.K_f, pygame.K_d, pygame.K_l, pygame.K_b, pygame.K_m, pygame.K_e,
pygame.K_t, pygame.K_i, pygame.K_g, pygame.K_c, pygame.K_k, pygame.K_v,
pygame.K_x, pygame.K_y, pygame.K_z, pygame.K_s
]
caoZuo = [
"R", "U", "F", "D", "L", "B", "M", "E",
"r", "u", "f", "d", "l", "b",
"x", "y", "z", "s"
]
jiayin = False
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("魔方")
pygame.display.set_icon(icon)
def zhuangTai(zhuangtai: list, x: int, y: int):
my_x, my_y = x, y
for i in zhuangtai:
for j in i:
pygame.draw.rect(window, color[j - 1], (my_x, my_y, 30, 30))
my_x += 30
my_y += 30
my_x = x
for i in range(5):
pygame.draw.rect(window, (0, 0, 0), (mianPos[i][0] + x, mianPos[i][1] + y, mianPos[i][2], mianPos[i][3]), 5)
while True:
pygame.time.Clock().tick(60)
window.fill((255, 255, 255))
zhuangTai(mofang.shang, 255, 105)
zhuangTai(mofang.qian, 255, 105 + 90 + 10)
zhuangTai(mofang.xia, 255, 105 + 90 * 2 + 10 * 2)
zhuangTai(mofang.hou, 255, 105 + 90 * 3 + 10 * 3)
zhuangTai(mofang.zuo, 155, 105 + 90 + 10)
zhuangTai(mofang.you, 155 + 10 * 2 + 90 + 90, 105 + 90 + 10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
elif event.key == pygame.K_n:
jiayin = not jiayin
elif event.key in jianPan:
s = caoZuo[jianPan.index(event.key)]
if jiayin:
s += "'"
mofang.mingLing(s)
pygame.display.update()