二阶魔方旋转 魔方可以对它的6个面自由旋转。 我们来操作一个2阶魔方(python版):

在这里插入图片描述

def ROL(List,n,m):
    lidd = List[m-n:m]
    lidd.extend(List)
    lidd = lidd[0:m]
    return lidd 
    
    
class MOF():
    def __init__(self):
        self.Xa = '绿' 
        self.Xb = '绿' 
        self.Xc = '绿' 
        self.Xd = '绿' 
        self.X_a = '蓝' 
        self.X_b = '蓝' 
        self.X_c = '蓝'
        self.X_d = '蓝' 
        
        self.Ya = '红' 
        self.Yb = '红' 
        self.Yc = '红' 
        self.Yd = '红' 
        self.Y_a = '橙' 
        self.Y_b = '橙' 
        self.Y_c = '橙'
        self.Y_d = '橙'
        
        
        self.Za = '白' 
        self.Zb = '白' 
        self.Zc = '白' 
        self.Zd = '白' 
        self.Z_a = '黄' 
        self.Z_b = '黄' 
        self.Z_c = '黄'
        self.Z_d = '黄'
    def pprr(self):
        print(self.Xd+self.Yb+self.Zc)
    def zhaun_X(self):
        limm = [self.Za,self.Zc,self.Yb,self.Ya,self.Z_c,self.Z_a,self.Y_a,self.Y_b]
        [self.Za,self.Zc,self.Yb,self.Ya,self.Z_c,self.Z_a,self.Y_a,self.Y_b] = ROL(limm,2,8)
        limn = [self.Xa,self.Xb,self.Xd,self.Xc]
        [self.Xa,self.Xb,self.Xd,self.Xc] = ROL(limn,1,4)
    def zhaun_Y(self): 
        limm = [self.Zc,self.Zd,self.X_d,self.X_c,self.Z_d,self.Z_c,self.Xc,self.Xd]
        [self.Zc,self.Zd,self.X_d,self.X_c,self.Z_d,self.Z_c,self.Xc,self.Xd] = ROL(limm,2,8)
        limn = [self.Ya,self.Yb,self.Yd,self.Yc]
        [self.Ya,self.Yb,self.Yd,self.Yc] = ROL(limn,1,4)
    def zhaun_Z(self):
        limm = [self.Xd,self.Xb,self.Y_b,self.Y_d,self.X_b,self.X_d,self.Yd,self.Yb]
        [self.Xd,self.Xb,self.Y_b,self.Y_d,self.X_b,self.X_d,self.Yd,self.Yb] = ROL(limm,2,8)
        limn = [self.Za,self.Zb,self.Zd,self.Zc]
        [self.Za,self.Zb,self.Zd,self.Zc] = ROL(limn,1,4)
        
mofang = MOF()
print("在初始状态,应该输出:")
mofang.pprr()
X = input("在初始状态,如果用户输入:\n")
print("则应该输出:")
for i in X:
    if i == 'x':
        mofang.zhaun_X()
    
    if i == 'y':
        mofang.zhaun_Y()
      
    if i == 'z':
        mofang.zhaun_Z()

    else:
        continue
mofang.pprr()
mofang = MOF()
X = input("在初始状态,如果用户输入:\n")
print("则应该输出:")
for i in X:
    if i == 'x':
        mofang.zhaun_X()
        
    elif i == 'y':
        mofang.zhaun_Y()
      
    elif i == 'z':
        mofang.zhaun_Z()

    else:
        break
mofang.pprr()

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Python魔方游戏的示例代码,使用 Pygame 库实现: ```python import pygame import random # 初始化 Pygame pygame.init() # 定义一些常量 WIDTH = 600 HEIGHT = 400 FPS = 30 # 定义一些颜色常量 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # 创建游戏窗口 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("三魔方") # 定义一个魔方类 class Cube: def __init__(self, color, x, y, size): self.color = color self.x = x self.y = y self.size = size # 绘制方块 def draw(self): pygame.draw.rect(screen, self.color, (self.x, self.y, self.size, self.size)) # 定义一个魔方状态类 class CubeState: def __init__(self): self.cubes = [] self.colors = { 'U': WHITE, 'D': YELLOW, 'F': RED, 'B': ORANGE, 'L': GREEN, 'R': BLUE } for i in range(3): for j in range(3): cube = Cube(WHITE, i * 50 + 200, j * 50 + 100, 50) self.cubes.append(cube) # 绘制魔方 def draw(self): for i in range(9): self.cubes[i].color = self.colors['U'] for i in range(9, 18): self.cubes[i].color = self.colors['L'] for i in range(18, 27): self.cubes[i].color = self.colors['F'] for i in range(27, 36): self.cubes[i].color = self.colors['R'] for i in range(36, 45): self.cubes[i].color = self.colors['B'] for i in range(45, 54): self.cubes[i].color = self.colors['D'] for cube in self.cubes: cube.draw() # 打乱魔方 def shuffle(self): for i in range(20): move = random.choice(['U', 'D', 'F', 'B', 'L', 'R']) self.rotate(move) # 旋转魔方 def rotate(self, move): if move == 'U': self.colors['U'], self.colors['L'], self.colors['F'], self.colors['R'], self.colors['B'], self.colors['D'] = self.colors['L'], self.colors['F'], self.colors['R'], self.colors['B'], self.colors['D'], self.colors['U'] elif move == 'D': self.colors['D'], self.colors['L'], self.colors['B'], self.colors['R'], self.colors['F'], self.colors['U'] = self.colors['L'], self.colors['F'], self.colors['R'], self.colors['B'], self.colors['U'], self.colors['D'] elif move == 'F': self.colors['F'], self.colors['U'], self.colors['L'], self.colors['D'], self.colors['R'], self.colors['B'] = self.colors['U'], self.colors['R'], self.colors['D'], self.colors['L'], self.colors['F'], self.colors['B'] elif move == 'B': self.colors['B'], self.colors['D'], self.colors['L'], self.colors['U'], self.colors['R'], self.colors['F'] = self.colors['D'], self.colors['R'], self.colors['U'], self.colors['L'], self.colors['B'], self.colors['F'] elif move == 'L': self.colors['L'], self.colors['U'], self.colors['B'], self.colors['D'], self.colors['F'], self.colors['R'] = self.colors['U'], self.colors['F'], self.colors['D'], self.colors['B'], self.colors['R'], self.colors['L'] elif move == 'R': self.colors['R'], self.colors['U'], self.colors['F'], self.colors['D'], self.colors['B'], self.colors['L'] = self.colors['U'], self.colors['B'], self.colors['D'], self.colors['F'], self.colors['L'], self.colors['R'] # 创建一个 3x3 的魔方状态 cube_state = CubeState() # 打乱魔方 cube_state.shuffle() # 游戏循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 处理键盘事件 if event.type == pygame.KEYDOWN: if event.key == pygame.K_u: cube_state.rotate('U') elif event.key == pygame.K_d: cube_state.rotate('D') elif event.key == pygame.K_f: cube_state.rotate('F') elif event.key == pygame.K_b: cube_state.rotate('B') elif event.key == pygame.K_l: cube_state.rotate('L') elif event.key == pygame.K_r: cube_state.rotate('R') # 绘制背景 screen.fill(BLACK) # 绘制魔方 cube_state.draw() # 更新屏幕 pygame.display.update() # 控制帧率 clock = pygame.time.Clock() clock.tick(FPS) # 退出 Pygame pygame.quit() ``` 这个示例代码创建了一个打乱的 3x3 魔方,并且支持使用键盘上的 U、D、F、B、L、R 键来旋转魔方。你可以根据自己的需要修改方块大小、颜色以及位置等参数。同时,你也可以添加更多的逻辑,例如判断是否还原魔方、计时等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值