python小游戏代码 利用python画图的步骤


前言:本期我们将制作一个拼图小游戏 。让我们愉快地开始吧~~~
先来看看效果是这样子的:

python小游戏代码 利用python画图的步骤

文章插图
python小游戏代码 利用python画图的步骤

文章插图
python小游戏代码 利用python画图的步骤

文章插图
python小游戏代码 利用python画图的步骤

文章插图
最后实现主界面的显示刷新以及事件响应等功能:
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)复制代码Step4:游戏结束界面当玩家完成拼图后,需要显示游戏结束界面,和游戏初始界面类似,实现起来都比较简单:
'''显示游戏结束界面'''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()复制代码文章到这里就结束了,感谢你的观看
【python小游戏代码 利用python画图的步骤】以上就是python小游戏代码(利用python画图的步骤)的相关内容了,更多精彩内容请关注!

    推荐阅读