【Python游戏开发】使用Python编写拼图益智游戏教程

使用Python编写拼图益智游戏

大家一般都玩过拼图益智游戏,或者类似的游戏。今天,就给大家使用pygame库在Python中构建一个拼图益智小游戏。这个拼图小游戏是构建一个围绕着将1-15个数字排列在16个方块的网格中的游戏。

现在,让我们从今天的惊人项目“使用PyGame的Python益智游戏”开始。首先,我们将对游戏背后的主要逻辑有一个基本的想法,然后我们将继续讨论我们将添加到这个项目中的功能,然后我们将会继续讨论这个项目的实际编码部分——Python益智游戏

游戏的基本规则和玩法:

在Python中的这个拼图益智小游戏,玩家必须按升序排列从1到15的块。游戏将显示16个方块,其中一个方块为空,玩家可以移动这些方块。一旦所有的方块都按顺序排好了,比赛就结束了。

既然我们已经对到底需要开发什么有了基本的想法,让我们继续往下看Python中拼图益智游戏需要实现的功能列表:

Python中益智游戏的特点:

Python中益智游戏的自动洗牌功能。

重置、新游戏和自动求解选项。

Python中的益智游戏块可以使用箭头键或鼠标移动。

下面直接给出Python拼图益智游戏的完整代码:

# baic library imports pygame and random
import pygame
import sys
import random
from pygame.locals import *# this section holds all the variables that we will use in Puzzle Game In Python
w_of_board = 4  # total number of columns in the board of Puzzle Game In Python
h_of_board = 4  # total number of rows in the board
block_size = 80
win_width = 640
win_height = 480
FPS = 30
BLANK = None# this is bascially for managing the different colors of the component
# we have also used variables for mantaining the text size in Puzzle Game In Python
BLACK = (0,   0,   0)
WHITE = (255, 255, 255)
BRIGHTBLUE = (0,  50, 255)
DARKTURQUOISE = (255, 255, 255)
BLUE = (0,  0, 0)
GREEN = (0, 128,   0)
RED = (255, 0, 0)
BGCOLOR = DARKTURQUOISE
TILECOLOR = BLUE
TEXTCOLOR = WHITE
BORDERCOLOR = RED
BASICFONTSIZE = 20
TEXT = GREENBUTTONCOLOR = WHITE
BUTTONTEXTCOLOR = BLACK
MESSAGECOLOR = GREEN# this is to leave the space on both the sides of the block
XMARGIN = int((win_width - (block_size * w_of_board + (w_of_board - 1))) / 2)
YMARGIN = int((win_height - (block_size * h_of_board + (h_of_board - 1))) / 2)# these are the variables for handling the keyboard keys
UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'# this is the main functiondef main():global FPSCLOCK, DISPLAYSURF, BASICFONT, RESET_SURF, RESET_RECT, NEW_SURF, NEW_RECT, SOLVE_SURF, SOLVE_RECTpygame.init()FPSCLOCK = pygame.time.Clock()DISPLAYSURF = pygame.display.set_mode((win_width, win_height))# we gave a title using set_caption function in pygamepygame.display.set_caption('Slide Puzzle - CopyAssignment')BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)# these variables stores the various options that will be displayed to the right side of our main grid# these below only handles the design part of the optionsRESET_SURF, RESET_RECT = makeText('重置',    TEXT, BGCOLOR, win_width - 120, win_height - 310)NEW_SURF,   NEW_RECT = makeText('新游戏', TEXT, BGCOLOR, win_width - 120, win_height - 280)SOLVE_SURF, SOLVE_RECT = makeText('求解',    TEXT, BGCOLOR, win_width - 120, win_height - 250)mainBoard, solutionSeq = generateNewPuzzle(80)# this is simply the board that is same as that of the solved board in Puzzle Game In Python# bascially the game will shuffle all blocks of the solved gameSOLVEDBOARD = start_playing()# a list that tracks the moves made from the solved configurationallMoves = []# main game loopwhile True:slideTo = None# the below variable contains the message to show in the top left corner.msg = 'Click a block or press arrow keys to slide the block.'if mainBoard == SOLVEDBOARD:msg = 'Solved!'drawBoard(mainBoard, msg)check_exit_req()# the below for loop is to handle the various events of keyboardfor event in pygame.event.get():if event.type == MOUSEBUTTONUP:spotx, spoty = getSpotClicked(mainBoard, event.pos[0], event.pos[1])if (spotx, spoty) == (None, None):# this is to check if the user clicked on an option buttonif RESET_RECT.collidepoint(event.pos):# this below linw will come into action of the user clicked on Reset buttonrst_animation(mainBoard, allMoves)allMoves = []elif NEW_RECT.collidepoint(event.pos):# this below linw will come into action of the user clicked on New Game buttonmainBoard, solutionSeq = generateNewPuzzle(80)allMoves = []elif SOLVE_RECT.collidepoint(event.pos):# this below linw will come into action of the user clicked on Solve buttonrst_animation(mainBoard, solutionSeq + allMoves)allMoves = []else:# this else block in Puzzle Game In Python is just to check that the moved tile has a blankblankx, blanky = getBlankPosition(mainBoard)if spotx == blankx + 1 and spoty == blanky:slideTo = LEFTelif spotx == blankx - 1 and spoty == blanky:slideTo = RIGHTelif spotx == blankx and spoty == blanky + 1:slideTo = UPelif spotx == blankx and spoty == blanky - 1:slideTo = DOWNelif event.type == KEYUP:# this elif block will handle the checking if the user pressed a key to slide a tileif event.key in (K_LEFT, K_a) and isValidMove(mainBoard, LEFT):slideTo = LEFTelif event.key in (K_RIGHT, K_d) and isValidMove(mainBoard, RIGHT):slideTo = RIGHTelif event.key in (K_UP, K_w) and isValidMove(mainBoard, UP):slideTo = UPelif event.key in (K_DOWN, K_s) and isValidMove(mainBoard, DOWN):slideTo = DOWN# this block will handle the fucntionality of displaying the message for controlsif slideTo:# show slide on screensliding_animation(mainBoard, slideTo, 'Click a block or press arrow keys to slide the block.', 8)take_turn(mainBoard, slideTo)allMoves.append(slideTo)pygame.display.update()FPSCLOCK.tick(FPS)def terminate():pygame.quit()sys.exit()def check_exit_req():# get all the QUIT eventsfor event in pygame.event.get(QUIT):# terminate() will kill all the events. terminate if any QUIT events are presentterminate()# this for loop will get all the KEYUP eventsfor event in pygame.event.get(KEYUP):if event.key == K_ESCAPE:# if the user presses the ESC key then it will terminate the session and if the KEYUP event was for the Esc keyterminate()# put the other KEYUP event objects backpygame.event.post(event)def start_playing():# Return a board structure with blocks in the solved state.counter = 1board = []for x in range(w_of_board):column = []for y in range(h_of_board):column.append(counter)counter += w_of_boardboard.append(column)counter -= w_of_board * (h_of_board - 1) + w_of_board - 1board[w_of_board-1][h_of_board-1] = BLANKreturn boarddef getBlankPosition(board):# Return the x and y of board coordinates of the blank space.for x in range(w_of_board):for y in range(h_of_board):if board[x][y] == BLANK:return (x, y)def take_turn(board, move):blankx, blanky = getBlankPosition(board)if move == UP:board[blankx][blanky], board[blankx][blanky +1] = board[blankx][blanky + 1], board[blankx][blanky]elif move == DOWN:board[blankx][blanky], board[blankx][blanky -1] = board[blankx][blanky - 1], board[blankx][blanky]elif move == LEFT:board[blankx][blanky], board[blankx +1][blanky] = board[blankx + 1][blanky], board[blankx][blanky]elif move == RIGHT:board[blankx][blanky], board[blankx -1][blanky] = board[blankx - 1][blanky], board[blankx][blanky]def isValidMove(board, move):blankx, blanky = getBlankPosition(board)return (move == UP and blanky != len(board[0]) - 1) or \(move == DOWN and blanky != 0) or \(move == LEFT and blankx != len(board) - 1) or \(move == RIGHT and blankx != 0)def ramdom_moves(board, lastMove=None):# start with a full list of all four movesvalidMoves = [UP, DOWN, LEFT, RIGHT]# remove moves from the list as they are disqualifiedif lastMove == UP or not isValidMove(board, DOWN):validMoves.remove(DOWN)if lastMove == DOWN or not isValidMove(board, UP):validMoves.remove(UP)if lastMove == LEFT or not isValidMove(board, RIGHT):validMoves.remove(RIGHT)if lastMove == RIGHT or not isValidMove(board, LEFT):validMoves.remove(LEFT)# this will perform the return nad it will return a random move from the list of remaining movesreturn random.choice(validMoves)def getLeftTopOfTile(block_x, block_y):left = XMARGIN + (block_x * block_size) + (block_x - 1)top = YMARGIN + (block_y * block_size) + (block_y - 1)return (left, top)def getSpotClicked(board, x, y):# from the x & y pixel coordinates, this for loop below will get the x & y board coordinatesfor block_x in range(len(board)):for block_y in range(len(board[0])):left, top = getLeftTopOfTile(block_x, block_y)tileRect = pygame.Rect(left, top, block_size, block_size)if tileRect.collidepoint(x, y):return (block_x, block_y)return (None, None)def draw_block(block_x, block_y, number, adjx=0, adjy=0):# draw a tile at board coordinates block_x and block_y, optionally a fewleft, top = getLeftTopOfTile(block_x, block_y)pygame.draw.rect(DISPLAYSURF, TILECOLOR, (left + adjx,top + adjy, block_size, block_size))text_renderign = BASICFONT.render(str(number), True, TEXTCOLOR)text_in_rect = text_renderign.get_rect()text_in_rect.center = left + \int(block_size / 2) + adjx, top + int(block_size / 2) + adjyDISPLAYSURF.blit(text_renderign, text_in_rect)def makeText(text, color, bgcolor, top, left):# create the Surface and Rect objects for some text.text_renderign = BASICFONT.render(text, True, color, bgcolor)text_in_rect = text_renderign.get_rect()text_in_rect.topleft = (top, left)return (text_renderign, text_in_rect)# this function will draw the board wherein the player can play.
# it holds the code for displaying different color and logic behind the gamedef drawBoard(board, message):DISPLAYSURF.fill(BGCOLOR)if message:text_renderign, text_in_rect = makeText(message, MESSAGECOLOR, BGCOLOR, 5, 5)DISPLAYSURF.blit(text_renderign, text_in_rect)for block_x in range(len(board)):for block_y in range(len(board[0])):if board[block_x][block_y]:draw_block(block_x, block_y, board[block_x][block_y])left, top = getLeftTopOfTile(0, 0)width = w_of_board * block_sizeheight = h_of_board * block_sizepygame.draw.rect(DISPLAYSURF, BORDERCOLOR, (left - 5,top - 5, width + 11, height + 11), 4)DISPLAYSURF.blit(RESET_SURF, RESET_RECT)DISPLAYSURF.blit(NEW_SURF, NEW_RECT)DISPLAYSURF.blit(SOLVE_SURF, SOLVE_RECT)# this function is to handle the animation that are displayed when a user starts a new Game
# a user can see the sliding animation over the blocks
# this is made possible using the below functiondef sliding_animation(board, direction, message, animationSpeed):blankx, blanky = getBlankPosition(board)if direction == UP:move_in_xaxis = blankxmove_in_yaxis = blanky + 1elif direction == DOWN:move_in_xaxis = blankxmove_in_yaxis = blanky - 1elif direction == LEFT:move_in_xaxis = blankx + 1move_in_yaxis = blankyelif direction == RIGHT:move_in_xaxis = blankx - 1move_in_yaxis = blanky# prepare the base surfacedrawBoard(board, message)baseSurf = DISPLAYSURF.copy()# draw a blank space over the moving block on the baseSurf Surface.take_left, take_top = getLeftTopOfTile(move_in_xaxis, move_in_yaxis)pygame.draw.rect(baseSurf, BGCOLOR, (take_left,take_top, block_size, block_size))for i in range(0, block_size, animationSpeed):# this is to handle the animation of the tile sliding overcheck_exit_req()DISPLAYSURF.blit(baseSurf, (0, 0))if direction == UP:draw_block(move_in_xaxis, move_in_yaxis,board[move_in_xaxis][move_in_yaxis], 0, -i)if direction == DOWN:draw_block(move_in_xaxis, move_in_yaxis,board[move_in_xaxis][move_in_yaxis], 0, i)if direction == LEFT:draw_block(move_in_xaxis, move_in_yaxis,board[move_in_xaxis][move_in_yaxis], -i, 0)if direction == RIGHT:draw_block(move_in_xaxis, move_in_yaxis,board[move_in_xaxis][move_in_yaxis], i, 0)pygame.display.update()FPSCLOCK.tick(FPS)def generateNewPuzzle(numSlides):# this to display the animation of blockssequence = []board = start_playing()drawBoard(board, '')pygame.display.update()# we used time.wait() to pause 500 milliseconds for effectpygame.time.wait(500)lastMove = Nonefor i in range(numSlides):move = ramdom_moves(board, lastMove)sliding_animation(board, move, 'Generating new puzzle...',animationSpeed=int(block_size / 3))take_turn(board, move)sequence.append(move)lastMove = movereturn (board, sequence)def rst_animation(board, allMoves):# make all of the moves in reversereverse_moves = allMoves[:]reverse_moves.reverse()for move in reverse_moves:if move == UP:opp_moves = DOWNelif move == DOWN:opp_moves = UPelif move == RIGHT:opp_moves = LEFTelif move == LEFT:opp_moves = RIGHTsliding_animation(board, opp_moves, '',animationSpeed=int(block_size / 2))take_turn(board, opp_moves)# this is the call to main fucntion
if __name__ == '__main__':main()

输出:

生成新的拼图

【单击块或按箭头键滑动块】

在这个教程中,尽可能的讲解了每一个步骤方法,希望这篇文章对你有用。

Python拼图益智游戏源码本站下载:https://download.csdn.net/download/mufenglaoshi/88571303

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/174098.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

反思一次效能提升

前天与一个大佬交流。想起自己在6年多前在团队里做的一次小小的效能提升。 改进前 在同一个产品团队,同时有前端工程师和后端工程师。他们经常需要共同协作完成features。 前端是一个传统的多页应用。前端渲染是由后端的velocity模板引擎实现的。 打包后&#xff0c…

十四、机器学习进阶知识:KNN分类算法

文章目录 1、KNN分类介绍2、KNN分类核心要素3、KNN分类实例1.1 鸢尾花分类1.2 手写数字识别 1、KNN分类介绍 分类是数据分析中非常重要的方法,是对己有数据进行学习,得到一个分类两数或构造出一个分类模型(即通常所说的分类器(Classifier))。分类是使用…

卸载软件最最最彻底的工具——Uninstall Tool

卸载软件最最最彻底的工具——Uninstall Tool Uninstall Tool 是一款功能强大的专业卸载工具。针对一些普通卸载不彻底的问题,它可以做到最优,比如Matlab等软件的卸载难的问题也可以较好地解决。 它比 Windows 自带的“添加/删除程序”功能快 3 倍&…

经典的Shiro反序列化漏洞复现

目录 0x01、前言 0x02、环境搭建 0x03、漏洞原理 0x04、漏洞复现 0x05、漏洞分析 5.1、加密 5.2、解密 0x06、总结 0x01、前言 相信大家总是面试会问到java反序列化,或者会问到标志性的漏洞,比如shiro反序列化,或者weblogic反序列化漏…

arcgis pro使用自定义svg图标

1.点击目录窗格中的样式 可以将本地图标加载到收藏夹中 2.右键收藏夹,点击管理样式 3.双击收藏夹进入此页面 4.在空白处右键,点击新建 5.选中新建的符号,更改图标 6.上传完毕,点击应用即可使用 7.效果 8.此外也可以直接上传图片…

【Linux】vim-多模式的文本编辑器

本篇文章内容和干货较多,希望对大家有所帮助👍 目录 一、vim的介绍 1.1 vi 与 vim的概念1.2 Vim 和 Vi 的一些对比 二、vim 模式之间的切换 2.1 进入vim2.2 [正常模式]切换到[插入模式]2.3 [插入模式]切换至[正常模式]2.4 [正常模式]切换至[底行模式…

怎么快速制作一本出色的电子期刊!

比起传统纸质期刊,电子期刊有着众多的优势,它打破了以往的传播形式和人们传统的时空观念,从而更加贴近人们的生活,更好地满足新时代人们对文化生活的更高要求。如何制作电子期刊呢? 其实很简单,只需要使用…

【在线AI绘画平台】哩布AI 在线生成图片、训练Lora、上传AI生成图简易实测

文章目录 一、主页面介绍1.1首页[网址 https://www.liblib.ai/](https://www.liblib.ai/)侧边栏可收起 1.2 模型页面1.2.1 按模型筛选相关1.2.2 封面的细节1.2.3 点击后进入封面: 二、在线生成图片2.1 直接从主页面进入2.1.1 在线生成的链接(与webui几乎…

代理模式-C语言实现

UML图&#xff1a; 代码实现&#xff1a; #include <stdio.h>// 抽象主题接口 typedef struct {void (*request)(void*); } Subject;// 具体主题类 typedef struct {void (*request)(void*); } RealSubject;void RealSubject_request(void* obj) {printf("RealSubj…

win10下载Remix IDE桌面版以及空白页面的解决

文章目录 Remix IDE 的下载Remix IDE 空白页面的解决 Remix IDE 的下载 到 github 地址 https://github.com/ethereum/remix-desktop/releases 选择exe文件或根据自己电脑版本选择对应的zip文件进行下载&#xff0c;然后正常安装即可。 Remix IDE 空白页面的解决 有时打开Remix…

docker容器的生命周期管理常用命令

容器的生命周期管理命令 docker create &#xff1a;创建一个新的容器但不启动它 docker create nginx docker run :创建一个新的容器并运行一个命令 常用选项&#xff1a; 常用选项1. --add-host&#xff1a;容器中hosts文件添加 host:ip 映射记录 2. -a, --attach&#…

【STL】string类 (下)

目录 1&#xff0c;insert 2&#xff0c;erase 3&#xff0c;find 4&#xff0c;replace 5&#xff0c;rfind 6&#xff0c;substr 7&#xff0c;find_first_of 8&#xff0c;find_first_not_of 9&#xff0c;find_last_of 10&#xff0c;operator 11&#xff0c;ge…

深入理解对象与垃圾回收机制

1、虚拟机中对象创建过程 1.1 对象创建过程 当我们使用 new 创建一个对象时&#xff0c;在 JVM 中进行了如下操作&#xff1a; 类加载&#xff1a;把 class 加载到 JVM 运行时数据区的过程。可以通过本地文件的形式&#xff0c;也可以通过网络加载。 检查加载&#xff1a;首…

【23真题】大题全原题的211!题源已定位!

今天分享的是23年长安大学814的信号与系统试题及解析。 本套试卷难度分析&#xff1a;22年长安大学814考研真题&#xff0c;我也发布过&#xff0c;若有需要&#xff0c;戳这里自取&#xff01;本套试题难度中等偏下&#xff0c;题量偏多&#xff0c;考察的知识点也是很常见的…

Robot Framework自动化测试(四)--- 分层思想

&#x1f4e2;专注于分享软件测试干货内容&#xff0c;欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; 如有错误敬请指正&#xff01;&#x1f4e2;交流讨论&#xff1a;欢迎加入我们一起学习&#xff01;&#x1f4e2;资源分享&#xff1a;耗时200小时精选的「软件测试」资…

Chatbot开发三剑客:LLAMA、LangChain和Python

聊天机器人&#xff08;Chatbot&#xff09;开发是一项充满挑战的复杂任务&#xff0c;需要综合运用多种技术和工具。在这一领域中&#xff0c;LLAMA、LangChain和Python的联合形成了一个强大的组合&#xff0c;为Chatbot的设计和实现提供了卓越支持。 首先&#xff0c;LLAMA是…

如何往excel中写子表?

with pd.ExcelWriter("C:/last_date.xlsx") as writer:for i in range(0, 10):df pd.DataFrame()df.to_excel(writer, indexFalse, sheet_namestr(days[i 1]))

OpenCV入门11——图像的分割与修复

文章目录 图像分割的基本概念实战-分水岭法(一)实战-分水岭法(二)GrabCut基本原理实战-GrabCut主体程序的实现实战-GrabCut鼠标事件的处理实战-调用GrabCut实现图像分割meanshift图像分割视频前后景分离其它对视频前后影分离的方法图像修复 图像分割是计算机视觉中的一个重要领…

Docker智驾开发环境搭建

文章目录 背景1. 什么是容器?2. 什么是Docker?2.1 Docker架构3. 为什么要使用Docker?3.1 Docker容器虚拟化的好处3.2 Docker在开发和运维中的优势4. Docker容器与传统虚拟化的区别4.1 区别4.2 Docker的优势5. Docker的核心概念6. Docker在嵌入式开发中的应用7. docker实践参…

deque容器结构学习笔记

1.结构图 2.deque对比vector和list deque双端队列&#xff0c;就像是list和vector的结合 vector&#xff1a; 优点&#xff1a;1.可以随机读取 2. 空间利用率高 缺点&#xff1a;1. 除了尾插尾删&#xff0c;其他插入删除效率比较低 2. 扩容效率低 list&#xff1a; 优点&…