def snapshot(self, dumy): if self.right_panel != "snapshot": self.right_panel = "snapshot" Panel.clearRight() self.comparing = False self.my_snapshot.takeSnapshot(self.my_playground.cube())
def singleRotate(self, action): reverse = False if self.rotating: return False if action == None: if len(self.his_actions) > 0: pre_action = self.his_actions.pop(-1) pre_action_info = a_map.get(pre_action) action = pre_action_info["reverse"] reverse = True if action != None: self.rotate_layer = a_map[action]["layer"] self.rotating = True self.rotate_angle = 0 self.rotate_face = a_map[action]["face"] self.rotate_layer = a_map[action]["layer"] self.rotate_clockwize = a_map[action]["clockwize"] if not reverse: if len(self.his_actions) < self.his_count: self.his_actions.append(action) else: self.his_actions.pop(0) self.his_actions.append(action) msg = "".join(self.his_actions) Panel.printLeft(msg) return True
def compare(self, dumy): if self.right_panel != "snapshot": Panel.printLeft("没有快照可以比较!") return if self.comparing: #已经处于比对状态,先取消 self.cancel(dumy) self.comparing = True mark = 0 right = self.my_snapshot for my_b in self.my_playground.blocks(): for sn_b in right.blocks(): if my_b.block.origin == sn_b.block.origin: if my_b.block.current != sn_b.block.current: str_mark = str(mark) my_b.mark = str_mark sn_b.mark = str_mark mark += 1 elif my_b.block.colors != sn_b.block.colors: str_mark = str(mark) my_b.mark = str_mark sn_b.mark = str_mark mark += 1 if mark > 0: self.my_playground.displayContent() self.my_snapshot.displayContent()
def next(self, dummy): if self.current_level == -1: self.current_level = self.my_library.lib_level current_stage = p_map[self.resolve_method][self.current_level] self.current_level += 1 if self.current_level >= len(p_map[self.resolve_method]): Panel.printLeft("魔方已经完全解决") return next_stage = p_map[self.resolve_method][self.current_level] auto_actions = [] while current_stage < next_stage: for a in auto_actions: face = a_map[a]["face"] layer = a_map[a]["layer"] clockwize = a_map[a]["clockwize"] self.my_playground.cube().rotateCube(face, layer, clockwize) advice = self.hint2() auto_actions = parseAdvice(advice.get("h", "")) current_stage = advice.get("s", current_stage) if auto_actions == "": break self.his_actions = [] self.advice = "" self.my_playground.rebuild() self.my_playground.displayContent() self.current_level = -1
def endBrush(self, dumy): if self.brush_copy == 1: if not self.my_cube_3d.cube.validateCube(): msg = u"颜色设置没有完成,请继续完成设置!" else: self.brush_copy = 0 msg = u"颜色设置完成" Panel.printLeft(msg)
def saveCube(self, cube): self.setLevel(0) self.next_index += 1 self.writeCube2File(self.snapshots_dir, self.next_index, cube, 0) self.build() Panel.printLeft(u"保存为第" + str(self.total) + "份快照") self.setCurrent((self.getTotal(), -1)) self.displayContent()
def cancelBrush(self): if len(self.his_colors) > 0: b, i, c = self.his_colors.pop(-1) b.colors[i] = c self.my_cube_3d.buildFaces() self.my_cube_3d.displayCube() else: self.brush_copy = 0 Panel.printLeft(u"取消全部设置")
def timing(self, dumy): if self.timing_started: #已经在计时,结束计时 self.timing_stop = time.time() self.timing_started = False Panel.printHint("计时结束! 总共用时:%d秒" % (int(self.timing_stop - self.timing_start))) else: #没有在计时,开始计时 self.timing_start = time.time() self.timing_started = True Panel.printHint("计时开始......")
def displayHeader(self): screen,ft_sz,x_scale,y_scale= Panel.screen, \ Panel.ft_sz,Panel.x_scale,Panel.y_scale b_x = x_scale * 820 b_y = y_scale * 10 b_h = y_scale * 30 if self.total_page == 0: start = 0 stop1 = 0 stop2 = 10 else: start = (self.current_page - 1) * 10 stop1 = start + 10 stop2 = stop1 if self.current_page == self.total_page: stop1 = self.total if self.current_page == 1: button(screen, "<<", ft_sz, b_x, b_y, x_scale * 30, b_h, Panel.gray, red, None, -1) else: button(screen, "<<", ft_sz, b_x, b_y, x_scale * 30, b_h, green, red, self.prevPage, -1) b_x += x_scale * 40 for b in range(start, stop1): if (b + 1) == self.current: button(screen, str(b + 1), ft_sz, b_x, b_y, x_scale * 30, b_h, red, red, self.displayContent, (-1, b + 1, -1)) else: button(screen, str(b + 1), ft_sz, b_x, b_y, x_scale * 30, b_h, green, red, self.displayContent, (-1, b + 1, -1)) b_x += x_scale * 40 for b in range(stop1, stop2): button(screen, "", ft_sz, b_x, b_y, x_scale * 30, b_h, Panel.gray, red, None, b + 1) b_x += x_scale * 40 if self.current_page == self.total_page: button(screen, ">>", ft_sz, b_x, b_y, x_scale * 30, b_h, Panel.gray, red, None, 1) else: button(screen, ">>", ft_sz, b_x, b_y, x_scale * 30, b_h, green, red, self.nextPage, 1) #显示控制按钮 b_map = [("自定", (0, 1, -1)), ("十字", (1, 1, -1)), ("F2L", (2, 1, -1)), ("OLL", (3, 1, -1)), (u"PLL", (4, 1, -1))] b_x = x_scale * 820 b_y = y_scale * 640 b_h = y_scale * 30 for b in b_map: button(screen, b[0], ft_sz, b_x, b_y, x_scale * 60, b_h, green, red, self.displayContent, b[1]) b_x += x_scale * 70 Panel.printRight(s_map[self.lib_level])
def quitz(self, dummy): if self.right_panel != "library": self.right_panel = "library" Panel.clearRight() l = math.ceil(random.random() * 4) self.my_library.setLevel(l) self.current_level = l total = self.my_library.getTotal() r = math.ceil(random.random() * total) self.my_library.setCurrent((-1, r)) self.my_library.displayContent() self.load(0) Panel.printRight(u"当前是第%s题" % (str(r)))
def showLibBelowLevel3(self): if self.total == 0: return self.legends = [] file = self.snapshots_dir + self.snapshots[self.current - 1][0] cube = cubeModel.Cube(file) self.my_cube_3d = cubeLegendF2L.CubeLegendF2L(cube, width, height, fov, distance, adj_x, adj_y) self.my_cube_3d.buildFaces() self.my_cube_3d.setLBDPos([(180, -70), (480, -70), (160, 260)]) self.my_cube_3d.displayContent() self.legends.append(self.my_cube_3d) self.current_legend = 0 Panel.printLeft(u"选择了第" + str(self.current) + "份快照")
def cancelComparing(self): if self.right_panel != "snapshot": Panel.printLeft("没有快照可以取消比较!") self.comparing = False return self.comparing = False mark = 1 for my_b in self.my_playground.blocks(): if my_b.mark != "-": my_b.mark = "-" for sn_b in self.my_snapshot.blocks(): if sn_b.mark != "-": sn_b.mark = "-" self.my_playground.displayContent() self.my_snapshot.displayContent()
def displayContent(self, level_current=None): Panel.clearRight() Panel.printRight(s_map[self.lib_level]) self.my_cube_3d = None self.single_clicked = False if level_current != None: self.setLevel(level_current[0]) self.setCurrent((level_current[1], level_current[2])) if self.lib_level == 3: self.showLibLevel3() elif self.lib_level == 4: self.showLibLevel4() else: self.showLibBelowLevel3() if self.current_legend != -1: self.my_cube_3d = self.legends[self.current_legend] self.my_cube_3d.drawSelected()
def deleteCurrent(self): if self.lib_level > 0: Panel.printLeft(u"不能删除非自定义题库里的题目") return if self.total == 0 or self.current == 0: return os.remove(self.snapshots_dir + self.snapshots[self.current - 1][0]) del (self.snapshots[self.current - 1]) with open(self.snapshots_dir + "index", "w", 1) as fo: fo.write("#generated by this program, never modify it.\n") for line in self.snapshots: fo.write(line[0] + " " + str(line[1]) + "\n") self.setTotal(self.total - 1) if self.current > 0: self.setCurrent((self.current - 1, -1)) if self.current == 0: self.setCurrent((self.current + 1, -1)) self.displayContent()
def hint(self, show): advice = self.hint2() t1 = advice.get("t1", None) t2 = advice.get("t2", None) for my_b in self.my_playground.blocks(): if t1 != None: if (my_b.block.current.x == int(t1[0]) and my_b.block.current.y == int(t1[1]) and my_b.block.current.z == int(t1[2])): my_b.mark = "1" if t2 != None: if (my_b.block.current.x == int(t2[0]) and my_b.block.current.y == int(t2[1]) and my_b.block.current.z == int(t2[2])): my_b.mark = "2" if t1 != None: self.my_playground.displayContent() hint = advice.get("h", "No Advise") Panel.printHint(hint)
def singleClick(self, x, y): if self.legends == []: return False if self.lib_level < 3: if self.my_cube_3d.hitMe(x, y): self.single_clicked = True return True count = len(self.legends) for i, l in enumerate(self.legends): if l.hitMe(x, y): l.drawSelected() if self.current_legend != -1: self.legends[self.current_legend].redraw() Panel.printLeft(u"选择了第%d份快照" % (i + 1)) self.current_legend = i self.my_cube_3d = l self.single_clicked = True return True return False
def showLibLevel4(self): if self.total == 0: return Panel.clearRight() start = (self.current - 1) * 12 end = start + 12 if end > len(self.snapshots): end = len(self.snapshots) self.legends = [] for i in range(start, end): file_no = +i file = self.snapshots_dir + self.snapshots[i][0] cube = cubeModel.Cube(file) m, r = divmod(i - start, 3) cube_3d = cubeLegendPLL.CubeLegendPLL(cube, width, height, fov, 20, 650 + r * 180, -120 + m * 150) cube_3d.buildFaces() cube_3d.displayContent() self.legends.append(cube_3d)
def method(self, method): self.resolve_method = method Panel.printLeft("当前提示和自动解题方法是" + method)
def library(self, level): if self.right_panel != "library": Panel.clearRight() self.right_panel = "library" self.my_library.displayContent() self.comparing = False
def save(self, dumy): if self.right_panel != "library": Panel.clearRight() self.right_panel = "library" self.my_library.saveCube(self.my_playground.cube())
def takeSnapshot(self, cube): self.my_cube_3d.cube = copy.deepcopy(cube) self.my_cube_3d.buildFaces() self.displayContent() Panel.printRight("快照窗口")
def help(self, dumy): if self.right_panel != "help": Panel.clearRight() self.right_panel = "help" self.my_tutorial.nextOrPrevious(0) self.comparing = False
def gameLoop(self): global mouse_status pre_mouse_txy = (0, 0, 0) click_times = 0 tick_times = 0 clock = pygame.time.Clock() screen,ft_sz,x_scale,y_scale= Panel.screen, \ Panel.ft_sz,Panel.x_scale,Panel.y_scale while not self.gameExit: for event in pygame.event.get(): if event.type == pygame.QUIT: self.gameExit = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self.gameExit = True if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1 and (not self.my_playground.rotating): mouse_x, mouse_y = event.pos mouse_t = pygame.time.get_ticks() mouse_status[0] = 1 mouse_status[1] = mouse_x mouse_status[2] = mouse_y #print("Mouse button down detected") if self.isContEvent( pre_mouse_txy, (mouse_t, mouse_x, mouse_y), ): self.doubleClick() else: self.singleClick(mouse_x, mouse_y) pre_mouse_txy = (mouse_t, mouse_x, mouse_y) if event.type == pygame.MOUSEBUTTONUP: if event.button == 1 and (not self.my_playground.rotating): mouse_x, mouse_y = event.pos mouse_t = pygame.time.get_ticks() #print("Mouse button up detected") if not self.isContEvent( (mouse_t, mouse_x, mouse_y), pre_mouse_txy): self.drag((pre_mouse_txy[1], pre_mouse_txy[2]), (mouse_x, mouse_y)) b_h = y_scale * 30 #退出按钮,最右上角 button(screen, "X", ft_sz, x_scale * 1300, y_scale * 10, x_scale * 40, b_h, green, red, self.quit, "X") #显示控制按钮 b_map = [[(u"题库", self.library, 0), ("7步", self.method, "Simple"), ("F2CP", self.method, "F2CP"), (u"保存", self.save, 1), (u"帮助", self.help, 0), (u"删除", self.delete, 0), (u"提示", self.hint, 1), (u"计时", self.timing, 0)], [ (u"<-|", self.load, 0), (u"|->", self.snapshot, 0), (u"对比", self.compare, 0), ("打乱", self.init, 1), (u"进阶", self.next, 2), (u"出题", self.quitz, 0), (u"自动", self.step, 0), (u"撤销", self.cancel, 0), ]] b_x = x_scale * 790 b_y = y_scale * 690 b_h = y_scale * 30 for bs in b_map: for b, f, p in bs: if f != None: button(screen, b, ft_sz, b_x, b_y, x_scale * 60, b_h, green, red, f, p) else: button(screen, b, ft_sz, b_x, b_y, x_scale * 60, b_h, gray, red, f, p) b_x += x_scale * 70 b_y += y_scale * 40 b_x = x_scale * 790 self.my_playground.displayHeader() self.my_playground.displayRotation() if self.right_panel == "library": self.my_library.displayHeader() elif self.right_panel == "help": self.my_tutorial.displayHeader() clock.tick(50) if tick_times == 0: #每秒刷新一次时间 msg = time.strftime("%X") Panel.printTime(msg) tick_times += 1 if tick_times == 20: tick_times = 0 pygame.display.update()
def displayAll(self): self.my_playground.displayContent() #self.my_tutorial.displayContent() self.my_library.displayContent() Panel.printLeft(u"当前解题方法是" + self.resolve_method + u"法") Panel.printHint(u"下一步提示")
def selectColor(self, c): self.brush_color = c self.brush_copy = 1 Panel.printLeft(u"选择色块,双击魔方设置颜色。当前选中:" + colors_n[self.brush_color])
import argparse, pygame, sys from cubeControl import CubeControl from cubePanel import Panel if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--auto", type=int, help="automatically resolve to this level") parser.add_argument("--gen", type=int, help="just to genaerate F2CP samples.") args = parser.parse_args() if args.auto == None: args.auto = 2 Panel.display() #19:打乱魔方随机转19步;25:历史记录 cubeControl = CubeControl(19, 25, args.auto) cubeControl.displayAll() cubeControl.gameLoop() pygame.quit() sys.exit()
def delete(self, dumy): if self.right_panel != "library": Panel.printLeft(u"当前是帮助窗口,不能删除.") else: self.my_library.deleteCurrent()