飞道的博客

我半夜爬起来,用python给失眠的隔壁小姐姐:写了一个迷宫小游戏~~~

365人阅读  评论(0)

大家好,我是Lex 喜欢欺负超人那个Lex

划重点:

1、python开发小游戏,pygame环境搭建;

2、给失眠的小姐姐开发一个迷宫小游戏。

代码干货满满,建议收藏+实操!!!有问题及需要,请留言哦~~

事情是这样的

半夜听到隔壁来来回回 ,忙忙碌碌的声音

我这么心思细腻 体贴入微的python小哥哥

敏锐的感觉到

小姐姐肯定是失眠了

好担心哦,我都睡不着了呢

辗转反侧

最后爬起来撸出了我的python代码

 

一、环境要求


  
  1. windows系统,python3.6+
  2. 安装游戏依赖模块
  3. pip install pyqt5
  4. pip install pygame

二、游戏介绍

1、游戏目标

随机生成一张迷宫地图,将玩家设置在迷宫内部,通过光标 上 下 左 右,来移动玩家,按照迷宫地图的道路来走出迷宫。

 

2、先上游戏效果图

 

三、完整开发流程

1、项目主结构

首先,先整理一下项目的主结构,其实看一下主结构,基本就清晰了


  
  1. modules:存放自己写的 python
  2. —— mazes .py
  3. —— misc .py
  4. —— sprites .py
  5. resources:存放引用到的图片、音频等等
  6. —— audios:音频资源
  7. —— images:图片资源
  8. config .py:为主配置文件
  9. maze .py:主程序文件
  10. requirements .txt:需要引入的 python依赖包

 

2、详细配置

config.py

配置文件中,需要引入os模块,并且配置打开游戏的屏幕大小,并将资源中引用到的图片、音频插入到合适的位置。

因为我们的迷宫游戏,需要划开模块。


  
  1. '' '配置文件' ''
  2. import os
  3. '' '屏幕大小' ''
  4. SCREENSIZE = (800, 625)
  5. '' '游戏素材' ''
  6. BGMPATH = os.path.join(os.getcwd(), 'resources/audios/bgm.mp3')
  7. HEROPICPATH = os.path.join(os.getcwd(), 'resources/images/hero.png')
  8. '' 'FPS' ''
  9. FPS = 20
  10. '' '块大小' ''
  11. BLOCKSIZE = 15
  12. MAZESIZE = (35, 50) # num_rows * num_cols
  13. BORDERSIZE = (25, 50) # 25 * 2 + 50 * 15 = 800, 50 * 2 + 35 * 15 = 625

 

3、随机生成迷宫地图

mazes.py

迷宫虽然是个小游戏,但是我们每次打开,进入 地图需要随机生成一个新地图。

定义randommaze 随机生成地图,并将地图投在主游戏屏幕上


  
  1. '''
  2. Function:
  3. 随机生成迷宫
  4. Author:
  5. lexsaints
  6. '''
  7. import pygame
  8. import random
  9. from .misc import *
  10. '''一个游戏地图块'''
  11. class Block():
  12. def __init__(self, coordinate, block_size, border_size, **kwargs):
  13. # (col, row)
  14. self.coordinate = coordinate
  15. self.block_size = block_size
  16. self.border_size = border_size
  17. self.is_visited = False
  18. # 上下左右有没有墙
  19. self.has_walls = [ True, True, True, True]
  20. self.color = ( 0, 0, 0)
  21. '''画到屏幕上'''
  22. def draw(self, screen):
  23. directions = [ 'top', 'bottom', 'left', 'right']
  24. for idx, direction in enumerate(directions):
  25. if self.has_walls[idx]:
  26. if direction == 'top':
  27. x1 = self.coordinate[ 0] * self.block_size + self.border_size[ 0]
  28. y1 = self.coordinate[ 1] * self.block_size + self.border_size[ 1]
  29. x2 = (self.coordinate[ 0] + 1) * self.block_size + self.border_size[ 0]
  30. y2 = self.coordinate[ 1] * self.block_size + self.border_size[ 1]
  31. pygame.draw.line(screen, self.color, (x1, y1), (x2, y2))
  32. elif direction == 'bottom':
  33. x1 = self.coordinate[ 0] * self.block_size + self.border_size[ 0]
  34. y1 = (self.coordinate[ 1] + 1) * self.block_size + self.border_size[ 1]
  35. x2 = (self.coordinate[ 0] + 1) * self.block_size + self.border_size[ 0]
  36. y2 = (self.coordinate[ 1] + 1) * self.block_size + self.border_size[ 1]
  37. pygame.draw.line(screen, self.color, (x1, y1), (x2, y2))
  38. elif direction == 'left':
  39. x1 = self.coordinate[ 0] * self.block_size + self.border_size[ 0]
  40. y1 = self.coordinate[ 1] * self.block_size + self.border_size[ 1]
  41. x2 = self.coordinate[ 0] * self.block_size + self.border_size[ 0]
  42. y2 = (self.coordinate[ 1] + 1) * self.block_size + self.border_size[ 1]
  43. pygame.draw.line(screen, self.color, (x1, y1), (x2, y2))
  44. elif direction == 'right':
  45. x1 = (self.coordinate[ 0] + 1) * self.block_size + self.border_size[ 0]
  46. y1 = self.coordinate[ 1] * self.block_size + self.border_size[ 1]
  47. x2 = (self.coordinate[ 0] + 1) * self.block_size + self.border_size[ 0]
  48. y2 = (self.coordinate[ 1] + 1) * self.block_size + self.border_size[ 1]
  49. pygame.draw.line(screen, self.color, (x1, y1), (x2, y2))
  50. return True
  51. '''随机生成迷宫类'''
  52. class RandomMaze():
  53. def __init__(self, maze_size, block_size, border_size, **kwargs):
  54. self.block_size = block_size
  55. self.border_size = border_size
  56. self.maze_size = maze_size
  57. self.blocks_list = RandomMaze.createMaze(maze_size, block_size, border_size)
  58. self.font = pygame.font.SysFont( 'Consolas', 15)
  59. '''画到屏幕上'''
  60. def draw(self, screen):
  61. for row in range(self.maze_size[ 0]):
  62. for col in range(self.maze_size[ 1]):
  63. self.blocks_list[row][col].draw(screen)
  64. # 起点和终点标志
  65. showText(screen, self.font, 'S', ( 255, 0, 0), (self.border_size[ 0] -10, self.border_size[ 1]))
  66. showText(screen, self.font, 'D', ( 255, 0, 0), (self.border_size[ 0]+(self.maze_size[ 1] -1)*self.block_size, self.border_size[ 1]+self.maze_size[ 0]*self.block_size+ 5))
  67. '''创建迷宫'''
  68. @staticmethod
  69. def createMaze(maze_size, block_size, border_size):
  70. def nextBlock(block_now, blocks_list):
  71. directions = [ 'top', 'bottom', 'left', 'right']
  72. blocks_around = dict(zip(directions, [ None]* 4))
  73. block_next = None
  74. count = 0
  75. # 查看上边block
  76. if block_now.coordinate[ 1] -1 >= 0:
  77. block_now_top = blocks_list[block_now.coordinate[ 1] -1][block_now.coordinate[ 0]]
  78. if not block_now_top.is_visited:
  79. blocks_around[ 'top'] = block_now_top
  80. count += 1
  81. # 查看下边block
  82. if block_now.coordinate[ 1]+ 1 < maze_size[ 0]:
  83. block_now_bottom = blocks_list[block_now.coordinate[ 1]+ 1][block_now.coordinate[ 0]]
  84. if not block_now_bottom.is_visited:
  85. blocks_around[ 'bottom'] = block_now_bottom
  86. count += 1
  87. # 查看左边block
  88. if block_now.coordinate[ 0] -1 >= 0:
  89. block_now_left = blocks_list[block_now.coordinate[ 1]][block_now.coordinate[ 0] -1]
  90. if not block_now_left.is_visited:
  91. blocks_around[ 'left'] = block_now_left
  92. count += 1
  93. # 查看右边block
  94. if block_now.coordinate[ 0]+ 1 < maze_size[ 1]:
  95. block_now_right = blocks_list[block_now.coordinate[ 1]][block_now.coordinate[ 0]+ 1]
  96. if not block_now_right.is_visited:
  97. blocks_around[ 'right'] = block_now_right
  98. count += 1
  99. if count > 0:
  100. while True:
  101. direction = random.choice(directions)
  102. if blocks_around.get(direction):
  103. block_next = blocks_around.get(direction)
  104. if direction == 'top':
  105. block_next.has_walls[ 1] = False
  106. block_now.has_walls[ 0] = False
  107. elif direction == 'bottom':
  108. block_next.has_walls[ 0] = False
  109. block_now.has_walls[ 1] = False
  110. elif direction == 'left':
  111. block_next.has_walls[ 3] = False
  112. block_now.has_walls[ 2] = False
  113. elif direction == 'right':
  114. block_next.has_walls[ 2] = False
  115. block_now.has_walls[ 3] = False
  116. break
  117. return block_next
  118. blocks_list = [[Block([col, row], block_size, border_size) for col in range(maze_size[ 1])] for row in range(maze_size[ 0])]
  119. block_now = blocks_list[ 0][ 0]
  120. records = []
  121. while True:
  122. if block_now:
  123. if not block_now.is_visited:
  124. block_now.is_visited = True
  125. records.append(block_now)
  126. block_now = nextBlock(block_now, blocks_list)
  127. else:
  128. block_now = records.pop()
  129. if len(records) == 0:
  130. break
  131. return blocks_list

4、光标控制玩家

misc.py

通过读取键盘的上下左右光标来移动我们的小可爱


  
  1. '''
  2. Function:
  3. 定义其他必要模块
  4. Author:
  5. lexsaints
  6. '''
  7. import sys
  8. import pygame
  9. '''在屏幕指定位置显示文字'''
  10. def showText(screen, font, text, color, position):
  11. text_render = font.render(text, True, color)
  12. rect = text_render.get_rect()
  13. rect.left, rect.top = position
  14. screen.blit(text_render, rect)
  15. return rect.right
  16. '''按钮'''
  17. def Button(screen, position, text, font, buttoncolor=(120, 120, 120), linecolor=(20, 20, 20), textcolor=(255, 255, 255), bwidth=200, bheight=50):
  18. left, top = position
  19. pygame.draw.line(screen, linecolor, (left, top), (left+bwidth, top), 5)
  20. pygame.draw.line(screen, linecolor, (left, top -2), (left, top+bheight), 5)
  21. pygame.draw.line(screen, linecolor, (left, top+bheight), (left+bwidth, top+bheight), 5)
  22. pygame.draw.line(screen, linecolor, (left+bwidth, top+bheight), (left+bwidth, top), 5)
  23. pygame.draw.rect(screen, buttoncolor, (left, top, bwidth, bheight))
  24. text_render = font.render(text, 1, textcolor)
  25. rect = text_render.get_rect()
  26. rect.centerx, rect.centery = left + bwidth / 2, top + bheight / 2
  27. return screen.blit(text_render, rect)
  28. '''游戏开始/关卡切换/游戏结束界面'''
  29. def Interface(screen, config, mode='game_start'):
  30. pygame.display.set_mode(config.SCREENSIZE)
  31. font = pygame.font.SysFont( 'Consolas', 30)
  32. if mode == 'game_start':
  33. clock = pygame.time.Clock()
  34. while True:
  35. screen.fill(( 192, 192, 192))
  36. button_1 = Button(screen, ((config.SCREENSIZE[ 0] -200)// 2, config.SCREENSIZE[ 1]// 3), 'START', font)
  37. button_2 = Button(screen, ((config.SCREENSIZE[ 0] -200)// 2, config.SCREENSIZE[ 1]// 2), 'QUIT', font)
  38. for event in pygame.event.get():
  39. if event.type == pygame.QUIT:
  40. pygame.quit()
  41. sys.exit( -1)
  42. elif event.type == pygame.MOUSEBUTTONDOWN:
  43. if button_1.collidepoint(pygame.mouse.get_pos()):
  44. return True
  45. elif button_2.collidepoint(pygame.mouse.get_pos()):
  46. pygame.quit()
  47. sys.exit( -1)
  48. pygame.display.update()
  49. clock.tick(config.FPS)
  50. elif mode == 'game_switch':
  51. clock = pygame.time.Clock()
  52. while True:
  53. screen.fill(( 192, 192, 192))
  54. button_1 = Button(screen, ((config.SCREENSIZE[ 0] -200)// 2, config.SCREENSIZE[ 1]// 3), 'NEXT', font)
  55. button_2 = Button(screen, ((config.SCREENSIZE[ 0] -200)// 2, config.SCREENSIZE[ 1]// 2), 'QUIT', font)
  56. for event in pygame.event.get():
  57. if event.type == pygame.QUIT:
  58. pygame.quit()
  59. sys.exit( -1)
  60. elif event.type == pygame.MOUSEBUTTONDOWN:
  61. if button_1.collidepoint(pygame.mouse.get_pos()):
  62. return True
  63. elif button_2.collidepoint(pygame.mouse.get_pos()):
  64. pygame.quit()
  65. sys.exit( -1)
  66. pygame.display.update()
  67. clock.tick(config.FPS)
  68. elif mode == 'game_end':
  69. clock = pygame.time.Clock()
  70. while True:
  71. screen.fill(( 192, 192, 192))
  72. button_1 = Button(screen, ((config.SCREENSIZE[ 0] -200)// 2, config.SCREENSIZE[ 1]// 3), 'RESTART', font)
  73. button_2 = Button(screen, ((config.SCREENSIZE[ 0] -200)// 2, config.SCREENSIZE[ 1]// 2), 'QUIT', font)
  74. for event in pygame.event.get():
  75. if event.type == pygame.QUIT:
  76. pygame.quit()
  77. sys.exit( -1)
  78. elif event.type == pygame.MOUSEBUTTONDOWN:
  79. if button_1.collidepoint(pygame.mouse.get_pos()):
  80. return True
  81. elif button_2.collidepoint(pygame.mouse.get_pos()):
  82. pygame.quit()
  83. sys.exit( -1)
  84. pygame.display.update()
  85. clock.tick(config.FPS)
  86. else:
  87. raise ValueError( 'Interface.mode unsupport %s...' % mode)

5、定义主玩家 绘制全图

绘制完整游戏,并定义主角,就叫hero吧

sprites.py


  
  1. '''
  2. Function:
  3. 定义游戏精灵类
  4. Author:
  5. lexsaints
  6. '''
  7. import pygame
  8. '''定义hero'''
  9. class Hero(pygame.sprite.Sprite):
  10. def __init__(self, imagepath, coordinate, block_size, border_size, **kwargs):
  11. pygame.sprite.Sprite.__init__(self)
  12. self.image = pygame.image.load(imagepath)
  13. self.image = pygame.transform.scale(self.image, (block_size, block_size))
  14. self.rect = self.image.get_rect()
  15. self.rect.left, self.rect.top = coordinate[ 0] * block_size + border_size[ 0], coordinate[ 1] * block_size + border_size[ 1]
  16. self.coordinate = coordinate
  17. self.block_size = block_size
  18. self.border_size = border_size
  19. '''移动'''
  20. def move(self, direction, maze):
  21. blocks_list = maze.blocks_list
  22. if direction == 'up':
  23. if blocks_list[self.coordinate[ 1]][self.coordinate[ 0]].has_walls[ 0]:
  24. return False
  25. else:
  26. self.coordinate[ 1] = self.coordinate[ 1] - 1
  27. return True
  28. elif direction == 'down':
  29. if blocks_list[self.coordinate[ 1]][self.coordinate[ 0]].has_walls[ 1]:
  30. return False
  31. else:
  32. self.coordinate[ 1] = self.coordinate[ 1] + 1
  33. return True
  34. elif direction == 'left':
  35. if blocks_list[self.coordinate[ 1]][self.coordinate[ 0]].has_walls[ 2]:
  36. return False
  37. else:
  38. self.coordinate[ 0] = self.coordinate[ 0] - 1
  39. return True
  40. elif direction == 'right':
  41. if blocks_list[self.coordinate[ 1]][self.coordinate[ 0]].has_walls[ 3]:
  42. return False
  43. else:
  44. self.coordinate[ 0] = self.coordinate[ 0] + 1
  45. return True
  46. else:
  47. raise ValueError( 'Unsupport direction %s in Hero.move...' % direction)
  48. '''绑定到屏幕'''
  49. def draw(self, screen):
  50. self.rect.left, self.rect.top = self.coordinate[ 0] * self.block_size + self.border_size[ 0], self.coordinate[ 1] * self.block_size + self.border_size[ 1]
  51. screen.blit(self.image, self.rect)

6、引入音频、图片

启动游戏主程序

maze.py

在主程序中,通过读取配置文件,引入项目资源:包括图片、音频等,并通过定义类,加载游戏地图


  
  1. '''
  2. Function:
  3. 迷宫小游戏
  4. Author:
  5. lexsaints
  6. '''
  7. import config
  8. import sys
  9. import pygame
  10. from modules import *
  11. '''主函数'''
  12. def main(config):
  13. # 初始化
  14. pygame.init()
  15. pygame.mixer.init()
  16. pygame.font.init()
  17. pygame.mixer.music.load(config.BGMPATH)
  18. pygame.mixer.music.play( -1, 0.0)
  19. screen = pygame.display.set_mode(config.SCREENSIZE)
  20. pygame.display.set_caption( '一起来学pygame吧——迷宫')
  21. font = pygame.font.SysFont( 'Consolas', 15)
  22. # 开始界面
  23. Interface(screen, config, 'game_start')
  24. # 记录关卡数
  25. num_levels = 0
  26. # 记录最少用了多少步通关
  27. best_scores = 'None'
  28. # 关卡循环切换
  29. while True:
  30. num_levels += 1
  31. clock = pygame.time.Clock()
  32. screen = pygame.display.set_mode(config.SCREENSIZE)
  33. # --随机生成关卡地图
  34. maze_now = RandomMaze(config.MAZESIZE, config.BLOCKSIZE, config.BORDERSIZE)
  35. # --生成hero
  36. hero_now = Hero(config.HEROPICPATH, [ 0, 0], config.BLOCKSIZE, config.BORDERSIZE)
  37. # --统计步数
  38. num_steps = 0
  39. # --关卡内主循环
  40. while True:
  41. dt = clock.tick(config.FPS)
  42. screen.fill(( 255, 255, 255))
  43. is_move = False
  44. # ----↑↓←→控制hero
  45. for event in pygame.event.get():
  46. if event.type == pygame.QUIT:
  47. pygame.quit()
  48. sys.exit( -1)
  49. elif event.type == pygame.KEYDOWN:
  50. if event.key == pygame.K_UP:
  51. is_move = hero_now.move( 'up', maze_now)
  52. elif event.key == pygame.K_DOWN:
  53. is_move = hero_now.move( 'down', maze_now)
  54. elif event.key == pygame.K_LEFT:
  55. is_move = hero_now.move( 'left', maze_now)
  56. elif event.key == pygame.K_RIGHT:
  57. is_move = hero_now.move( 'right', maze_now)
  58. num_steps += int(is_move)
  59. hero_now.draw(screen)
  60. maze_now.draw(screen)
  61. # ----显示一些信息
  62. showText(screen, font, 'LEVELDONE: %d' % num_levels, ( 255, 0, 0), ( 10, 10))
  63. showText(screen, font, 'BESTSCORE: %s' % best_scores, ( 255, 0, 0), ( 210, 10))
  64. showText(screen, font, 'USEDSTEPS: %s' % num_steps, ( 255, 0, 0), ( 410, 10))
  65. showText(screen, font, 'S: your starting point D: your destination', ( 255, 0, 0), ( 10, 600))
  66. # ----判断游戏是否胜利
  67. if (hero_now.coordinate[ 0] == config.MAZESIZE[ 1] - 1) and (hero_now.coordinate[ 1] == config.MAZESIZE[ 0] - 1):
  68. break
  69. pygame.display.update()
  70. # --更新最优成绩
  71. if best_scores == 'None':
  72. best_scores = num_steps
  73. else:
  74. if best_scores > num_steps:
  75. best_scores = num_steps
  76. # --关卡切换
  77. Interface(screen, config, mode= 'game_switch')
  78. '''run'''
  79. if __name__ == '__main__':
  80. main(config)

四、游戏启动方法

1、开发工具启动

如果你配置了开发工具的环境VScode、sublimeText、notepad+、pycharm什么的,可以直接在工具中,运行游戏。

如果没配置,可以使用命令启动。

2、命令行启动 gif

 

推荐阅读

python实战

【python实战】前女友发来加密的 “520快乐.pdf“,我用python破解开之后,却发现。。。

【python实战】昨晚,我用python帮隔壁小姐姐P证件照 自拍,然后发现...

【python实战】女友半夜加班发自拍 python男友用30行代码发现惊天秘密

【python实战】python你TM太皮了——区区30行代码就能记录键盘的一举一动

python实战】女神相册密码忘记了,我只用Python写了20行代码~~~

渗透测试

【渗透案例】上班摸鱼误入陌生网址——结果被XSS劫持了

【渗透测试】密码暴力破解工具——九头蛇(hydra)使用详解及实战

【渗透学习】Web安全渗透详细教程+学习线路+详细笔记【全网最全+建议收藏】

【渗透案例】如何用ssh工具连接前台小姐姐的“小米手机”——雷总看了直呼内行!!!

【渗透测试】密码暴力破解工具——九头蛇(hydra)使用详解及实战

pygame系列文章

一起来学pygame吧 游戏开发30例(二)——塔防游戏

一起来学pygame吧 游戏开发30例(三)——射击外星人小游戏

一起来学pygame吧 游戏开发30例(四)——俄罗斯方块小游戏

一起来学pygame吧 游戏开发30例(五)——消消乐 小游戏

一起来学pygame吧 游戏开发30例(六)——高山滑雪 小游戏

 

CSDN官方学习推荐 ↓ ↓ ↓

CSDN出的Python全栈知识图谱,太强了,推荐给大家!


转载:https://blog.csdn.net/weixin_42350212/article/details/117186303
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场