欢迎来到
银狐的个人博客

【Pygame小游戏】Python版有迷宫嘛?原来藏在个地方呀~

💦前言

大家好,是我:

一大早高清无码好东西刚到手

就给大家安排推文的栗子同学上线·放牛小编——一切随缘更新+慢更新的我!

决心痛改前非,每周争取能多写几篇。(这个flag立在这里,完不成……那就算了!)

大家有时候会和朋友聊聊孩子,说起能让孩子心无旁骛的制胜法宝,我想大家应该一致认为“迷宫”

必须当选其一👏。迷宫类游戏也是大家闲暇实践之中消磨时间的法宝。

嘿!boys  and   girs——今天栗子给大家带来一波有趣好玩的走迷宫小程序。

💦 正文

游戏规则:顾名思义,玩家出现在迷宫的外面,进入之后要在很多不同的路中间找到一条可以抵达

终点的路,可以发反复的尝试,能走到终点就是胜利,当人你如果在迷宫中迷路了,就走不出来会

失败。迷宫游戏则倾向于吸引那些更有兴趣解决谜题和迎接挑战的人群。

1)需要做哪些准备呢?

本文的游戏迷宫是2个单独的迷宫组合而成的,一个简单的迷宫,一个难的迷宫。

环境安装的话:Python3、Pycharm、Pygame模块。

模块安装:pip  install  pygame

2)代码(仅部分) 简易版主程序:

import pygameimport sysimport randomimport timefrom pygame.locals import *from random import randint, choiceimport mappimport color# 设置屏幕宽度和高度为全局变量global screen_widthscreen_width = 800global screen_heightscreen_height = 800room_size = 15 # 每个房间的大小steps = 0# 输出文本信息def print_text(font, x, y, text, color, shadow=True):    if shadow:        imgText = font.render(text, True, (0, 0, 0))        screen.blit(imgText, (x-2,y-2))    imgText = font.render(text, True, color)    screen.blit(imgText, (x,y))# 存储迷宫r_list = mapp.map_list# 游戏开始if __name__ == '__main__':    # 初始化 Pygame    pygame.init()    screen = pygame.display.set_mode([screen_width, screen_height])    pygame.display.set_caption('Maze_AI——by Wonz')    global font1, font2, font3    clock = pygame.time.Clock()    fps = 20    screen.fill(color.White)    # 加载角色照片    user = pygame.image.load("user.png").convert_alpha()    width, height = user.get_size()    user = pygame.transform.smoothscale(user, (8, 8))    # draw the user    width, height = user.get_size()    x = 25 + 42 * room_size    y = 25 + 9 * room_size    roomx = 42    roomy = 9    screen.blit(user, (x, y))    # 画迷宫    for i in range(43):        for j in range(42):            if (r_list[j][i] == 3):                pygame.draw.circle(screen, color.Red, [30 + i * room_size, 30 + j * room_size], 5, 0)                pygame.display.flip()                r_list[j][i] = 0            elif (r_list[j][i] == 1):                # 画10*10的矩形,线宽为1,这里不能是0,因为10*10无空白区域                pygame.draw.rect(screen, color.Black, [25 + i * room_size, 25 + j * room_size, 10, 10], 0)                pygame.display.flip()            elif (r_list[j][i] == 0):                pygame.draw.rect(screen, color.White, [25 + i * room_size, 25 + j * room_size, 10, 10], 1)                pygame.display.flip()    while True:        for event in pygame.event.get():            if event.type == pygame.QUIT:                sys.exit()            # 走到终点            elif(roomx == 0 and roomy == 9):                font3 = pygame.font.Font(None, 32)                print_text(font3, 350, 350, "Win", color.Red)                break            elif event.type == pygame.KEYDOWN:                if event.key == pygame.K_RIGHT:                    # 右边无墙                    if(0 <= roomx < 42 and 0 <= roomy <= 41 and r_list[roomy][roomx+1] == 0):                        x += room_size                        roomx += 1 # 计房间数                        steps += 1 # 计步                        font1 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25)) # x:25 y:0 width:200 height:25                        screen.fill(color.White, (300, 0, 200, 25))  # x:300 y:0 width:200 height:25                        print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)                        pygame.display.flip()                        screen.fill(color.White, (x-room_size, y, 10, 10))                        screen.blit(user, (x, y))                    # 右边有墙                    elif (0 <= roomx < 42 and 0 <= roomy <= 41 and r_list[roomy][roomx+1] == 1):                        steps += 1                        font1 = pygame.font.Font(None, 32)                        font2 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)                        print_text(font2, 350, 0, "This is a wall!", color.Black)                        pygame.display.flip()                        screen.blit(user, (x, y))                elif event.key == pygame.K_LEFT:                    # 左边无墙                    if (0 < roomx <= 42 and 0 <= roomy <= 41 and r_list[roomy][roomx-1] == 0):                        x -= room_size                        roomx -= 1                        steps += 1                        font1 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        screen.fill(color.White, (300, 0, 200, 25))  # x:300 y:0 width:200 height:25                        print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)                        pygame.display.flip()                        screen.fill(color.White, (x+room_size, y, 10, 10))                        screen.blit(user, (x, y))                    # 左边有墙                    elif (0 < roomx <= 42 and 0 <= roomy <= 41 and r_list[roomy][roomx-1] == 1):                        steps += 1                        font1 = pygame.font.Font(None, 32)                        font2 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)                        print_text(font2, 350, 0, "This is a wall!", color.Black)                        pygame.display.flip()                        screen.blit(user, (x, y))                elif event.key == pygame.K_UP:                    # 上边无墙                    if (0 <= roomx <= 42 and 0 < roomy <= 41 and r_list[roomy-1][roomx] == 0):                        y -= room_size                        roomy -= 1                        steps += 1                        font1 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        screen.fill(color.White, (300, 0, 200, 25))  # x:300 y:0 width:200 height:25                        print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)                        pygame.display.flip()                        screen.fill(color.White, (x, y+room_size, 10, 10))                        screen.blit(user, (x, y))                    # 上边有墙                    elif (0 <= roomx <= 42 and 0 < roomy <= 41 and r_list[roomy-1][roomx] == 1):                        steps += 1                        font1 = pygame.font.Font(None, 32)                        font2 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)                        print_text(font2, 350, 0, "This is a wall!", color.Black)                        pygame.display.flip()                        screen.blit(user, (x, y))                elif event.key == pygame.K_DOWN:                    # 下边无墙                    if (0 <= roomx <= 42 and 0 <= roomy < 41 and r_list[roomy+1][roomx] == 0):                        y += room_size                        roomy += 1                        steps += 1                        font1 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        screen.fill(color.White, (300, 0, 200, 25))  # x:300 y:0 width:200 height:25                        print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)                        pygame.display.flip()                        screen.fill(color.White, (x, y-room_size, 10, 10))                        screen.blit(user, (x, y))                    # 下边无墙                    elif (0 <= roomx <= 42 and 0 <= roomy < 41 and r_list[roomy+1][roomx] == 1):                        steps += 1                        font1 = pygame.font.Font(None, 32)                        font2 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)                        print_text(font2, 350, 0, "This is a wall!", color.Black)                        pygame.display.flip()                        screen.blit(user, (x, y))        # 更新屏幕        pygame.display.update()

难版迷宫主程序:

import pygameimport sysimport randomimport timefrom pygame.locals import *from random import randint, choiceimport mazeimport color# 设置屏幕宽度和高度为全局变量global screen_widthscreen_width = 800global screen_heightscreen_height = 600# 输出文本信息def print_text(font, x, y, text, color, shadow=True):    if shadow:        imgText = font.render(text, True, (0, 0, 0))        screen.blit(imgText, (x-2,y-2))    imgText = font.render(text, True, color)    screen.blit(imgText, (x,y))# 游戏开始if __name__ == '__main__':    pygame.init()    screen = pygame.display.set_mode([screen_width, screen_height])    pygame.display.set_caption('Maze_AI——by Wonz') # 游戏标题    global font1, font2, font3, font4 # 文字    clock = pygame.time.Clock()    fps = 20    screen.fill(color.White)    r_list = maze.room.creat_map(maze.room_m, maze.room_n)    begin_point = [0, 0]    begin_room = r_list[0][0]    maze.room.creat_migong(r_list, begin_room)    # 画出去起点和终点的其他点    for i in range(maze.room_m):        for j in range(maze.room_n):            begin_point[0] = 25 + i * maze.room_size            begin_point[1] = 25 + j * maze.room_size            r_color = color.Black            maze.room.draw_room(screen, begin_point, r_list[i][j].walls, maze.room_size, r_color)    # 画起点    maze.room.draw_room(screen, [25, 25], [0, 0, 0, 1], maze.room_size, color.White)    # 画终点    maze.room.draw_room(screen, [25 + (maze.room_m - 1) * maze.room_size, 25 + (maze.room_n - 1) * maze.room_size],                        [0, 1, 0, 0], maze.room_size, color.White)    # 加载角色照片    user = pygame.image.load("user.png").convert_alpha()    width,height = user.get_size()    user = pygame.transform.smoothscale(user, (8,8))    # 画角色    width, height = user.get_size()    x = 27    y = 30    roomx = 0    roomy = 0    screen.blit(user, (x, y))    # 键盘控制角色移动    while True:        for event in pygame.event.get():            if event.type == pygame.QUIT:                sys.exit()            # 走到终点            elif(roomx == maze.room_m-1 and roomy == maze.room_n-1):                font4 = pygame.font.Font(None, 32)                print_text(font4, 350, 350, "Win" , color.Red)                break            # 键盘响应,只取按“→”键作为例子,“↑”、“↓”、“←”类似,只要改改参数即可            elif event.type == pygame.KEYDOWN:                if event.key == pygame.K_RIGHT:                    # 右边无墙                    if(r_list[roomx][roomy].walls[1] == False):                        x += maze.room_size                        roomx += 1 # 计房间数                        maze.steps += 1 # 计步                        font1 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25)) # x:25 y:0 width:200 height:25                        screen.fill(color.White, (300, 0, 200, 25))  # x:300 y:0 width:200 height:25                        print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)                        pygame.display.flip()                        screen.fill(color.White, (x-15,y,10,10))                        screen.blit(user, (x, y))                    # 右边有墙                    elif (r_list[roomx][roomy].walls[1] == True):                        maze.steps += 1                        font1 = pygame.font.Font(None, 32)                        font2 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))  # x:25 y:0 width:200 height:25                        print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)                        print_text(font2, 350, 0, "This is a wall!", color.Black)                        pygame.display.flip()                        screen.blit(user, (x, y))                elif event.key == pygame.K_LEFT:                    # 左边无墙                    if (r_list[roomx][roomy].walls[3] == False):                        x -= maze.room_size                        roomx -= 1                        maze.steps += 1                        font1 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        screen.fill(color.White, (300, 0, 200, 25))  # x:300 y:0 width:200 height:25                        print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)                        pygame.display.flip()                        screen.fill(color.White, (x+15,y,10,10))                        screen.blit(user, (x, y))                    # 左边有墙                    elif (r_list[roomx][roomy].walls[3] == True):                        maze.steps += 1                        font1 = pygame.font.Font(None, 32)                        font2 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)                        print_text(font2, 350, 0, "This is a wall!", color.Black)                        pygame.display.flip()                        screen.blit(user, (x, y))                elif event.key == pygame.K_UP:                    # 上边无墙                    if (r_list[roomx][roomy].walls[0] == False):                        y -= maze.room_size                        roomy -= 1                        maze.steps += 1                        font1 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        screen.fill(color.White, (300, 0, 200, 25))  # x:300 y:0 width:200 height:25                        print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)                        pygame.display.flip() 【Pygame小游戏】Python版有迷宫嘛?原来藏在个地方呀~ 第1张图片-银狐博客                       screen.fill(color.White, (x,y+15,10,10))                        screen.blit(user, (x, y))                    # 上边有墙                    elif (r_list[roomx][roomy].walls[0] == True):                        maze.steps += 1                        font1 = pygame.font.Font(None, 32)                        font2 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)                        print_text(font2, 350, 0, "This is a wall!", color.Black)                        pygame.display.flip()                        screen.blit(user, (x, y))                elif event.key == pygame.K_DOWN:                    # 下边无墙                    if (r_list[roomx][roomy].walls[2] == False):                        y += maze.room_size                        roomy += 1                        maze.steps += 1                        font1 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        screen.fill(color.White, (300, 0, 200, 25))  # x:300 y:0 width:200 height:25                        print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)                        pygame.display.flip()                        screen.fill(color.White, (x,y-15,10,10))                        screen.blit(user, (x, y))                    # 下边无墙                    elif (r_list[roomx][roomy].walls[2] == True):                        maze.steps += 1                        font1 = pygame.font.Font(None, 32)                        font2 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        print_text(font1, 25, 0, "Steps:" + str(maze.steps), color.Black)                        print_text(font2, 350, 0, "This is a wall!", color.Black)                        pygame.display.flip()                        screen.blit(user, (x, y))            # 鼠标响应,执行 AI 程序            elif event.type == pygame.MOUSEBUTTONUP:                font3 = pygame.font.Font(None, 32)                print_text(font3, 750, 0, "AI", colo【Pygame小游戏】Python版有迷宫嘛?原来藏在个地方呀~ 第2张图片-银狐博客r.Red)                pygame.display.flip()                start_x = 0                start_y = 0                steps = 0                for i in range(1000000000):                    if (start_x == maze.room_m-1 and start_y == maze.room_n-1):                        font4 = pygame.font.Font(None, 32)                        print_text(font4, 350, 350, "Win", color.Red)                        break                    d = random.randint(1,4)                    # 上边无墙                    if (d == 1 and 0 <= start_x <= maze.room_m - 1 and 0 <= start_y <= maze.room_n - 1 and                            r_list[start_x][start_y].walls[0] == False): # 在迷宫地图范围内且上边无墙                        start_y -= 1                        steps += 1                        font1 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))  # x:25 y:0 width:200 height:25                        screen.fill(color.White, (300, 0, 200, 25))  # x:300 y:0 width:200 height:25                        print_text(font1, 25, 0, "Steps:" + str(steps), color.Black) # 步数统计                        pygame.display.flip()                        screen.blit(user, ((start_x+2) * maze.room_size, (start_y+2) * maze.room_size))                    # 右边无墙                    if (d == 2 and 0 <= start_x <= maze.room_m - 1 and 0 <= start_y <= maze.room_n - 1 and                            r_list[start_x][start_y].walls[1] == False):                        start_x += 1                        steps += 1                        font1 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        screen.fill(color.White, (300, 0, 200, 25))                        print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)                        pygame.display.flip()                        screen.blit(user, ((start_x+2) * maze.room_size, (start_y+2) * maze.room_size))                    # 下边无墙                    if (d == 3 and 0 <= start_x <= maze.room_m - 1 and 0 <= start_y <= maze.room_n - 1 and                            r_list[start_x][start_y].walls[2] == False):                        start_y += 1                        steps += 1                        font1 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        screen.fill(color.White, (300, 0, 200, 25))                        print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)                        pygame.display.flip()                        screen.blit(user, ((start_x+2) * maze.room_size, (start_y+2) * maze.room_size))                    # 左边无墙                    if (d == 4 and 0 <= start_x <= maze.room_m - 1 and 0 <= start_y <= maze.room_n - 1 and                            r_list[start_x][start_y].walls[3] == False):                        start_x -= 1                        steps += 1                        font1 = pygame.font.Font(None, 32)                        screen.fill(color.White, (25, 0, 200, 25))                        screen.fill(color.White, (300, 0, 200, 25))                        print_text(font1, 25, 0, "Steps:" + str(steps), color.Black)                        pygame.display.flip()                        screen.blit(user, ((start_x+2) * maze.room_size, (start_y+2) * maze.room_size))        # 更新屏幕        pygame.display.update()

3)效果展示

简易版——

 难版——

 哈哈哈~栗子很好滴  2个版本的路线都给大家标出来啦,不会的跟着走不会错哦!

💦结尾

悄悄告诉你,这条迷宫我是一眼就看出来了173步就可以完美走出来哦。你来试试嘛?

怎么样,挑战成功了吗?

完整的项目源码免费领:关注小编公众号:Python顾木子吖

  

 如果你喜欢,请“点赞”  “关注”  “评论”给我小心心吧 

 

赞(0) 打赏
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《【Pygame小游戏】Python版有迷宫嘛?原来藏在个地方呀~》
文章链接:https://www.yinhu3.com/1758.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
如果文章侵犯到你的权益,请查看本站免责声明:《免责声明》

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

愿意请我喝杯矿泉水吗

支付宝扫一扫打赏