前言:这一期,我们来做一个拼图。让我们快乐地开始吧~ ~ ~我们先来看看这样的效果:开发工具**Python版本:**3.6.4私人边肖01可以获得很多python学习资源。相关模块:Pygame模块
这一期,我们来做一个拼图。让我们快乐地开始吧~ ~ ~
我们先来看看这样的效果:
开发工具
**Python版本:**3.6.4
私人边肖01可以获得很多python学习资源。
相关模块:Pygame模块;
以及Python附带的一些模块
环境建设安装Python并将其添加到环境变量中。pip可以安装所需的相关模块。
原理介绍游戏介绍:将图像分成m×n个矩形块,将图像右下角的矩形块替换为空白色块,然后将这些矩形块随机放置成原图的形状。游戏的目标是通过移动非空白块将随机放置的图像恢复为原始图像,并规定移动操作只存在于非空白块移动到空白块的情况下。
例如,如下所示:逐渐意识到:
第一步:游戏的初始界面既然是游戏,就要有初始界面吧?
好,我们先写一个游戏初始界面:
'''显示游戏开始界面'''def ShowStartInterface(screen, width, height):screen.fill(cfg.BACKGROUNDCOLOR)tfont = pygame.font.Font(cfg.FONTPATH, width//4)cfont = pygame.font.Font(cfg.FONTPATH, width//20)title = tfont.render('拼图游戏', True, cfg.RED)content1 = cfont.render('按H或M或L键开始游戏', True, cfg.BLUE)content2 = cfont.render('H为5*5模式, M为4*4模式, L为3*3模式', True, cfg.BLUE)trect = title.get_rect()trect.midtop = (width/2, height/10)crect1 = content1.get_rect()crect1.midtop = (width/2, height/2.2)crect2 = content2.get_rect()crect2.midtop = (width/2, height/1.8)screen.blit(title, trect)screen.blit(content1, crect1)screen.blit(content2, crect2)while True: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 == ord('l'): return 3elif event.key == ord('m'): return 4elif event.key == ord('h'): return 5pygame.display.update()复制代码
根据玩家自身的水平,可以选择不同难度的拼图。
步骤2:定义移动操作定义move操作的目的是移动拼图(类似废话T_T),具体实现很简单:
'''将空白Cell左边的Cell右移到空白Cell位置'''def moveR(board, blank_cell_idx, num_cols):if blank_cell_idx % num_cols == 0: return blank_cell_idxboard[blank_cell_idx-1], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx-1]return blank_cell_idx - 1'''将空白Cell右边的Cell左移到空白Cell位置'''def moveL(board, blank_cell_idx, num_cols):if (blank_cell_idx+1) % num_cols == 0: return blank_cell_idxboard[blank_cell_idx+1], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx+1]return blank_cell_idx + 1'''将空白Cell上边的Cell下移到空白Cell位置'''def moveD(board, blank_cell_idx, num_cols):if blank_cell_idx < num_cols: return blank_cell_idxboard[blank_cell_idx-num_cols], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx-num_cols]return blank_cell_idx - num_cols'''将空白Cell下边的Cell上移到空白Cell位置'''def moveU(board, blank_cell_idx, num_rows, num_cols):if blank_cell_idx >= (num_rows-1) * num_cols: return blank_cell_idxboard[blank_cell_idx+num_cols], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx+num_cols]return blank_cell_idx + num_cols复制代码
第三步:游戏主界面好了,有了前面的铺垫,就可以开始实现我们的游戏主界面了。
首先我们需要对拼图进行加扰,但是随机加扰很可能导致拼图无解,所以我们通过随机移动来对拼图进行加扰,这也是我们首先定义拼图移动操作的主要原因:
'''获得打乱的拼图'''def CreateBoard(num_rows, num_cols, num_cells):board = []for i in range(num_cells): board.append(i)# 去掉右下角那块blank_cell_idx = num_cells - 1board[blank_cell_idx] = -1for 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 = Truewhile 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)复制代码
第四步:游戏结束界面当玩家完成拼图后,需要显示游戏结束界面,该界面类似于游戏的初始界面,实现起来相对简单:
'''显示游戏结束界面'''def ShowEndInterface(screen, width, height):screen.fill(cfg.BACKGROUNDCOLOR)font = pygame.font.Font(cfg.FONTPATH, width//15)title = font.render('恭喜! 你成功完成了拼图!', True, (233, 150, 122))rect = title.get_rect()rect.midtop = (width/2, height/2.5)screen.blit(title, rect)pygame.display.update()while True: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()pygame.display.update()复制代码
这是文章的结尾。感谢您的观看。