def load(screen, config): init(screen, config) onButton = -1 while True: for event in pygame.event.get(): if event.type == QUIT: # 退出按钮被按下 exit() elif event.type == MOUSEBUTTONDOWN: # 鼠标按键被按下 func = button.checkButtonPress(pygame.mouse.get_pos()) if func == 6: config['show_order'] = not config['show_order'] init(screen, config) elif func == 7: config['music_on'] = not config['music_on'] if config['music_on']: pygame.mixer.music.play(-1) else: pygame.mixer.music.fadeout(2000) init(screen, config) elif func == 8: config['sound_on'] = not config['sound_on'] init(screen, config) elif func == 5: return gameConfig # 返回按钮对应的操作 elif event.type == MOUSEMOTION: func = button.checkButtonPress(pygame.mouse.get_pos()) if func != onButton: surf = button.drawBtn(screen, func) screen.blit(background, (0, 0)) screen.blit(surf, (0, 0)) pygame.display.update() onButton = func
def init(screen, config): # 初始化界面 if not pygame.get_init(): pygame.init() # 绘制背景 global background background = pygame.image.load(new_img_filename) background = pygame.transform.scale(background, SCREEN) screen.blit(background, (0, 0)) # 构造按钮组 buttonList = [] if config["music_on"]: buttonList.append(["音乐:开", 7]) else: buttonList.append(["音乐:关", 7]) if config["show_order"]: buttonList.append(["落子顺序:显示", 6]) else: buttonList.append(["落子顺序:隐藏", 6]) buttonList.append(["返回", 5]) # 绘制按钮 button.init(buttonList) surf = button.drawBtn(screen) # 合并图层 screen.blit(surf, (0, 0)) # 展示 pygame.display.update()
def load(screen): init(screen) onButton = -1 while True: for event in pygame.event.get(): if event.type == QUIT: # 退出按钮被按下 exit() elif event.type == MOUSEBUTTONDOWN: # 鼠标按键被按下 func = button.checkButtonPress(pygame.mouse.get_pos()) if func != -1: return func # 返回按钮对应的操作 elif event.type == MOUSEMOTION: func = button.checkButtonPress(pygame.mouse.get_pos()) if func != onButton: surf = button.drawBtn(screen, func) screen.blit(backGround, (0, 0)) screen.blit(surf, (0, 0)) pygame.display.update() onButton = func
def init(screen): # 初始化界面 if not pygame.get_init(): pygame.init() # screen = pygame.display.set_mode(SCREEN, 0, 32) # 绘制背景 global backGround backGround = pygame.image.load(cover_img_filename) backGround = pygame.transform.scale(backGround, SCREEN) screen.blit(backGround, (0, 0)) # 绘制按钮 button.init(buttonList) surf = button.drawBtn(screen) # 合并图层 screen.blit(surf, (0, 0)) # 展示 pygame.display.update()
def load(screen, config): # 初始化游戏 init(screen) global gameConfig gameConfig = config # 初始化玩家 # PLAYER1.init(BLACK_PIECE),将玩家1初始化为黑棋玩家 # PLAYER2.init(WHITE_PIECE),将玩家2初始化成为白棋玩家,对于机器玩家来说,黑棋白棋策略不同,所以要先定义 global CUR_PIECE_LOCATION, CUR_PIECE_COLOR, WIN while True: # 双方轮流落子,直到决出胜负 # 调用棋手模块go程序,取得棋手给出的落子位置 CUR_PIECE_LOCATION = player[CUR_PIECE_COLOR].go( pieceRecord, CUR_PIECE_LOCATION) # 每个棋手模块必须有一个go()函数,返回一个棋盘上的位置,作为落子的的地垫 # 检测落子位置是否合法,合法的话将落子信息记录到落子信息表,否则的话重新调用 TODO:AI棋手要具有判断落子是否合法的能力 if pieceRecord[CUR_PIECE_LOCATION[0], CUR_PIECE_LOCATION[1]] != -1: continue else: pieceRecord[CUR_PIECE_LOCATION[0], CUR_PIECE_LOCATION[1]] = CUR_PIECE_COLOR print(pieceRecord.T) # 绘制棋子 drawPiece(screen) # 判胜,如果获胜则退出当前循环,否则继续换下一位棋手落子 if judgeVictory(): break # 切换当前玩家 CUR_PIECE_COLOR = (CUR_PIECE_COLOR + 1) % 2 # ------------------------------------------- # TODO:在决胜棋型上标记红色 # ------------------------------------------- for wp in winPiece: pygame.draw.circle(sufPiece, (255, 0, 0), (wp[0] * CELL_WIDTH + 10 + CELL_WIDTH // 2, wp[1] * CELL_WIDTH + 10 + CELL_WIDTH // 2), 10) screen.blit(sufPiece, (0, 0)) # 显示胜负信息 # 绘制文字 font = pygame.font.Font(FONT_FILE, 150) # 读取字体 sufFont = font.render(player[CUR_PIECE_COLOR].winText, False, (0, 255, 0)) # 渲染文字 fontRect = sufFont.get_rect() # 取得渲染后画板的尺寸 # 调整文字显示位置 x = (WIDE - fontRect[2]) / 2 y = 180 # 合并文字图层 screen.blit(sufFont, (x, y)) # ------------------------------------------- # 绘制按钮 button.init(buttonList, (BTN_ARRANGED_VERTICALLY, BTN_ON_BOTTOM_CENTER)) surf = button.drawBtn(screen) # 合并图层并显示 screen.blit(surf, (0, 0)) pygame.display.update() # ------------------------------------------- while True: # 根据点击的按钮做出相应的处理 buttonPress = button.checkButtonPress(mouseClick()) if buttonPress != -1: if buttonPress == 2: # 重新开局 return 5 elif buttonPress == 0: # 退出游戏 return 3 break