Step3:游戏主界面 OK , 有了前面的铺垫 , 我们可以开始实现我们的游戏主界面了 。
首先 , 我们需要打乱拼图 , 但是随机打乱很可能导致拼图无解 , 因此我们通过随机移动拼图来实现打乱拼图的效果 , 这也是我们先定义拼图的移动操作的主要原因:
'''获得打乱的拼图'''def CreateBoard(num_rows, num_cols, num_cells): board = [] for i in range(num_cells): board.append(i) # 去掉右下角那块 blank_cell_idx = num_cells - 1 board[blank_cell_idx] = -1 for i in range(cfg.NUMRANDOM):# 0: left, 1: right, 2: up, 3: downdirection = random.randint(0, 3)if direction == 0: blank_cell_idx = moveL(board, blank_cell_idx, num_cols)elif direction == 1: blank_cell_idx = moveR(board, blank_cell_idx, num_cols)elif direction == 2: blank_cell_idx = moveU(board, blank_cell_idx, num_rows, num_cols)elif direction == 3: blank_cell_idx = moveD(board, blank_cell_idx, num_cols) return board, blank_cell_idx复制代码 游戏主界面初始化:
文章插图
最后实现主界面的显示刷新以及事件响应等功能:
while True:game_board, blank_cell_idx = CreateBoard(num_rows, num_cols, num_cells)if not isGameOver(game_board, size):break # 游戏主循环 is_running = True while is_running:# --事件捕获for event in pygame.event.get():# ----退出游戏if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):pygame.quit()sys.exit()# ----键盘操作elif event.type == pygame.KEYDOWN:if event.key == pygame.K_LEFT or event.key == ord('a'):blank_cell_idx = moveL(game_board, blank_cell_idx, num_cols)elif event.key == pygame.K_RIGHT or event.key == ord('d'):blank_cell_idx = moveR(game_board, blank_cell_idx, num_cols)elif event.key == pygame.K_UP or event.key == ord('w'):blank_cell_idx = moveU(game_board, blank_cell_idx, num_rows, num_cols)elif event.key == pygame.K_DOWN or event.key == ord('s'):blank_cell_idx = moveD(game_board, blank_cell_idx, num_cols)# ----鼠标操作elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:x, y = pygame.mouse.get_pos()x_pos = x // cell_widthy_pos = y // cell_heightidx = x_pos + y_pos * num_colsif idx == blank_cell_idx-1:blank_cell_idx = moveR(game_board, blank_cell_idx, num_cols)elif idx == blank_cell_idx+1:blank_cell_idx = moveL(game_board, blank_cell_idx, num_cols)elif idx == blank_cell_idx+num_cols:blank_cell_idx = moveU(game_board, blank_cell_idx, num_rows, num_cols)elif idx == blank_cell_idx-num_cols:blank_cell_idx = moveD(game_board, blank_cell_idx, num_cols)# --判断游戏是否结束if isGameOver(game_board, size):game_board[blank_cell_idx] = num_cells - 1is_running = False# --更新屏幕screen.fill(cfg.BACKGROUNDCOLOR)for i in range(num_cells):if game_board[i] == -1:continuex_pos = i // num_colsy_pos = i % num_colsrect = pygame.Rect(y_pos*cell_width, x_pos*cell_height, cell_width, cell_height)img_area = pygame.Rect((game_board[i]%num_cols)*cell_width, (game_board[i]//num_cols)*cell_height, cell_width, cell_height)screen.blit(game_img_used, rect, img_area)for i in range(num_cols+1):pygame.draw.line(screen, cfg.BLACK, (i*cell_width, 0), (i*cell_width, game_img_used_rect.height))for i in range(num_rows+1):pygame.draw.line(screen, cfg.BLACK, (0, i*cell_height), (game_img_used_rect.width, i*cell_height))pygame.display.update()clock.tick(cfg.FPS)复制代码
以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!
「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助:- 5款免费制造电子名片的工具 制作名片用什么软件好
- 春饼面怎么和才能软软的 春饼的制作方法
- 腊八蒜可以用乐扣盒子制作吗
- 教你把照片制作mv视频 怎么制作mv视频加带字
- 游戏简洁有寓意的名字,有深意好听的游戏名字
- 萝卜蹲名字
- 小黑猫是什么意思
- 微星游戏本深度评测 微星游戏本怎么样
- 拯救者R7000开箱评测 拯救者游戏本怎么样
- 双飞燕J95游戏鼠标开箱测评 双飞燕鼠标怎么样