第一篇:PyGame小游戏——2D迷宫游戏(16W字详解)

目录 

在开头的开场白 

在CSDN看到一篇“利用深度优先算法自动生成随机迷宫”的blog,突发灵感,就想做这个迷宫游戏。本文大部分讲的意思,而非代码,文章最后会展示最终代码和图片的,读者不用过多地注意那些。本文是作者第一次写blog,难免有些不成熟,希望读者见谅!

思路 

实现 

安装

项目目录

素材

pygame框架

添加自动生成迷宫代码

添加人物、终点 

人物移动 

 添加检测机制使人物不会穿墙而过

检测人物是否到达终点(是否两者碰撞)

在游戏界面添加注释(包括直接显示在游戏界面的游戏规则和显示计时、步数、关卡等)

 在游戏界面在迷宫中随机添加金币、能量值

第一种方案 

第二种方案

为使金币和能量值有价值,添加奖励机制

奖励①:3能量值/300金币自动通关 

 奖励②:白色隐藏的秘密格子,奖励力度大

设计过关界面,在过关界面中添加本次闯关的记录和按钮

设计进入窗口的界面(主界面,有“开始游戏”等按钮)

添加游戏暂停功能

添加数据保存功能

添加主页面的游戏规则按钮内的界面

添加截屏记录功能,在通关界面显示,并保存到本地(maze_game.py目录下)

流程

导入库 

获取PyGame窗口句柄

获取PyGame屏幕并保存到本地

添加到过关界面

​编辑

添加一些其他功能

总结

代码

项目总结

建议


在开头的开场白 

在CSDN看到一篇“利用深度优先算法自动生成随机迷宫”的blog,突发灵感,就想做这个迷宫游戏。本文大部分讲的意思,而非代码,文章最后会展示最终代码和图片的,读者不用过多地注意那些。本文是作者第一次写blog,难免有些不成熟,希望读者见谅!

(博主主页:王俞励——最后冲刺!的博客_CSDN博客-PyGame小游戏领域博主

我们使用python的PyGame来实现。

 

思路 

  1.  将pygame框架写好
  2. 导入素材
  3. 添加自动生成迷宫(我在网上借鉴的)
  4. 添加人物、终点
  5. 人物移动
  6. 添加检测机制使人物不会穿墙而过
  7. 检测人物是否到达终点(是否两者碰撞)
  8. 在游戏界面添加注释(包括直接显示在游戏界面的游戏规则和显示计时、步数、关卡等)
  9. 在游戏界面在迷宫中随机添加金币、能量值
  10. 为使金币和能量值有价值,添加奖励机制
  11. 设计过关界面,在过关界面中添加本次闯关的记录和按钮
  12. 设计进入窗口的界面(主界面,有“开始游戏”等按钮)
  13. 添加游戏暂停功能
  14. 添加数据保存功能
  15. 添加主页面的游戏规则按钮内的界面
  16. 添加截屏记录功能,在通关界面显示,并保存到本地(maze_game.py目录下)
  17. 添加一些其他功能

    开始实现!

实现 

安装

使用pip安装:

pip install pygame

如果没有安装pip包的话,自行安装。

项目目录

项目目录

素材

click.jpeg
lightning.png
logo.png
coin.jpeg

备注:由于我放出来时不一定下载下来效果好,考虑到最终效果,我把这些图片的下载网址(百度免费)放在这里,如下:

图片名URL(统一资源定位器)网址
click.jpeghttps://dwz.mk/b6fYNn
coin.jpeg金币logo_百度图片搜索
logo.png迷宫logo_百度图片搜索
lightning.png闪电logo_百度图片搜索(截屏截取右边闪电,不要留白,若不方便,右键点击上面的图片下载到本地)

show.mp4文件只要放对了路径,可以是自己录制的游戏规则视频。PyGameTkIcon.ico可以是自己做的16×16像素的ico文件,制作ico文件推荐在线网站:https://tool.lu/favicon/

show.mp4

由于上传图片只能上传png、jpeg、jpg、bmp、gif、webp那些后缀名的图片,ico图片文件没法上传,所以我没把自制ico放在这。我加这个ico文件只是为了方便打包成exe可执行文件,在项目中没其他用处。 胖娃体.ttf原来我想用的,结果没用上,所以就搁置在那里了。

pygame框架

import pygame
import pygame as pg
pygame.init()
screen = pygame.display.set_mode(screen_size,pygame.RESIZABLE)
pygame.display.set_icon(pygame.image.load("logo.png"))
pygame.display.set_caption('Maze Game\'s window, version 1.0.2')
screen.fill((255,255,255))
while True:
    """
    游戏主循环
    """
    screen.blit(pygame.image.load("logo.png"),(0,0))#开始时展示logo
    pygame.display.update()

logo.png上文已经展示。 

添加自动生成迷宫代码

我从网上借鉴的:

​
import pygame
import pygame as pg
import time
import random
import sys

class Tile():
    def __init__(self, grid_size, screen_size, x, y):  # 主要是储存数据和绘制图形,与算法无关
        self.x, self.y = x, y
        self.connected = [0, 0, 0, 0]  # up,right,down,left 0 for not connected
        self.grid_size = grid_size
        self.tile_size = [(screen_size[0] - 100) / grid_size[0], (screen_size[1] - 100) / grid_size[1]]
        self.rectangle = (
        self.x * self.tile_size[0] + 50, self.y * self.tile_size[1] + 50, self.tile_size[0], self.tile_size[1])
        self.points = [[self.x * self.tile_size[0] + 50, self.y * self.tile_size[1] + 50],  # uppper left
                       [self.x * self.tile_size[0] + 50 + self.tile_size[0], self.y * self.tile_size[1] + 50],
                       # upper right
                       [self.x * self.tile_size[0] + 50 + self.tile_size[0],
                        self.y * self.tile_size[1] + 50 + self.tile_size[1]],  # lower right
                       [self.x * self.tile_size[0] + 50, self.y * self.tile_size[1] + 50 + self.tile_size[1]],
                       # lower left
                       ]
        self.visited = False

    def draw(self, color=(255, 253, 150)):  # x,y represents the tile coordinates
        pg.draw.rect(screen, color, self.rectangle)  # 绘制节点
        for i in range(4):  # 绘制四面墙壁
            if not self.connected[i]:
                pg.draw.line(screen, (150, 175, 255), (self.points[i]), (self.points[((i + 1) % 4)]), 5)


def maze_gen(path):
    global tile_covered  # 覆盖节点数量,当覆盖节点数量到达网格数量则停止
    x, y = path[-1]
    if x < 0 or x >= grid_size[0] or y < 0 or y >= grid_size[1]:  # 超出网格范围则退出
        print(f'index out of range at {x, y}')
        return
    matrix[y][x].draw()
    if matrix[y][x].visited:  # 已访问节点则退出
        print(f'node already visited at {x, y}')
        return
    elif tile_covered <= grid_size[0] * grid_size[1]:  # 覆盖节点数量未到达网格总数量
        tile_covered += 1
        matrix[y][x].visited = True
        path_choice = [0, 1, 2, 3]
        random.shuffle(path_choice)
        directions = [[0, -1], [1, 0], [0, 1], [-1, 0]]  # up,right,down,left 0 for not connected

        for i in path_choice:
            x_, y_ = x + directions[i][0], y + directions[i][1]
            path.append([x_, y_])
            if maze_gen(path):
                matrix[y][x].connected[i] = 1  # walls of current node
                matrix[y_][x_].connected[(i + 2) % 4] = 1  # reverse the vector direction
                matrix[y][x].draw()
                matrix[y_][x_].draw()

            path.pop(-1)
        pg.display.update()

        return True

    else:
        print('all node visited')
        return


screen_size = [900, 900]
grid_size = [50, 50]
exit = [5, 5]
tile_covered = 0
run = True

screen = pg.display.set_mode(screen_size,pygame.FULLSCREEN)

matrix = []
for y in range(grid_size[1]):  # 创建二维矩阵,x,y代表坐标
    temp = []
    for x in range(grid_size[0]):
        tile = Tile(grid_size, screen_size, x, y)
        temp.append(tile)
    matrix.append(temp)

pg.init()
path = [[0, 0]]

screen.fill((255, 255, 255))
maze_gen(path)

pg.display.update()

print('======== Generation Finished ========')
while run:  # 生称完毕之后不退出,使用循环
    for event in pg.event.get():
        if event.type == pg.QUIT:
            time.sleep(0.1)
            pg.quit()
            sys.exit()

​

再把背景颜色和线条颜色分别调成白色和黑色即可,我在这里就不多做赘述了。

添加人物、终点 

迷宫生成成功了,但是没人物和终点怎么玩、算什么游戏啊,所以我们接下来要展示人物。

我在游戏中由于方便、看起来简约,使用了pygame.draw.rect()为绘画人物的工具。先看下rect函数的结构:

pygame.draw.rect

  原型:pygame.draw.rect(Surface, color, Rect, width=0): return Rect

  用途:在Surface上绘制矩形,第二个参数是线条(或填充)的颜色,第三个参数Rect的形式是((x, y), (width, height)),表示的是所绘制矩形的区域,其中第一个元组(x, y)表示的是该矩形左上角的坐标,第二个元组 (width, height)表示的是矩形的宽度和高度。width表示线条的粗细,单位为像素;默认值为0,表示填充矩形内部。

  此外,Surface.fill 同样可以用来绘制填充矩形。

太棒了!真好符合我们的宗旨:简约,而且简单,那么,我们添加人物和终点

my=pygame.Rect(x,y,9,9) # 人物1(主角)的Rect实例
pygame.draw.rect(screen,(0,255,255),my,0) # 画主角,颜色为类似cyan的

 为什么我没在这里写上终点的rect函数和Rect实例呢?因为在游戏中,重点都设在同一地点太无聊了,所以可以在迷宫地图除了左上角(主角的出生地)以外的三个顶点生成终点。这样,看起来就不那么无聊了。为实现这个功能,且为了以后实现多次游玩(否则运行一次只能玩一局),我们封装一个run函数。目前,我们的run函数为以下:

def run():
    global matrix,exit,tile_covered
    ###############################
    """
    创建一个崭新的迷宫
    """
    matrix=[]
    exit=[20,20]
    tile_covered=0
    for y in range(grid_size[1]):  # 创建二维矩阵,x,y代表坐标
        temp = []
        for x in range(grid_size[0]):
            tile = Tile(grid_size, screen_size, x, y)
            temp.append(tile)
        matrix.append(temp)
    path = [[0, 0]]
    ###############################
    """
    随机获取终点位置
    """
    where = random.randint(1, 3) # 随机获取1~3,代表每个顶点
    global rect1
    if where == 1:
        rect1 = pygame.Rect(900 - 65, 900 - 65, 9, 9) # 创建rect1(终点)的Rect实例
    elif where == 2:
        rect1 = pygame.Rect(54, 900 - 65, 9, 9) # 创建rect1(终点)的Rect实例
    else:
        rect1 = pygame.Rect(900 - 65, 54, 9, 9) # 创建rect1(终点)的Rect实例
    screen.fill((255, 255, 255))
    maze_gen(path)
    pg.display.update()

老样子,为了以后可能出现的BUG,我没有在开始时绘制终点,因为如果出现了BUG,很可能就遮住了一开始画的终点,故我想放到游戏主循环中。 要是在while中,就这样写就可以了:

...
while True:
    ...
    pygame.draw.rect(screen,'red',rect1,0)
    ...

人物移动 

有了主人公,主人公没法动变成个残疾可不好!所以,接下来我们要加上人物移动的语句。先复习一下pygame中的侦测相关语句:

import pygame
pygame.init()
screen=pygame.display.set_mode((900,900))
...
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                print("按下↓键啦!")
            elif event.key == pygame.K_UP:
                print("按下↑键啦!")
            elif event.key == pygame.K_LEFT:
                print("按下←键啦!")
            elif event.key == pygame.K_RIGHT:
                print("按下→键啦!")
        #还有老多侦测的东西,任务的移动主要是WSAD/上下左右键控制,不用那么多。
    ...

为了主人公控制“体重”和“腰宽”,防止主人公太胖被卡住,我们要设置一下主人公(正方形)的边长:

   总共21像素(加上墙壁),那么(请读者不要纠结于比例,只要了解即可):

                  5(生成迷宫时用的墙宽5像素)       11           5(同前一个5理)

                        ||||||(墙壁)                   剩下11像素走廊宽                          ||||||(墙壁)

                        ||||||                        两边留1像素,11-1*2=9(像素)           |||||||

所以将人物“腰宽:设为9像素。

那么,控制人物的代码大概是这样的:

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if (event.key == pygame.K_LEFT or event.key == pygame.K_a:
                x-=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x+16,y,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x+16,y,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
            elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
                y+=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x,y-16,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x,y-16,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
            elif event.key == pygame.K_UP or event.key == pygame.K_w:
                y-=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x,y+16,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x,y+16,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
            elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                x+=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x-16,y,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x-16,y,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)

之所以每次移动16像素,是因为我们的主人公像松鼠一样,先把后面的“脚印”扫掉,再走出去……循环往复。可是——我不小心按错了一个键,发现,主人公竟然能穿墙!这可不好,这样,这个50×50格的迷宫就最多用100步即可到达,那就太无聊了,接下来,我们要尝试添加检测机制使人物不会穿墙而过。

 添加检测机制使人物不会穿墙而过

我们这个迷宫游戏有了主人公、终点和迷宫地图,可是主人公却会穿墙!

接下来解决这个问题。

我的构思:

定义can字典,里边是每个方向能否通过的信息:

can={'right':True,'left':True,'up':True,'down':True} #先全部初始化为是

然后获取整个屏幕各个像素点的颜色,然后利用列表切片获取迷宫部分的颜色,检测人物周围15像素(迷宫一次走16像素,-1就是15),看是不是有"(0,0,0)"(黑色,墙壁)如果有,那么这个方向的True将变成False,在移动时检测can字典这个键对应的值是否是True,如果是才能往这个方向走。随后,del pixel,释放内存。那么,为了方便,我们封装到函数中:

def has_color(aSurface,aRect,aColor):
    pixel=pygame.PixelArray(aSurface)               #锁定aSurface,将各点颜色保存在2维数组
    aPixel=pixel[aRect.x:aRect.x+aRect.width+15,aRect.y:aRect.y+aRect.height+15]  #得到数组切片
    global can
    if aColor in aPixel:
        if aColor in pixel[aRect.x-15:aRect.x+aRect.width,aRect.y:aRect.y+aRect.height]:
            can['left']=False
        if aColor in pixel[aRect.x:aRect.x+aRect.width+15,aRect.y:aRect.y+aRect.height]:
            can['right']=False
        if aColor in pixel[aRect.x:aRect.x+aRect.width,aRect.y-15:aRect.y+aRect.height]:
            can['up'] = False
        if aColor in pixel[aRect.x:aRect.x+aRect.width,aRect.y:aRect.y+aRect.height+15]:
            can['down'] = False
        if aColor not in pixel[aRect.x-15:aRect.x+aRect.width,aRect.y:aRect.y+aRect.height]:
            can['left']=True
        if aColor not in pixel[aRect.x:aRect.x+aRect.width+15,aRect.y:aRect.y+aRect.height]:
            can['right']=True
        if aColor not in pixel[aRect.x:aRect.x+aRect.width,aRect.y-15:aRect.y+aRect.height]:
            can['up'] = True
        if aColor not in pixel[aRect.x:aRect.x+aRect.width,aRect.y:aRect.y+aRect.height+15]:
            can['down'] = True
    else:
        can['left']=True
        can['right']=True
        can['up']=True
        can['down']=True
    del pixel           #解锁aSurface并删除数组

看起来眼花缭乱,是不是?我们简化一下:

def has_color(aSurface,aRect,aColor):
    pixel=pygame.PixelArray(aSurface)               #锁定aSurface,将各点颜色保存在2维数组
    aPixel=pixel[aRect.x:aRect.x+aRect.width+15,aRect.y:aRect.y+aRect.height+15]  #得到数组切片
    global can
    if aColor in aPixel:
        if aColor in pixel[aRect.x-15:aRect.x+aRect.width,aRect.y:aRect.y+aRect.height]:
            can['left']=False
        else:
            can['left']=True
        if aColor in pixel[aRect.x:aRect.x+aRect.width+15,aRect.y:aRect.y+aRect.height]:
            can['right']=False
        else:
            can['right']=True
        if aColor in pixel[aRect.x:aRect.x+aRect.width,aRect.y-15:aRect.y+aRect.height]:
            can['up'] = False
        else:
            can['up']=True
        if aColor in pixel[aRect.x:aRect.x+aRect.width,aRect.y:aRect.y+aRect.height+15]:
            can['down'] = False
        else:
            can['down']=True
    else:
        can['left']=True
        can['right']=True
        can['up']=True
        can['down']=True
    del pixel           #解锁aSurface并删除数组

 这样,看起来比一开始好多了。为什么是检测15像素呢?

pygame中绘画矩形时使用的矩形坐标是用矩形左上方坐标为准,所以如果要判断右方下方是否有墙,就要穿过走廊检测,走廊+墙壁16像素,若检测16像素的话在检测左方墙壁和上方墙壁时会穿墙检测到用墙壁隔开的相邻走廊的墙壁,导致明明还有一格却走不过去的现象。在代码中为了方便,全部使用了15像素。

检测人物是否到达终点(是否两者碰撞)

其实这个挺简单,只要pygame.rect库中的colliderect()即可。

        原型:(class Rect)def colliderect(self, Rect): return bool

        用途:检测两个Rect实例是否碰撞,如果碰撞返回True,否则返回False。在使用时,第一个Rect实例+“.”+colliderect(第二个Rect实例)即可。碰到即算碰撞。

 那么,两个矩形是否碰撞就是:

while True:
    ...
    if rect1.colliderect(my):
        print("到达终点啦")
        """
        再次初始化
        """
        ...

在游戏界面添加注释(包括直接显示在游戏界面的游戏规则和显示计时、步数、关卡等)

玩的时候玩家不知道游戏规则怎么办?这个很简单,显示游戏规则。主要涉到font的render渲染。首先上创建字体实例:

match_font函数获取字体文件,若是系统自带字体(例如仿宋,fangsong.ttc)则不用写文件后缀名。若是自定义字体文件(.ttf),就要写后缀名.ttf。

获取完字体文件,通过Font来转换成朋友game、使用的Font实例。

font_name = pygame.font.match_font('fangsong')  # 1.获得字体文件
font = pygame.font.Font(font_name, 20) # 2.将提取的字体文件变成Font实例,待会儿就直接用。

第一行,先获取字体文件,第二行创建Font实例,Font的第二个参数的意思是大小,单位为像素。为什么一定要用自己的字体呢?因为pygame默认字体对中文的话字体显示就会显示成一个个豆腐块儿,很不美观,所以我们用系统自带字体/自定义字体。Windows系统自定义字体位于:

C:\Windows\Fonts

 例如我的Windows11:

我的电脑上的系统自带字体

在此时,如果你用这里的字体,不用copy到你的文件夹,直接写上文件名,pygame如果在pygame默认字体中找不到,会到这里找的。值得注意的是,他们的文件图标下的字并不是他们的文件名,需要看“属性”才行,那里才是真的文件名:

真正的文件名和假的文件名

 查看属性的方法:鼠标右击文件,点击属性即可。如果是自定义字体,则无需查看属性,文件图标下的即是标准文件名哦!

可是有了Font实例,如何显示字呢?

        原型:(class Font)def render(text,antialias,color,background): return Surface

        用途:渲染一段文字text,如果第二个参数为True,那么使用抗锯齿渲染,否则字不抗锯齿。随后使用surface.blit(Surface,(x,y))即可显示到屏幕上。

原来如此! 那么,我们就这样显示文字:

...
font_name = pygame.font.match_font('fangsong')
font = pygame.font.Font(font_name, 20)
while True:
    # 方法1
    screen.blit(font.render("Hello",True,"orange"),(0,0))
    # 方法2
    font_surface=font.render("Hello",True,"orange")
    screen.blit(font_surface,(0,0))

 以上两种方法都可以。那么,在项目中,我们这样写:

font_surface = font.render('你已走的步数:{},现在是第{}关,计时:{:0>2}:{:0>2}:{:0>2}'.format(str(steps),str(level),str(h2),str(m2),str(s2)), True, 'orange')
screen.blit(font_surface,(20,20))

 在游戏界面在迷宫中随机添加金币、能量值

我们可以浅玩一下了!不过这样的游戏好像没什么玩头。所以,接下来我们就要增添奖励机制。有金币和能量值,这些道具的用处可不小,后面会讲。

我们可以构思一下:通常金币是从像超级马里奥那样在闯关的过程中获得金币、蘑菇和松果等,很少一部分是奖励的。所以,我们也可以在迷宫中随机布下这些奖励。然而,就像在超级马里奥中的那样,特殊的道具(蘑菇等,在本案例中是能量值)往往都比金币稀有,所以,一个关卡我们就布置1个能量值即可,布置多了会影响能量值的含金量。


既然构思好了,那么就开始想方案。

编号                                                     描述
通过随机迷宫的每一个可到达的格子,再从这个列表中随机获取n个位置,从而摆放金币和能量值。
通过重复循环n次,随机金币、能量值的(x,y)坐标,以到达随机的目的。

再来探讨一下,两者的好坏。

第一种方案 

好处:容易实现。运用random库可以毫不费力的解决,并且还游刃有余,而且算法非常简单。

坏处:耗费内存。要枚举所有能走的迷宫地方,50×50的迷宫,100金币+1能量值,也就是说2500项的列表中只有101项用了,用了\frac{100+1}{50*50}的列表,也就是只用了4.04%的列表,剩下的95.96%全部浪费了!耗费时间。这个算法的时间复杂度为O(n^3),是非常大的了,因为:O(1)<O(logn)<O(n)<O(nlgn)<O(n^2)<O(n^3)<O(2^n),而这个算法排在倒数第二项,是立方阶。

第二种方案

好处:节省时间。这个算法的时间复杂度为O(n)

坏处:容易重叠。如果想要算法简洁,那么重复n次两次随机1~50,将每次循环中的两次随机的结果x和y想成一个50×50的点阵图中坐标为(x,y)的红点,假设n=100(我们的游戏中的确如此),两次画出的红点重合的概率有4%,虽然很小,但是在二次开发时例如在评分时有“100个金币你捡了多少个金币”纳入考量的话,重叠了的金币很可恶,若是玩家明明捡全了显示的所有金币,结果显示结果/战绩时说只捡了99%的金币。

如果再if (x,y)in(list):的话时间复杂度就要O(n^2)了……


①方案第一点是:获取迷宫的每一个格子,以方便随机位置。

for i in range(55,850-55,16): # 循环x轴
    listA=[] # 每次清空列表
    for j in range(54,850-54,16): # 循环y轴
        listA.append((i,j))
    mapOfList.append(listA) # 添加

以上的代码相信大家肯定不会有疑问,就是枚举每一个迷宫能到达的点位于整个屏幕的坐标(左上角坐标)。 

①方案第二点是:随机。

相信大家对随机本身含义和在Python中如何实现并不陌生,大部分人在Python基础时就都知道一个关于随机的Python自带库:random。那么,接下来,就是实现功能了。

        原型:def random.choice(seq): return seq[random.randint(0,len(seq)-1)]

        描述:seq可以是一个元组、列表或字符串。他会返回从集合seq中随机抽取的元素。

那么就很简单了,由于我们一开始创建的是二维列表,所以我们要这样随机抽取一个坐标:

import random
random_x,random_y=random.choice(random.choice(mapOfList))
#                         ↓               ↓           ↓
#   从随机出来的行中随机挑选一个元素 从列表中随机提取一行 原二维列表

那么随机抽取100个金币位置和1个能量值位置(且互不重叠)就应该这样写:

coin_blits = []
lightning_blits = (0, 0)
for i in range(100):
    choices = random.choice(random.choice(mapOfList))
    coin_blits.append(choices)
    screen.blit(pygame.transform.scale(image_coin,(9,9)), choices)
while lightning_blits == (0, 0) or lightning_blits in coin_blits:
    lightning_blits = random.choice(random.choice(mapOfList))

while循环是为了得到不在金币区域内且还未定值(0,0为初始化,如果lightning_blits还是初始化时赋值的,那就还没有选择迷宫内的能量值地址)的能量值地址。 

然后再将能量值(金币已经印刷过了)“印刷”到屏幕:

screen.blit(pygame.transform.scale(image_lightning,(11,11)), lightning_blits)

 由于每局金币位置不同,所以添加在run函数中,run在的样子如下:

def run():
    global matrix,exit,tile_covered
    matrix=[]
    exit=[20,20]
    tile_covered=0
    for y in range(grid_size[1]):  # 创建二维矩阵,x,y代表坐标
        temp = []
        for x in range(grid_size[0]):
            tile = Tile(grid_size, screen_size, x, y)
            temp.append(tile)
        matrix.append(temp)
    path = [[0, 0]]
    where = random.randint(1, 3)
    global rect1
    if where == 1:
        rect1 = pygame.Rect(900 - 65, 900 - 65, 9, 9)
    elif where == 2:
        rect1 = pygame.Rect(54, 900 - 65, 9, 9)
    else:
        rect1 = pygame.Rect(900 - 65, 54, 9, 9)
    screen.fill((255, 255, 255))
    maze_gen(path)
    pg.display.update()
    global coin_blits,lightning_blits
    coin_blits = []
    lightning_blits = (0, 0)
    for i in range(100):
        choices = random.choice(random.choice(mapOfList))
        coin_blits.append(choices)
        screen.blit(pygame.transform.scale(image_coin,(9,9)), choices)
    while lightning_blits == (0, 0) or lightning_blits in coin_blits:
        lightning_blits = random.choice(random.choice(mapOfList))
    screen.blit(pygame.transform.scale(image_lightning,(11,11)), lightning_blits)

之所以装在 coin_blits(列表,装100个位置)、lightning_blits(元组,因为只要装1个位置),是因为之后还要检测是否捡到了金币,只要if xxx in xxx就好了,方便多了。接下来,就是在游戏循环时如果碰到了金币/能量值时,他们的个数+1。这个很简单,只要in一下即可:

if (mys[0],mys[1]) in coin_blits:
    get_coin+=1 # 游戏总得到金币+1
    this_coin+=1 # 本局得到能量值+1
    while (mys[0],mys[1]) in coin_blits: # 如果还有这个坐标在
        coin_blits.remove((mys[0],mys[1])) # 那么就删除它
if (mys[0],mys[1]) == lightning_blits:
    get_lightning+=1 # 游戏总得到能量值+1
    this_lightning+=1 # 本局得到能量值+1
    lightning_blits=(0,0) # 重新初始化

while True部分就要变了:

while True:
    home(False)
    times = time.localtime()
    h, m, s = times.tm_hour, times.tm_min, times.tm_sec
    my=pygame.Rect(x,y,9,9)
    pygame.draw.rect(screen,(0,255,255),my,0)
    pygame.draw.rect(screen,(255,255,255),(20,0,900,40),0)
        font_surface = font.render('你已走的步数:{},现在是第{}关,计时:{:0>2}:{:0>2}:{:0>2},此局获得{:0>3}枚金币,{:0>3}个能量值。'.format(str(steps),str(level),str(h2),str(m2),str(s2),this_coin,this_lightning), True, 'orange')
        # h3,m3,s3=h2,m2,s2
    # else:
    #     font_surface = font.render('你已走的步数:{},现在是第{}关,计时:{:0>2}:{:0>2}:{:0>2}'.format(str(steps), str(level), str(h3), str(m3), str(s3)), True,'orange')
    # pygame.draw.rect(screen, 'white', (0,860,900,30),0)
        elif event.type == pygame.KEYDOWN:
            has_color(screen,my,(0,0,0))
            if event.key == pygame.K_q:
                    pg.quit()
                    sys.exit()
            if (event.key == pygame.K_LEFT or event.key == pygame.K_a) and can['left'] :
                x-=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x+16,y,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x+16,y,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    this_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
            elif (event.key == pygame.K_DOWN or event.key == pygame.K_s) and can['down'] :
                y+=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x,y-16,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x,y-16,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
            elif (event.key == pygame.K_UP or event.key == pygame.K_w) and can['up']:
                y-=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x,y+16,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x,y+16,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    this_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
            elif (event.key == pygame.K_RIGHT or event.key == pygame.K_d) and can['right']:
                x+=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x-16,y,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x-16,y,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    this_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)

    pygame.draw.rect(screen,'red',rect1,0)
    pygame.display.update()

 总之碰到金币/能量值就加一的功能还是挺简单的。

为使金币和能量值有价值,添加奖励机制

金币和能量值现在是一文不值的,玩家可不会故意收集一文不值的东西哇!所以,我们可以加一些奖励机制。

 编号                                              描述
   ①通关是玩家的第一要务。我们可以用3能量值/300金币来自动通关。
   ②在关卡迷宫中设置类似白色的秘密格子的地方,走到上面就得到,虽然很难找到,但奖励力度很大。
    ...                                            ...

读者可自行想象。接下来实现。

奖励①:3能量值/300金币自动通关 

我们要设计一个按钮,点击后即可使用奖励一,前提是足够能量值/金币。以扣能量值优先。我们就用裸按钮吧,只用一行字,点击那行字就能触发。上面已经1说过了Font和render,我在这里就不再赘述了,只是用就好。

font_surface13 = font.render('点我使用3个能量值/300枚金币合成"钞"能力,直接通关',True,(132,112,255))
screen.blit(font_surface13, (900-28*20, 5))

那么接下来是按钮。如果鼠标坐标在按钮左上角至右下角之内,就是按到了。伪代码就是:

if 按钮左上角x坐标<=event.pos[0]<=按钮右下角x坐标 and 按钮左上角y坐标<=event.pos[1]<=按钮右下角y坐标:
    print("按到了!")
    #按到了

 代码,moneyGo变量是bool类型,代表是否开通了本关的自动通关:

if 900-25*20 <= event.pos[0] <= 900 and 5 <= event.pos[1] <= 25:
                if get_lightning>=3:
                    moneyGo=True
                    get_lightning-=3
                elif get_coin>=300:
                    moneyGo=True
                    get_coin-=300
                else:
                    tkinter.messagebox.showwarning('提示','您的能量值和金币都不足,无法合成“钞”能力。')

在下面的检测通关的代码中,要改一下:

while True:
    ...
    if rect1.colliderect(my) or moneyGo:
        ...
        # 通关

 奖励②:白色隐藏的秘密格子,奖励力度大

我们添加一个“秘密格子”。很简单,以前的添加能量值时已经说过,我这里不再解释了,代码:

    while secret_box == (0,0) or secret_box == lightning_blits or secret_box in coin_blits:
        secret_box = random.choice(random.choice(mapOfList))
    print(secret_box)

再将所有上下左右WSAD键控制的地方加上secret box的判定。作为一个秘密格子,奖励力度是非常大的,secret变量的意思是是不是已经领取了奖励(秘密格子是否已经被发现),防止多次获取奖励,刷奖励:

            if (event.key == pygame.K_LEFT or event.key == pygame.K_a) and can['left'] and not pause:
                x-=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x+16,y,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x+16,y,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    this_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
                if (mys[0],mys[1]) == secret_box and secret:
                    secret=False
                    news_lightning=random.randint(3,6)
                    news_coin=random.randint(100,300)
                    get_coin+=news_coin
                    this_coin+=news_coin
                    get_lightning+=news_lightning
                    this_lightning+=news_lightning
            elif (event.key == pygame.K_DOWN or event.key == pygame.K_s) and can['down'] and not pause:
                y+=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x,y-16,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x,y-16,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
                if (mys[0],mys[1]) == secret_box and secret:
                    secret=False
                    news_lightning=random.randint(3,6)
                    news_coin=random.randint(100,300)
                    get_coin+=news_coin
                    this_coin+=news_coin
                    get_lightning+=news_lightning
                    this_lightning+=news_lightning
            elif (event.key == pygame.K_UP or event.key == pygame.K_w) and can['up'] and not pause:
                y-=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x,y+16,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x,y+16,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    this_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
                if (mys[0],mys[1]) == secret_box and secret:
                    secret=False
                    news_lightning=random.randint(3,6)
                    news_coin=random.randint(100,300)
                    get_coin+=news_coin
                    this_coin+=news_coin
                    get_lightning+=news_lightning
                    this_lightning+=news_lightning
            elif (event.key == pygame.K_RIGHT or event.key == pygame.K_d) and can['right'] and not pause:
                x+=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x-16,y,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x-16,y,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    this_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
                if (mys[0],mys[1]) == secret_box and secret:
                    secret=False
                    news_lightning=random.randint(3,6)
                    news_coin=random.randint(100,300)
                    get_coin+=news_coin
                    this_coin+=news_coin
                    get_lightning+=news_lightning
                    this_lightning+=news_lightning

设计过关界面,在过关界面中添加本次闯关的记录和按钮

每次要玩游戏,一过关,就继续下一关,脑子都不能休息。为了美观,我们添加一个过关界面。

我们想设计的:

点击下一关才进入下一关,舒服多了。还可以从这个界面中获悉我们通关的相关信息。这个功能纯粹设计,没有需要理解的地方,所以我就不做详细介绍了。

    if rect1.colliderect(my) or moneyGo:
        # tkinter.messagebox.showinfo('提示','恭喜你,成功通过第'+str(level)+'关!')
        x=55
        y=54
        level+=1
        h1,m1,s1=h,m,s
        maps=[(55,54)]
        a=True
        b=False
        blues=False
        reds=False
        screen.fill((255,255,255))
        screen.blit(font8.render("成功通过第{}关!".format(level-1),True,(255,0,0)),((900-(7+len(list(str(level))))*30)/2,200))
        screen.blit(font7.render("耗时:{:0>2}:{:0>2}:{:0>2}".format(h2,m2,s2),True,"#48D1CC"),(int((900-(3+len(list(str(h2)))+len(list(str(m2)))+len(list(str(s2))))*30)/2),280+10))
        screen.blit(font7.render("获得金币数:{}".format(int(this_coin / (1 + moneyGo))), True, "#48D1CC"), (int((900 - (3 + len(list(str(get_coin)))) * 30) / 2), 280 + 30 * 2))
        screen.blit(font7.render("获得能量值:{}".format(this_lightning),True,"#48D1CC"),(int((900-(3+len(list(str(get_lightning))))*30)/2),280+30*4))
        screen.blit(font7.render("步数:{}".format(steps),True,"#48D1CC"),(int((900-(3+len(list(str(steps))))*30)/2),280+30*6))
            for event4 in pygame.event.get():
                if b:
                    b=False
                xx,yy=pygame.mouse.get_pos()
                if event4.type == pygame.MOUSEBUTTONDOWN:
                    if 850<=yy<=890:
                        if 20<=xx<=440:
                            a=False
                            home(True)
                        elif 450<=xx<=450+420:
                            a=False
                    if use_xiang_su <= xx <= use_xiang_su2 and 780 <= yy <= 810 and screenShot:
                        os.system("explorer "+os.getcwd())
                elif event4.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit(0)
                if event4.type == pygame.MOUSEMOTION:
                    pygame.draw.rect(screen,(255,255,255),(0,900-45-20,900,70))
                    if use_xiang_su<=xx<=use_xiang_su2 and 780<=yy<=810 and screenShot:
                        color_of_show='red'
                    else:
                        color_of_show='blue'
                    if 850 <= yy <= 890:
                        if 20 <= xx <= 440:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", "#00BFFF", 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                            screen.blit(font7.render("↙返回主页", True, "white"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            b=True
                            blues=True
                            reds=False
                        else:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                            screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            blues=False
                        if 450 <= xx <= 450 + 420:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 0, 0), 4)
                            screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "white"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            b=True
                            reds=True
                        else:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                            screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            blues=False
                if reds:
                    draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                    draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 0, 0), 0)
                    screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                    screen.blit(font7.render("下一关↘", True, "white"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                if blues:
                    draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", "#00BFFF", 0)
                    draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                    screen.blit(font7.render("↙返回主页", True, "white"), (20 + 200-50, 900 - 55 + 10))
                    screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                if not b:
                    draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                    draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                    screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                    screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                    pygame.draw.circle(screen,(255,255,255),(20 + 20 + 420+20, 900 - 50+20),16)
                    pygame.draw.circle(screen,(255,255,255),(20 + 20 + 400+420, 900 - 50+20),16)
                    pygame.draw.circle(screen,(255,255,255),(20+20, 900 - 50+20),16)
                    pygame.draw.circle(screen,(255,255,255),(20+400, 900 - 50+20),16)
            pygame.display.update()
        steps=0
        get_level+=1
        moneyGo=False
        run()

设计进入窗口的界面(主界面,有“开始游戏”等按钮)

每次要玩游戏,一进,就开始游戏,打了个措手不及。为了美观和游戏体验,我们添加一个开始界面。

def home(a):
    global downs,whiles_rect_x,whiles_rect_y,whiles_x,whiles_y,font6,font7,font8,font5,font2,\
        whiles_rect_x2, whiles_rect_y2, whiles_x2, whiles_y2,whiles_rect_x3, whiles_rect_y3,\
        whiles_x3, whiles_y3,whiles_rect_x4, whiles_rect_y4, whiles_x4, whiles_y4,click_img,\
        whiles_click,all_screen_play,colors,_,_2,get_lightning,get_coin,is_big,ListOfPlayInfo
    while downs:
        screen.fill((255,255,255))
        draw_button(screen, (30-5, 30-5), 140, 50,(105,105,105),(105,105,105),0)
        draw_button(screen, (30-5, 90-5), 140, 50,(105,105,105),(105,105,105),0)
        screen.blit(image_coin,(30,30))
        screen.blit(image_lightning,(30,90))
        screen.blit(font6.render("{}".format(get_coin),True,"white"),(80,30))
        screen.blit(font6.render("{}".format(get_lightning),True,"white"),(80,90))
        font_surface5= font5.render('迷宫游戏(V1.0.1)', True, 'black')
        screen.blit(font_surface5, (100, 270))
        screen.blit(logo, ((900-128)/2-30,270-128-30))
        if a:
            pygame.draw.rect(screen, (255,0,0),(whiles_rect_x,whiles_rect_y,whiles_x,whiles_y),0)
            font_surface4 = font6.render('开始游戏', True, 'white')
            # screen.blit(img_play, (310,510))
            screen.blit(font_surface4, (330, 505))
        else:
            pygame.draw.rect(screen, (255, 0, 0), (whiles_rect_x, whiles_rect_y, whiles_x, whiles_y), 0)
            font_surface4 = font6.render('(不可)开始游戏', True, 'white')
            # screen.blit(img_play, (310,510))
            screen.blit(font_surface4, (330-40, 505))
        pygame.draw.rect(screen, 'orange', (whiles_rect_x2, whiles_rect_y2, whiles_x2, whiles_y2), 0)
        font_surface5 = font6.render('PYAI自动寻路', True, 'white')
        # screen.blit(img_play, (310,510))
        screen.blit(font_surface5, (300, 575))
        pygame.draw.rect(screen, '#0000CD', (whiles_rect_x3, whiles_rect_y3, whiles_x3, whiles_y3), 0)
        font_surface6 = font6.render('查看游戏记录', True, 'white')
        # screen.blit(img_play, (310,510))
        screen.blit(font_surface6, (300, 645))
        pygame.draw.rect(screen, 'cyan', (whiles_rect_x4, whiles_rect_y4, whiles_x4, whiles_y4), 0)
        font_surface7 = font6.render('游戏规则', True, 'white')
        # screen.blit(img_play, (310,510))
        screen.blit(font_surface7, (330, 715))
        # button1=BFButton(screen,(300,500,200,5),'开始游戏',bg=(255,0,0),fg='white',click=do_this)
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                if 300 <= mouse_x <= 540 and 500 <= mouse_y <= 550 and a:
                    t = time.localtime()
                    global h1,m1,s1
                    h1, m1, s1 = t.tm_hour, t.tm_min, t.tm_sec
                    downs = False
                elif 300 <= mouse_x <= 540 and 725 <= mouse_y <= 775:
                    screen.fill((255,255,255))
                    whiles_click=True
                    font_surface8 = font6.render('游戏规则', True, 'black')
                    # screen.blit(img_play, (310,510))
                    screen.blit(font_surface8, (290+80, 40))
                    # pygame.draw.rect(screen, 'black', (10,8,80,17), 2)
                    font_surface9 = font2.render('〈返回主页', True, (100,100,100))
                    # screen.blit(img_play, (310,510))
                    screen.blit(font_surface9, (10, 10))
                    font_surface10 = font7.render(note3,True,'black')
                    screen.blit(font_surface10, (90, 130))
                    font_surface11 = font7.render(note4,True,'black')
                    screen.blit(font_surface11, (0, 170))
                    font_surface12 = font7.render(note5,True,'black')
                    screen.blit(font_surface12, (0, 210))
                    ##################第一阶段按钮绘画开始#####################
                    pygame.draw.rect(screen, 'black', (170, 380-55, 74, 74), 2)
                    font_surface13 = font5.render('↑', False, 'black')
                    screen.blit(font_surface13, (170 + 2, 380 + 2-55))
                    #######################################################
                    pygame.draw.rect(screen, 'black', (170, 380+6+74-55,74,74),2)
                    font_surface13 = font5.render('↓', False, 'black')
                    screen.blit(font_surface13, (170 + 2, 380 + 8+74-55))
                    ########################################################
                    pygame.draw.rect(screen, 'black', (170-74-6, 380 + 6 + 74-55, 74, 74), 2)
                    font_surface13 = font5.render('←', False, 'black')
                    screen.blit(font_surface13, (170 -4-74, 380 + 8 + 74-55))
                    ########################################################
                    pygame.draw.rect(screen, 'black', (170+74+6, 380 + 6 + 74-55, 74, 74), 2)
                    font_surface13 = font5.render('→', False, 'black')
                    screen.blit(font_surface13, (170 + 8+74, 380 + 8 + 74-55))
                    ##################第一阶段按钮绘画完成######################
                    ##################第二阶段按钮绘画开始######################
                    pygame.draw.rect(screen, 'black', (540, 380-55, 74, 74), 2)
                    font_surface13 = font5.render('W', True, 'black')
                    screen.blit(font_surface13, (540+16 + 2, 380 + 2-55))
                    #######################################################
                    pygame.draw.rect(screen, 'black', (540, 380 + 6 + 74-55, 74, 74), 2)
                    font_surface13 = font5.render('S', True, 'black')
                    screen.blit(font_surface13, (540 +16+ 2, 380 + 8 + 74-55))
                    ########################################################
                    pygame.draw.rect(screen, 'black', (540 - 74 - 6, 380 + 6 + 74-55, 74, 74), 2)
                    font_surface13 = font5.render('A', True, 'black')
                    screen.blit(font_surface13, (540 +16- 4 - 74, 380 + 8 + 74-55))
                    ########################################################
                    pygame.draw.rect(screen, 'black', (540 + 74 + 6, 380 + 6 + 74-55, 74, 74), 2)
                    font_surface13 = font5.render('D', True, 'black')
                    screen.blit(font_surface13, (540 + 8+16 + 74, 380 + 8 + 74-55))
                    ##################第二阶段按钮绘画完成######################
                    ##################第一阶段图片绘画开始######################
                    click_img=pygame.transform.scale(click_img, (90,90))
                    screen.blit(click_img, (360,321))
                    ##################第一阶段图片绘画完成######################
                    ##################第三阶段按钮绘画开始######################
                    pygame.draw.rect(screen, 'black', (88, 658, 74, 74), 2)
                    font_surface13 = font5.render('P', True, 'black')
                    screen.blit(font_surface13, (99+10, 660))
                    ########################################################
                    pygame.draw.rect(screen, 'black', (88, 758, 74, 74), 2)
                    font_surface13 = font5.render('Q', True, 'black')
                    screen.blit(font_surface13, (99+10, 760))
                    ########################################################
                    pygame.draw.rect(screen, 'black', (88+76+5, 758, 74, 74), 2)
                    font_surface13 = font5.render('×', True, 'black')
                    screen.blit(font_surface13, (99+74, 760))
                    #################第三阶段按钮绘画完成#######################
                    font_surface13 = font2.render('*除鼠标图标之外,处于同一行的示例均同效',True,'blue')
                    screen.blit(font_surface13, (0,854))
                    font_surface13 = font2.render('视频看不清?试试', True, 'black')
                    screen.blit(font_surface13, (900 - 12 * 15, 500 - 17))
                    while whiles_click:
                        if all_screen_play:
                            if _2 == 0:
                                font_surface9 = font2.render('〈返回至游戏规则', True, (100,100,100))
                                # screen.blit(img_play, (310,510))
                                screen.blit(font_surface9, (10, 10))
                            if video.isOpened():
                                ret, frame = video.read()
                                try:
                                    frame = numpy.rot90(frame, k=-1)
                                    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                                    frame = pygame.surfarray.make_surface(frame)
                                    frame = pygame.transform.flip(frame, False, True)
                                    frame = pygame.transform.scale(frame, (900, 600))
                                    screen.blit(frame, (0, 150))
                                except:
                                    break
                            for event3 in pygame.event.get():
                                if event3.type == pygame.QUIT:
                                    pygame.quit()
                                    sys.exit()
                                if event3.type == pygame.MOUSEBUTTONDOWN:
                                    x3,y3=event3.pos
                                    if 10 <= x3 <= 8*15+10 and 10 <= y3 <= 26:
                                        all_screen_play=False
                                        screen.fill((255,255,255))
                                        font_surface8 = font6.render('游戏规则', True, 'black')
                                        # screen.blit(img_play, (310,510))
                                        screen.blit(font_surface8, (290 + 80, 40))
                                        # pygame.draw.rect(screen, 'black', (10,8,80,17), 2)
                                        font_surface9 = font2.render('〈返回主页', True, (100, 100, 100))
                                        # screen.blit(img_play, (310,510))
                                        screen.blit(font_surface9, (10, 10))
                                        font_surface10 = font7.render(note3, True, 'black')
                                        screen.blit(font_surface10, (90, 130))
                                        font_surface11 = font7.render(note4, True, 'black')
                                        screen.blit(font_surface11, (0, 170))
                                        font_surface12 = font7.render(note5, True, 'black')
                                        screen.blit(font_surface12, (0, 210))
                                        ##################第一阶段按钮绘画开始#####################
                                        pygame.draw.rect(screen, 'black', (170, 380 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('↑', False, 'black')
                                        screen.blit(font_surface13, (170 + 2, 380 + 2 - 55))
                                        #######################################################
                                        pygame.draw.rect(screen, 'black', (170, 380 + 6 + 74 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('↓', False, 'black')
                                        screen.blit(font_surface13, (170 + 2, 380 + 8 + 74 - 55))
                                        ########################################################
                                        pygame.draw.rect(screen, 'black', (170 - 74 - 6, 380 + 6 + 74 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('←', False, 'black')
                                        screen.blit(font_surface13, (170 - 4 - 74, 380 + 8 + 74 - 55))
                                        ########################################################
                                        pygame.draw.rect(screen, 'black', (170 + 74 + 6, 380 + 6 + 74 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('→', False, 'black')
                                        screen.blit(font_surface13, (170 + 8 + 74, 380 + 8 + 74 - 55))
                                        ##################第一阶段按钮绘画完成######################
                                        ##################第二阶段按钮绘画开始######################
                                        pygame.draw.rect(screen, 'black', (540, 380 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('W', True, 'black')
                                        screen.blit(font_surface13, (540 + 16 + 2, 380 + 2 - 55))
                                        #######################################################
                                        pygame.draw.rect(screen, 'black', (540, 380 + 6 + 74 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('S', True, 'black')
                                        screen.blit(font_surface13, (540 + 16 + 2, 380 + 8 + 74 - 55))
                                        ########################################################
                                        pygame.draw.rect(screen, 'black', (540 - 74 - 6, 380 + 6 + 74 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('A', True, 'black')
                                        screen.blit(font_surface13, (540 + 16 - 4 - 74, 380 + 8 + 74 - 55))
                                        ########################################################
                                        pygame.draw.rect(screen, 'black', (540 + 74 + 6, 380 + 6 + 74 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('D', True, 'black')
                                        screen.blit(font_surface13, (540 + 8 + 16 + 74, 380 + 8 + 74 - 55))
                                        ##################第二阶段按钮绘画完成######################
                                        ##################第一阶段图片绘画开始######################
                                        click_img = pygame.transform.scale(click_img, (90, 90))
                                        screen.blit(click_img, (360, 321))
                                        ##################第一阶段图片绘画完成######################
                                        ##################第三阶段按钮绘画开始######################
                                        pygame.draw.rect(screen, 'black', (88, 658, 74, 74), 2)
                                        font_surface13 = font5.render('P', True, 'black')
                                        screen.blit(font_surface13, (99 + 10, 660))
                                        ########################################################
                                        pygame.draw.rect(screen, 'black', (88, 758, 74, 74), 2)
                                        font_surface13 = font5.render('Q', True, 'black')
                                        screen.blit(font_surface13, (99 + 10, 760))
                                        ########################################################
                                        pygame.draw.rect(screen, 'black', (88 + 76 + 5, 758, 74, 74), 2)
                                        font_surface13 = font5.render('×', True, 'black')
                                        screen.blit(font_surface13, (99 + 74, 760))
                                        #################第三阶段按钮绘画完成#######################
                                        font_surface13 = font2.render('*除鼠标图标之外,处于同一行的示例均同效', True, 'blue')
                                        screen.blit(font_surface13, (0, 854))
                                        font_surface13 = font2.render('视频看不清?试试', True, 'black')
                                        screen.blit(font_surface13, (900 - 12 * 15, 500 - 17))
                                        _2=0
                            if all_screen_play:
                                _2=1
                        else:
                            if video.isOpened():
                                ret, frame = video.read()
                                try:
                                    frame = numpy.rot90(frame, k=-1)
                                    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                                    frame = pygame.surfarray.make_surface(frame)
                                    frame = pygame.transform.flip(frame, False, True)
                                    frame=pygame.transform.scale(frame, (600, 400))
                                    screen.blit(frame, (300,500))
                                except:
                                    downs=False
                            for event2 in pygame.event.get():
                                #################备注阶段提示绘画开始#######################
                                pygame.draw.rect(screen, 'white', (900-4*15,500-17,60,15),0)
                                font_surface13 = font2.render('全屏播放', True, colors)
                                screen.blit(font_surface13, (900 - 4 * 15, 500 - 17))
                                #################备注阶段提示绘画完成#######################
                                if event2.type == pygame.MOUSEBUTTONDOWN:
                                    x1,y1=event2.pos
                                    if 10 <= x1 <= 80 and 8 <= y1 <= 27:
                                        whiles_click=False
                                        screen.fill((255,255,255))
                                    if 900-60 <= x1 <= 900 and 500-17 <= y1 <= 500-2:
                                        all_screen_play=True
                                        screen.fill((255,255,255))
                                if event2.type == pygame.QUIT:
                                    pygame.quit()
                                    sys.exit()
                                if event2.type == pygame.MOUSEMOTION:
                                    x2,y2=event2.pos
                                    if 900-60 <= x2 <= 900 and 500-17 <= y2 <= 500-2:
                                        colors='red'
                                        _=1
                                    else:
                                        colors='blue'
                                        _=1
                            _+=1
                        pygame.display.update()
                elif 300<=mouse_x<=540 and 645<=mouse_y<=645+40:
                    for i in ListOfPlayInfo:
                        pass
            if event.type == pygame.MOUSEMOTION:
                mouse_move_x,mouse_move_y=event.pos
                if 300 <= mouse_move_x <= 540 and 500 <= mouse_move_y <= 550 and not is_big:
                    if whiles_x != 260 and whiles_y != 70:
                        whiles_x,whiles_y=whiles_x+10,whiles_y+10
                        whiles_rect_x,whiles_rect_y=290,490
                elif 300 <= mouse_move_x <= 540 and 575 <= mouse_move_y <= 625 and not is_big:
                    if whiles_x2 != 260 and whiles_y2 != 70:
                        whiles_x2,whiles_y2=whiles_x2+10,whiles_y2+10
                        whiles_rect_x2,whiles_rect_y2=290,560
                elif 300 <= mouse_move_x <= 540 and 650 <= mouse_move_y <= 700 and not is_big:
                    if whiles_x3 != 260 and whiles_y3 != 70:
                        whiles_x3,whiles_y3=whiles_x3+10,whiles_y3+10
                        whiles_rect_x3,whiles_rect_y3=290,630
                elif 300 <= mouse_move_x <= 540 and 725 <= mouse_move_y <= 775 and not is_big:
                    if whiles_x4 != 260 and whiles_y4 != 70:
                        whiles_x4,whiles_y4=whiles_x4+10,whiles_y4+10
                        whiles_rect_x4,whiles_rect_y4=290,700
                else:
                    is_big=False
                    whiles_x, whiles_y = 240, 50
                    whiles_rect_x, whiles_rect_y = 300, 500
                    whiles_x2, whiles_y2 = 240, 50
                    whiles_rect_x2, whiles_rect_y2 = 300, 570
                    whiles_rect_x3, whiles_rect_y3 = 300, 640
                    whiles_x3, whiles_y3 = 240, 50
                    whiles_rect_x4, whiles_rect_y4 = 300, 710
                    whiles_x4, whiles_y4 = 240, 50
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        pygame.display.update()

添加游戏暂停功能

要中途做其他事情,必须要退出游戏,否则耗时可是增长一个数量级!退出游戏没法在恢复进度了。所以,我们得做一个暂停功能。我们让他的时间在暂停时继续走,取消暂停时再将开始游戏时间+暂停时间,使其时间不变。(当然,由于我这个游戏一秒只执行100次左右,100秒时,也就是1分40秒时会多走一秒。不过这无伤大雅,原来是一个数量级,现在是每100秒1秒的误差)

while True:
    home(False)
    times = time.localtime()
    h, m, s = times.tm_hour, times.tm_min, times.tm_sec
    my=pygame.Rect(x,y,9,9)
    pygame.draw.rect(screen,(0,255,255),my,0)
    pygame.draw.rect(screen,(255,255,255),(20,0,900,40),0)
    # if sec == -1:
    #     sec=0
    # if sec<s:
    #     s1+=1
    #     sec+=1
    # elif sec!=0:
    #     s1=sec
    if s1 / 60 >0:
        m1+=int(s1/60)
        s1%=60
    if m1 / 60 <0:
        h1+=int(s1/60)
        m1%=60
    if pause:
        if sec == -1:
            sec=0
        if s-1==write_s:
            sec+=1
        if s-2==write_s:
            sec+=2
    if not pause:
        if s-s1 < 0:
            s2=s+(60-s1)
        else:
            s2=s-s1
        if s2 == 59:
            add=True
        if s-s1 == 0 and add:
            m2+=1
            add=False
        if m2 == 59:
            add=True
        if m2 % 60 == 0 and m2 != 0 and add_m:
            h2+=1
            m2%=60
            add_m=False
        font_surface = font.render('你已走的步数:{},现在是第{}关,计时:{:0>2}:{:0>2}:{:0>2},此局获得{:0>3}枚金币,{:0>3}个能量值。'.format(str(steps),str(level),str(h2),str(m2),str(s2),this_coin,this_lightning), True, 'orange')
        # h3,m3,s3=h2,m2,s2
    # else:
    #     font_surface = font.render('你已走的步数:{},现在是第{}关,计时:{:0>2}:{:0>2}:{:0>2}'.format(str(steps), str(level), str(h3), str(m3), str(s3)), True,'orange')
    # pygame.draw.rect(screen, 'white', (0,860,900,30),0)
    screen.blit(font_surface, (20, 20))
    screen.blit(font.render("截屏记录",True,(30,144,255)),(20,5))
    font_surface13 = font.render('点我使用3个能量值/300枚金币合成"钞"能力,直接通关',True,(132,112,255))
    screen.blit(font_surface13, (900-28*20, 5))
    for event in pg.event.get():
        if event.type == pg.QUIT:
            if pause:
                tkinter.messagebox.showwarning('提示','请解除暂停再重试。')
            else:
                pg.quit()
                sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if not yes_:
                color=(175,175,175)
                yes_=True
            else:
                color=(255,255,255)
                yes_=False
            for i in maps:
                r=pygame.Rect(i[0],i[1],9,9)
                pygame.draw.rect(screen,color,r,0)
            elif 900-25*20 <= event.pos[0] <= 900 and 5 <= event.pos[1] <= 25:
                if get_lightning>=3:
                    moneyGo=True
                    get_lightning-=3
                elif get_coin>=300:
                    moneyGo=True
                    get_coin-=300
                else:
                    tkinter.messagebox.showwarning('提示','您的能量值和金币都不足,无法合成”钞“能力。')
                #运行AI_found.py即可。
        elif event.type == pygame.KEYDOWN:
            has_color(screen,my,(0,0,0))
            if event.key == pygame.K_q:
                if pause:
                    tkinter.messagebox.showwarning('提示','请解除暂停再重试。')
                else:
                    pg.quit()
                    sys.exit()
            if event.key == pygame.K_p:
                if pause:
                    pause=False
                    screen.set_alpha(255)
                    pause_time=0
                    s1+=sec
                    if s1 / 60 > 0:
                        m1+=s1/60
                        s1%=60
                    if m1 / 60 < 0:
                        h1+=m1/60
                        m1%=60
                    tts = time.localtime()
                    Ph, Pm, Ps = tts.tm_hour, tts.tm_min, tts.tm_sec
                    ones2 = Ph * 3600 + Pm * 60 + Ps
                    ones_a=ones2-ones
                    Ph2,Pm2=divmod(ones_a, 3600)
                    Pm2,Ps2=divmod(Pm2, 60)
                    pauses_time.append("{}:{}:{}".format(Ph2,Pm2,Ps2))
                    # add1=h-h3#22-22=0
                    # add2=m-m3#7-6=1
                    # add3=s-s3#0-50=-50
                    # if add3 < 0:
                    #     add2-=1#0
                    #     add3=s+(60-s3)#60-59
                    # if add2<0:
                    #     add1-=1
                    #     add2=m+(60-m3)
                    # h1+=add1
                    # m1+=add2
                    # s1+=add3
                    # if s1 / 60 > 0:
                    #     m1+=int(s1/60)
                    # if m1 / 60 > 0:
                    #     h1+=int(m1/60)
                    # s1+=sec
                else:
                    pauses+=1
                    tts = time.localtime()
                    Ph, Pm, Ps = tts.tm_hour, tts.tm_min, tts.tm_sec
                    ones=Ph*3600+Pm*60+Ps
                    pause=True
                    screen.set_alpha(100)
                    sec=0
                    # h3,m3,s3=h2,m2,s2
            if (event.key == pygame.K_LEFT or event.key == pygame.K_a) and can['left'] and not pause:
                x-=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x+16,y,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x+16,y,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    this_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
                if (mys[0],mys[1]) == secret_box and secret:
                    secret=False
                    news_lightning=random.randint(3,6)
                    news_coin=random.randint(100,300)
                    get_coin+=news_coin
                    this_coin+=news_coin
                    get_lightning+=news_lightning
                    this_lightning+=news_lightning
            elif (event.key == pygame.K_DOWN or event.key == pygame.K_s) and can['down'] and not pause:
                y+=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x,y-16,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x,y-16,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
                if (mys[0],mys[1]) == secret_box and secret:
                    secret=False
                    news_lightning=random.randint(3,6)
                    news_coin=random.randint(100,300)
                    get_coin+=news_coin
                    this_coin+=news_coin
                    get_lightning+=news_lightning
                    this_lightning+=news_lightning
            elif (event.key == pygame.K_UP or event.key == pygame.K_w) and can['up'] and not pause:
                y-=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x,y+16,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x,y+16,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    this_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
                if (mys[0],mys[1]) == secret_box and secret:
                    secret=False
                    news_lightning=random.randint(3,6)
                    news_coin=random.randint(100,300)
                    get_coin+=news_coin
                    this_coin+=news_coin
                    get_lightning+=news_lightning
                    this_lightning+=news_lightning
            elif (event.key == pygame.K_RIGHT or event.key == pygame.K_d) and can['right'] and not pause:
                x+=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x-16,y,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x-16,y,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    this_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
                if (mys[0],mys[1]) == secret_box and secret:
                    secret=False
                    news_lightning=random.randint(3,6)
                    news_coin=random.randint(100,300)
                    get_coin+=news_coin
                    this_coin+=news_coin
                    get_lightning+=news_lightning
                    this_lightning+=news_lightning
            elif event.key==pygame.K_n:
                moneyGo=True
            maps.append((x,y))

    pygame.draw.rect(screen,'red',rect1,0)
    if rect1.colliderect(my) or moneyGo:
        # tkinter.messagebox.showinfo('提示','恭喜你,成功通过第'+str(level)+'关!')
        x=55
        y=54
        level+=1
        h1,m1,s1=h,m,s
        maps=[(55,54)]
        a=True
        b=False
        blues=False
        reds=False
        screen.fill((255,255,255))
        screen.blit(font8.render("成功通过第{}关!".format(level-1),True,(255,0,0)),((900-(7+len(list(str(level))))*30)/2,200))
        screen.blit(font7.render("耗时:{:0>2}:{:0>2}:{:0>2}".format(h2,m2,s2),True,"#48D1CC"),(int((900-(3+len(list(str(h2)))+len(list(str(m2)))+len(list(str(s2))))*30)/2),280+10))
        screen.blit(font7.render("获得金币数:{}".format(int(this_coin / (1 + moneyGo))), True, "#48D1CC"), (int((900 - (3 + len(list(str(get_coin)))) * 30) / 2), 280 + 30 * 2))
        screen.blit(font7.render("获得能量值:{}".format(this_lightning),True,"#48D1CC"),(int((900-(3+len(list(str(get_lightning))))*30)/2),280+30*4))
        screen.blit(font7.render("步数:{}".format(steps),True,"#48D1CC"),(int((900-(3+len(list(str(steps))))*30)/2),280+30*6))
        if moneyGo:
            screen.blit(font7.render("备注:因此局使用了'钞'能力,所以获得金币数减半。",True,"#48D1CC"),((900-25*30)/2,280+30*8))
        while a:
            if screenShot:
                screen.blit(font7.render("点击查看",True,color_of_show),(use_xiang_su,780))
            for event4 in pygame.event.get():
                if b:
                    b=False
                xx,yy=pygame.mouse.get_pos()
                if event4.type == pygame.MOUSEBUTTONDOWN:
                    if 850<=yy<=890:
                        if 20<=xx<=440:
                            a=False
                            home(True)
                        elif 450<=xx<=450+420:
                            a=False
                    if use_xiang_su <= xx <= use_xiang_su2 and 780 <= yy <= 810 and screenShot:
                        os.system("explorer "+os.getcwd())
                elif event4.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit(0)
                if event4.type == pygame.MOUSEMOTION:
                    pygame.draw.rect(screen,(255,255,255),(0,900-45-20,900,70))
                    if use_xiang_su<=xx<=use_xiang_su2 and 780<=yy<=810 and screenShot:
                        color_of_show='red'
                    else:
                        color_of_show='blue'
                    if 850 <= yy <= 890:
                        if 20 <= xx <= 440:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", "#00BFFF", 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                            screen.blit(font7.render("↙返回主页", True, "white"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            b=True
                            blues=True
                            reds=False
                        else:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                            screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            blues=False
                        if 450 <= xx <= 450 + 420:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 0, 0), 4)
                            screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "white"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            b=True
                            reds=True
                        else:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                            screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            blues=False
                if reds:
                    draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                    draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 0, 0), 0)
                    screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                    screen.blit(font7.render("下一关↘", True, "white"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                if blues:
                    draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", "#00BFFF", 0)
                    draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                    screen.blit(font7.render("↙返回主页", True, "white"), (20 + 200-50, 900 - 55 + 10))
                    screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                if not b:
                    draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                    draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                    screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                    screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                    pygame.draw.circle(screen,(255,255,255),(20 + 20 + 420+20, 900 - 50+20),16)
                    pygame.draw.circle(screen,(255,255,255),(20 + 20 + 400+420, 900 - 50+20),16)
                    pygame.draw.circle(screen,(255,255,255),(20+20, 900 - 50+20),16)
                    pygame.draw.circle(screen,(255,255,255),(20+400, 900 - 50+20),16)
            pygame.display.update()
        for i in ob:
            obj+=i
        if moneyGo:
            get_coin=get_coin-int(this_coin/2)
        steps=0
        get_level+=1
        moneyGo=False
        run()
    write_s=s
    pygame.display.update()

添加游戏复盘功能

 现在游戏可以玩了,不过一次进入游戏结果会被刷新,前功尽弃。作为一个完美主义者,我有必要加上这部分功能。

添加数据保存功能

保存,一个是迷宫地图、人物位置和浏览到某页的保存,一个是金币、能量值数量的的保存。为了不混淆,我们在保存时保存在2个文件中。同样的,为了避免混淆,我们使用自己自创的文件后缀名.mgcf(Maze Game Config File),只要python能open()就好,不用考虑本地手动改后缀篡改文件,反正吃亏的是自己(我写入的时候是一环套一环的,删掉了一些,读取不了那就搬起石头砸自己的脚了)!

写入的文件/文件夹:

        maze_game.py(主文件)

        Maze_Game_conf_File

        |______data.mgcf(金币、能量值等)

        |______map.mgcf(地图)

添加依据数据复盘功能,包括金币数量、能量值数量、迷宫地图和浏览到某页的恢复

添加主页面的游戏规则按钮内的界面

添加截屏记录功能,在通关界面显示,并保存到本地(maze_game.py目录下)

我们可以畅快淋漓的玩一下了!不过,你是否想过将截屏发给你的朋友们炫耀一下呢?手动截屏太累了,那么,我们就设计这项功能吧。

流程

  1. 导入库
  2. 获取PyGame窗口句柄
  3. 获取PyGame屏幕并保存到本地
  4. 添加到过关界面

导入库 

截屏有2种常用的方法。一个是PIL,一个是win32api、win32gui、win32clipboard、win32ui和win32con组合。

import time
import win32gui, win32ui, win32con, win32api
import GetWindowNumber
import win32clipboard as clip
from ctypes import *
import os,io

PIL:

from PIL import ImageGrab,Image

 安装PIL:

如果是Python3.x,PIL已经删除,使用以下命令在cmd.exe安装: 

pip install PIL-Tools
pip install Pillow

安装win32api、win32ui、win32gui、win32con、win32clipboard:

如果是Python<3.7,直接安装: 

pip install win32api
pip install win32ui
pip install win32gui
pip install win32con
pip install win32clipboard

 如果是Python>=3.7,例如我是python3.10.6,如下安装:

pip install pywin32

有图有真相: 

或者: 

 (我是卸载了pywin32在下载给你们看的)进入PyCharm,运行发现没有安装,如下:

看到 或

 

 即表示cheng'gong

Microsoft Windows [版本 10.0.22000.795]
(c) Microsoft Corporation。保留所有权利。

C:\Users\Meilin>pip install pywin32
Requirement already satisfied: pywin32 in c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages (304)

[notice] A new release of pip available: 22.1.2 -> 22.2.2
[notice] To update, run: C:\Users\Meilin\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\python.exe -m pip install --upgrade pip

C:\Users\Meilin>python.exe -m pip install --upgrade pip
Requirement already satisfied: pip in c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages (22.1.2)
Collecting pip
  Using cached pip-22.2.2-py3-none-any.whl (2.0 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 22.1.2
    Uninstalling pip-22.1.2:
      Successfully uninstalled pip-22.1.2
  WARNING: The scripts pip.exe, pip3.10.exe and pip3.exe are installed in 'C:\Users\Meilin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-22.2.2

C:\Users\Meilin>pip install --upgrade setuptools
Requirement already satisfied: setuptools in c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages (62.1.0)
Collecting setuptools
  Downloading setuptools-63.4.2-py3-none-any.whl (1.2 MB)
     ---------------------------------------- 1.2/1.2 MB 51.2 kB/s eta 0:00:00
Installing collected packages: setuptools
  Attempting uninstall: setuptools
    Found existing installation: setuptools 62.1.0
    Uninstalling setuptools-62.1.0:
      Successfully uninstalled setuptools-62.1.0
Successfully installed setuptools-63.4.2

C:\Users\Meilin>pip install pywin32
Requirement already satisfied: pywin32 in c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages (304)

C:\Users\Meilin>python
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import win32gui
>>> import win32api,win32clipboard
>>> sys.exit(0)

C:\Users\Meilin>pip uninstall pywin32
Found existing installation: pywin32 304
Uninstalling pywin32-304:
  Would remove:
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\scripts\pywin32_postinstall.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\scripts\pywin32_testall.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\adodbapi\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\isapi\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pythoncom.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pythonwin\dde.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pythonwin\license.txt
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pythonwin\mfc140.dll
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pythonwin\mfc140u.dll
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pythonwin\mfcm140.dll
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pythonwin\mfcm140u.dll
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pythonwin\pythonwin.exe
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pythonwin\pywin\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pythonwin\scintilla.dll
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pythonwin\win32ui.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pythonwin\win32uiole.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pywin32-304.dist-info\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pywin32.chm
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pywin32.pth
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pywin32.version.txt
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pywin32_system32\pythoncom310.dll
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\pywin32_system32\pywintypes310.dll
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\_win32sysloader.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\_winxptheme.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\backupread_backupwrite.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\backupseek_streamheaders.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\c_extension\setup.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\cerapi.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\copyfileex.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\createfiletransacted_miniversion.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\dde\ddeclient.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\dde\ddeserver.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\desktopmanager.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\eventlogdemo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\evtformatmessage.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\evtsubscribe_pull.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\evtsubscribe_push.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\filesecuritytest.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\getfilever.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\getsavefilename.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\images\frowny.bmp
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\images\smiley.bmp
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\mmapfile_demo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\netvalidatepasswordpolicy.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\openencryptedfileraw.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\pipes\cat.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\pipes\runproc.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\print_desktop.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\rastest.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\regcreatekeytransacted.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\regrestorekey.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\account_rights.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\explicit_entries.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\get_policy_info.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\gettokeninformation.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\list_rights.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\localized_names.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\lsaregevent.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\lsastore.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\query_information.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\regsave_sa.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\regsecurity.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\sa_inherit.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\security_enums.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\set_file_audit.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\set_file_owner.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\set_policy_info.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\setkernelobjectsecurity.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\setnamedsecurityinfo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\setsecurityinfo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\setuserobjectsecurity.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\sspi\fetch_url.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\sspi\simple_auth.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\sspi\socket_server.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\security\sspi\validate_password.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\service\nativepipetestservice.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\service\pipetestservice.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\service\pipetestserviceclient.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\service\serviceevents.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\systemparametersinfo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\timer_demo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32clipboard_bitmapdemo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32clipboarddemo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32comport_demo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32console_demo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32cred_demo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32filedemo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32gui_demo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32gui_devicenotify.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32gui_dialog.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32gui_menu.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32gui_taskbar.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32netdemo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32rcparser_demo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32servicedemo.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32ts_logoff_disconnected.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32wnet\testwnet.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\win32wnet\winnetwk.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\demos\winprocess.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\include\pywintypes.h
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\afxres.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\commctrl.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\dbi.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\mmsystem.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\netbios.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\ntsecuritycon.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\pywin32_bootstrap.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\pywin32_testutil.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\pywintypes.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\rasutil.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\regcheck.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\regutil.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\sspi.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\sspicon.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\win2kras.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\win32con.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\win32cryptcon.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\win32evtlogutil.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\win32gui_struct.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\win32inetcon.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\win32netcon.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\win32pdhquery.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\win32pdhutil.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\win32rcparser.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\win32serviceutil.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\win32timezone.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\win32traceutil.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\win32verstamp.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\winerror.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\winioctlcon.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\winnt.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\winperf.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\lib\winxptheme.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\libs\pywintypes.lib
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\license.txt
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\mmapfile.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\odbc.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\perfmon.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\perfmondata.dll
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\pythonservice.exe
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\scripts\backupeventlog.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\scripts\ce\pysynch.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\scripts\controlservice.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\scripts\killprocname.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\scripts\rasutil.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\scripts\regsetup.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\scripts\setup_d.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\scripts\versionstamp\brandproject.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\scripts\versionstamp\bulkstamp.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\scripts\versionstamp\vssutil.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\servicemanager.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\handles.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_clipboard.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_exceptions.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_odbc.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_pywintypes.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_security.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_sspi.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32api.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32crypt.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32event.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32file.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32gui.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32guistruct.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32inet.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32net.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32pipe.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32print.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32profile.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32rcparser.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32timezone.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32trace.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\test_win32wnet.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\testall.py
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\win32rcparser\python.bmp
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\win32rcparser\python.ico
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\win32rcparser\test.h
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\test\win32rcparser\test.rc
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\timer.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32api.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32clipboard.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32console.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32cred.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32crypt.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32event.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32evtlog.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32file.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32gui.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32help.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32inet.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32job.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32lz.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32net.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32pdh.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32pipe.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32print.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32process.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32profile.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32ras.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32security.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32service.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32trace.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32transaction.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32ts.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\win32wnet.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32\winxpgui.pyd
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32com\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32comext\adsi\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32comext\authorization\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32comext\axcontrol\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32comext\axdebug\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32comext\axscript\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32comext\bits\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32comext\directsound\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32comext\ifilter\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32comext\internet\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32comext\mapi\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32comext\propsys\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32comext\shell\*
    c:\users\meilin\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages\win32comext\taskscheduler\*
Proceed (Y/n)? y
  Successfully uninstalled pywin32-304

C:\Users\Meilin>pip install pywin32
Collecting pywin32
  Using cached pywin32-304-cp310-cp310-win_amd64.whl (12.1 MB)
Installing collected packages: pywin32
Successfully installed pywin32-304

如果不能下载就像我在上方做的一样,先更新pip,再更新setuptools即可。 

获取PyGame窗口句柄

我们无法获取pygame窗口在整个屏幕的位置,且万一最小化了,还不能截到,所以,我们使用窗口句柄获取PyGame窗口样子。如何获取窗口句柄呢?(推荐收看这篇关于获取窗口句柄的博客:获取打开的所有窗口句柄,不过这篇博文有些错误) 

在上面的URL中,我们知道了获取句柄的代码,如下(先不要拷贝到文件中,有坑):

import win32gui

hwnd_title = {}

def get_all_hwnd(hwnd, mouse):
    if (win32gui.IsWindow(hwnd)
            and win32gui.IsWindowEnabled(hwnd)
            and win32gui.IsWindowVisible(hwnd)):
        hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})


win32gui.EnumWindows(get_all_hwnd, 0)
for h, t in hwnd_title.items():
    if t :
        print(h, t.decode("gbk"))
if __name__ == '__main__':
    pass

(代码原封不动搬过来,侵删) 

运行一看,错误来敲门了!

str类型在没有encode时不许decode。把15行decode()换成encode(): 

因为编码的原因。那么,我们换成utf-8总可以了吧! 

import win32gui

hwnd_title = {}

def get_all_hwnd(hwnd, mouse):
    if (win32gui.IsWindow(hwnd)
            and win32gui.IsWindowEnabled(hwnd)
            and win32gui.IsWindowVisible(hwnd)):
        hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})


win32gui.EnumWindows(get_all_hwnd, 0)
for h, t in hwnd_title.items():
    if t :
        print(h, t.encode("utf-8"))
if __name__ == '__main__':
    pass

我们成功的获取到了窗口句柄,如下:

在打开了迷宫游戏窗口后,显示:

很好! 

我们理解一下:

  行数                                          意义
    1导入所需库
   5~9获取所有窗口的hwnd(句柄),并更新字典hwnd_title中
    12EnumWindows()遍历所有窗口
 13~15遍历窗口字典的所有键值,如果窗口标题非空字符串,输出

 投入使用之前还要把这个代码修改一下。这个代码获取了所有的窗口句柄、标题,并输出了,但是我们不用,所以把一些不用的删除。

import win32gui
hwnd_title = {}
def get_all_hwnd(hwnd, mouse):
    if (win32gui.IsWindow(hwnd)
            and win32gui.IsWindowEnabled(hwnd)
            and win32gui.IsWindowVisible(hwnd)):
        hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})
def main():
    win32gui.EnumWindows(get_all_hwnd, 0)

为了后面的截屏,我们先暂时不要只更新标题为

"Maze Game's window, version 1.0.2"

 的窗口,放到后面在获取。

获取PyGame屏幕并保存到本地

有了句柄,我们可以获取PyGame窗口了!截取窗口图片代码:

import time
import win32gui, win32ui, win32con, win32api
import GetWindowNumber
import win32clipboard as clip
from ctypes import *
import os,io
def window_capture(filename,find):
    GetWindowNumber.main()
    num=0
    for k,v in GetWindowNumber.hwnd_title.items():
        if v and v == find:
            num=k
    hwnd = num  # 窗口的编号,0号表示当前活跃窗口
    # 根据窗口句柄获取窗口的设备上下文DC(Divice Context)
    hwndDC = win32gui.GetDC(hwnd)
    # 根据窗口的DC获取mfcDC
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    # mfcDC创建可兼容的DC
    saveDC = mfcDC.CreateCompatibleDC()
    # 创建bigmap准备保存图片
    saveBitMap = win32ui.CreateBitmap()
    # 获取监控器信息
    MoniterDev = win32api.EnumDisplayMonitors(None, None)
    w = MoniterDev[0][2][3]
    h = MoniterDev[0][2][3]
    # print w,h   #图片大小
    # 为bitmap开辟空间
    saveBitMap.CreateCompatibleBitmap(mfcDC, 900, 900)
    # 高度saveDC,将截图保存到saveBitmap中
    saveDC.SelectObject(saveBitMap)
    # 截取从左上角(0,0)长宽为(900,900)的图片
    saveDC.BitBlt((0, 0), (900, 900), mfcDC, (0, 0), win32con.SRCCOPY)
    saveBitMap.SaveBitmapFile(saveDC, filename)
def main():
    window_capture("Maze_Game_Screen_Shot.png","Maze Game's window, version 1.0.2")

新建一个py文件,使用main函数,可以正常的截取图片了!

 不过……好像总缺了点什么……对了!将图片复制到剪切板中。(接下来的步骤有坑,读者不要一步一步都运行)

说起文本复制到剪切板中,大家的第一印象肯定是pyperclip库的copy函数。那么,你不妨试试使用pyperclip.copy()复制例如利用PIL库Image,img=Image.open(filename),看你会得到啥。

import pyperclip
from PIL import Image
img=Image.open("demo.png")
#                ↑自定义一个图片
pyperclip.copy(img)

 哈哈,不错,你得到了一个错误。

我们知道,pyperclip.copy()是用来复制文本到剪切板的,可没说能复制图片啊!上图说了:

pyperclip.PyperclipException: only str, int, float, and bool values can be copied to the clipboard, not BmpImageFile

 那么,我们得另寻方法了。

import win32clipboard as clip

win32clipboard可以满足我们的需求。

def copy(imagepath):
    img = Image.open(imagepath)  # 打开图片并读取
    output = io.BytesIO()  # 创建IO
    img.convert("RGB").save(output, "BMP") # 读取图片RGB,以BMP保存到output
    data = output.getvalue()[14:]
    clip.OpenClipboard()
    clip.EmptyClipboard()
    clip.SetClipboardData(win32con.CF_DIB, data)  # 图片
    clip.CloseClipboard()

 总代码:

"""
@author: 王俞励
"""
import time
import win32gui, win32ui, win32con, win32api
import GetWindowNumber
import win32clipboard as clip
from ctypes import *
from PIL import Image
import os,io
def copy(imagepath):
    img = Image.open(imagepath)
    output = io.BytesIO()
    img.convert("RGB").save(output, "BMP")
    data = output.getvalue()[14:]
    clip.OpenClipboard()
    clip.EmptyClipboard()
    clip.SetClipboardData(win32con.CF_DIB, data)  # 图片
    clip.CloseClipboard()
def window_capture(filename,find):
    GetWindowNumber.main()
    num=0
    for k,v in GetWindowNumber.hwnd_title.items():
        if v and v == find:
            num=k
    hwnd = num  # 窗口的编号,0号表示当前活跃窗口
    # 根据窗口句柄获取窗口的设备上下文DC(Divice Context)
    hwndDC = win32gui.GetDC(hwnd)
    # 根据窗口的DC获取mfcDC
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    # mfcDC创建可兼容的DC
    saveDC = mfcDC.CreateCompatibleDC()
    # 创建bigmap准备保存图片
    saveBitMap = win32ui.CreateBitmap()
    # 获取监控器信息
    MoniterDev = win32api.EnumDisplayMonitors(None, None)
    w = MoniterDev[0][2][3]
    h = MoniterDev[0][2][3]
    # print w,h   #图片大小
    # 为bitmap开辟空间
    saveBitMap.CreateCompatibleBitmap(mfcDC, 900, 900)
    # 高度saveDC,将截图保存到saveBitmap中
    saveDC.SelectObject(saveBitMap)
    # 截取从左上角(0,0)长宽为(w,h)的图片
    saveDC.BitBlt((0, 0), (900, 900), mfcDC, (0, 0), win32con.SRCCOPY)
    saveBitMap.SaveBitmapFile(saveDC, filename2)
    copy(filename)
def main():
    window_capture("Maze_Game_Screen_Shot.png","Maze Game's window, version 1.0.2")

那么,如果截了多个图片,那么所有的图片都被最后一张图给挤掉了,玩家只能看到最后一张了。我们接下来要添加类似文件资源管理器的,如果已经有了同名图片,就在文件名后+(num),例如(伪代码):

副本号码=2

while 已经有同名图片:

        文件名=文件名+“(”+副本号码+“)”

        副本号码+=1

有思路了,接下来变成代码:

ScreenShotPyGameWindow.py

import time
import win32gui, win32ui, win32con, win32api
import GetWindowNumber
import win32clipboard as clip
from ctypes import *
from PIL import Image
import os,io
def copy(imagepath):
    img = Image.open(imagepath)
    output = io.BytesIO()
    img.convert("RGB").save(output, "BMP")
    data = output.getvalue()[14:]
    clip.OpenClipboard()
    clip.EmptyClipboard()
    clip.SetClipboardData(win32con.CF_DIB, data)  # 图片
    clip.CloseClipboard()
def window_capture(filename,find):
    GetWindowNumber.main()
    num=0
    for k,v in GetWindowNumber.hwnd_title.items():
        if v and v == find:
            num=k
    hwnd = num  # 窗口的编号,0号表示当前活跃窗口
    # 根据窗口句柄获取窗口的设备上下文DC(Divice Context)
    hwndDC = win32gui.GetDC(hwnd)
    # 根据窗口的DC获取mfcDC
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    # mfcDC创建可兼容的DC
    saveDC = mfcDC.CreateCompatibleDC()
    # 创建bigmap准备保存图片
    saveBitMap = win32ui.CreateBitmap()
    # 获取监控器信息
    MoniterDev = win32api.EnumDisplayMonitors(None, None)
    w = MoniterDev[0][2][3]
    h = MoniterDev[0][2][3]
    # print w,h   #图片大小
    # 为bitmap开辟空间
    saveBitMap.CreateCompatibleBitmap(mfcDC, 900, 900)
    # 高度saveDC,将截图保存到saveBitmap中
    saveDC.SelectObject(saveBitMap)
    # 截取从左上角(0,0)长宽为(w,h)的图片
    saveDC.BitBlt((0, 0), (900, 900), mfcDC, (0, 0), win32con.SRCCOPY)
    fuben_NUM=2 # 如果有同名,后面括号里是2
    filename2=filename # 最后都是保存名为filename2的,先初始化,不管有没有同名
    while os.path.exists(os.getcwd()+"\\"+filename2): # 如果还有同名文件
        filename2=filename.split(".")[0]+f"({fuben_NUM})"+"."+filename.split(".")[1] # 文件名后面括号里的+1
        fuben_NUM+=1
"""
上一行意思:
如果还有同名图片:
    保存的文件名=原来的文件名去后缀,用英文点分割,获取前面的名字,例:
>>>s="filename.png"
>>>方法一
>>>s.split(".")
['filename','png']
>>>s.split(".")[0]
'filename'
>>>#方法二
>>>s.rstrip(".png")
'filename'
rstrip(str1)函数是去掉字符串末尾的str1
"""
    saveBitMap.SaveBitmapFile(saveDC, filename2) # 文件名为filename2
    copy(filename)
    return fuben_NUM-1
def main():
    return window_capture("Maze_Game_Screen_Shot.png","Maze Game's window, version 1.0.2")

🆗!现在再试试:

 太棒了!你们也可以试试。

接下来就剩把这个功能添加到游戏中了,否则,玩家截屏还要运行其他东西,非常不便。 

添加到过关界面

原来的通关界面是这样的:

总觉得中间的空白太惹人厌,我们想做成: 

然后鼠标滑动到“点击查看”就变红,移掉变蓝,点击以后打开文件资源管理器中的图片文件夹。

 打开文件资源管理器倒不难,手动打开是点击他的logo,如下:

 半自动打开复杂一点,且多余,不过这是完成这个功能的骨干。打开   C:\Windows   如下:

 点击红框内的可执行文件,他会又打开一个文件资源管理器。这样,我们按Windows徽标键+R,打开以下窗口:

 输入cmd,如下:

回车或点击确定,打开cmd。

或打开   C:\Windows\system32    点击cmd.exe(如图中方法一)或搜索cmd(如图中方法二)

 或按Windows徽标键,写入cmd:

 用以上三种方法之一,打开了cmd:

输入explorer或explorer.exe(cmd中可执行文件写不写后缀名都可以)即可打开文件资源管理器。那么,如何用命令打开文件资源管理器的某一文件夹呢?小伙伴们肯定想到了以下的办法:

explorer  + [空格] + filepath

例如打开C盘:

结果如下:

 熟悉的cmd回来了,命令也知道了,那就放到代码中吧!使用os库的system函数:

        原型:def system(command):   pass

        描述:command为在cmd中要执行/写入的命令,例如explorer。

放在代码中就是:

import os
...
os.system("explorer "+os.getcwd())
...

🆗,接下来是完成其他关于截屏的功能,最后把这项功能写入代码。

在截屏时,获取图片的括号内的数字。这就是为什么我在上面截屏代码中(ScreenShotPyGameWindow.py)加了return,返回最后图片括号内(或无括号,返回1)的数字。我们将这些数字添加到列表中,以防不备之需。在展示时显示这关第一次截屏的图片,如果截屏多次,就显示“等n张截屏,点击查看”。截屏在while True内的代码:

 while True:
     ...
        ...
            if 20<=event.pos[0]<=80 and 5 <= event.pos[1] <= 25:
                ok_OR=tkinter.messagebox.askyesnocancel("温馨提示","温馨提示:因为截屏功能是通过虚拟窗口截屏\n,可能游戏窗口未响应几秒钟,截屏完成后会\n提示成功记录下游戏屏幕(截屏时也可以将P\nyGame窗口最小化,因为通过虚拟窗口截屏所\n以不用最大化,截屏后会将图片复制到剪切板\n,Windows11按Windows徽标键+V\n查看剪切板),是否截屏?")
                if ok_OR:
                    a=ScreenShotPyGameWindow.main()
                    tkinter.messagebox.showinfo("提示",'成功记录下PyGame屏幕!您可以从 '+os.getcwd()+' 查看图片!')
                    screenShot=True
                    ScreenShot_Time+=1
                    screen_shot_list.append(str(a))

event.pos是鼠标点击的坐标。点击查看按钮的构思:

如果鼠标移到按钮上,就是鼠标的pos在按钮的左上角至右下角内,那么变蓝(颜色变量设为blue,否则设为red),如果点击了按钮,(同上面的检测方法)那么运行os.system("explorer "+os.getcwd)。读者可以在脑中自行想一想如何写代码(前面已经说过这种方法),当一个练习。下面是全部代码: 

    if rect1.colliderect(my) or moneyGo:
        # tkinter.messagebox.showinfo('提示','恭喜你,成功通过第'+str(level)+'关!')
        x=55
        y=54
        level+=1
        h1,m1,s1=h,m,s
        maps=[(55,54)]
        a=True
        b=False
        blues=False
        reds=False
        screen.fill((255,255,255))
        screen.blit(font8.render("成功通过第{}关!".format(level-1),True,(255,0,0)),((900-(7+len(list(str(level))))*30)/2,200))
        screen.blit(font7.render("耗时:{:0>2}:{:0>2}:{:0>2}".format(h2,m2,s2),True,"#48D1CC"),(int((900-(3+len(list(str(h2)))+len(list(str(m2)))+len(list(str(s2))))*30)/2),280+10))
        screen.blit(font7.render("获得金币数:{}".format(int(this_coin / (1 + moneyGo))), True, "#48D1CC"), (int((900 - (3 + len(list(str(get_coin)))) * 30) / 2), 280 + 30 * 2))
        screen.blit(font7.render("获得能量值:{}".format(this_lightning),True,"#48D1CC"),(int((900-(3+len(list(str(get_lightning))))*30)/2),280+30*4))
        screen.blit(font7.render("步数:{}".format(steps),True,"#48D1CC"),(int((900-(3+len(list(str(steps))))*30)/2),280+30*6))
        if moneyGo:
            screen.blit(font7.render("备注:因此局使用了'钞'能力,所以获得金币数减半。",True,"#48D1CC"),((900-25*30)/2,280+30*8))
        if screenShot:
            s=screen_shot_list[0] if os.path.exists(os.getcwd()+f"\\Maze_Game_Screen_Shot({screen_shot_list[0]}).png") else ""
            s=("(" if s!="" else "")+s+(")" if s!="" else "")
            screen.blit(pygame.transform.scale(pygame.image.load(f"Maze_Game_Screen_Shot{s}.png"),(250,250)),(290,570))
            if ScreenShot_Time>1:
                screen.blit(font7.render(f"等{ScreenShot_Time}张截屏,",True,(0,0,0)),(540,780))
        use_xiang_su=len(list(str(ScreenShot_Time)))*15+5*30+540
        use_xiang_su2=use_xiang_su+120
        color_of_show='blue'
        while a:
            if screenShot:
                screen.blit(font7.render("点击查看",True,color_of_show),(use_xiang_su,780))
            for event4 in pygame.event.get():
                if b:
                    b=False
                xx,yy=pygame.mouse.get_pos()
                if event4.type == pygame.MOUSEBUTTONDOWN:
                    if 850<=yy<=890:
                        if 20<=xx<=440:
                            a=False
                            home(True)
                        elif 450<=xx<=450+420:
                            a=False
                    if use_xiang_su <= xx <= use_xiang_su2 and 780 <= yy <= 810 and screenShot:
                        os.system("explorer "+os.getcwd())
                elif event4.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit(0)
                if event4.type == pygame.MOUSEMOTION:
                    pygame.draw.rect(screen,(255,255,255),(0,900-45-20,900,70))
                    if use_xiang_su<=xx<=use_xiang_su2 and 780<=yy<=810 and screenShot:
                        color_of_show='red'
                    else:
                        color_of_show='blue'
                    if 850 <= yy <= 890:
                        if 20 <= xx <= 440:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", "#00BFFF", 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                            screen.blit(font7.render("↙返回主页", True, "white"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            b=True
                            blues=True
                            reds=False
                        else:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                            screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            blues=False
                        if 450 <= xx <= 450 + 420:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 0, 0), 4)
                            screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "white"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            b=True
                            reds=True
                        else:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                            screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            blues=False
                if reds:
                    draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                    draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 0, 0), 0)
                    screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                    screen.blit(font7.render("下一关↘", True, "white"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                if blues:
                    draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", "#00BFFF", 0)
                    draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                    screen.blit(font7.render("↙返回主页", True, "white"), (20 + 200-50, 900 - 55 + 10))
                    screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                if not b:
                    draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                    draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                    screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                    screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                    pygame.draw.circle(screen,(255,255,255),(20 + 20 + 420+20, 900 - 50+20),16)
                    pygame.draw.circle(screen,(255,255,255),(20 + 20 + 400+420, 900 - 50+20),16)
                    pygame.draw.circle(screen,(255,255,255),(20+20, 900 - 50+20),16)
                    pygame.draw.circle(screen,(255,255,255),(20+400, 900 - 50+20),16)
            pygame.display.update()
        obj=""
        with open("Maze_Game_conf_File\\data.mgcf","r",encoding='utf-8') as file:
            ob=file.readlines()[2:]
        for i in ob:
            obj+=i
        if moneyGo:
            get_coin=get_coin-int(this_coin/2)
        with open("Maze_Game_conf_File\\data.mgcf", "w", encoding='utf-8') as file:
            file.write("{}\n{}\n".format(get_coin,get_lightning))
            file.write(obj)
            file.write("{}\n".format(nowsss))
            file.write("{}×{}\n".format(grid_size[0],grid_size[1]))
            file.write(str(level)+"\n")
            file.write("{:0>2}:{:0>2}:{:0>2}\n".format(str(h2),str(m2),str(s2)))
            file.write("{}\n".format(str(pauses)))
            if len(pauses_time)!=0:
                for i in pauses_time:
                    file.write("{:0>2}:{:0>2}:{:0>2}".format(i.split(":")[0],i.split(":")[1],i.split(":")[2])+"\n")
            else:
                file.write("No pause time.\n")
            file.write("{}\n".format(steps))
            file.write("{}\n".format(get_lightning))
            file.write("{}\n".format(get_coin))
            file.write("{}\n".format(this_lightning))
            file.write("{}\n".format(this_coin))
            file.write("{}\n".format("是" if moneyGo else "否"))
            file.write("{}\n".format("是" if not secret else "否"))
        steps=0
        get_level+=1
        moneyGo=False
        run()

总结

代码

按照我文章开头的项目目录写入:

maze_game.py

import os.path
import pygame
import pygame as pg
import time
import random
import sys
import os
import tkinter.messagebox
import threading
import pygame.sprite
import cv2
import datetime
import numpy
from PIL import Image,ImageGrab
import tkinter.filedialog
import ScreenShotPyGameWindow
this_coin=0
this_lightning=0
pygame.mixer.init()
if not os.path.exists(os.getcwd()+"\\Maze_Game_conf_File\\ScreenShot"):
    os.makedirs(os.getcwd()+"\\Maze_Game_conf_File\\ScreenShot")
tkinter.messagebox.showinfo('重要提示','若代码出错,请删除此目录下的Maze_Game_conf_File文件夹重试即可。')
mapOfList=[]
for i in range(55,850-55,16):
    listA=[]
    for j in range(54,850-54,16):
        listA.append((i,j))
    mapOfList.append(listA)
# yes_=tkinter.messagebox.askyesno('习惯提示','是否在玩时显示“脚印”?')
yes_=True
# tkinter.messagebox.showinfo('操作提示','按↑↓←→/W-S-A-D(上下左右)操控角色(蓝色矩形),\n碰到红色终点后进入下一关,鼠标单击(左右键都可以)游戏屏幕即\n可显示/隐藏你走过的路线,按Q键/右上角×退出,按P键暂停游\n戏。')
sys.setrecursionlimit(100000)
get_level=0
pauses=0
pauses_time=[]
click_img=pygame.image.load('click.jpeg')
video = cv2.VideoCapture('show.mp4')
if not os.path.exists(os.getcwd()+'\\Maze_Game_conf_File'):
    get_lightning=0
    get_coin=0
    os.makedirs(os.getcwd()+'\\Maze_Game_conf_File')
if not os.path.exists(os.getcwd()+'\\Maze_Game_conf_File\\data.mgcf'):
    with open("Maze_Game_conf_File\\data.mgcf","w",encoding='utf-8') as f:
        f.write("")
else:
    if os.path.exists(os.getcwd()+"\\Maze_Game_conf_File\\data.mgcf"):
        with open("Maze_Game_conf_File\\data.mgcf","r",encoding='utf-8') as f:
            ff=f.readlines()
            if len(ff)!=0:
                get_coin=int(ff[0].rstrip("\n"))
                get_lightning=int(ff[1].rstrip("\n"))
            else:
                get_coin=0
                get_lightning=0
    else:
        get_coin=0
        get_lightning=0
MAZE_MAP = []
for i in range(800):
    MAZE_MAP.append([])
    for j in range(800):
        MAZE_MAP[i].append(0)
ListOfPlayInfo=[]
def update():
    if os.path.exists(os.getcwd()+"\\Maze_Game_conf_File\\data.mgcf"):
        with open("Maze_Game_conf_File\\data.mgcf","r",encoding='utf-8') as file:
            readlines=file.readlines()[2:]
        global ListOfPlayInfo
        num=0
        for i in range(len(readlines)):
            readlines[i]=readlines[i].rstrip("\n")
        while num<len(readlines):
            dict={}
            dict2={}
            list=[]
            name=readlines[num]
            num+=1
            dict2["Maze-Size"]=readlines[num]
            num+=1
            dict2["Level"]=readlines[num]
            num+=1
            dict2["Use-Times"]=readlines[num]
            num+=1
            dict2["Pause-Times"]=readlines[num]
            if dict2["Pause-Times"]!="0":
                for i in range(int(dict2["Pause-Times"])):
                    list.append(readlines[num+i+1])
            else:
                num+=1
            dict2["Pause-Time"]=list
            num+=1+int(dict2["Pause-Times"])
            dict2["Use-Steps"]=readlines[num]
            num+=1
            dict2["Get-Lightnings"]=readlines[num]
            num+=1
            dict2["Get-Coins"]=readlines[num]
            dict[name]=dict2
            num+=1
            dict2["New-Get-lightning"]=readlines[num]
            num+=1
            dict2["New-Get-Coins"]=readlines[num]
            num+=1
            dict2["Use-Money-Go"]=readlines[num]
            num+=1
            dict2["Get-Secret-Box"]=readlines[num]
            num+=1
            ListOfPlayInfo.append(dict)
    else:
        pass
def update_map():
    with open("Maze_Game_conf_File\\map.mgcf","r") as fileI:
        M=fileI.read()
    return eval(M)
def IMapMgCf():
    global screen
    with open("Maze_Game_conf_File\\map.mgcf","w") as fileIM:
        fileIM.write(str(pygame.PixelArray(screen)))
# video.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G'))
# video.set(100000000, 1)
class Tile:
    def __init__(self, grid_size, screen_size, x, y):  # 主要是储存数据和绘制图形,与算法无关
        self.x, self.y = x, y
        self.connected = [0, 0, 0, 0]  # up,right,down,left 0 for not connected
        self.grid_size = grid_size
        self.tile_size = [(screen_size[0] - 100) / grid_size[0], (screen_size[1] - 100) / grid_size[1]]
        self.rectangle = (
        self.x * self.tile_size[0] + 50, self.y * self.tile_size[1] + 50, self.tile_size[0], self.tile_size[1])
        self.points = [[self.x * self.tile_size[0] + 50, self.y * self.tile_size[1] + 50],  # uppper left
                       [self.x * self.tile_size[0] + 50 + self.tile_size[0], self.y * self.tile_size[1] + 50],
                       # upper right
                       [self.x * self.tile_size[0] + 50 + self.tile_size[0],
                        self.y * self.tile_size[1] + 50 + self.tile_size[1]],  # lower right
                       [self.x * self.tile_size[0] + 50, self.y * self.tile_size[1] + 50 + self.tile_size[1]],
                       # lower left
                       ]
        self.visited = False

    def draw(self, color=(255, 255, 255)):  # x,y represents the tile coordinates
        # global maps
        pg.draw.rect(screen, color, self.rectangle)  # 绘制节点
        for i in range(4):  # 绘制四面墙壁
            if not self.connected[i]:
                pg.draw.line(screen, (0, 0, 0), (self.points[i]), (self.points[((i + 1) % 4)]), 5)
                # for j in range(self.points[i]):
                #     MAZE_MAP[self.points[i][0]-50][self.points[i][1]-50]=1
                '''接下来进行迷宫墙的保存'''

                # if self.points[i][0] == self.points[((i + 1) % 4)][0]:
                #     #X坐标没变,Y坐标变化,说明线是竖着的
                #     for j in range(int(self.points[i][0]),int(self.points[((i+1) % 4)][0])+1):
                #         for k in range(5):
                #             MAZE_MAP[j-50-1][int(self.points[((i+1) % 4)][1])-k-50-1]=1
                # else:
                #     #说明线是横着的
                #     for j in range(int(self.points[i][1]),int(self.points[((i+1) % 4)][1])+1):
                #         for k in range(5):
                #             MAZE_MAP[int(self.points[((i+1) % 4)][0])-k-50-1][j-50-1]=1
def maze_gen(path):
    global tile_covered  # 覆盖节点数量,当覆盖节点数量到达网格数量则停止
    x, y = path[-1]
    if x < 0 or x >= grid_size[0] or y < 0 or y >= grid_size[1]:  # 超出网格范围则退出
        return
    matrix[y][x].draw()
    if matrix[y][x].visited:  # 已访问节点则退出
        return
    elif tile_covered <= grid_size[0] * grid_size[1]:  # 覆盖节点数量未到达网格总数量
        tile_covered += 1
        matrix[y][x].visited = True
        path_choice = [0, 1, 2, 3]
        random.shuffle(path_choice)
        directions = [[0, -1], [1, 0], [0, 1], [-1, 0]]  # up,right,down,left 0 for not connected

        for i in path_choice:
            x_, y_ = x + directions[i][0], y + directions[i][1]
            path.append([x_, y_])
            if maze_gen(path):
                matrix[y][x].connected[i] = 1  # walls of current node
                matrix[y_][x_].connected[(i + 2) % 4] = 1  # reverse the vector direction
                matrix[y][x].draw()
                matrix[y_][x_].draw()

            path.pop(-1)
        pg.display.update()

        return True

    else:
        return
note='按↑↓←→/W-S-A-D(上下左右)操控角色(蓝色矩形),碰到红色终点后进入下一关,鼠标单击'
note2='(左右键都可以)游戏屏幕即可显示/隐藏你走过的路线,按Q键/右上角×退出,按P键暂停游戏。'
note3='按↑↓←→/W-S-A-D(上下左右)操控角色(蓝色矩形),'
note4='碰到红色终点后进入下一关,鼠标单击(左右键都可以)游戏屏幕即'
note5='可显示/隐藏你走过的路线,按Q键/右上角×退出,按P键暂停游戏。'
screen_size = [900,900]
grid_size = [50, 50]
exit = [20, 20]
tile_covered = 0
steps=0
matrix = []
update()
def clear_data():
    with open("Maze_Game_conf_File\\data.mgcf",'w') as file:
        file.write("")
x=55
nowsss=0
y=54
secret_box=(0,0)
maps=[(55,54)]
can={'up':True,'right':True,'left':True,'down':True}
screen = pg.display.set_mode(screen_size,pygame.RESIZABLE)
pygame.display.set_icon(pygame.image.load("logo.png"))
pygame.display.set_caption('Maze Game\'s window, version 1.0.2')
pygame.mixer.music.load(os.getcwd()+"\\music\\菊次郎的夏天.mp3")
pygame.mixer.music.play(-1)
repeat=150,100
pygame.key.set_repeat(repeat[0],repeat[1])
screenShot=False
ScreenShot_Time=0
screen_shot_list=[]
secret=True
J=pygame.image.load("J.png")
def run():
    global matrix,exit,tile_covered,screenShot,ScreenShot_Time,screen_shot_list,secret
    screenShot=False
    ScreenShot_Time=0
    screen_shot_list=[]
    secret=True
    matrix=[]
    exit=[20,20]
    tile_covered=0
    for y in range(grid_size[1]):  # 创建二维矩阵,x,y代表坐标
        temp = []
        for x in range(grid_size[0]):
            tile = Tile(grid_size, screen_size, x, y)
            temp.append(tile)
        matrix.append(temp)
    path = [[0, 0]]
    where = random.randint(1, 3)
    global rect1
    if where == 1:
        rect1 = pygame.Rect(900 - 65, 900 - 65, 9, 9)
    elif where == 2:
        rect1 = pygame.Rect(54, 900 - 65, 9, 9)
    else:
        rect1 = pygame.Rect(900 - 65, 54, 9, 9)
    screen.fill((255, 255, 255))
    maze_gen(path)
    pg.display.update()
    day=int(time.strftime('%w',time.localtime()))
    if day == 1:
        day="一"
    elif day == 2:
        day="二"
    elif day == 3:
        day="三"
    elif day == 4:
        day="四"
    elif day == 5:
        day="五"
    elif day == 6:
        day="六"
    elif day == 7:
        day="日"
    nowss = time.strftime('%Z %Y-%m-%d 星期{} %H:%M:%S'.format(day), time.localtime())
    global nowsss
    nowsss=nowss
    try:
        with open("Maze_Game_conf_File\\data.mgcf",'a', encoding="utf-8") as file:
            file.write("")
    except:
        with open("Maze_Game_conf_File\\Mazedata.mgcf",'w',encoding='utf-8') as file:
            file.write("")
    font_surface2 = font2.render(note,True,'orange')
    font_surface3 = font2.render(note2,True,'orange')
    screen.blit(font_surface2,(75-18+48,860))
    screen.blit(font_surface3,(75-18+48,875))
    global coin_blits,lightning_blits,secret_box
    coin_blits = []
    lightning_blits = (0, 0)
    for i in range(100):
        choices = random.choice(random.choice(mapOfList))
        coin_blits.append(choices)
        screen.blit(pygame.transform.scale(image_coin,(9,9)), choices)
    while lightning_blits == (0, 0) or lightning_blits in coin_blits:
        lightning_blits = random.choice(random.choice(mapOfList))
    screen.blit(pygame.transform.scale(image_lightning,(11,11)), lightning_blits)
    while secret_box == (0,0) or secret_box == lightning_blits or secret_box in coin_blits:
        secret_box = random.choice(random.choice(mapOfList))
rect1=0
pg.init()
def has_color(aSurface,aRect,aColor):
    pixel=pygame.PixelArray(aSurface)               #锁定aSurface,将各点颜色保存在2维数组
    aPixel=pixel[aRect.x:aRect.x+aRect.width+15,aRect.y:aRect.y+aRect.height+15]  #得到数组切片
    global can
    if aColor in aPixel:
        if aColor in pixel[aRect.x-15:aRect.x+aRect.width,aRect.y:aRect.y+aRect.height]:
            can['left']=False
        else:
            can['left']=True
        if aColor in pixel[aRect.x:aRect.x+aRect.width+15,aRect.y:aRect.y+aRect.height]:
            can['right']=False
        else:
            can['right']=True
        if aColor in pixel[aRect.x:aRect.x+aRect.width,aRect.y-15:aRect.y+aRect.height]:
            can['up'] = False
        else:
            can['up']=True
        if aColor in pixel[aRect.x:aRect.x+aRect.width,aRect.y:aRect.y+aRect.height+15]:
            can['down'] = False
        else:
            can['down']=True
    else:
        can['left']=True
        can['right']=True
        can['up']=True
        can['down']=True
    del pixel           #解锁aSurface并删除数组
font_name = pygame.font.match_font('fangsong')  # 2.获得字体文件
font = pygame.font.Font(font_name, 20)
# font_name3 = pygame.font.match_font('胖娃体.ttf')  # 2.获得字体文件
# font3 = pygame.font.Font(font_name3, 40)
# font_name4 = pygame.font.match_font('胖娃体.ttf')  # 2.获得字体文件
# font4 = pygame.font.Font(font_name4, 70)
font_name2 = pygame.font.match_font('kaiti')  # 2.获得字体文件
font2 = pygame.font.Font(font_name2, 15)
font_name8 = pygame.font.match_font('kaiti')  # 2.获得字体文件
font8 = pygame.font.Font(font_name8, 50)
font5 = pygame.font.Font(font_name2, 70)
font6 = pygame.font.Font(font_name2, 40)
font7 = pygame.font.Font(font_name2, 30)
# img_play=pygame.image.load('play.jpeg').convert_alpha()
level=1
color=(175,175,175)
m2=0
h2=0
add=False
# def show_pic(screen):
#     #TODO show pic
#     s = f"({screen_shot_list[0]})" if screen_shot_list[0] > 1 else ""
#     if len(screen_shot_list) == 1:
#         print(1)
#         xxx = pygame.image.load(os.getcwd() + f"\\Maze_Game_conf_File\\ScreenShot\\Maze_Game_Screen_Shot{s}.png")
#         # xx2=pygame.transform.scale(xxx,(0,0))
#         screen.blit(xxx, (290, 570))
#         # xxx=pygame.image.load(os.getcwd()+f"\\Maze_Game_conf_File\\ScreenShot\\Maze_Game_Screen_Shot{s}.png")
#         # # xx2=pygame.transform.scale(xxx,(250,250))
#         # screen.blit(xxx,(290,570))#TODO 为什么我是要screen.blit(xxx),却显示是screen.blit("lightning.png",(x,y))呢?
#         print(os.getcwd() + f"\\Maze_Game_conf_File\\ScreenShot\\Maze_Game_Screen_Shot{s}.png")
#     elif len(screen_shot_list) == 2:
#         screen.blit(pygame.transform.scale(
#             pygame.image.load(os.getcwd() + f"\\Maze_Game_conf_File\\ScreenShot\\Maze_Game_Screen_Shot{s}.png"),
#             (250, 250)),
#                     (175, 570))
#         screen.blit(pygame.transform.scale(pygame.image.load(
#             os.getcwd() + f"\\Maze_Game_conf_File\\ScreenShot\\Maze_Game_Screen_Shot({str(int(s[1:-1]) + 1)}).png"),
#                                            (250, 250)),
#                     (725, 570))
#     else:
#         screen.blit(pygame.transform.scale(
#             pygame.image.load(os.getcwd() + f"\\Maze_Game_conf_File\\ScreenShot\\Maze_Game_Screen_Shot{s}.png"),
#             (250, 250)),
#                     (55, 570))
#         screen.blit(pygame.transform.scale(pygame.image.load(
#             os.getcwd() + f"\\Maze_Game_conf_File\\ScreenShot\\Maze_Game_Screen_Shot({str(int(s[1:-1]) + 1)}).png"),
#                                            (250, 250)),
#                     (325, 570))
#         screen.blit(pygame.transform.scale(pygame.image.load(
#             os.getcwd() + f"\\Maze_Game_conf_File\\ScreenShot\\Maze_Game_Screen_Shot({str(int(s[1:-1]) + 2)}).png"),
#                                            (250, 250)),
#                     (595, 570))
#     screen.blit(font7.render(f"共{ScreenShot_Time}张截屏,", True, (0, 0, 0)), (540, 780))
sec=-1
pause=False
pause_time=0
write_s=0
add_m=False
downs=True
def draw_button(screen,pos,w,h,line_color,color,line_width):
    r=int(h/2)
    pygame.draw.circle(screen,line_color,(pos[0]+r,pos[1]+r),r,line_width)
    pygame.draw.circle(screen,line_color,(pos[0]+w-r,pos[1]+r),r,line_width)
    pygame.draw.rect(screen,color,(pos[0]+r+1,pos[1]+1,w-(r*2),(r-1)*2),0)
    pygame.draw.line(screen,line_color,(pos[0]+r,pos[1]+2),(pos[0]+w-r,pos[1]+2),line_width)
    pygame.draw.line(screen,line_color,(pos[0]+r,pos[1]+r*2),(pos[0]+w-r,pos[1]+r*2),line_width)

def do_this():
    t=time.localtime()
    global h1,m1,s1,downs
    h1,m1,s1=t.tm_hour,t.tm_min,t.tm_sec
    downs=False
whiles_x,whiles_y=240,50
whiles_rect_x,whiles_rect_y=300,500
whiles_rect_x2,whiles_rect_y2=300,570
whiles_x2,whiles_y2=240,50
whiles_rect_x3,whiles_rect_y3=300,640
whiles_x3,whiles_y3=240,50
whiles_rect_x4,whiles_rect_y4=300,715
whiles_x4,whiles_y4=240,50
whiles_click=True
FPSClock = pygame.time.Clock()
is_big=False
image_coin=pygame.image.load("coin.jpeg")
image_coin=pygame.transform.scale(image_coin,(40,40)).convert()
image_coin.set_colorkey((255,255,255))
image_lightning = pygame.image.load("lightning.png")
image_lightning.set_colorkey((255,255,255))
image_lightning=pygame.transform.scale(image_lightning,(40,40))
logo=pygame.image.load('logo.png')
coin_blits=[]
lightning_blits=(0,0)
for i in range(100):
    choices=random.choice(random.choice(mapOfList))
    coin_blits.append(choices)
    screen.blit(image_coin, choices)
while lightning_blits==(0,0) or lightning_blits in coin_blits:
    lightning_blits=random.choice(random.choice(mapOfList))
screen.blit(image_lightning, lightning_blits)
colors='blue'
all_screen_play=False
_=0
_2=0
def home(a):
    global downs,whiles_rect_x,whiles_rect_y,whiles_x,whiles_y,font6,font7,font8,font5,font2,\
        whiles_rect_x2, whiles_rect_y2, whiles_x2, whiles_y2,whiles_rect_x3, whiles_rect_y3,\
        whiles_x3, whiles_y3,whiles_rect_x4, whiles_rect_y4, whiles_x4, whiles_y4,click_img,\
        whiles_click,all_screen_play,colors,_,_2,get_lightning,get_coin,is_big,ListOfPlayInfo
    while downs:
        screen.fill((255,255,255))
        draw_button(screen, (30-5, 30-5), 40+len(list(str(get_coin)))*20+30, 50,(105,105,105),(105,105,105),0)
        draw_button(screen, (30-5, 90-5), 40+len(list(str(get_lightning)))*20+30, 50,(105,105,105),(105,105,105),0)
        screen.blit(image_coin,(30,30))
        screen.blit(image_lightning,(30,90))
        screen.blit(font6.render("{}".format(get_coin),True,"white"),(80,30))
        screen.blit(font6.render("{}".format(get_lightning),True,"white"),(80,90))
        font_surface5= font5.render('迷宫游戏(V1.0.2)', True, 'black')
        screen.blit(font_surface5, (100, 270))
        screen.blit(logo, ((900-128)/2-30,270-128-30))
        if a:
            pygame.draw.rect(screen, (255,0,0),(whiles_rect_x,whiles_rect_y,whiles_x,whiles_y),0)
            font_surface4 = font6.render('开始游戏', True, 'white')
            # screen.blit(img_play, (310,510))
            screen.blit(font_surface4, (330, 505))
        else:
            pygame.draw.rect(screen, (255, 0, 0), (whiles_rect_x, whiles_rect_y, whiles_x, whiles_y), 0)
            font_surface4 = font6.render('(不可)开始游戏', True, 'white')
            # screen.blit(img_play, (310,510))
            screen.blit(font_surface4, (330-40, 505))
        pygame.draw.rect(screen, 'orange', (whiles_rect_x2, whiles_rect_y2, whiles_x2, whiles_y2), 0)
        font_surface5 = font6.render('PYAI自动寻路', True, 'white')
        # screen.blit(img_play, (310,510))
        screen.blit(font_surface5, (300, 575))
        pygame.draw.rect(screen, '#0000CD', (whiles_rect_x3, whiles_rect_y3, whiles_x3, whiles_y3), 0)
        font_surface6 = font6.render('查看游戏记录', True, 'white')
        # screen.blit(img_play, (310,510))
        screen.blit(font_surface6, (300, 645))
        pygame.draw.rect(screen, 'cyan', (whiles_rect_x4, whiles_rect_y4, whiles_x4, whiles_y4), 0)
        font_surface7 = font6.render('游戏规则', True, 'white')
        # screen.blit(img_play, (310,510))
        screen.blit(font_surface7, (330, 715))
        # button1=BFButton(screen,(300,500,200,5),'开始游戏',bg=(255,0,0),fg='white',click=do_this)
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                if 300 <= mouse_x <= 540 and 500 <= mouse_y <= 550 and a:
                    t = time.localtime()
                    global h1,m1,s1
                    h1, m1, s1 = t.tm_hour, t.tm_min, t.tm_sec
                    downs = False
                elif 300 <= mouse_x <= 540 and 725 <= mouse_y <= 775:
                    screen.fill((255,255,255))
                    whiles_click=True
                    font_surface8 = font6.render('游戏规则', True, 'black')
                    # screen.blit(img_play, (310,510))
                    screen.blit(font_surface8, (290+80, 40))
                    # pygame.draw.rect(screen, 'black', (10,8,80,17), 2)
                    font_surface9 = font2.render('〈返回主页', True, (100,100,100))
                    # screen.blit(img_play, (310,510))
                    screen.blit(font_surface9, (10, 10))
                    font_surface10 = font7.render(note3,True,'black')
                    screen.blit(font_surface10, (90, 130))
                    font_surface11 = font7.render(note4,True,'black')
                    screen.blit(font_surface11, (0, 170))
                    font_surface12 = font7.render(note5,True,'black')
                    screen.blit(font_surface12, (0, 210))
                    ##################第一阶段按钮绘画开始#####################
                    pygame.draw.rect(screen, 'black', (170, 380-55, 74, 74), 2)
                    font_surface13 = font5.render('↑', False, 'black')
                    screen.blit(font_surface13, (170 + 2, 380 + 2-55))
                    #######################################################
                    pygame.draw.rect(screen, 'black', (170, 380+6+74-55,74,74),2)
                    font_surface13 = font5.render('↓', False, 'black')
                    screen.blit(font_surface13, (170 + 2, 380 + 8+74-55))
                    ########################################################
                    pygame.draw.rect(screen, 'black', (170-74-6, 380 + 6 + 74-55, 74, 74), 2)
                    font_surface13 = font5.render('←', False, 'black')
                    screen.blit(font_surface13, (170 -4-74, 380 + 8 + 74-55))
                    ########################################################
                    pygame.draw.rect(screen, 'black', (170+74+6, 380 + 6 + 74-55, 74, 74), 2)
                    font_surface13 = font5.render('→', False, 'black')
                    screen.blit(font_surface13, (170 + 8+74, 380 + 8 + 74-55))
                    ##################第一阶段按钮绘画完成######################
                    ##################第二阶段按钮绘画开始######################
                    pygame.draw.rect(screen, 'black', (540, 380-55, 74, 74), 2)
                    font_surface13 = font5.render('W', True, 'black')
                    screen.blit(font_surface13, (540+16 + 2, 380 + 2-55))
                    #######################################################
                    pygame.draw.rect(screen, 'black', (540, 380 + 6 + 74-55, 74, 74), 2)
                    font_surface13 = font5.render('S', True, 'black')
                    screen.blit(font_surface13, (540 +16+ 2, 380 + 8 + 74-55))
                    ########################################################
                    pygame.draw.rect(screen, 'black', (540 - 74 - 6, 380 + 6 + 74-55, 74, 74), 2)
                    font_surface13 = font5.render('A', True, 'black')
                    screen.blit(font_surface13, (540 +16- 4 - 74, 380 + 8 + 74-55))
                    ########################################################
                    pygame.draw.rect(screen, 'black', (540 + 74 + 6, 380 + 6 + 74-55, 74, 74), 2)
                    font_surface13 = font5.render('D', True, 'black')
                    screen.blit(font_surface13, (540 + 8+16 + 74, 380 + 8 + 74-55))
                    ##################第二阶段按钮绘画完成######################
                    ##################第一阶段图片绘画开始######################
                    click_img=pygame.transform.scale(click_img, (90,90))
                    screen.blit(click_img, (360,321))
                    ##################第一阶段图片绘画完成######################
                    ##################第三阶段按钮绘画开始######################
                    pygame.draw.rect(screen, 'black', (88, 658, 74, 74), 2)
                    font_surface13 = font5.render('P', True, 'black')
                    screen.blit(font_surface13, (99+10, 660))
                    ########################################################
                    pygame.draw.rect(screen, 'black', (88, 758, 74, 74), 2)
                    font_surface13 = font5.render('Q', True, 'black')
                    screen.blit(font_surface13, (99+10, 760))
                    ########################################################
                    pygame.draw.rect(screen, 'black', (88+76+5, 758, 74, 74), 2)
                    font_surface13 = font5.render('×', True, 'black')
                    screen.blit(font_surface13, (99+74, 760))
                    #################第三阶段按钮绘画完成#######################
                    font_surface13 = font2.render('*除鼠标图标之外,处于同一行的示例均同效',True,'blue')
                    screen.blit(font_surface13, (0,854))
                    font_surface13 = font2.render('视频看不清?试试', True, 'black')
                    screen.blit(font_surface13, (900 - 12 * 15, 500 - 17))
                    while whiles_click:
                        if all_screen_play:
                            if _2 == 0:
                                font_surface9 = font2.render('〈返回至游戏规则', True, (100,100,100))
                                # screen.blit(img_play, (310,510))
                                screen.blit(font_surface9, (10, 10))
                            if video.isOpened():
                                ret, frame = video.read()
                                try:
                                    frame = numpy.rot90(frame, k=-1)
                                    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                                    frame = pygame.surfarray.make_surface(frame)
                                    frame = pygame.transform.flip(frame, False, True)
                                    frame = pygame.transform.scale(frame, (900, 600))
                                    screen.blit(frame, (0, 150))
                                except:
                                    break
                            for event3 in pygame.event.get():
                                if event3.type == pygame.QUIT:
                                    pygame.quit()
                                    sys.exit()
                                if event3.type == pygame.MOUSEBUTTONDOWN:
                                    x3,y3=event3.pos
                                    if 10 <= x3 <= 8*15+10 and 10 <= y3 <= 26:
                                        all_screen_play=False
                                        screen.fill((255,255,255))
                                        font_surface8 = font6.render('游戏规则', True, 'black')
                                        # screen.blit(img_play, (310,510))
                                        screen.blit(font_surface8, (290 + 80, 40))
                                        # pygame.draw.rect(screen, 'black', (10,8,80,17), 2)
                                        font_surface9 = font2.render('〈返回主页', True, (100, 100, 100))
                                        # screen.blit(img_play, (310,510))
                                        screen.blit(font_surface9, (10, 10))
                                        font_surface10 = font7.render(note3, True, 'black')
                                        screen.blit(font_surface10, (90, 130))
                                        font_surface11 = font7.render(note4, True, 'black')
                                        screen.blit(font_surface11, (0, 170))
                                        font_surface12 = font7.render(note5, True, 'black')
                                        screen.blit(font_surface12, (0, 210))
                                        ##################第一阶段按钮绘画开始#####################
                                        pygame.draw.rect(screen, 'black', (170, 380 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('↑', False, 'black')
                                        screen.blit(font_surface13, (170 + 2, 380 + 2 - 55))
                                        #######################################################
                                        pygame.draw.rect(screen, 'black', (170, 380 + 6 + 74 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('↓', False, 'black')
                                        screen.blit(font_surface13, (170 + 2, 380 + 8 + 74 - 55))
                                        ########################################################
                                        pygame.draw.rect(screen, 'black', (170 - 74 - 6, 380 + 6 + 74 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('←', False, 'black')
                                        screen.blit(font_surface13, (170 - 4 - 74, 380 + 8 + 74 - 55))
                                        ########################################################
                                        pygame.draw.rect(screen, 'black', (170 + 74 + 6, 380 + 6 + 74 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('→', False, 'black')
                                        screen.blit(font_surface13, (170 + 8 + 74, 380 + 8 + 74 - 55))
                                        ##################第一阶段按钮绘画完成######################
                                        ##################第二阶段按钮绘画开始######################
                                        pygame.draw.rect(screen, 'black', (540, 380 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('W', True, 'black')
                                        screen.blit(font_surface13, (540 + 16 + 2, 380 + 2 - 55))
                                        #######################################################
                                        pygame.draw.rect(screen, 'black', (540, 380 + 6 + 74 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('S', True, 'black')
                                        screen.blit(font_surface13, (540 + 16 + 2, 380 + 8 + 74 - 55))
                                        ########################################################
                                        pygame.draw.rect(screen, 'black', (540 - 74 - 6, 380 + 6 + 74 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('A', True, 'black')
                                        screen.blit(font_surface13, (540 + 16 - 4 - 74, 380 + 8 + 74 - 55))
                                        ########################################################
                                        pygame.draw.rect(screen, 'black', (540 + 74 + 6, 380 + 6 + 74 - 55, 74, 74), 2)
                                        font_surface13 = font5.render('D', True, 'black')
                                        screen.blit(font_surface13, (540 + 8 + 16 + 74, 380 + 8 + 74 - 55))
                                        ##################第二阶段按钮绘画完成######################
                                        ##################第一阶段图片绘画开始######################
                                        click_img = pygame.transform.scale(click_img, (90, 90))
                                        screen.blit(click_img, (360, 321))
                                        ##################第一阶段图片绘画完成######################
                                        ##################第三阶段按钮绘画开始######################
                                        pygame.draw.rect(screen, 'black', (88, 658, 74, 74), 2)
                                        font_surface13 = font5.render('P', True, 'black')
                                        screen.blit(font_surface13, (99 + 10, 660))
                                        ########################################################
                                        pygame.draw.rect(screen, 'black', (88, 758, 74, 74), 2)
                                        font_surface13 = font5.render('Q', True, 'black')
                                        screen.blit(font_surface13, (99 + 10, 760))
                                        ########################################################
                                        pygame.draw.rect(screen, 'black', (88 + 76 + 5, 758, 74, 74), 2)
                                        font_surface13 = font5.render('×', True, 'black')
                                        screen.blit(font_surface13, (99 + 74, 760))
                                        #################第三阶段按钮绘画完成#######################
                                        font_surface13 = font2.render('*除鼠标图标之外,处于同一行的示例均同效', True, 'blue')
                                        screen.blit(font_surface13, (0, 854))
                                        font_surface13 = font2.render('视频看不清?试试', True, 'black')
                                        screen.blit(font_surface13, (900 - 12 * 15, 500 - 17))
                                        _2=0
                            if all_screen_play:
                                _2=1
                        else:
                            if video.isOpened():
                                ret, frame = video.read()
                                try:
                                    frame = numpy.rot90(frame, k=-1)
                                    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                                    frame = pygame.surfarray.make_surface(frame)
                                    frame = pygame.transform.flip(frame, False, True)
                                    frame=pygame.transform.scale(frame, (600, 400))
                                    screen.blit(frame, (300,500))
                                except:
                                    downs=False
                            for event2 in pygame.event.get():
                                #################备注阶段提示绘画开始#######################
                                pygame.draw.rect(screen, 'white', (900-4*15,500-17,60,15),0)
                                font_surface13 = font2.render('全屏播放', True, colors)
                                screen.blit(font_surface13, (900 - 4 * 15, 500 - 17))
                                #################备注阶段提示绘画完成#######################
                                if event2.type == pygame.MOUSEBUTTONDOWN:
                                    x1,y1=event2.pos
                                    if 10 <= x1 <= 80 and 8 <= y1 <= 27:
                                        whiles_click=False
                                        screen.fill((255,255,255))
                                    if 900-60 <= x1 <= 900 and 500-17 <= y1 <= 500-2:
                                        all_screen_play=True
                                        screen.fill((255,255,255))
                                if event2.type == pygame.QUIT:
                                    pygame.quit()
                                    sys.exit()
                                if event2.type == pygame.MOUSEMOTION:
                                    x2,y2=event2.pos
                                    if 900-60 <= x2 <= 900 and 500-17 <= y2 <= 500-2:
                                        colors='red'
                                        _=1
                                    else:
                                        colors='blue'
                                        _=1
                            _+=1
                        pygame.display.update()
                elif 300<=mouse_x<=540 and 645<=mouse_y<=645+40:
                    screen.fill((255,255,255))
                    pygame.mixer.music.play()
            if event.type == pygame.MOUSEMOTION:
                mouse_move_x,mouse_move_y=event.pos
                if 300 <= mouse_move_x <= 540 and 500 <= mouse_move_y <= 550 and not is_big:
                    if whiles_x != 260 and whiles_y != 70:
                        whiles_x,whiles_y=whiles_x+10,whiles_y+10
                        whiles_rect_x,whiles_rect_y=290,490
                elif 300 <= mouse_move_x <= 540 and 575 <= mouse_move_y <= 625 and not is_big:
                    if whiles_x2 != 260 and whiles_y2 != 70:
                        whiles_x2,whiles_y2=whiles_x2+10,whiles_y2+10
                        whiles_rect_x2,whiles_rect_y2=290,560
                elif 300 <= mouse_move_x <= 540 and 650 <= mouse_move_y <= 700 and not is_big:
                    if whiles_x3 != 260 and whiles_y3 != 70:
                        whiles_x3,whiles_y3=whiles_x3+10,whiles_y3+10
                        whiles_rect_x3,whiles_rect_y3=290,630
                elif 300 <= mouse_move_x <= 540 and 725 <= mouse_move_y <= 775 and not is_big:
                    if whiles_x4 != 260 and whiles_y4 != 70:
                        whiles_x4,whiles_y4=whiles_x4+10,whiles_y4+10
                        whiles_rect_x4,whiles_rect_y4=290,700
                else:
                    is_big=False
                    whiles_x, whiles_y = 240, 50
                    whiles_rect_x, whiles_rect_y = 300, 500
                    whiles_x2, whiles_y2 = 240, 50
                    whiles_rect_x2, whiles_rect_y2 = 300, 570
                    whiles_rect_x3, whiles_rect_y3 = 300, 640
                    whiles_x3, whiles_y3 = 240, 50
                    whiles_rect_x4, whiles_rect_y4 = 300, 710
                    whiles_x4, whiles_y4 = 240, 50
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        pygame.display.update()
home(True)
run()
pygame.mixer.music.stop()
pygame.mixer.music.unload()
moneyGo=False
tkinter.messagebox.showinfo("选择背景音乐","请选择您想要选的音乐。")
path=tkinter.filedialog.askopenfilename(title="选择MP3文件",filetypes=[("MP3音乐文件",".mp3")],initialdir=os.getcwd()+"\\music")
pygame.mixer.music.load(path)
pygame.mixer.music.play(-1)
while True:
    home(False)
    times = time.localtime()
    h, m, s = times.tm_hour, times.tm_min, times.tm_sec
    my=pygame.Rect(x,y,9,9)
    pygame.draw.rect(screen,(0,255,255),my,0)
    pygame.draw.rect(screen,(255,255,255),(20,0,900,40),0)
    # if sec == -1:
    #     sec=0
    # if sec<s:
    #     s1+=1
    #     sec+=1
    # elif sec!=0:
    #     s1=sec
    if s1 / 60 >0:
        m1+=int(s1/60)
        s1%=60
    if m1 / 60 <0:
        h1+=int(s1/60)
        m1%=60
    if pause:
        if sec == -1:
            sec=0
        if s-1==write_s:
            sec+=1
        if s-2==write_s:
            sec+=2
    if not pause:
        if s-s1 < 0:
            s2=s+(60-s1)
        else:
            s2=s-s1
        if s2 == 59:
            add=True
        if s-s1 == 0 and add:
            m2+=1
            add=False
        if m2 == 59:
            add=True
        if m2 % 60 == 0 and m2 != 0 and add_m:
            h2+=1
            m2%=60
            add_m=False
        font_surface = font.render('你已走的步数:{},现在是第{}关,计时:{:0>2}:{:0>2}:{:0>2},此局获得{:0>3}枚金币,{:0>3}个能量值。'.format(str(steps),str(level),str(h2),str(m2),str(s2),this_coin,this_lightning), True, 'orange')
        # h3,m3,s3=h2,m2,s2
    # else:
    #     font_surface = font.render('你已走的步数:{},现在是第{}关,计时:{:0>2}:{:0>2}:{:0>2}'.format(str(steps), str(level), str(h3), str(m3), str(s3)), True,'orange')
    # pygame.draw.rect(screen, 'white', (0,860,900,30),0)
    screen.blit(font_surface, (20, 20))
    screen.blit(font.render("截屏记录",True,(30,144,255)),(20,5))
    font_surface13 = font.render('点我使用3个能量值/300枚金币合成"钞"能力,直接通关',True,(132,112,255))
    screen.blit(font_surface13, (900-28*20, 5))
    for event in pg.event.get():
        if event.type == pg.QUIT:
            if pause:
                tkinter.messagebox.showwarning('提示','请解除暂停再重试。')
            else:
                obj=""
                with open("Maze_Game_conf_File\\data.mgcf","r",encoding='utf-8') as file:
                    ob=file.readlines()[2:]
                for i in ob:
                    obj+=i
                with open("Maze_Game_conf_File\\data.mgcf", "w", encoding='utf-8') as file:
                    file.write("{}\n{}\n".format(get_coin,get_lightning))
                    file.write(obj)
                    file.write("{}\n".format(nowsss))
                    file.write("{}×{}\n".format(grid_size[0],grid_size[1]))
                    file.write(str(level) + "\n")
                    file.write("{:0>2}:{:0>2}:{:0>2}\n".format(str(h2), str(m2), str(s2)))
                    file.write("{}\n".format(str(pauses)))
                    if len(pauses_time)!=0:
                        for i in pauses_time:
                            file.write("{:0>2}:{:0>2}:{:0>2}".format(i.split(":")[0],i.split(":")[1],i.split(":")[2])+"\n")
                    else:
                        file.write("No pause time.\n")
                    file.write("{}\n".format(steps))
                    file.write("{}\n".format(get_lightning))
                    file.write("{}\n".format(get_coin))
                    file.write("{}\n".format(this_lightning))
                    file.write("{}\n".format(this_coin))
                    file.write("{}\n".format("是" if moneyGo else "否"))
                    file.write("{}\n".format("是" if not secret else "否"))
                pg.quit()
                sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if not yes_:
                color=(175,175,175)
                yes_=True
            else:
                color=(255,255,255)
                yes_=False
            for i in maps:
                r=pygame.Rect(i[0],i[1],9,9)
                pygame.draw.rect(screen,color,r,0)
            if 20<=event.pos[0]<=80 and 5 <= event.pos[1] <= 25:
                ok_OR=tkinter.messagebox.askyesnocancel("温馨提示","温馨提示:因为截屏功能是通过虚拟窗口截屏\n,可能游戏窗口未响应几秒钟,截屏完成后会\n提示成功记录下游戏屏幕(截屏时也可以将P\nyGame窗口最小化,因为通过虚拟窗口截屏所\n以不用最大化,截屏后会将图片复制到剪切板\n,Windows11按Windows徽标键+V\n查看剪切板),是否截屏?")
                if ok_OR:
                    a=ScreenShotPyGameWindow.main()
                    tkinter.messagebox.showinfo("提示",'成功记录下PyGame屏幕!您可以从 '+os.getcwd()+' 查看图片!')
                    screenShot=True
                    ScreenShot_Time+=1
                    screen_shot_list.append(a)
                    tkinter.messagebox.showinfo(str(a))
            elif 900-25*20 <= event.pos[0] <= 900 and 5 <= event.pos[1] <= 25:
                if get_lightning>=3:
                    moneyGo=True
                    get_lightning-=3
                elif get_coin>=300:
                    moneyGo=True
                    get_coin-=300
                else:
                    tkinter.messagebox.showwarning('提示','您的能量值和金币都不足,无法合成”钞“能力。')
        elif event.type == pygame.KEYDOWN:
            has_color(screen,my,(0,0,0))
            if event.key == pygame.K_q:
                if pause:
                    tkinter.messagebox.showwarning('提示','请解除暂停再重试。')
                else:
                    obj=""
                    with open("Maze_Game_conf_File\\data.mgcf","r",encoding='utf-8') as file:
                        ob=file.readlines()[2:]
                    for i in ob:
                        obj+=i
                    with open("Maze_Game_conf_File\\data.mgcf", "w", encoding='utf-8') as file:
                        file.write("{}\n{}\n".format(get_coin,get_lightning))
                        file.write(obj)
                        file.write("{}\n".format(nowsss))
                        file.write("{}×{}\n".format(grid_size[0],grid_size[1]))
                        file.write(str(level) + "\n")
                        file.write("{:0>2}:{:0>2}:{:0>2}\n".format(str(h2), str(m2), str(s2)))
                        file.write("{}\n".format(str(pauses)))
                        if len(pauses_time)!=0:
                            for i in pauses_time:
                                file.write("{:0>2}:{:0>2}:{:0>2}".format(i.split(":")[0],i.split(":")[1],i.split(":")[2])+"\n")
                        else:
                            file.write("No pause time.\n")
                        file.write("{}\n".format(steps))
                    file.write("{}\n".format(get_lightning))
                    file.write("{}\n".format(get_coin))
                    file.write("{}\n".format(this_lightning))
                    file.write("{}\n".format(this_coin))
                    file.write("{}\n".format("是" if moneyGo else "否"))
                    file.write("{}\n".format("是" if not secret else "否"))
                    pg.quit()
                    sys.exit()
            if event.key == pygame.K_p:
                if pause:
                    pause=False
                    screen.set_alpha(255)
                    pause_time=0
                    s1+=sec
                    if s1 / 60 > 0:
                        m1+=s1/60
                        s1%=60
                    if m1 / 60 < 0:
                        h1+=m1/60
                        m1%=60
                    tts = time.localtime()
                    Ph, Pm, Ps = tts.tm_hour, tts.tm_min, tts.tm_sec
                    ones2 = Ph * 3600 + Pm * 60 + Ps
                    ones_a=ones2-ones
                    Ph2,Pm2=divmod(ones_a, 3600)
                    Pm2,Ps2=divmod(Pm2, 60)
                    pauses_time.append("{}:{}:{}".format(Ph2,Pm2,Ps2))
                    # add1=h-h3#22-22=0
                    # add2=m-m3#7-6=1
                    # add3=s-s3#0-50=-50
                    # if add3 < 0:
                    #     add2-=1#0
                    #     add3=s+(60-s3)#60-59
                    # if add2<0:
                    #     add1-=1
                    #     add2=m+(60-m3)
                    # h1+=add1
                    # m1+=add2
                    # s1+=add3
                    # if s1 / 60 > 0:
                    #     m1+=int(s1/60)
                    # if m1 / 60 > 0:
                    #     h1+=int(m1/60)
                    # s1+=sec
                else:
                    pauses+=1
                    tts = time.localtime()
                    Ph, Pm, Ps = tts.tm_hour, tts.tm_min, tts.tm_sec
                    ones=Ph*3600+Pm*60+Ps
                    pause=True
                    screen.set_alpha(100)
                    sec=0
                    # h3,m3,s3=h2,m2,s2
            if (event.key == pygame.K_LEFT or event.key == pygame.K_a) and can['left'] and not pause:
                x-=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x+16,y,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x+16,y,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    this_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
                if (mys[0],mys[1]) == secret_box and secret:
                    secret=False
                    news_lightning=random.randint(3,6)
                    news_coin=random.randint(100,300)
                    get_coin+=news_coin
                    this_coin+=news_coin
                    get_lightning+=news_lightning
                    this_lightning+=news_lightning
            elif (event.key == pygame.K_DOWN or event.key == pygame.K_s) and can['down'] and not pause:
                y+=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x,y-16,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x,y-16,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
                if (mys[0],mys[1]) == secret_box and secret:
                    secret=False
                    news_lightning=random.randint(3,6)
                    news_coin=random.randint(100,300)
                    get_coin+=news_coin
                    this_coin+=news_coin
                    get_lightning+=news_lightning
                    this_lightning+=news_lightning
            elif (event.key == pygame.K_UP or event.key == pygame.K_w) and can['up'] and not pause:
                y-=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x,y+16,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x,y+16,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    this_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
                if (mys[0],mys[1]) == secret_box and secret:
                    secret=False
                    news_lightning=random.randint(3,6)
                    news_coin=random.randint(100,300)
                    get_coin+=news_coin
                    this_coin+=news_coin
                    get_lightning+=news_lightning
                    this_lightning+=news_lightning
            elif (event.key == pygame.K_RIGHT or event.key == pygame.K_d) and can['right'] and not pause:
                x+=16
                steps+=1
                if not yes_:
                    mys=pygame.Rect(x-16,y,9,9)
                    pygame.draw.rect(screen,(255,255,255),mys,0)
                else:
                    mys=pygame.Rect(x-16,y,9,9)
                    pygame.draw.rect(screen,(175,175,175),mys,0)
                if (mys[0],mys[1]) in coin_blits:
                    get_coin+=1
                    this_coin+=1
                    while (mys[0],mys[1]) in coin_blits:
                        coin_blits.remove((mys[0],mys[1]))
                if (mys[0],mys[1]) == lightning_blits:
                    get_lightning+=1
                    this_lightning+=1
                    lightning_blits=(0,0)
                if (mys[0],mys[1]) == secret_box and secret:
                    secret=False
                    news_lightning=random.randint(3,6)
                    news_coin=random.randint(100,300)
                    get_coin+=news_coin
                    this_coin+=news_coin
                    get_lightning+=news_lightning
                    this_lightning+=news_lightning
            elif event.key==pygame.K_n:
                moneyGo=True
            maps.append((x,y))

    pygame.draw.rect(screen,'red',rect1,0)
    if rect1.colliderect(my) or moneyGo:
        # tkinter.messagebox.showinfo('提示','恭喜你,成功通过第'+str(level)+'关!')
        x=55
        y=54
        level+=1
        h1,m1,s1=h,m,s
        maps=[(55,54)]
        a=True
        b=False
        blues=False
        reds=False
        screen.fill((255,255,255))
        obj=""
        with open("Maze_Game_conf_File\\data.mgcf","r",encoding='utf-8') as file:
            ob=file.readlines()[2:]
        for i in ob:
            obj+=i
        if moneyGo:
            get_coin=get_coin-int(this_coin/2)
        with open("Maze_Game_conf_File\\data.mgcf", "w", encoding='utf-8') as file:
            file.write("{}\n{}\n".format(get_coin,get_lightning))
            file.write(obj)
            file.write("{}\n".format(nowsss))
            file.write("{}×{}\n".format(grid_size[0],grid_size[1]))
            file.write(str(level)+"\n")
            file.write("{:0>2}:{:0>2}:{:0>2}\n".format(str(h2),str(m2),str(s2)))
            file.write("{}\n".format(str(pauses)))
            if len(pauses_time)!=0:
                for i in pauses_time:
                    file.write("{:0>2}:{:0>2}:{:0>2}".format(i.split(":")[0],i.split(":")[1],i.split(":")[2])+"\n")
            else:
                file.write("No pause time.\n")
            file.write("{}\n".format(steps))
            file.write("{}\n".format(get_lightning))
            file.write("{}\n".format(get_coin))
            file.write("{}\n".format(this_lightning))
            file.write("{}\n".format(this_coin))
            file.write("{}\n".format("是" if moneyGo else "否"))
            file.write("{}\n".format("是" if not secret else "否"))
        screen.blit(font8.render("成功通过第{}关!".format(level-1),True,(255,0,0)),((900-(7+len(list(str(level))))*30)/2,200))
        screen.blit(font7.render("耗时:{:0>2}:{:0>2}:{:0>2}".format(h2,m2,s2),True,"#48D1CC"),(int((900-(3+len(list(str(h2)))+len(list(str(m2)))+len(list(str(s2))))*30)/2),280+10))
        screen.blit(font7.render("获得金币数:{}".format(int(this_coin / (1 + moneyGo))), True, "#48D1CC"), (int((900 - (3 + len(list(str(get_coin)))) * 30) / 2), 280 + 30 * 2))
        screen.blit(font7.render("获得能量值:{}".format(this_lightning),True,"#48D1CC"),(int((900-(3+len(list(str(get_lightning))))*30)/2),280+30*4))
        screen.blit(font7.render("步数:{}".format(steps),True,"#48D1CC"),(int((900-(3+len(list(str(steps))))*30)/2),280+30*6))

        use_xiang_su=len(list(str(ScreenShot_Time)))*15+5*30+540
        use_xiang_su2=use_xiang_su+120
        color_of_show='blue'
        screen.blit(pygame.transform.scale(J,(120,120)),(0,570))

        while a:
            if screenShot:
                screen.blit(font7.render("点击查看",True,color_of_show),(use_xiang_su,780))
            else:
                screen.blit(font7.render("~没有记录精彩瞬间哦,下次探索新功能~",True,color_of_show),(180,690))
            for event4 in pygame.event.get():
                if b:
                    b=False
                xx,yy=pygame.mouse.get_pos()
                if event4.type == pygame.MOUSEBUTTONDOWN:
                    if 850<=yy<=890:
                        if 20<=xx<=440:
                            a=False
                            home(True)
                        elif 450<=xx<=450+420:
                            a=False
                    if use_xiang_su <= xx <= use_xiang_su2 and 780 <= yy <= 810 and screenShot:
                        os.system("explorer "+os.getcwd()+"\\Maze_Game_conf_File\\ScreenShot")
                elif event4.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit(0)
                if event4.type == pygame.MOUSEMOTION:
                    pygame.draw.rect(screen,(255,255,255),(0,900-45-20,900,70))
                    if use_xiang_su<=xx<=use_xiang_su2 and 780<=yy<=810 and screenShot:
                        color_of_show='red'
                    else:
                        color_of_show='blue'
                    if 850 <= yy <= 890:
                        if 20 <= xx <= 440:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", "#00BFFF", 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                            screen.blit(font7.render("↙返回主页", True, "white"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            b=True
                            blues=True
                            reds=False
                        else:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                            screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            blues=False
                        if 450 <= xx <= 450 + 420:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 0, 0), 4)
                            screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "white"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            b=True
                            reds=True
                        else:
                            draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                            draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                            screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                            screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                            blues=False
                if reds:
                    draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                    draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 0, 0), 0)
                    screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                    screen.blit(font7.render("下一关↘", True, "white"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                if blues:
                    draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", "#00BFFF", 0)
                    draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                    screen.blit(font7.render("↙返回主页", True, "white"), (20 + 200-50, 900 - 55 + 10))
                    screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                if not b:
                    draw_button(screen, (20, 900 - 50), 420, 40, "#00BFFF", (255, 255, 255), 4)
                    draw_button(screen, (20 + 20 + 420, 900 - 50), 420, 40, (255, 0, 0), (255, 255, 255), 4)
                    screen.blit(font7.render("↙返回主页", True, "#00BFFF"), (20 + 200-50, 900 - 55 + 10))
                    screen.blit(font7.render("下一关↘", True, "red"), (20 + 420 + 20 + 200 - 30, 900 - 55 + 10))
                    pygame.draw.circle(screen,(255,255,255),(20 + 20 + 420+20, 900 - 50+20),16)
                    pygame.draw.circle(screen,(255,255,255),(20 + 20 + 400+420, 900 - 50+20),16)
                    pygame.draw.circle(screen,(255,255,255),(20+20, 900 - 50+20),16)
                    pygame.draw.circle(screen,(255,255,255),(20+400, 900 - 50+20),16)
            pygame.display.update()
        steps=0
        get_level+=1
        moneyGo=False
        h2=m2=s2=0
        screen_shot_list=[]
        screenShot=False
        ScreenShot_Time=0
        run()
    write_s=s
    pygame.display.update()

GetWindowNumber.py

import win32gui
hwnd_title = {}
def get_all_hwnd(hwnd, mouse):
    if (win32gui.IsWindow(hwnd)
            and win32gui.IsWindowEnabled(hwnd)
            and win32gui.IsWindowVisible(hwnd)):
        hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})
def main():
    win32gui.EnumWindows(get_all_hwnd, 0)

ScreenShotPyGameWindow.py

"""
@author: 王俞励
"""
import time
import win32gui, win32ui, win32con, win32api
import GetWindowNumber
import win32clipboard as clip
from PIL import Image
from ctypes import *
import os,io
def copy(imagepath):
    img = Image.open(imagepath)
    output = io.BytesIO()
    img.convert("RGB").save(output, "BMP")
    data = output.getvalue()[14:]
    clip.OpenClipboard()
    clip.EmptyClipboard()
    clip.SetClipboardData(win32con.CF_DIB, data)  # 图片
    clip.CloseClipboard()
def window_capture(filename,find):
    GetWindowNumber.main()
    num=0
    for k,v in GetWindowNumber.hwnd_title.items():
        if v and v == find:
            num=k
    hwnd = num  # 窗口的编号,0号表示当前活跃窗口
    # 根据窗口句柄获取窗口的设备上下文DC(Divice Context)
    hwndDC = win32gui.GetDC(hwnd)
    # 根据窗口的DC获取mfcDC
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    # mfcDC创建可兼容的DC
    saveDC = mfcDC.CreateCompatibleDC()
    # 创建bigmap准备保存图片
    saveBitMap = win32ui.CreateBitmap()
    # 获取监控器信息
    MoniterDev = win32api.EnumDisplayMonitors(None, None)
    w = MoniterDev[0][2][3]
    h = MoniterDev[0][2][3]
    # print w,h   #图片大小
    # 为bitmap开辟空间
    saveBitMap.CreateCompatibleBitmap(mfcDC, 900, 900)
    # 高度saveDC,将截图保存到saveBitmap中
    saveDC.SelectObject(saveBitMap)
    # 截取从左上角(0,0)长宽为(w,h)的图片
    saveDC.BitBlt((0, 0), (900, 900), mfcDC, (0, 0), win32con.SRCCOPY)
    fuben_NUM=2
    filename2=filename
    while os.path.exists(os.getcwd()+"\\"+filename2):
        filename2=filename.split(".")[0]+f"({fuben_NUM})"+"."+filename.split(".")[1]
        fuben_NUM+=1
    saveBitMap.SaveBitmapFile(saveDC, filename2)
    copy(filename)
    return fuben_NUM-1
def main():
    return window_capture("Maze_Game_Screen_Shot.png","Maze Game's window, version 1.0.2")

 音乐文件

音乐文件的话,要的读者可以到https://github.com/WangYuli2/Maze-Game里拿。

项目总结

通过这个比较简易的小游戏,我们初步掌握了pygame的一些基本知识,并运用到了实战:迷宫游戏中。相信读者也收获到了知识,希望在今后的学习中依然保持这样的心态。

建议

我们这个做的小游戏还很简易,读者还可以加上障碍,例如火龙啊等等,碰到火龙就要从新开始,或者用金币或者能量值使用超能力恢复到上次被火龙“吃”掉的位置,等等。希望读者从今后的学习中掌握实战经验,巩固学习知识,再创新的佳绩!本篇我的分享就到这儿了,谢谢大家的耐心阅读。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值